Skip to content

Commit 97f3114

Browse files
authored
Update gopkg.in/macaron.v1/otelmacaron instrumentation to use TracerProvider (#374)
* Update gopkg.in/macaron.v1/otelmacaron inst to use TracerProvider * Add changes to changelog
1 parent a59885b commit 97f3114

File tree

4 files changed

+92
-91
lines changed

4 files changed

+92
-91
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2121

2222
## [0.12.0] - 2020-09-25
2323

24+
### Changed
25+
26+
- Replace `WithTracer` with `WithTracerProvider` in the `go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron` instrumentation. (#374)
27+
2428
### Added
2529

2630
- Benchmark tests for the gRPC instrumentation. (#296)

instrumentation/gopkg.in/macaron.v1/otelmacaron/config.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,54 @@
1515
package otelmacaron
1616

1717
import (
18+
"go.opentelemetry.io/otel/api/global"
1819
"go.opentelemetry.io/otel/api/propagation"
1920
"go.opentelemetry.io/otel/api/trace"
2021
)
2122

22-
// config is a helper struct to configure Macaron options
23+
// config is a group of options for this instrumentation.
2324
type config struct {
24-
Tracer trace.Tracer
25-
Propagators propagation.Propagators
25+
TracerProvider trace.TracerProvider
26+
Propagators propagation.Propagators
2627
}
2728

28-
// Option specifies instrumentation configuration options.
29-
type Option func(*config)
29+
// Option applies an option value for a config.
30+
type Option interface {
31+
Apply(*config)
32+
}
3033

31-
// WithTracer specifies a tracer to use for creating spans. If none is
32-
// specified, a tracer named
33-
// "go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron"
34-
// from the global provider is used.
35-
func WithTracer(tracer trace.Tracer) Option {
36-
return func(cfg *config) {
37-
cfg.Tracer = tracer
34+
// newConfig returns a config configured with all the passed Options.
35+
func newConfig(opts []Option) *config {
36+
c := &config{
37+
Propagators: global.Propagators(),
38+
TracerProvider: global.TracerProvider(),
39+
}
40+
for _, o := range opts {
41+
o.Apply(c)
3842
}
43+
return c
3944
}
4045

41-
// WithPropagators specifies propagators to use for extracting
42-
// information from the HTTP requests. If none are specified, global
43-
// ones will be used.
44-
func WithPropagators(propagators propagation.Propagators) Option {
45-
return func(cfg *config) {
46-
cfg.Propagators = propagators
47-
}
46+
type propagatorsOption struct{ p propagation.Propagators }
47+
48+
func (o propagatorsOption) Apply(c *config) {
49+
c.Propagators = o.p
50+
}
51+
52+
// WithPropagators returns an Option to use the Propagators when extracting
53+
// and injecting trace context from HTTP requests.
54+
func WithPropagators(p propagation.Propagators) Option {
55+
return propagatorsOption{p: p}
56+
}
57+
58+
type tracerProviderOption struct{ tp trace.TracerProvider }
59+
60+
func (o tracerProviderOption) Apply(c *config) {
61+
c.TracerProvider = o.tp
62+
}
63+
64+
// WithTracerProvider returns an Option to use the TracerProvider when
65+
// creating a Tracer.
66+
func WithTracerProvider(tp trace.TracerProvider) Option {
67+
return tracerProviderOption{tp: tp}
4868
}

instrumentation/gopkg.in/macaron.v1/otelmacaron/macaron.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,23 @@ import (
2020

2121
"gopkg.in/macaron.v1"
2222

23-
otelglobal "go.opentelemetry.io/otel/api/global"
2423
otelpropagation "go.opentelemetry.io/otel/api/propagation"
2524
oteltrace "go.opentelemetry.io/otel/api/trace"
2625
"go.opentelemetry.io/otel/semconv"
27-
)
2826

29-
const (
30-
tracerName = "go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron"
27+
otelcontrib "go.opentelemetry.io/contrib"
3128
)
3229

30+
// instrumentationName is the name of this instrumentation package.
31+
const instrumentationName = "go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron"
32+
3333
// Middleware returns a macaron Handler to trace requests to the server.
3434
func Middleware(service string, opts ...Option) macaron.Handler {
35-
cfg := config{}
36-
for _, opt := range opts {
37-
opt(&cfg)
38-
}
39-
if cfg.Tracer == nil {
40-
cfg.Tracer = otelglobal.Tracer(tracerName)
41-
}
42-
if cfg.Propagators == nil {
43-
cfg.Propagators = otelglobal.Propagators()
44-
}
35+
cfg := newConfig(opts)
36+
tracer := cfg.TracerProvider.Tracer(
37+
instrumentationName,
38+
oteltrace.WithInstrumentationVersion(otelcontrib.SemVersion()),
39+
)
4540
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
4641
savedCtx := c.Req.Request.Context()
4742
defer func() {
@@ -60,7 +55,7 @@ func Middleware(service string, opts ...Option) macaron.Handler {
6055
if spanName == "" {
6156
spanName = fmt.Sprintf("HTTP %s route not found", c.Req.Method)
6257
}
63-
ctx, span := cfg.Tracer.Start(ctx, spanName, opts...)
58+
ctx, span := tracer.Start(ctx, spanName, opts...)
6459
defer span.End()
6560

6661
// pass the span through the request context

instrumentation/gopkg.in/macaron.v1/otelmacaron/macaron_test.go

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,27 @@ import (
2424
"github.com/stretchr/testify/require"
2525
"gopkg.in/macaron.v1"
2626

27-
mocktrace "go.opentelemetry.io/contrib/internal/trace"
2827
b3prop "go.opentelemetry.io/contrib/propagators/b3"
2928
otelglobal "go.opentelemetry.io/otel/api/global"
3029
otelpropagation "go.opentelemetry.io/otel/api/propagation"
3130
oteltrace "go.opentelemetry.io/otel/api/trace"
31+
"go.opentelemetry.io/otel/api/trace/tracetest"
3232
"go.opentelemetry.io/otel/label"
3333
)
3434

3535
func TestChildSpanFromGlobalTracer(t *testing.T) {
36-
otelglobal.SetTracerProvider(&mocktrace.TracerProvider{})
36+
otelglobal.SetTracerProvider(tracetest.NewTracerProvider())
3737

3838
m := macaron.Classic()
3939
m.Use(Middleware("foobar"))
4040
m.Get("/user/:id", func(ctx *macaron.Context) {
4141
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
42-
_, ok := span.(*mocktrace.Span)
42+
_, ok := span.(*tracetest.Span)
4343
assert.True(t, ok)
4444
spanTracer := span.Tracer()
45-
mockTracer, ok := spanTracer.(*mocktrace.Tracer)
45+
mockTracer, ok := spanTracer.(*tracetest.Tracer)
4646
require.True(t, ok)
47-
assert.Equal(t, "go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron", mockTracer.Name)
48-
ctx.Resp.WriteHeader(http.StatusOK)
49-
})
50-
51-
r := httptest.NewRequest("GET", "/user/123", nil)
52-
w := httptest.NewRecorder()
53-
54-
m.ServeHTTP(w, r)
55-
}
56-
57-
func TestChildSpanFromCustomTracer(t *testing.T) {
58-
tracer := mocktrace.NewTracer("test-tracer")
59-
60-
m := macaron.Classic()
61-
m.Use(Middleware("foobar", WithTracer(tracer)))
62-
m.Get("/user/:id", func(ctx *macaron.Context) {
63-
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
64-
_, ok := span.(*mocktrace.Span)
65-
assert.True(t, ok)
66-
spanTracer := span.Tracer()
67-
mockTracer, ok := spanTracer.(*mocktrace.Tracer)
68-
require.True(t, ok)
69-
assert.Equal(t, "test-tracer", mockTracer.Name)
47+
assert.Equal(t, instrumentationName, mockTracer.Name)
7048
ctx.Resp.WriteHeader(http.StatusOK)
7149
})
7250

@@ -77,10 +55,11 @@ func TestChildSpanFromCustomTracer(t *testing.T) {
7755
}
7856

7957
func TestChildSpanNames(t *testing.T) {
80-
tracer := mocktrace.NewTracer("test-tracer")
58+
sr := new(tracetest.StandardSpanRecorder)
59+
tp := tracetest.NewTracerProvider(tracetest.WithSpanRecorder(sr))
8160

8261
m := macaron.Classic()
83-
m.Use(Middleware("foobar", WithTracer(tracer)))
62+
m.Use(Middleware("foobar", WithTracerProvider(tp)))
8463
m.Get("/user/:id", func(ctx *macaron.Context) {
8564
ctx.Resp.WriteHeader(http.StatusOK)
8665
})
@@ -94,30 +73,32 @@ func TestChildSpanNames(t *testing.T) {
9473
r := httptest.NewRequest("GET", "/user/123", nil)
9574
w := httptest.NewRecorder()
9675
m.ServeHTTP(w, r)
97-
spans := tracer.EndedSpans()
98-
require.Len(t, spans, 1)
99-
span := spans[0]
100-
assert.Equal(t, "/user/123", span.Name) // TODO: span name should show router template, eg /user/:id
101-
assert.Equal(t, oteltrace.SpanKindServer, span.Kind)
102-
assert.Equal(t, label.StringValue("foobar"), span.Attributes["http.server_name"])
103-
assert.Equal(t, label.IntValue(http.StatusOK), span.Attributes["http.status_code"])
104-
assert.Equal(t, label.StringValue("GET"), span.Attributes["http.method"])
105-
assert.Equal(t, label.StringValue("/user/123"), span.Attributes["http.target"])
106-
// TODO: span name should show router template, eg /user/:id
107-
//assert.Equal(t, label.StringValue("/user/:id"), span.Attributes["http.route"])
10876

10977
r = httptest.NewRequest("GET", "/book/foo", nil)
11078
w = httptest.NewRecorder()
11179
m.ServeHTTP(w, r)
112-
spans = tracer.EndedSpans()
113-
require.Len(t, spans, 1)
114-
span = spans[0]
115-
assert.Equal(t, "/book/foo", span.Name) // TODO: span name should show router template, eg /book/:title
116-
assert.Equal(t, oteltrace.SpanKindServer, span.Kind)
117-
assert.Equal(t, label.StringValue("foobar"), span.Attributes["http.server_name"])
118-
assert.Equal(t, label.IntValue(http.StatusOK), span.Attributes["http.status_code"])
119-
assert.Equal(t, label.StringValue("GET"), span.Attributes["http.method"])
120-
assert.Equal(t, label.StringValue("/book/foo"), span.Attributes["http.target"])
80+
81+
spans := sr.Completed()
82+
require.Len(t, spans, 2)
83+
span := spans[0]
84+
assert.Equal(t, "/user/123", span.Name()) // TODO: span name should show router template, eg /user/:id
85+
assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind())
86+
attrs := span.Attributes()
87+
assert.Equal(t, label.StringValue("foobar"), attrs["http.server_name"])
88+
assert.Equal(t, label.IntValue(http.StatusOK), attrs["http.status_code"])
89+
assert.Equal(t, label.StringValue("GET"), attrs["http.method"])
90+
assert.Equal(t, label.StringValue("/user/123"), attrs["http.target"])
91+
// TODO: span name should show router template, eg /user/:id
92+
//assert.Equal(t, label.StringValue("/user/:id"), span.Attributes["http.route"])
93+
94+
span = spans[1]
95+
assert.Equal(t, "/book/foo", span.Name()) // TODO: span name should show router template, eg /book/:title
96+
assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind())
97+
attrs = span.Attributes()
98+
assert.Equal(t, label.StringValue("foobar"), attrs["http.server_name"])
99+
assert.Equal(t, label.IntValue(http.StatusOK), attrs["http.status_code"])
100+
assert.Equal(t, label.StringValue("GET"), attrs["http.method"])
101+
assert.Equal(t, label.StringValue("/book/foo"), attrs["http.target"])
121102
// TODO: span name should show router template, eg /book/:title
122103
//assert.Equal(t, label.StringValue("/book/:title"), span.Attributes["http.route"])
123104
}
@@ -138,7 +119,7 @@ func TestGetSpanNotInstrumented(t *testing.T) {
138119
}
139120

140121
func TestPropagationWithGlobalPropagators(t *testing.T) {
141-
tracer := mocktrace.NewTracer("test-tracer")
122+
tracer := tracetest.NewTracerProvider().Tracer("test-tracer")
142123

143124
r := httptest.NewRequest("GET", "/user/123", nil)
144125
w := httptest.NewRecorder()
@@ -147,21 +128,22 @@ func TestPropagationWithGlobalPropagators(t *testing.T) {
147128
otelpropagation.InjectHTTP(ctx, otelglobal.Propagators(), r.Header)
148129

149130
m := macaron.Classic()
150-
m.Use(Middleware("foobar", WithTracer(tracer)))
131+
m.Use(Middleware("foobar"))
151132
m.Get("/user/:id", func(ctx *macaron.Context) {
152133
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
153-
mspan, ok := span.(*mocktrace.Span)
134+
mspan, ok := span.(*tracetest.Span)
154135
require.True(t, ok)
155136
assert.Equal(t, pspan.SpanContext().TraceID, mspan.SpanContext().TraceID)
156-
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID)
137+
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID())
157138
ctx.Resp.WriteHeader(http.StatusOK)
158139
})
159140

160141
m.ServeHTTP(w, r)
161142
}
162143

163144
func TestPropagationWithCustomPropagators(t *testing.T) {
164-
tracer := mocktrace.NewTracer("test-tracer")
145+
tp := tracetest.NewTracerProvider()
146+
tracer := tp.Tracer("test-tracer")
165147
b3 := b3prop.B3{}
166148
props := otelpropagation.New(
167149
otelpropagation.WithExtractors(b3),
@@ -175,13 +157,13 @@ func TestPropagationWithCustomPropagators(t *testing.T) {
175157
otelpropagation.InjectHTTP(ctx, props, r.Header)
176158

177159
m := macaron.Classic()
178-
m.Use(Middleware("foobar", WithTracer(tracer), WithPropagators(props)))
160+
m.Use(Middleware("foobar", WithTracerProvider(tp), WithPropagators(props)))
179161
m.Get("/user/:id", func(ctx *macaron.Context) {
180162
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
181-
mspan, ok := span.(*mocktrace.Span)
163+
mspan, ok := span.(*tracetest.Span)
182164
require.True(t, ok)
183165
assert.Equal(t, pspan.SpanContext().TraceID, mspan.SpanContext().TraceID)
184-
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID)
166+
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID())
185167
ctx.Resp.WriteHeader(http.StatusOK)
186168
})
187169

0 commit comments

Comments
 (0)