This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacaron_test.go
94 lines (77 loc) · 2.71 KB
/
macaron_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelmacaron
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/macaron.v1"
b3prop "go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
oteltrace "go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
)
func TestGetSpanNotInstrumented(t *testing.T) {
m := macaron.Classic()
m.Get("/user/:id", func(ctx *macaron.Context) {
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
ok := !span.SpanContext().IsValid()
assert.True(t, ok)
ctx.Resp.WriteHeader(http.StatusOK)
})
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
}
func TestPropagationWithGlobalPropagators(t *testing.T) {
tracer := noop.NewTracerProvider().Tracer("test-tracer")
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(noop.NewTracerProvider())
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx := context.Background()
sc := oteltrace.NewSpanContext(oteltrace.SpanContextConfig{
TraceID: oteltrace.TraceID{0x01},
SpanID: oteltrace.SpanID{0x01},
})
ctx = oteltrace.ContextWithRemoteSpanContext(ctx, sc)
ctx, _ = tracer.Start(ctx, "test")
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header))
m := macaron.Classic()
m.Use(Middleware("foobar"))
m.Get("/user/:id", func(ctx *macaron.Context) {
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID())
assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID())
ctx.Resp.WriteHeader(http.StatusOK)
})
m.ServeHTTP(w, r)
}
func TestPropagationWithCustomPropagators(t *testing.T) {
tp := noop.NewTracerProvider()
otel.SetTracerProvider(tp)
tracer := tp.Tracer("test-tracer")
b3 := b3prop.New()
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx := context.Background()
sc := oteltrace.NewSpanContext(oteltrace.SpanContextConfig{
TraceID: oteltrace.TraceID{0x01},
SpanID: oteltrace.SpanID{0x01},
})
ctx = oteltrace.ContextWithRemoteSpanContext(ctx, sc)
ctx, _ = tracer.Start(ctx, "test")
b3.Inject(ctx, propagation.HeaderCarrier(r.Header))
m := macaron.Classic()
m.Use(Middleware("foobar", WithTracerProvider(tp), WithPropagators(b3)))
m.Get("/user/:id", func(ctx *macaron.Context) {
span := oteltrace.SpanFromContext(ctx.Req.Request.Context())
assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID())
assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID())
ctx.Resp.WriteHeader(http.StatusOK)
})
m.ServeHTTP(w, r)
}