@@ -3,6 +3,7 @@ package jaegertracing
33import (
44 "bytes"
55 "errors"
6+ "fmt"
67 "net/http"
78 "net/http/httptest"
89 "testing"
@@ -117,15 +118,62 @@ func TestTraceWithDefaultConfig(t *testing.T) {
117118
118119 e := echo .New ()
119120 e .Use (Trace (tracer ))
120- req := httptest .NewRequest (http .MethodGet , "/" , nil )
121- rec := httptest .NewRecorder ()
122- e .ServeHTTP (rec , req )
123121
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+ })
129177}
130178
131179func TestTraceWithConfig (t * testing.T ) {
@@ -134,7 +182,7 @@ func TestTraceWithConfig(t *testing.T) {
134182 e := echo .New ()
135183 e .Use (TraceWithConfig (TraceConfig {
136184 Tracer : tracer ,
137- componentName : "EchoTracer" ,
185+ ComponentName : "EchoTracer" ,
138186 }))
139187 req := httptest .NewRequest (http .MethodGet , "/trace" , nil )
140188 rec := httptest .NewRecorder ()
@@ -153,7 +201,7 @@ func TestTraceWithConfigOfBodyDump(t *testing.T) {
153201 e := echo .New ()
154202 e .Use (TraceWithConfig (TraceConfig {
155203 Tracer : tracer ,
156- componentName : "EchoTracer" ,
204+ ComponentName : "EchoTracer" ,
157205 IsBodyDump : true ,
158206 }))
159207 e .POST ("/trace" , func (c echo.Context ) error {
@@ -169,6 +217,8 @@ func TestTraceWithConfigOfBodyDump(t *testing.T) {
169217 assert .Equal (t , "/trace" , tracer .currentSpan ().getTag ("http.url" ))
170218 assert .Equal (t , `{"name": "Lorem"}` , tracer .currentSpan ().getTag ("http.req.body" ))
171219 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" ))
172222 assert .Equal (t , true , tracer .hasStartSpanWithOption )
173223
174224}
0 commit comments