Add emicklei/go-restful instrumentation (#115)

* Adds a go-restful trace instrumenter via a FilterFunc

A filter may be added at any one of the following:
   * container scope
   * webservice scope
   * route scope

Includes a docker-compose example with instructions.

* Add entry to CHANGELOG.md

* Satisfy go-lint

* Take my own advice and run make precommit

* s/OtelFilter/OTelFilter/

* Add PR# to CHANGELOG

* Standardize line breaks
This commit is contained in:
ET 2020-07-01 10:47:06 -07:00 committed by GitHub
parent 9b5da8f410
commit a24a7d579f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 703 additions and 0 deletions

View File

@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed
- Add `emicklei/go-restful/v3` trace instrumentation. (#115)
- Update `CONTRIBUTING.md` to ask for updates to `CHANGELOG.md` with each pull request. (#114)
- Create this `CHANGELOG.md`. (#114)

View File

@ -0,0 +1,48 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package restful
import (
otelpropagation "go.opentelemetry.io/otel/api/propagation"
oteltrace "go.opentelemetry.io/otel/api/trace"
)
// Config is used to configure the go-restful middleware.
type Config struct {
Tracer oteltrace.Tracer
Propagators otelpropagation.Propagators
}
// Option specifies instrumentation configuration options.
type Option func(*Config)
// WithTracer specifies a tracer to use for creating spans. If none is
// specified, a tracer named
// "go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful" from the global
// provider is used.
func WithTracer(tracer oteltrace.Tracer) Option {
return func(cfg *Config) {
cfg.Tracer = tracer
}
}
// WithPropagators specifies propagators to use for extracting
// information from the HTTP requests. If none are specified, global
// ones will be used.
func WithPropagators(propagators otelpropagation.Propagators) Option {
return func(cfg *Config) {
cfg.Propagators = propagators
}
}

View File

@ -0,0 +1,21 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package go-restful provides functions to trace the emicklei/go-restful/v3
// package (https://github.com/emicklei/go-restful).
//
// Instrumentation of an incoming request is achieved via a go-restful
// FilterFunc which may be applied at the container level, at the
// webservice level or at a route level
package restful // import "go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful"

View File

@ -0,0 +1,20 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM golang:1.14-alpine AS base
COPY . /src/
WORKDIR /src/instrumentation/emicklei/go-restful
FROM base AS go-restful-server
RUN go install ./example/server.go
CMD ["/go/bin/server"]

View File

@ -0,0 +1,28 @@
# emicklei/go-restful instrumentation example
An HTTP server using emicklei/go-restful and instrumentation. The server has a
`/users/{id:[0-9]+}` endpoint. The server generates span information to
`stdout`.
These instructions assume you have
[docker-compose](https://docs.docker.com/compose/) installed.
Bring up the `go-restful-server` and `go-restful-client` services to run the
example:
```sh
docker-compose up --detach go-restful-server go-restful-client
```
The `go-restful-client` service sends just one HTTP request to `go-restful-server`
and then exits. View the span generated by `go-restful-server` in the logs:
```sh
docker-compose logs go-restful-server
```
Shut down the services when you are finished with the example:
```sh
docker-compose down
```

View File

@ -0,0 +1,39 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
version: "3.7"
services:
go-restful-client:
image: golang:1.14-alpine
networks:
- example
command:
- "/bin/sh"
- "-c"
- "wget -O - http://go-restful-server:8080/users/123"
depends_on:
- go-restful-server
go-restful-server:
build:
dockerfile: $PWD/Dockerfile
context: ../../../..
ports:
- "8080:80"
command:
- "/bin/sh"
- "-c"
- "/go/bin/server"
networks:
- example
networks:
example:

View File

@ -0,0 +1,96 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"log"
"net/http"
"strconv"
"github.com/emicklei/go-restful/v3"
restfultrace "go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful"
otelglobal "go.opentelemetry.io/otel/api/global"
otelkv "go.opentelemetry.io/otel/api/kv"
oteltrace "go.opentelemetry.io/otel/api/trace"
oteltracestdout "go.opentelemetry.io/otel/exporters/trace/stdout"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
var tracer oteltrace.Tracer
type UserResource struct{}
func (u UserResource) WebService() *restful.WebService {
ws := &restful.WebService{}
ws.Path("/users").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/{user-id}").To(u.getUser).
Param(ws.PathParameter("user-id", "identifier of the user").DataType("integer").DefaultValue("1")).
Writes(User{}). // on the response
Returns(200, "OK", User{}).
Returns(404, "Not Found", nil))
return ws
}
func main() {
initTracer()
u := UserResource{}
// create the Otel filter
filter := restfultrace.OTelFilter("my-service")
// use it
restful.DefaultContainer.Filter(filter)
restful.DefaultContainer.Add(u.WebService())
_ = http.ListenAndServe(":8080", nil)
}
func initTracer() {
exporter, err := oteltracestdout.NewExporter(oteltracestdout.Options{PrettyPrint: true})
if err != nil {
log.Fatal(err)
}
cfg := sdktrace.Config{
DefaultSampler: sdktrace.AlwaysSample(),
}
tp, err := sdktrace.NewProvider(
sdktrace.WithConfig(cfg),
sdktrace.WithSyncer(exporter),
)
if err != nil {
log.Fatal(err)
}
otelglobal.SetTraceProvider(tp)
tracer = otelglobal.TraceProvider().Tracer("go-restful-server", oteltrace.WithInstrumentationVersion("0.1"))
}
func (u UserResource) getUser(req *restful.Request, resp *restful.Response) {
uid := req.PathParameter("user-id")
_, span := tracer.Start(req.Request.Context(), "getUser", oteltrace.WithAttributes(otelkv.String("id", uid)))
defer span.End()
id, err := strconv.Atoi(uid)
if err == nil && id >= 100 {
_ = resp.WriteEntity(User{id})
return
}
_ = resp.WriteErrorString(http.StatusNotFound, "User could not be found.")
}
type User struct {
ID int `json:"id" description:"identifier of the user"`
}

View File

@ -0,0 +1,13 @@
module go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful
go 1.14
replace go.opentelemetry.io/contrib => ../../..
require (
github.com/emicklei/go-restful/v3 v3.0.0
github.com/json-iterator/go v1.1.10 // indirect
github.com/stretchr/testify v1.6.1
go.opentelemetry.io/contrib v0.7.0
go.opentelemetry.io/otel v0.7.0
)

View File

@ -0,0 +1,105 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7 h1:qELHH0AWCvf98Yf+CNIJx9vOZOfHFDDzgDRYsnNk/vs=
github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7/go.mod h1:Q5DbzQ+3AkgGwymQO7aZFNP7ns2lZKGtvRBzRXfdi60=
github.com/benbjohnson/clock v1.0.3 h1:vkLuvpK4fmtSCuo60+yC63p7y0BmQ8gm5ZXGuBCJyXg=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/go-restful/v3 v3.0.0 h1:Duxxa4x0WIHW3bYEDmoAPNjmy8Rbqn+utcF74dlF/G8=
github.com/emicklei/go-restful/v3 v3.0.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v0.7.0 h1:u43jukpwqR8EsyeJOMgrsUgZwVI1e1eVw7yuzRkD1l0=
go.opentelemetry.io/otel v0.7.0/go.mod h1:aZMyHG5TqDOXEgH2tyLiXSUKly1jT3yqE9PmrzIeCdo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03 h1:4HYDjxeNXAOTv3o1N2tjo8UUSlhQgAD52FVkwxnWgM8=
google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.30.0 h1:M5a8xTlYTxwMn5ZFkwhRabsygDY5G8TYLyQDBxJNAxE=
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

View File

@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package restful
import (
"github.com/emicklei/go-restful/v3"
otelglobal "go.opentelemetry.io/otel/api/global"
otelpropagation "go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/standard"
oteltrace "go.opentelemetry.io/otel/api/trace"
)
const (
tracerName = "go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful"
tracerVersion = "1.0"
)
// OTelFilter returns a restful.FilterFunction which will trace an incoming request.
//
// The service parameter should describe the name of the (virtual) server handling
// the request. Options can be applied to configure the tracer and propagators
// used for this filter.
func OTelFilter(service string, opts ...Option) restful.FilterFunction {
cfg := Config{}
for _, opt := range opts {
opt(&cfg)
}
if cfg.Tracer == nil {
cfg.Tracer = otelglobal.TraceProvider().Tracer(tracerName, oteltrace.WithInstrumentationVersion(tracerVersion))
}
if cfg.Propagators == nil {
cfg.Propagators = otelglobal.Propagators()
}
return func(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
r := req.Request
ctx := otelpropagation.ExtractHTTP(r.Context(), cfg.Propagators, r.Header)
route := req.SelectedRoutePath()
spanName := route
opts := []oteltrace.StartOption{
oteltrace.WithAttributes(standard.NetAttributesFromHTTPRequest("tcp", r)...),
oteltrace.WithAttributes(standard.EndUserAttributesFromHTTPRequest(r)...),
oteltrace.WithAttributes(standard.HTTPServerAttributesFromHTTPRequest(service, route, r)...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
ctx, span := cfg.Tracer.Start(ctx, spanName, opts...)
defer span.End()
// pass the span through the request context
req.Request = req.Request.WithContext(ctx)
chain.ProcessFilter(req, resp)
attrs := standard.HTTPAttributesFromHTTPStatusCode(resp.StatusCode())
spanStatus, spanMessage := standard.SpanStatusFromHTTPStatusCode(resp.StatusCode())
span.SetAttributes(attrs...)
span.SetStatus(spanStatus, spanMessage)
}
}

View File

@ -0,0 +1,260 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package restful_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/emicklei/go-restful/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
restfultrace "go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful"
mocktrace "go.opentelemetry.io/contrib/internal/trace"
otelglobal "go.opentelemetry.io/otel/api/global"
otelvalue "go.opentelemetry.io/otel/api/kv/value"
otelpropagation "go.opentelemetry.io/otel/api/propagation"
oteltrace "go.opentelemetry.io/otel/api/trace"
)
func TestChildSpanFromGlobalTracer(t *testing.T) {
otelglobal.SetTraceProvider(&mocktrace.Provider{})
handlerFunc := func(req *restful.Request, resp *restful.Response) {
span := oteltrace.SpanFromContext(req.Request.Context())
_, ok := span.(*mocktrace.Span)
assert.True(t, ok)
spanTracer := span.Tracer()
mockTracer, ok := spanTracer.(*mocktrace.Tracer)
require.True(t, ok)
assert.Equal(t, "go.opentelemetry.io/contrib/instrumentation/emicklei/go-restful", mockTracer.Name)
resp.WriteHeader(http.StatusOK)
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id}").To(handlerFunc).
Returns(200, "OK", nil).
Returns(404, "Not Found", nil))
container := restful.NewContainer()
container.Filter(restfultrace.OTelFilter("my-service"))
container.Add(ws)
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
container.ServeHTTP(w, r)
}
func TestChildSpanFromCustomTracer(t *testing.T) {
tracer := mocktrace.NewTracer("test-tracer")
handlerFunc := func(req *restful.Request, resp *restful.Response) {
span := oteltrace.SpanFromContext(req.Request.Context())
_, ok := span.(*mocktrace.Span)
assert.True(t, ok)
spanTracer := span.Tracer()
mockTracer, ok := spanTracer.(*mocktrace.Tracer)
require.True(t, ok)
assert.Equal(t, "test-tracer", mockTracer.Name)
resp.WriteHeader(http.StatusOK)
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id}").To(handlerFunc))
container := restful.NewContainer()
container.Filter(restfultrace.OTelFilter("my-service", restfultrace.WithTracer(tracer)))
container.Add(ws)
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
container.ServeHTTP(w, r)
}
func TestChildSpanNames(t *testing.T) {
tracer := mocktrace.NewTracer("test-tracer")
handlerFunc := func(req *restful.Request, resp *restful.Response) {
resp.WriteHeader(http.StatusOK)
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id:[0-9]+}").To(handlerFunc))
container := restful.NewContainer()
container.Filter(restfultrace.OTelFilter("foobar", restfultrace.WithTracer(tracer)))
container.Add(ws)
ws.Route(ws.GET("/book/{title}").To(func(req *restful.Request, resp *restful.Response) {
_, _ = resp.Write(([]byte)("ok"))
}))
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
container.ServeHTTP(w, r)
spans := tracer.EndedSpans()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/user/{id:[0-9]+}", span.Name)
assert.Equal(t, oteltrace.SpanKindServer, span.Kind)
assert.Equal(t, otelvalue.String("foobar"), span.Attributes["http.server_name"])
assert.Equal(t, otelvalue.Int(http.StatusOK), span.Attributes["http.status_code"])
assert.Equal(t, otelvalue.String("GET"), span.Attributes["http.method"])
assert.Equal(t, otelvalue.String("/user/123"), span.Attributes["http.target"])
assert.Equal(t, otelvalue.String("/user/{id:[0-9]+}"), span.Attributes["http.route"])
r = httptest.NewRequest("GET", "/book/foo", nil)
w = httptest.NewRecorder()
container.ServeHTTP(w, r)
spans = tracer.EndedSpans()
require.Len(t, spans, 1)
span = spans[0]
assert.Equal(t, "/book/{title}", span.Name)
assert.Equal(t, oteltrace.SpanKindServer, span.Kind)
assert.Equal(t, otelvalue.String("foobar"), span.Attributes["http.server_name"])
assert.Equal(t, otelvalue.Int(http.StatusOK), span.Attributes["http.status_code"])
assert.Equal(t, otelvalue.String("GET"), span.Attributes["http.method"])
assert.Equal(t, otelvalue.String("/book/foo"), span.Attributes["http.target"])
assert.Equal(t, otelvalue.String("/book/{title}"), span.Attributes["http.route"])
}
func TestGetSpanNotInstrumented(t *testing.T) {
handlerFunc := func(req *restful.Request, resp *restful.Response) {
span := oteltrace.SpanFromContext(req.Request.Context())
_, ok := span.(oteltrace.NoopSpan)
assert.True(t, ok)
resp.WriteHeader(http.StatusOK)
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id}").To(handlerFunc))
container := restful.NewContainer()
container.Add(ws)
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
container.ServeHTTP(w, r)
}
func TestPropagationWithGlobalPropagators(t *testing.T) {
tracer := mocktrace.NewTracer("test-tracer")
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx, pspan := tracer.Start(context.Background(), "test")
otelpropagation.InjectHTTP(ctx, otelglobal.Propagators(), r.Header)
handlerFunc := func(req *restful.Request, resp *restful.Response) {
span := oteltrace.SpanFromContext(req.Request.Context())
mspan, ok := span.(*mocktrace.Span)
require.True(t, ok)
assert.Equal(t, pspan.SpanContext().TraceID, mspan.SpanContext().TraceID)
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID)
w.WriteHeader(http.StatusOK)
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id}").To(handlerFunc))
container := restful.NewContainer()
container.Filter(restfultrace.OTelFilter("foobar", restfultrace.WithTracer(tracer)))
container.Add(ws)
container.ServeHTTP(w, r)
}
func TestPropagationWithCustomPropagators(t *testing.T) {
tracer := mocktrace.NewTracer("test-tracer")
b3 := oteltrace.B3{}
props := otelpropagation.New(
otelpropagation.WithExtractors(b3),
otelpropagation.WithInjectors(b3),
)
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx, pspan := tracer.Start(context.Background(), "test")
otelpropagation.InjectHTTP(ctx, props, r.Header)
handlerFunc := func(req *restful.Request, resp *restful.Response) {
span := oteltrace.SpanFromContext(req.Request.Context())
mspan, ok := span.(*mocktrace.Span)
require.True(t, ok)
assert.Equal(t, pspan.SpanContext().TraceID, mspan.SpanContext().TraceID)
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID)
w.WriteHeader(http.StatusOK)
}
ws := &restful.WebService{}
ws.Route(ws.GET("/user/{id}").To(handlerFunc))
container := restful.NewContainer()
container.Filter(restfultrace.OTelFilter("foobar",
restfultrace.WithTracer(tracer),
restfultrace.WithPropagators(props)))
container.Add(ws)
container.ServeHTTP(w, r)
}
func TestMultiFilters(t *testing.T) {
tracer1 := mocktrace.NewTracer("tracer1")
tracer2 := mocktrace.NewTracer("tracer2")
tracer3 := mocktrace.NewTracer("tracer3")
wrappedFunc := func(tracerName string) restful.RouteFunction {
return func(req *restful.Request, resp *restful.Response) {
span := oteltrace.SpanFromContext(req.Request.Context())
_, ok := span.(*mocktrace.Span)
assert.True(t, ok)
spanTracer := span.Tracer()
mockTracer, ok := spanTracer.(*mocktrace.Tracer)
require.True(t, ok)
assert.Equal(t, tracerName, mockTracer.Name)
resp.WriteHeader(http.StatusOK)
}
}
ws1 := &restful.WebService{}
ws1.Path("/user")
ws1.Route(ws1.GET("/{id}").
Filter(restfultrace.OTelFilter("my-service", restfultrace.WithTracer(tracer1))).
To(wrappedFunc("tracer1")))
ws1.Route(ws1.GET("/{id}/books").
Filter(restfultrace.OTelFilter("book-service", restfultrace.WithTracer(tracer2))).
To(wrappedFunc("tracer2")))
ws2 := &restful.WebService{}
ws2.Path("/library")
ws2.Filter(restfultrace.OTelFilter("library-service", restfultrace.WithTracer(tracer3)))
ws2.Route(ws2.GET("/{name}").To(wrappedFunc("tracer3")))
container := restful.NewContainer()
container.Add(ws1)
container.Add(ws2)
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
container.ServeHTTP(w, r)
r = httptest.NewRequest("GET", "/user/123/books", nil)
w = httptest.NewRecorder()
container.ServeHTTP(w, r)
r = httptest.NewRequest("GET", "/library/metropolitan", nil)
w = httptest.NewRecorder()
container.ServeHTTP(w, r)
}