@@ -3,6 +3,7 @@ package jaegertracing
3
3
import (
4
4
"bytes"
5
5
"errors"
6
+ "fmt"
6
7
"net/http"
7
8
"net/http/httptest"
8
9
"testing"
@@ -117,15 +118,62 @@ func TestTraceWithDefaultConfig(t *testing.T) {
117
118
118
119
e := echo .New ()
119
120
e .Use (Trace (tracer ))
120
- req := httptest .NewRequest (http .MethodGet , "/" , nil )
121
- rec := httptest .NewRecorder ()
122
- e .ServeHTTP (rec , req )
123
121
124
- assert .Equal (t , "GET" , tracer .currentSpan ().getTag ("http.method" ))
125
- assert .Equal (t , "/" , tracer .currentSpan ().getTag ("http.url" ))
126
- assert .Equal (t , defaultComponentName , tracer .currentSpan ().getTag ("component" ))
127
- assert .Equal (t , uint16 (200 ), tracer .currentSpan ().getTag ("http.status_code" ))
128
- assert .Equal (t , true , tracer .currentSpan ().getTag ("error" ))
122
+ e .GET ("/hello" , func (c echo.Context ) error {
123
+ return c .String (http .StatusOK , "world" )
124
+ })
125
+
126
+ e .GET ("/giveme400" , func (c echo.Context ) error {
127
+ return echo .NewHTTPError (http .StatusBadRequest , "baaaad request" )
128
+ })
129
+
130
+ e .GET ("/givemeerror" , func (c echo.Context ) error {
131
+ return fmt .Errorf ("internal stuff went wrong" )
132
+ })
133
+
134
+ t .Run ("successful call" , func (t * testing.T ) {
135
+ req := httptest .NewRequest (http .MethodGet , "/hello" , nil )
136
+ rec := httptest .NewRecorder ()
137
+ e .ServeHTTP (rec , req )
138
+
139
+ assert .Equal (t , "GET" , tracer .currentSpan ().getTag ("http.method" ))
140
+ assert .Equal (t , "/hello" , tracer .currentSpan ().getTag ("http.url" ))
141
+ assert .Equal (t , defaultComponentName , tracer .currentSpan ().getTag ("component" ))
142
+ assert .Equal (t , uint16 (200 ), tracer .currentSpan ().getTag ("http.status_code" ))
143
+ assert .NotEqual (t , true , tracer .currentSpan ().getTag ("error" ))
144
+ })
145
+
146
+ t .Run ("error from echo" , func (t * testing.T ) {
147
+ req := httptest .NewRequest (http .MethodGet , "/idontexist" , nil )
148
+ rec := httptest .NewRecorder ()
149
+ e .ServeHTTP (rec , req )
150
+
151
+ assert .Equal (t , "GET" , tracer .currentSpan ().getTag ("http.method" ))
152
+ assert .Equal (t , "/idontexist" , tracer .currentSpan ().getTag ("http.url" ))
153
+ assert .Equal (t , defaultComponentName , tracer .currentSpan ().getTag ("component" ))
154
+ assert .Equal (t , uint16 (404 ), tracer .currentSpan ().getTag ("http.status_code" ))
155
+ assert .Equal (t , true , tracer .currentSpan ().getTag ("error" ))
156
+ })
157
+
158
+ t .Run ("custom http error" , func (t * testing.T ) {
159
+ req := httptest .NewRequest (http .MethodGet , "/giveme400" , nil )
160
+ rec := httptest .NewRecorder ()
161
+ e .ServeHTTP (rec , req )
162
+
163
+ assert .Equal (t , uint16 (400 ), tracer .currentSpan ().getTag ("http.status_code" ))
164
+ assert .Equal (t , true , tracer .currentSpan ().getTag ("error" ))
165
+ assert .Equal (t , "baaaad request" , tracer .currentSpan ().getTag ("error.message" ))
166
+ })
167
+
168
+ t .Run ("unknown error" , func (t * testing.T ) {
169
+ req := httptest .NewRequest (http .MethodGet , "/givemeerror" , nil )
170
+ rec := httptest .NewRecorder ()
171
+ e .ServeHTTP (rec , req )
172
+
173
+ assert .Equal (t , uint16 (500 ), tracer .currentSpan ().getTag ("http.status_code" ))
174
+ assert .Equal (t , true , tracer .currentSpan ().getTag ("error" ))
175
+ assert .Equal (t , "internal stuff went wrong" , tracer .currentSpan ().getTag ("error.message" ))
176
+ })
129
177
}
130
178
131
179
func TestTraceWithConfig (t * testing.T ) {
@@ -134,7 +182,7 @@ func TestTraceWithConfig(t *testing.T) {
134
182
e := echo .New ()
135
183
e .Use (TraceWithConfig (TraceConfig {
136
184
Tracer : tracer ,
137
- componentName : "EchoTracer" ,
185
+ ComponentName : "EchoTracer" ,
138
186
}))
139
187
req := httptest .NewRequest (http .MethodGet , "/trace" , nil )
140
188
rec := httptest .NewRecorder ()
@@ -153,7 +201,7 @@ func TestTraceWithConfigOfBodyDump(t *testing.T) {
153
201
e := echo .New ()
154
202
e .Use (TraceWithConfig (TraceConfig {
155
203
Tracer : tracer ,
156
- componentName : "EchoTracer" ,
204
+ ComponentName : "EchoTracer" ,
157
205
IsBodyDump : true ,
158
206
}))
159
207
e .POST ("/trace" , func (c echo.Context ) error {
@@ -169,6 +217,8 @@ func TestTraceWithConfigOfBodyDump(t *testing.T) {
169
217
assert .Equal (t , "/trace" , tracer .currentSpan ().getTag ("http.url" ))
170
218
assert .Equal (t , `{"name": "Lorem"}` , tracer .currentSpan ().getTag ("http.req.body" ))
171
219
assert .Equal (t , `Hi` , tracer .currentSpan ().getTag ("http.resp.body" ))
220
+ assert .Equal (t , uint16 (200 ), tracer .currentSpan ().getTag ("http.status_code" ))
221
+ assert .Equal (t , nil , tracer .currentSpan ().getTag ("error" ))
172
222
assert .Equal (t , true , tracer .hasStartSpanWithOption )
173
223
174
224
}
0 commit comments