| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Based on https://github.com/DataDog/dd-trace-go/blob/8fb554ff7cf694267f9077ae35e27ce4689ed8b6/contrib/gin-gonic/gin/gintrace_test.go |
| 5 |
|
| 6 |
package otelecho_test |
| 7 |
|
| 8 |
import ( |
| 9 |
"errors" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/labstack/echo/v4" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"go.opentelemetry.io/otel" |
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
"go.opentelemetry.io/otel/codes" |
| 20 |
"go.opentelemetry.io/otel/sdk/trace" |
| 21 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 22 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 23 |
|
| 24 |
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho" |
| 25 |
) |
| 26 |
|
| 27 |
func TestChildSpanFromGlobalTracer(t *testing.T) { |
| 28 |
sr := tracetest.NewSpanRecorder() |
| 29 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 30 |
otel.SetTracerProvider(provider) |
| 31 |
|
| 32 |
router := echo.New() |
| 33 |
router.Use(otelecho.Middleware("foobar")) |
| 34 |
router.GET("/user/:id", func(c echo.Context) error { |
| 35 |
return c.NoContent(http.StatusOK) |
| 36 |
}) |
| 37 |
|
| 38 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 39 |
w := httptest.NewRecorder() |
| 40 |
|
| 41 |
router.ServeHTTP(w, r) |
| 42 |
assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") |
| 43 |
assert.Len(t, sr.Ended(), 1) |
| 44 |
} |
| 45 |
|
| 46 |
func TestChildSpanFromCustomTracer(t *testing.T) { |
| 47 |
sr := tracetest.NewSpanRecorder() |
| 48 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 49 |
|
| 50 |
router := echo.New() |
| 51 |
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider))) |
| 52 |
router.GET("/user/:id", func(c echo.Context) error { |
| 53 |
return c.NoContent(http.StatusOK) |
| 54 |
}) |
| 55 |
|
| 56 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 57 |
w := httptest.NewRecorder() |
| 58 |
|
| 59 |
router.ServeHTTP(w, r) |
| 60 |
assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") |
| 61 |
assert.Len(t, sr.Ended(), 1) |
| 62 |
} |
| 63 |
|
| 64 |
func TestTrace200(t *testing.T) { |
| 65 |
sr := tracetest.NewSpanRecorder() |
| 66 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 67 |
|
| 68 |
router := echo.New() |
| 69 |
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider))) |
| 70 |
router.GET("/user/:id", func(c echo.Context) error { |
| 71 |
id := c.Param("id") |
| 72 |
return c.String(http.StatusOK, id) |
| 73 |
}) |
| 74 |
|
| 75 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 76 |
w := httptest.NewRecorder() |
| 77 |
|
| 78 |
// do and verify the request |
| 79 |
router.ServeHTTP(w, r) |
| 80 |
response := w.Result() |
| 81 |
require.Equal(t, http.StatusOK, response.StatusCode) |
| 82 |
|
| 83 |
// verify traces look good |
| 84 |
spans := sr.Ended() |
| 85 |
require.Len(t, spans, 1) |
| 86 |
span := spans[0] |
| 87 |
assert.Equal(t, "GET /user/:id", span.Name()) |
| 88 |
assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind()) |
| 89 |
attrs := span.Attributes() |
| 90 |
assert.Contains(t, attrs, attribute.String("server.address", "foobar")) |
| 91 |
assert.Contains(t, attrs, attribute.Int("http.response.status_code", http.StatusOK)) |
| 92 |
assert.Contains(t, attrs, attribute.String("http.request.method", "GET")) |
| 93 |
assert.Contains(t, attrs, attribute.String("http.route", "/user/:id")) |
| 94 |
} |
| 95 |
|
| 96 |
func TestError(t *testing.T) { |
| 97 |
sr := tracetest.NewSpanRecorder() |
| 98 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 99 |
|
| 100 |
// setup |
| 101 |
router := echo.New() |
| 102 |
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider))) |
| 103 |
wantErr := errors.New("oh no") |
| 104 |
// configure a handler that returns an error and 5xx status |
| 105 |
// code |
| 106 |
router.GET("/server_err", func(echo.Context) error { |
| 107 |
return wantErr |
| 108 |
}) |
| 109 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/server_err", http.NoBody) |
| 110 |
w := httptest.NewRecorder() |
| 111 |
router.ServeHTTP(w, r) |
| 112 |
response := w.Result() |
| 113 |
assert.Equal(t, http.StatusInternalServerError, response.StatusCode) |
| 114 |
|
| 115 |
// verify the errors and status are correct |
| 116 |
spans := sr.Ended() |
| 117 |
require.Len(t, spans, 1) |
| 118 |
span := spans[0] |
| 119 |
assert.Equal(t, "GET /server_err", span.Name()) |
| 120 |
attrs := span.Attributes() |
| 121 |
assert.Contains(t, attrs, attribute.String("server.address", "foobar")) |
| 122 |
assert.Contains(t, attrs, attribute.Int("http.response.status_code", http.StatusInternalServerError)) |
| 123 |
assert.Contains(t, attrs, attribute.String("echo.error", "oh no")) |
| 124 |
// server errors set the status |
| 125 |
assert.Equal(t, codes.Error, span.Status().Code) |
| 126 |
} |
| 127 |
|
| 128 |
func TestStatusError(t *testing.T) { |
| 129 |
for _, tc := range []struct { |
| 130 |
name string |
| 131 |
echoError string |
| 132 |
statusCode int |
| 133 |
spanCode codes.Code |
| 134 |
handler func(c echo.Context) error |
| 135 |
}{ |
| 136 |
{ |
| 137 |
name: "StandardError", |
| 138 |
echoError: "oh no", |
| 139 |
statusCode: http.StatusInternalServerError, |
| 140 |
spanCode: codes.Error, |
| 141 |
handler: func(echo.Context) error { |
| 142 |
return errors.New("oh no") |
| 143 |
}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "EchoHTTPServerError", |
| 147 |
echoError: "code=500, message=my error message", |
| 148 |
statusCode: http.StatusInternalServerError, |
| 149 |
spanCode: codes.Error, |
| 150 |
handler: func(echo.Context) error { |
| 151 |
return echo.NewHTTPError(http.StatusInternalServerError, "my error message") |
| 152 |
}, |
| 153 |
}, |
| 154 |
{ |
| 155 |
name: "EchoHTTPClientError", |
| 156 |
echoError: "code=400, message=my error message", |
| 157 |
statusCode: http.StatusBadRequest, |
| 158 |
spanCode: codes.Unset, |
| 159 |
handler: func(echo.Context) error { |
| 160 |
return echo.NewHTTPError(http.StatusBadRequest, "my error message") |
| 161 |
}, |
| 162 |
}, |
| 163 |
} { |
| 164 |
t.Run(tc.name, func(t *testing.T) { |
| 165 |
sr := tracetest.NewSpanRecorder() |
| 166 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(sr)) |
| 167 |
|
| 168 |
router := echo.New() |
| 169 |
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider))) |
| 170 |
router.GET("/err", tc.handler) |
| 171 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/err", http.NoBody) |
| 172 |
w := httptest.NewRecorder() |
| 173 |
router.ServeHTTP(w, r) |
| 174 |
|
| 175 |
spans := sr.Ended() |
| 176 |
require.Len(t, spans, 1) |
| 177 |
span := spans[0] |
| 178 |
assert.Equal(t, "GET /err", span.Name()) |
| 179 |
assert.Equal(t, tc.spanCode, span.Status().Code) |
| 180 |
|
| 181 |
attrs := span.Attributes() |
| 182 |
assert.Contains(t, attrs, attribute.String("server.address", "foobar")) |
| 183 |
assert.Contains(t, attrs, attribute.String("http.route", "/err")) |
| 184 |
assert.Contains(t, attrs, attribute.String("http.request.method", "GET")) |
| 185 |
assert.Contains(t, attrs, attribute.Int("http.response.status_code", tc.statusCode)) |
| 186 |
assert.Contains(t, attrs, attribute.String("echo.error", tc.echoError)) |
| 187 |
}) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func TestErrorNotSwallowedByMiddleware(t *testing.T) { |
| 192 |
e := echo.New() |
| 193 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/err", http.NoBody) |
| 194 |
w := httptest.NewRecorder() |
| 195 |
c := e.NewContext(r, w) |
| 196 |
h := otelecho.Middleware("foobar")(echo.HandlerFunc(func(echo.Context) error { |
| 197 |
return assert.AnError |
| 198 |
})) |
| 199 |
|
| 200 |
err := h(c) |
| 201 |
assert.Equal(t, assert.AnError, err) |
| 202 |
} |
| 203 |
|
| 204 |
func TestSpanNameFormatter(t *testing.T) { |
| 205 |
imsb := tracetest.NewInMemoryExporter() |
| 206 |
provider := trace.NewTracerProvider(trace.WithSyncer(imsb)) |
| 207 |
|
| 208 |
tests := []struct { |
| 209 |
name string |
| 210 |
method string |
| 211 |
path string |
| 212 |
url string |
| 213 |
expected string |
| 214 |
}{ |
| 215 |
// Test for standard methods |
| 216 |
{"standard method of GET", http.MethodGet, "/user/:id", "/user/123", "GET /user/:id"}, |
| 217 |
{"standard method of HEAD", http.MethodHead, "/user/:id", "/user/123", "HEAD /user/:id"}, |
| 218 |
{"standard method of POST", http.MethodPost, "/user/:id", "/user/123", "POST /user/:id"}, |
| 219 |
{"standard method of PUT", http.MethodPut, "/user/:id", "/user/123", "PUT /user/:id"}, |
| 220 |
{"standard method of PATCH", http.MethodPatch, "/user/:id", "/user/123", "PATCH /user/:id"}, |
| 221 |
{"standard method of DELETE", http.MethodDelete, "/user/:id", "/user/123", "DELETE /user/:id"}, |
| 222 |
{"standard method of CONNECT", http.MethodConnect, "/user/:id", "/user/123", "CONNECT /user/:id"}, |
| 223 |
{"standard method of OPTIONS", http.MethodOptions, "/user/:id", "/user/123", "OPTIONS /user/:id"}, |
| 224 |
{"standard method of TRACE", http.MethodTrace, "/user/:id", "/user/123", "TRACE /user/:id"}, |
| 225 |
{"standard method of GET, but it's another route.", http.MethodGet, "/", "/", "GET /"}, |
| 226 |
|
| 227 |
// Test for no route |
| 228 |
{"no route", http.MethodGet, "/", "/user/id", "GET"}, |
| 229 |
|
| 230 |
// Test for case-insensitive method |
| 231 |
{"all lowercase", "get", "/user/123", "/user/123", "GET /user/123"}, |
| 232 |
{"partial capitalization", "Get", "/user/123", "/user/123", "GET /user/123"}, |
| 233 |
{"full capitalization", "GET", "/user/:id", "/user/123", "GET /user/:id"}, |
| 234 |
|
| 235 |
// Test for invalid method |
| 236 |
{"invalid method", "INVALID", "/user/123", "/user/123", "HTTP /user/123"}, |
| 237 |
} |
| 238 |
|
| 239 |
for _, test := range tests { |
| 240 |
t.Run(test.name, func(t *testing.T) { |
| 241 |
defer imsb.Reset() |
| 242 |
|
| 243 |
router := echo.New() |
| 244 |
router.Use(otelecho.Middleware("foobar", otelecho.WithTracerProvider(provider))) |
| 245 |
router.Add(test.method, test.path, func(c echo.Context) error { |
| 246 |
return c.NoContent(http.StatusOK) |
| 247 |
}) |
| 248 |
|
| 249 |
r := httptest.NewRequestWithContext(t.Context(), test.method, test.url, http.NoBody) |
| 250 |
w := httptest.NewRecorder() |
| 251 |
router.ServeHTTP(w, r) |
| 252 |
|
| 253 |
spans := imsb.GetSpans() |
| 254 |
require.Len(t, spans, 1) |
| 255 |
assert.Equal(t, test.expected, spans[0].Name) |
| 256 |
}) |
| 257 |
} |
| 258 |
} |