Debian Codegraph @

modules/caddyhttp/tracing/module_test.go

raw symbols SCIP
1 package tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/
2
3 import (
4 "contextscip-go gomod github.com/golang/go/src go1.25.1 context/"
5 "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/"
6 "errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/"
7 "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/"
8 "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/"
9 "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/"
10 "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/"
11
12 "go.opentelemetry.io/otel/sdk/tracescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/"
13 "go.opentelemetry.io/otel/sdk/trace/tracetestscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace/tracetest`/"
14
15 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
16 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/"
17 "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/"
18 )
19
20 func TestTracing_UnmarshalCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_UnmarshalCaddyfile().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
21 tests := []struct {
22 name string
23 spanName string
24 spanAttributes map[string]string
25 d *caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#
26 wantErr bool
27 }{
28 {
29 name: "Full config",
30 spanName: "my-span",
31 spanAttributes: map[string]string{
32 "attr1": "value1",
33 "attr2": "value2",
34 },
35 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
36 tracing {
37 span my-span
38 span_attributes {
39 attr1 value1
40 attr2 value2
41 }
42 }`),
43 wantErr: false,
44 },
45 {
46 name: "Only span name in the config",
47 spanName: "my-span",
48 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
49 tracing {
50 span my-span
51 }`),
52 wantErr: false,
53 },
54 {
55 name: "Empty config",
56 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
57 tracing {
58 }`),
59 wantErr: false,
60 },
61 {
62 name: "Only span attributes",
63 spanAttributes: map[string]string{
64 "service.name": "my-service",
65 "service.version": "1.0.0",
66 },
67 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
68 tracing {
69 span_attributes {
70 service.name my-service
71 service.version 1.0.0
72 }
73 }`),
74 wantErr: false,
75 },
76 }
77 for _, tt := range tests {
78 t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
79 ot := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{}
80 if err := ot.UnmarshalCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#UnmarshalCaddyfile().(tt.d); (err != nil) != tt.wantErr {
81 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("UnmarshalCaddyfile() error = %v, wantErrType %v", err, tt.wantErr)
82 }
83
84 if ot.SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName. != tt.spanName {
85 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("UnmarshalCaddyfile() SpanName = %v, want SpanName %v", ot.SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName., tt.spanName)
86 }
87
88 if len(tt.spanAttributes) > 0 {
89 if ot.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes. == nil {
90 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("UnmarshalCaddyfile() SpanAttributes is nil, expected %v", tt.spanAttributes)
91 } else {
92 for key, expectedValue := range tt.spanAttributes {
93 if actualValue, exists := ot.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.[key]; !exists {
94 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("UnmarshalCaddyfile() SpanAttributes missing key %v", key)
95 } else if actualValue != expectedValue {
96 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("UnmarshalCaddyfile() SpanAttributes[%v] = %v, want %v", key, actualValue, expectedValue)
97 }
98 }
99 }
100 }
101 })
102 }
103 }
104
105 func TestTracing_UnmarshalCaddyfile_Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_UnmarshalCaddyfile_Error().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
106 tests := []struct {
107 name string
108 d *caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#
109 wantErr bool
110 }{
111 {
112 name: "Unknown parameter",
113 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
114 tracing {
115 foo bar
116 }`),
117 wantErr: true,
118 },
119 {
120 name: "Missed argument",
121 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
122 tracing {
123 span
124 }`),
125 wantErr: true,
126 },
127 {
128 name: "Span attributes missing value",
129 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
130 tracing {
131 span_attributes {
132 key
133 }
134 }`),
135 wantErr: true,
136 },
137 {
138 name: "Span attributes too many arguments",
139 d: caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewTestDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewTestDispenser().(`
140 tracing {
141 span_attributes {
142 key value extra
143 }
144 }`),
145 wantErr: true,
146 },
147 }
148 for _, tt := range tests {
149 t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
150 ot := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{}
151 if err := ot.UnmarshalCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#UnmarshalCaddyfile().(tt.d); (err != nil) != tt.wantErr {
152 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("UnmarshalCaddyfile() error = %v, wantErrType %v", err, tt.wantErr)
153 }
154 })
155 }
156 }
157
158 func TestTracing_ServeHTTP_Propagation_Without_Initial_Headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_ServeHTTP_Propagation_Without_Initial_Headers().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
159 ot := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{
160 SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName.: "mySpan",
161 }
162
163 req := createRequestWithContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/createRequestWithContext().("GET", "https://example.com/foo")
164 w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().()
165
166 var handler caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc# = func(writer httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error {
167 traceparent := request.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Traceparent")
168 if traceparent == "" || stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(traceparent, "00-00000000000000000000000000000000-0000000000000000") {
169 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Invalid traceparent: %v", traceparent)
170 }
171
172 return nil
173 }
174
175 ctx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()})
176 defer cancel()
177
178 if err := ot.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#Provision().(ctx); err != nil {
179 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Provision error: %v", err)
180 t.FailNowscip-go gomod github.com/golang/go/src go1.25.1 testing/common#FailNow().()
181 }
182
183 if err := ot.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#ServeHTTP().(w, req, handler); err != nil {
184 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("ServeHTTP error: %v", err)
185 }
186 }
187
188 func TestTracing_ServeHTTP_Propagation_With_Initial_Headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_ServeHTTP_Propagation_With_Initial_Headers().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
189 ot := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{
190 SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName.: "mySpan",
191 }
192
193 req := createRequestWithContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/createRequestWithContext().("GET", "https://example.com/foo")
194 req.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("traceparent", "00-11111111111111111111111111111111-1111111111111111-01")
195 w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().()
196
197 var handler caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc# = func(writer httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error {
198 traceparent := request.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Traceparent")
199 if !stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(traceparent, "00-11111111111111111111111111111111") {
200 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Invalid traceparent: %v", traceparent)
201 }
202
203 return nil
204 }
205
206 ctx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()})
207 defer cancel()
208
209 if err := ot.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#Provision().(ctx); err != nil {
210 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Provision error: %v", err)
211 t.FailNowscip-go gomod github.com/golang/go/src go1.25.1 testing/common#FailNow().()
212 }
213
214 if err := ot.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#ServeHTTP().(w, req, handler); err != nil {
215 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("ServeHTTP error: %v", err)
216 }
217 }
218
219 func TestTracing_ServeHTTP_Next_Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_ServeHTTP_Next_Error().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
220 ot := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{
221 SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName.: "mySpan",
222 }
223
224 req := createRequestWithContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/createRequestWithContext().("GET", "https://example.com/foo")
225 w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().()
226
227 expectErr := errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Newscip-go gomod github.com/golang/go/src go1.25.1 errors/New().("test error")
228
229 var handler caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc# = func(writer httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error {
230 return expectErr
231 }
232
233 ctx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()})
234 defer cancel()
235
236 if err := ot.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#Provision().(ctx); err != nil {
237 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Provision error: %v", err)
238 t.FailNowscip-go gomod github.com/golang/go/src go1.25.1 testing/common#FailNow().()
239 }
240
241 if err := ot.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#ServeHTTP().(w, req, handler); err == nil || !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, expectErr) {
242 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("expected error, got: %v", err)
243 }
244 }
245
246 func TestTracing_JSON_Configurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_JSON_Configuration().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
247 // Test that our struct correctly marshals to and from JSON
248 original := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{
249 SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName.: "test-span",
250 SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.: map[string]string{
251 "service.name": "test-service",
252 "service.version": "1.0.0",
253 "env": "test",
254 },
255 }
256
257 jsonData, err := jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Marshal().(original)
258 if err != nil {
259 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Failed to marshal to JSON: %v", err)
260 }
261
262 var unmarshaled Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#
263 if err := jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Unmarshal().(jsonData, &unmarshaled); err != nil {
264 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Failed to unmarshal from JSON: %v", err)
265 }
266
267 if unmarshaled.SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName. != original.SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName. {
268 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected SpanName %s, got %s", original.SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName., unmarshaled.SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName.)
269 }
270
271 if len(unmarshaled.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.) != len(original.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.) {
272 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected %d span attributes, got %d", len(original.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.), len(unmarshaled.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.))
273 }
274
275 for key, expectedValue := range original.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes. {
276 if actualValue, exists := unmarshaled.SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.[key]; !exists {
277 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected span attribute %s to exist", key)
278 } else if actualValue != expectedValue {
279 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected span attribute %s = %s, got %s", key, expectedValue, actualValue)
280 }
281 }
282
283 t.Logfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Logf().("JSON representation: %s", string(jsonData))
284 }
285
286 func TestTracing_OpenTelemetry_Span_Attributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/TestTracing_OpenTelemetry_Span_Attributes().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
287 // Create an in-memory span recorder to capture actual span data
288 spanRecorder := tracetestscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace/tracetest`/.NewSpanRecorderscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace/tracetest`/NewSpanRecorder().()
289 provider := tracescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/.NewTracerProviderscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/NewTracerProvider().(
290 tracescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/.WithSpanProcessorscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/WithSpanProcessor().(spanRecorder),
291 )
292
293 // Create our tracing module with span attributes that include placeholders
294 ot := &Tracingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#{
295 SpanNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanName.: "test-span",
296 SpanAttributesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#SpanAttributes.: map[string]string{
297 "static": "test-service",
298 "request-placeholder": "{http.request.method}",
299 "response-placeholder": "{http.response.header.X-Some-Header}",
300 "mixed": "prefix-{http.request.method}-{http.response.header.X-Some-Header}",
301 },
302 }
303
304 // Create a specific request to test against
305 req, _ := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().("POST", "https://api.example.com/v1/users?id=123", nil)
306 req.Hostscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Host. = "api.example.com"
307
308 w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().()
309
310 // Set up the replacer
311 repl := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewReplacer().()
312 ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey., repl)
313 ctx = contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(ctx, caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.VarsCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsCtxKey., make(map[string]any))
314 req = req.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx)
315
316 // Set up request placeholders
317 repl.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Set().("http.request.method", req.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.)
318 repl.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Set().("http.request.uri", req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RequestURI().())
319
320 // Handler to generate the response
321 var handler caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc# = func(writer httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, request *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error {
322 writer.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("X-Some-Header", "some-value")
323 writer.WriteHeaderscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#WriteHeader().(200)
324
325 // Make response headers available to replacer
326 repl.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Set().("http.response.header.X-Some-Header", writer.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("X-Some-Header"))
327
328 return nil
329 }
330
331 // Set up Caddy context
332 caddyCtx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()})
333 defer cancel()
334
335 // Override the global tracer provider with our test provider
336 // This is a bit hacky but necessary to capture the actual spans
337 originalProvider := globalTracerProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/globalTracerProvider.
338 globalTracerProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/globalTracerProvider. = &tracerProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/tracerProvider#{
339 tracerProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/tracerProvider#tracerProvider.: provider,
340 tracerProvidersCounterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/tracerProvider#tracerProvidersCounter.: 1, // Simulate one user
341 }
342 defer func() {
343 globalTracerProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/globalTracerProvider. = originalProvider
344 }()
345
346 // Provision the tracing module
347 if err := ot.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#Provision().(caddyCtx); err != nil {
348 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Provision error: %v", err)
349 t.FailNowscip-go gomod github.com/golang/go/src go1.25.1 testing/common#FailNow().()
350 }
351
352 // Execute the request
353 if err := ot.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/Tracing#ServeHTTP().(w, req, handler); err != nil {
354 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("ServeHTTP error: %v", err)
355 }
356
357 // Get the recorded spans
358 spans := spanRecorder.Endedscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace/tracetest`/SpanRecorder#Ended().()
359 if len(spans) == 0 {
360 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().("Expected at least one span to be recorded")
361 }
362
363 // Find our span (should be the one with our test span name)
364 var testSpan tracescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/.ReadOnlySpanscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/ReadOnlySpan#
365 for _, span := range spans {
366 if span.Namescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/ReadOnlySpan#Name().() == "test-span" {
367 testSpan = span
368 break
369 }
370 }
371
372 if testSpan == nil {
373 t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().("Could not find test span in recorded spans")
374 }
375
376 // Verify that the span attributes were set correctly with placeholder replacement
377 expectedAttributes := map[string]string{
378 "static": "test-service",
379 "request-placeholder": "POST",
380 "response-placeholder": "some-value",
381 "mixed": "prefix-POST-some-value",
382 }
383
384 actualAttributes := make(map[string]string)
385 for _, attr := range testSpan.Attributesscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/trace`/ReadOnlySpan#Attributes().() {
386 actualAttributes[string(attr.Keyscip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/attribute`/KeyValue#Key.)] = attr.Valuescip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/attribute`/KeyValue#Value..AsStringscip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/attribute`/Value#AsString().()
387 }
388
389 for key, expectedValue := range expectedAttributes {
390 if actualValue, exists := actualAttributes[key]; !exists {
391 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected span attribute %s to be set", key)
392 } else if actualValue != expectedValue {
393 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected span attribute %s = %s, got %s", key, expectedValue, actualValue)
394 }
395 }
396
397 t.Logfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Logf().("Recorded span attributes: %+v", actualAttributes)
398 }
399
400 func createRequestWithContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing`/createRequestWithContext().(method string, url string) *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request# {
401 r, _ := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().(method, url, nil)
402 repl := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewReplacer().()
403 ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey., repl)
404 r = r.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx)
405 return r
406 }