-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing_test.go
More file actions
252 lines (197 loc) · 6.86 KB
/
tracing_test.go
File metadata and controls
252 lines (197 loc) · 6.86 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
api "dappco.re/go/core/api"
)
// setupTracing creates an in-memory span exporter, wires it into a
// synchronous TracerProvider, and installs it as the global provider.
// The returned cleanup function restores the previous global state.
func setupTracing(t *testing.T) (*tracetest.InMemoryExporter, func()) {
t.Helper()
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSyncer(exporter),
)
prevTP := otel.GetTracerProvider()
prevProp := otel.GetTextMapPropagator()
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.TraceContext{})
cleanup := func() {
_ = tp.Shutdown(context.Background())
otel.SetTracerProvider(prevTP)
otel.SetTextMapPropagator(prevProp)
}
return exporter, cleanup
}
// hasAttribute returns true if the span stub's attributes contain a
// matching key (and optionally value).
func hasAttribute(attrs []attribute.KeyValue, key attribute.Key) (attribute.KeyValue, bool) {
for _, a := range attrs {
if a.Key == key {
return a, true
}
}
return attribute.KeyValue{}, false
}
// ── WithTracing ─────────────────────────────────────────────────────────
func TestWithTracing_Good_CreatesSpan(t *testing.T) {
gin.SetMode(gin.TestMode)
exporter, cleanup := setupTracing(t)
defer cleanup()
e, _ := api.New(api.WithTracing("test-service"))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span, got none")
}
// The span name should contain the route.
span := spans[0]
if span.Name == "" {
t.Fatal("expected span to have a name")
}
}
func TestWithTracing_Good_SpanHasHTTPAttributes(t *testing.T) {
gin.SetMode(gin.TestMode)
exporter, cleanup := setupTracing(t)
defer cleanup()
e, _ := api.New(api.WithTracing("test-service"))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span")
}
span := spans[0]
attrs := span.Attributes
// Check http.request.method attribute.
if kv, ok := hasAttribute(attrs, attribute.Key("http.request.method")); !ok {
t.Error("expected span to have http.request.method attribute")
} else if kv.Value.AsString() != "GET" {
t.Errorf("expected http.request.method=GET, got %q", kv.Value.AsString())
}
// Check http.route attribute.
if _, ok := hasAttribute(attrs, attribute.Key("http.route")); !ok {
t.Error("expected span to have http.route attribute")
}
// Check http.response.status_code attribute.
if kv, ok := hasAttribute(attrs, attribute.Key("http.response.status_code")); !ok {
t.Error("expected span to have http.response.status_code attribute")
} else if kv.Value.AsInt64() != 200 {
t.Errorf("expected http.response.status_code=200, got %d", kv.Value.AsInt64())
}
}
func TestWithTracing_Good_PropagatesTraceContext(t *testing.T) {
gin.SetMode(gin.TestMode)
exporter, cleanup := setupTracing(t)
defer cleanup()
e, _ := api.New(api.WithTracing("test-service"))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
// Inject a W3C traceparent header to simulate an upstream service.
// Format: version-traceID-spanID-flags
req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span")
}
span := spans[0]
// The span should have a parent with the trace ID from the traceparent header.
parentTraceID := span.Parent.TraceID()
expectedTraceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
if parentTraceID != expectedTraceID {
t.Errorf("expected parent trace ID %s, got %s", expectedTraceID, parentTraceID)
}
// The span should also share the same trace ID (trace propagation).
spanTraceID := span.SpanContext.TraceID()
if spanTraceID != expectedTraceID {
t.Errorf("expected span trace ID %s to match parent %s", spanTraceID, expectedTraceID)
}
// The parent span ID should match what was in the traceparent header.
parentSpanID := span.Parent.SpanID()
expectedSpanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
if parentSpanID != expectedSpanID {
t.Errorf("expected parent span ID %s, got %s", expectedSpanID, parentSpanID)
}
}
func TestWithTracing_Good_CombinesWithOtherMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
exporter, cleanup := setupTracing(t)
defer cleanup()
e, _ := api.New(
api.WithTracing("test-service"),
api.WithRequestID(),
)
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Tracing should produce spans.
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span from WithTracing")
}
// WithRequestID should set the X-Request-ID header.
if w.Header().Get("X-Request-ID") == "" {
t.Fatal("expected X-Request-ID header from WithRequestID")
}
}
func TestWithTracing_Good_ServiceNameInSpan(t *testing.T) {
gin.SetMode(gin.TestMode)
exporter, cleanup := setupTracing(t)
defer cleanup()
const serviceName = "my-awesome-api"
e, _ := api.New(api.WithTracing(serviceName))
e.Register(&stubGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/stub/ping", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected at least one span")
}
span := spans[0]
// otelgin uses the serviceName as the server.address attribute.
if kv, ok := hasAttribute(span.Attributes, attribute.Key("server.address")); !ok {
t.Error("expected span to have server.address attribute for service name")
} else if kv.Value.AsString() != serviceName {
t.Errorf("expected server.address=%q, got %q", serviceName, kv.Value.AsString())
}
}