@@ -24,49 +24,27 @@ import (
24
24
"github.com/stretchr/testify/require"
25
25
"gopkg.in/macaron.v1"
26
26
27
- mocktrace "go.opentelemetry.io/contrib/internal/trace"
28
27
b3prop "go.opentelemetry.io/contrib/propagators/b3"
29
28
otelglobal "go.opentelemetry.io/otel/api/global"
30
29
otelpropagation "go.opentelemetry.io/otel/api/propagation"
31
30
oteltrace "go.opentelemetry.io/otel/api/trace"
31
+ "go.opentelemetry.io/otel/api/trace/tracetest"
32
32
"go.opentelemetry.io/otel/label"
33
33
)
34
34
35
35
func TestChildSpanFromGlobalTracer (t * testing.T ) {
36
- otelglobal .SetTracerProvider (& mocktrace. TracerProvider {} )
36
+ otelglobal .SetTracerProvider (tracetest . NewTracerProvider () )
37
37
38
38
m := macaron .Classic ()
39
39
m .Use (Middleware ("foobar" ))
40
40
m .Get ("/user/:id" , func (ctx * macaron.Context ) {
41
41
span := oteltrace .SpanFromContext (ctx .Req .Request .Context ())
42
- _ , ok := span .(* mocktrace .Span )
42
+ _ , ok := span .(* tracetest .Span )
43
43
assert .True (t , ok )
44
44
spanTracer := span .Tracer ()
45
- mockTracer , ok := spanTracer .(* mocktrace .Tracer )
45
+ mockTracer , ok := spanTracer .(* tracetest .Tracer )
46
46
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 )
70
48
ctx .Resp .WriteHeader (http .StatusOK )
71
49
})
72
50
@@ -77,10 +55,11 @@ func TestChildSpanFromCustomTracer(t *testing.T) {
77
55
}
78
56
79
57
func TestChildSpanNames (t * testing.T ) {
80
- tracer := mocktrace .NewTracer ("test-tracer" )
58
+ sr := new (tracetest.StandardSpanRecorder )
59
+ tp := tracetest .NewTracerProvider (tracetest .WithSpanRecorder (sr ))
81
60
82
61
m := macaron .Classic ()
83
- m .Use (Middleware ("foobar" , WithTracer ( tracer )))
62
+ m .Use (Middleware ("foobar" , WithTracerProvider ( tp )))
84
63
m .Get ("/user/:id" , func (ctx * macaron.Context ) {
85
64
ctx .Resp .WriteHeader (http .StatusOK )
86
65
})
@@ -94,30 +73,32 @@ func TestChildSpanNames(t *testing.T) {
94
73
r := httptest .NewRequest ("GET" , "/user/123" , nil )
95
74
w := httptest .NewRecorder ()
96
75
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"])
108
76
109
77
r = httptest .NewRequest ("GET" , "/book/foo" , nil )
110
78
w = httptest .NewRecorder ()
111
79
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" ])
121
102
// TODO: span name should show router template, eg /book/:title
122
103
//assert.Equal(t, label.StringValue("/book/:title"), span.Attributes["http.route"])
123
104
}
@@ -138,7 +119,7 @@ func TestGetSpanNotInstrumented(t *testing.T) {
138
119
}
139
120
140
121
func TestPropagationWithGlobalPropagators (t * testing.T ) {
141
- tracer := mocktrace . NewTracer ("test-tracer" )
122
+ tracer := tracetest . NewTracerProvider (). Tracer ("test-tracer" )
142
123
143
124
r := httptest .NewRequest ("GET" , "/user/123" , nil )
144
125
w := httptest .NewRecorder ()
@@ -147,21 +128,22 @@ func TestPropagationWithGlobalPropagators(t *testing.T) {
147
128
otelpropagation .InjectHTTP (ctx , otelglobal .Propagators (), r .Header )
148
129
149
130
m := macaron .Classic ()
150
- m .Use (Middleware ("foobar" , WithTracer ( tracer ) ))
131
+ m .Use (Middleware ("foobar" ))
151
132
m .Get ("/user/:id" , func (ctx * macaron.Context ) {
152
133
span := oteltrace .SpanFromContext (ctx .Req .Request .Context ())
153
- mspan , ok := span .(* mocktrace .Span )
134
+ mspan , ok := span .(* tracetest .Span )
154
135
require .True (t , ok )
155
136
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 () )
157
138
ctx .Resp .WriteHeader (http .StatusOK )
158
139
})
159
140
160
141
m .ServeHTTP (w , r )
161
142
}
162
143
163
144
func TestPropagationWithCustomPropagators (t * testing.T ) {
164
- tracer := mocktrace .NewTracer ("test-tracer" )
145
+ tp := tracetest .NewTracerProvider ()
146
+ tracer := tp .Tracer ("test-tracer" )
165
147
b3 := b3prop.B3 {}
166
148
props := otelpropagation .New (
167
149
otelpropagation .WithExtractors (b3 ),
@@ -175,13 +157,13 @@ func TestPropagationWithCustomPropagators(t *testing.T) {
175
157
otelpropagation .InjectHTTP (ctx , props , r .Header )
176
158
177
159
m := macaron .Classic ()
178
- m .Use (Middleware ("foobar" , WithTracer ( tracer ), WithPropagators (props )))
160
+ m .Use (Middleware ("foobar" , WithTracerProvider ( tp ), WithPropagators (props )))
179
161
m .Get ("/user/:id" , func (ctx * macaron.Context ) {
180
162
span := oteltrace .SpanFromContext (ctx .Req .Request .Context ())
181
- mspan , ok := span .(* mocktrace .Span )
163
+ mspan , ok := span .(* tracetest .Span )
182
164
require .True (t , ok )
183
165
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 () )
185
167
ctx .Resp .WriteHeader (http .StatusOK )
186
168
})
187
169
0 commit comments