diff --git a/plugins/macaron/config.go b/plugins/macaron/config.go new file mode 100644 index 00000000000..f2c4cbd4126 --- /dev/null +++ b/plugins/macaron/config.go @@ -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 macaron + +import ( + "go.opentelemetry.io/otel/api/propagation" + "go.opentelemetry.io/otel/api/trace" +) + +// Config is a helper struct to configure Macaron options +type Config struct { + Tracer trace.Tracer + Propagators propagation.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/plugins/macaron" from the global +// provider is used. +func WithTracer(tracer trace.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 propagation.Propagators) Option { + return func(cfg *Config) { + cfg.Propagators = propagators + } +} diff --git a/plugins/macaron/doc.go b/plugins/macaron/doc.go new file mode 100644 index 00000000000..795d6cf7144 --- /dev/null +++ b/plugins/macaron/doc.go @@ -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. + +// Package macaron provides functions to trace the macaron.v1 package +// (https://github.com/go-macaron/macaron). +// +// Currently only the routing of a received message can be +// instrumented. To do it, use the Middleware function. +package macaron // import "go.opentelemetry.io/contrib/plugins/macaron" diff --git a/plugins/macaron/example/Dockerfile b/plugins/macaron/example/Dockerfile new file mode 100644 index 00000000000..a4a7d293265 --- /dev/null +++ b/plugins/macaron/example/Dockerfile @@ -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:alpine AS base +COPY . /src/ +WORKDIR /src/plugins/macaron + +FROM base AS macaron-server +RUN go install ./example/server.go +CMD ["/go/bin/server"] diff --git a/plugins/macaron/example/README.md b/plugins/macaron/example/README.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/plugins/macaron/example/docker-compose.yml b/plugins/macaron/example/docker-compose.yml new file mode 100644 index 00000000000..fe2494c00d7 --- /dev/null +++ b/plugins/macaron/example/docker-compose.yml @@ -0,0 +1,41 @@ +# 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: + macaron-client: + image: golang:alpine + networks: + - example + command: + - "/bin/sh" + - "-c" + - "wget http://macaron-server:8080/users/123 && cat 123" + depends_on: + - macaron-server + macaron-server: + build: + dockerfile: $PWD/Dockerfile + context: ../../.. + ports: + - "8080:80" + command: + - "/bin/sh" + - "-c" + - "/go/bin/server" + networks: + - example + environment: + - PORT=80 +networks: + example: diff --git a/plugins/macaron/example/server.go b/plugins/macaron/example/server.go new file mode 100644 index 00000000000..b73048648f7 --- /dev/null +++ b/plugins/macaron/example/server.go @@ -0,0 +1,71 @@ +// 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 ( + "context" + "log" + + "gopkg.in/macaron.v1" + + macarontrace "go.opentelemetry.io/contrib/plugins/macaron" + + 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 = otelglobal.Tracer("macaron-server") + +func main() { + initTracer() + m := macaron.Classic() + m.Use(macarontrace.Middleware("my-server")) + m.Get("/users/:id", func(ctx *macaron.Context) string { + id := ctx.Params("id") + name := getUser(ctx.Req.Context(), id) + return name + }) + m.Run() +} + +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) +} + +func getUser(ctx context.Context, id string) string { + _, span := tracer.Start(ctx, "getUser", oteltrace.WithAttributes(otelkv.String("id", id))) + defer span.End() + if id == "123" { + return "macarontrace tester" + } + return "unknown" +} diff --git a/plugins/macaron/go.mod b/plugins/macaron/go.mod new file mode 100644 index 00000000000..1bafef98547 --- /dev/null +++ b/plugins/macaron/go.mod @@ -0,0 +1,12 @@ +module go.opentelemetry.io/contrib/plugins/macaron + +go 1.14 + +require ( + github.com/stretchr/testify v1.5.1 + go.opentelemetry.io/contrib v0.0.0 + go.opentelemetry.io/otel v0.5.0 + gopkg.in/macaron.v1 v1.3.5 +) + +replace go.opentelemetry.io/contrib => ../../ diff --git a/plugins/macaron/go.sum b/plugins/macaron/go.sum new file mode 100644 index 00000000000..b14d84be3c5 --- /dev/null +++ b/plugins/macaron/go.sum @@ -0,0 +1,112 @@ +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.0 h1:78Jk/r6m4wCi6sndMpty7A//t4dw/RW5fV4ZgDVfX1w= +github.com/benbjohnson/clock v1.0.0/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/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/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/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191 h1:NjHlg70DuOkcAMqgt0+XA+NHwtu66MkTVVgR4fFWbcI= +github.com/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191/go.mod h1:VFI2o2q9kYsC4o7VP1HrEVosiZZTd+MVT3YZx4gqvJw= +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 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.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/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c h1:7lF+Vz0LqiRidnzC1Oq86fpX1q/iEv2KJdrCtttYjT4= +github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +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/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/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w= +github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM= +github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= +github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8= +github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e h1:GSGeB9EAKY2spCABz6xOX5DbxZEXolK+nBSvmsQwRjM= +github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM= +go.opentelemetry.io/otel v0.5.0 h1:tdIR1veg/z+VRJaw/6SIxz+QX3l+m+BDleYLTs+GC1g= +go.opentelemetry.io/otel v0.5.0/go.mod h1:jzBIgIzK43Iu1BpDAXwqOd6UPsSAk+ewVZ5ofSXw4Ek= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +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/net v0.0.0-20190404232315-eb5bcb51f2a3/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/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +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-20190328211700-ab21143f2384/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.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.28.1 h1:C1QC6KzgSiLyBabDi87BbjaGreoRgGUF5nOyvfrAZ1k= +google.golang.org/grpc v1.28.1/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= +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/ini.v1 v1.46.0 h1:VeDZbLYGaupuvIrsYCEOe/L/2Pcs5n7hdO1ZTjporag= +gopkg.in/ini.v1 v1.46.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/macaron.v1 v1.3.5 h1:FUA16VFBojxzfU75KqWrV/6BPv9O2R1GnybSGRie9QQ= +gopkg.in/macaron.v1 v1.3.5/go.mod h1:uMZCFccv9yr5TipIalVOyAyZQuOH3OkmXvgcWwhJuP4= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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= diff --git a/plugins/macaron/macaron.go b/plugins/macaron/macaron.go new file mode 100644 index 00000000000..074a22496de --- /dev/null +++ b/plugins/macaron/macaron.go @@ -0,0 +1,78 @@ +// 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 macaron + +import ( + "fmt" + "net/http" + + "gopkg.in/macaron.v1" + + "go.opentelemetry.io/contrib/internal/trace" + otelglobal "go.opentelemetry.io/otel/api/global" + otelpropagation "go.opentelemetry.io/otel/api/propagation" + oteltrace "go.opentelemetry.io/otel/api/trace" +) + +const ( + tracerName = "go.opentelemetry.io/contrib/plugins/macaron" +) + +// Middleware returns a macaron Handler to trace requests to the server. +func Middleware(service string, opts ...Option) macaron.Handler { + cfg := Config{} + for _, opt := range opts { + opt(&cfg) + } + if cfg.Tracer == nil { + cfg.Tracer = otelglobal.Tracer(tracerName) + } + if cfg.Propagators == nil { + cfg.Propagators = otelglobal.Propagators() + } + return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) { + savedCtx := c.Req.Request.Context() + defer func() { + c.Req.Request = c.Req.Request.WithContext(savedCtx) + }() + + ctx := otelpropagation.ExtractHTTP(savedCtx, cfg.Propagators, c.Req.Header) + opts := []oteltrace.StartOption{ + oteltrace.WithAttributes(trace.NetAttributesFromHTTPRequest("tcp", c.Req.Request)...), + oteltrace.WithAttributes(trace.EndUserAttributesFromHTTPRequest(c.Req.Request)...), + oteltrace.WithAttributes(trace.HTTPServerAttributesFromHTTPRequest(service, "", c.Req.Request)...), + oteltrace.WithSpanKind(oteltrace.SpanKindServer), + } + // TODO: span name should be router template not the actual request path, eg /user/:id vs /user/123 + spanName := c.Req.RequestURI + if spanName == "" { + spanName = fmt.Sprintf("HTTP %s route not found", c.Req.Method) + } + ctx, span := cfg.Tracer.Start(ctx, spanName, opts...) + defer span.End() + + // pass the span through the request context + c.Req.Request = c.Req.Request.WithContext(ctx) + + // serve the request to the next middleware + c.Next() + + status := c.Resp.Status() + attrs := trace.HTTPAttributesFromHTTPStatusCode(status) + spanStatus, spanMessage := trace.SpanStatusFromHTTPStatusCode(status) + span.SetAttributes(attrs...) + span.SetStatus(spanStatus, spanMessage) + } +} diff --git a/plugins/macaron/macaron_test.go b/plugins/macaron/macaron_test.go new file mode 100644 index 00000000000..0df71c13e4b --- /dev/null +++ b/plugins/macaron/macaron_test.go @@ -0,0 +1,188 @@ +// 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 macaron + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/macaron.v1" + + 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{}) + + m := macaron.Classic() + m.Use(Middleware("foobar")) + m.Get("/user/:id", func(ctx *macaron.Context) { + span := oteltrace.SpanFromContext(ctx.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/plugins/macaron", mockTracer.Name) + ctx.Resp.WriteHeader(http.StatusOK) + }) + + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, r) +} + +func TestChildSpanFromCustomTracer(t *testing.T) { + tracer := mocktrace.NewTracer("test-tracer") + + m := macaron.Classic() + m.Use(Middleware("foobar", WithTracer(tracer))) + m.Get("/user/:id", func(ctx *macaron.Context) { + span := oteltrace.SpanFromContext(ctx.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) + ctx.Resp.WriteHeader(http.StatusOK) + }) + + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() + + m.ServeHTTP(w, r) +} + +func TestChildSpanNames(t *testing.T) { + tracer := mocktrace.NewTracer("test-tracer") + + m := macaron.Classic() + m.Use(Middleware("foobar", WithTracer(tracer))) + m.Get("/user/:id", func(ctx *macaron.Context) { + ctx.Resp.WriteHeader(http.StatusOK) + }) + m.Get("/book/:title", func(ctx *macaron.Context) { + _, err := ctx.Resp.Write(([]byte)("ok")) + if err != nil { + t.Error(err) + } + }) + + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() + m.ServeHTTP(w, r) + spans := tracer.EndedSpans() + require.Len(t, spans, 1) + span := spans[0] + assert.Equal(t, "/user/123", span.Name) // TODO: span name should show router template, eg /user/:id + 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"]) + // TODO: span name should show router template, eg /user/:id + //assert.Equal(t, otelvalue.String("/user/:id"), span.Attributes["http.route"]) + + r = httptest.NewRequest("GET", "/book/foo", nil) + w = httptest.NewRecorder() + m.ServeHTTP(w, r) + spans = tracer.EndedSpans() + require.Len(t, spans, 1) + span = spans[0] + assert.Equal(t, "/book/foo", span.Name) // TODO: span name should show router template, eg /book/:title + 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"]) + // TODO: span name should show router template, eg /book/:title + //assert.Equal(t, otelvalue.String("/book/:title"), span.Attributes["http.route"]) +} + +func TestGetSpanNotInstrumented(t *testing.T) { + m := macaron.Classic() + m.Get("/user/:id", func(ctx *macaron.Context) { + span := oteltrace.SpanFromContext(ctx.Req.Request.Context()) + _, ok := span.(oteltrace.NoopSpan) + assert.True(t, ok) + ctx.Resp.WriteHeader(http.StatusOK) + }) + + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() + + m.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) + + m := macaron.Classic() + m.Use(Middleware("foobar", WithTracer(tracer))) + m.Get("/user/:id", func(ctx *macaron.Context) { + span := oteltrace.SpanFromContext(ctx.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) + ctx.Resp.WriteHeader(http.StatusOK) + }) + + m.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) + + m := macaron.Classic() + m.Use(Middleware("foobar", WithTracer(tracer), WithPropagators(props))) + m.Get("/user/:id", func(ctx *macaron.Context) { + span := oteltrace.SpanFromContext(ctx.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) + ctx.Resp.WriteHeader(http.StatusOK) + }) + + m.ServeHTTP(w, r) +}