forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_test.go
235 lines (198 loc) · 7.64 KB
/
echo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Based on https://github.com/DataDog/dd-trace-go/blob/8fb554ff7cf694267f9077ae35e27ce4689ed8b6/contrib/gin-gonic/gin/gintrace_test.go
package otelecho
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/propagation"
b3prop "go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel/label"
oteltrace "go.opentelemetry.io/otel/trace"
)
func TestChildSpanFromGlobalTracer(t *testing.T) {
otel.SetTracerProvider(oteltest.NewTracerProvider())
router := echo.New()
router.Use(Middleware("foobar"))
router.GET("/user/:id", func(c echo.Context) error {
span := oteltrace.SpanFromContext(c.Request().Context())
_, ok := span.(*oteltest.Span)
assert.True(t, ok)
spanTracer := span.Tracer()
mockTracer, ok := spanTracer.(*oteltest.Tracer)
require.True(t, ok)
assert.Equal(t, "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho", mockTracer.Name)
return c.NoContent(200)
})
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
}
func TestChildSpanFromCustomTracer(t *testing.T) {
provider := oteltest.NewTracerProvider()
router := echo.New()
router.Use(Middleware("foobar", WithTracerProvider(provider)))
router.GET("/user/:id", func(c echo.Context) error {
span := oteltrace.SpanFromContext(c.Request().Context())
_, ok := span.(*oteltest.Span)
assert.True(t, ok)
spanTracer := span.Tracer()
mockTracer, ok := spanTracer.(*oteltest.Tracer)
require.True(t, ok)
assert.Equal(t, tracerName, mockTracer.Name)
return c.NoContent(200)
})
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
}
func TestTrace200(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
router := echo.New()
router.Use(Middleware("foobar", WithTracerProvider(provider)))
router.GET("/user/:id", func(c echo.Context) error {
span := oteltrace.SpanFromContext(c.Request().Context())
mspan, ok := span.(*oteltest.Span)
require.True(t, ok)
assert.Equal(t, label.StringValue("foobar"), mspan.Attributes()["http.server_name"])
id := c.Param("id")
return c.String(200, id)
})
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
// do and verify the request
router.ServeHTTP(w, r)
response := w.Result()
require.Equal(t, http.StatusOK, response.StatusCode)
// verify traces look good
spans := sr.Completed()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/user/:id", span.Name())
assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind())
assert.Equal(t, label.StringValue("foobar"), span.Attributes()["http.server_name"])
assert.Equal(t, label.IntValue(http.StatusOK), span.Attributes()["http.status_code"])
assert.Equal(t, label.StringValue("GET"), span.Attributes()["http.method"])
assert.Equal(t, label.StringValue("/user/123"), span.Attributes()["http.target"])
assert.Equal(t, label.StringValue("/user/:id"), span.Attributes()["http.route"])
}
func TestError(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
// setup
router := echo.New()
router.Use(Middleware("foobar", WithTracerProvider(provider)))
wantErr := errors.New("oh no")
// configure a handler that returns an error and 5xx status
// code
router.GET("/server_err", func(c echo.Context) error {
return wantErr
})
r := httptest.NewRequest("GET", "/server_err", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
// verify the errors and status are correct
spans := sr.Completed()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/server_err", span.Name())
assert.Equal(t, label.StringValue("foobar"), span.Attributes()["http.server_name"])
assert.Equal(t, label.IntValue(http.StatusInternalServerError), span.Attributes()["http.status_code"])
assert.Equal(t, label.StringValue("oh no"), span.Attributes()["echo.error"])
// server errors set the status
assert.Equal(t, codes.Error, span.StatusCode())
}
func TestErrorOnlyHandledOnce(t *testing.T) {
router := echo.New()
timesHandlingError := 0
router.HTTPErrorHandler = func(e error, c echo.Context) {
timesHandlingError++
}
router.Use(Middleware("test-service"))
router.GET("/", func(c echo.Context) error {
return errors.New("Mock Error")
})
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
assert.Equal(t, 1, timesHandlingError)
}
func TestGetSpanNotInstrumented(t *testing.T) {
router := echo.New()
router.GET("/ping", func(c echo.Context) error {
// Assert we don't have a span on the context.
span := oteltrace.SpanFromContext(c.Request().Context())
ok := !span.SpanContext().IsValid()
assert.True(t, ok)
return c.String(200, "ok")
})
r := httptest.NewRequest("GET", "/ping", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)
}
func TestPropagationWithGlobalPropagators(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
otel.SetTextMapPropagator(propagation.TraceContext{})
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx, pspan := provider.Tracer(tracerName).Start(context.Background(), "test")
otel.GetTextMapPropagator().Inject(ctx, r.Header)
router := echo.New()
router.Use(Middleware("foobar", WithTracerProvider(provider)))
router.GET("/user/:id", func(c echo.Context) error {
span := oteltrace.SpanFromContext(c.Request().Context())
mspan, ok := span.(*oteltest.Span)
require.True(t, ok)
assert.Equal(t, pspan.SpanContext().TraceID, mspan.SpanContext().TraceID)
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID())
return c.NoContent(200)
})
router.ServeHTTP(w, r)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator())
}
func TestPropagationWithCustomPropagators(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
provider := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
b3 := b3prop.B3{}
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx, pspan := provider.Tracer(tracerName).Start(context.Background(), "test")
b3.Inject(ctx, r.Header)
router := echo.New()
router.Use(Middleware("foobar", WithTracerProvider(provider), WithPropagators(b3)))
router.GET("/user/:id", func(c echo.Context) error {
span := oteltrace.SpanFromContext(c.Request().Context())
mspan, ok := span.(*oteltest.Span)
require.True(t, ok)
assert.Equal(t, pspan.SpanContext().TraceID, mspan.SpanContext().TraceID)
assert.Equal(t, pspan.SpanContext().SpanID, mspan.ParentSpanID())
return c.NoContent(200)
})
router.ServeHTTP(w, r)
}