| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelmux_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/gorilla/mux" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/codes" |
| 18 |
"go.opentelemetry.io/otel/propagation" |
| 19 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 20 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 21 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 22 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 23 |
"go.opentelemetry.io/otel/trace" |
| 24 |
|
| 25 |
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux" |
| 26 |
) |
| 27 |
|
| 28 |
func TestDefaultTrace(t *testing.T) { |
| 29 |
sr := tracetest.NewSpanRecorder() |
| 30 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 31 |
router := mux.NewRouter() |
| 32 |
router.Use(otelmux.Middleware("foobar", otelmux.WithTracerProvider(provider))) |
| 33 |
|
| 34 |
router.HandleFunc("/user/{id}", func(w http.ResponseWriter, _ *http.Request) { |
| 35 |
w.WriteHeader(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 |
|
| 43 |
assert.Equal(t, http.StatusOK, w.Code, "unexpected status code") |
| 44 |
|
| 45 |
spans := sr.Ended() |
| 46 |
|
| 47 |
require.Len(t, sr.Ended(), 1) |
| 48 |
span := spans[0] |
| 49 |
attr := span.Attributes() |
| 50 |
assert.True(t, ensurePrefix(http.MethodGet, spans[0].Name())) |
| 51 |
assert.Equal(t, "GET /user/{id}", span.Name()) |
| 52 |
assert.Equal(t, trace.SpanKindServer, span.SpanKind()) |
| 53 |
assert.Contains(t, attr, attribute.Int("http.response.status_code", http.StatusOK)) |
| 54 |
assert.Contains(t, attr, attribute.String("http.request.method", "GET")) |
| 55 |
assert.Contains(t, attr, attribute.String("http.route", "/user/{id}")) |
| 56 |
assert.Equal(t, codes.Unset, span.Status().Code) |
| 57 |
assert.Empty(t, span.Status().Description) |
| 58 |
} |
| 59 |
|
| 60 |
func TestCustomSpanNameFormatter(t *testing.T) { |
| 61 |
exporter := tracetest.NewInMemoryExporter() |
| 62 |
|
| 63 |
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter)) |
| 64 |
|
| 65 |
routeTpl := "/user/{id}" |
| 66 |
|
| 67 |
testdata := []struct { |
| 68 |
spanNameFormatter func(string, *http.Request) string |
| 69 |
want string |
| 70 |
}{ |
| 71 |
{nil, setDefaultName(http.MethodGet, routeTpl)}, |
| 72 |
{ |
| 73 |
func(string, *http.Request) string { return "custom" }, |
| 74 |
"custom", |
| 75 |
}, |
| 76 |
{ |
| 77 |
func(name string, r *http.Request) string { |
| 78 |
return fmt.Sprintf("%s %s", r.Method, name) |
| 79 |
}, |
| 80 |
"GET " + routeTpl, |
| 81 |
}, |
| 82 |
} |
| 83 |
|
| 84 |
for i, d := range testdata { |
| 85 |
t.Run(fmt.Sprintf("%d_%s", i, d.want), func(t *testing.T) { |
| 86 |
router := mux.NewRouter() |
| 87 |
router.Use(otelmux.Middleware( |
| 88 |
"foobar", |
| 89 |
otelmux.WithTracerProvider(tp), |
| 90 |
otelmux.WithSpanNameFormatter(d.spanNameFormatter), |
| 91 |
)) |
| 92 |
router.HandleFunc(routeTpl, func(http.ResponseWriter, *http.Request) {}) |
| 93 |
|
| 94 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 95 |
w := httptest.NewRecorder() |
| 96 |
|
| 97 |
router.ServeHTTP(w, r) |
| 98 |
|
| 99 |
spans := exporter.GetSpans() |
| 100 |
require.Len(t, spans, 1) |
| 101 |
assert.Equal(t, d.want, spans[0].Name) |
| 102 |
|
| 103 |
exporter.Reset() |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func ok(http.ResponseWriter, *http.Request) {} |
| 109 |
func notfound(w http.ResponseWriter, _ *http.Request) { |
| 110 |
http.Error(w, "not found", http.StatusNotFound) |
| 111 |
} |
| 112 |
|
| 113 |
func TestSDKIntegration(t *testing.T) { |
| 114 |
sr := tracetest.NewSpanRecorder() |
| 115 |
provider := sdktrace.NewTracerProvider() |
| 116 |
provider.RegisterSpanProcessor(sr) |
| 117 |
|
| 118 |
reader := sdkmetric.NewManualReader() |
| 119 |
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 120 |
|
| 121 |
router := mux.NewRouter() |
| 122 |
router.Use(otelmux.Middleware("foobar", |
| 123 |
otelmux.WithTracerProvider(provider), |
| 124 |
otelmux.WithMeterProvider(meterProvider))) |
| 125 |
|
| 126 |
router.HandleFunc("/user/{id:[0-9]+}", ok) |
| 127 |
router.HandleFunc("/book/{title}", ok) |
| 128 |
|
| 129 |
tests := []struct { |
| 130 |
name string |
| 131 |
method string |
| 132 |
path string |
| 133 |
reqFunc func(r *http.Request) |
| 134 |
wantSpanName string |
| 135 |
wantMethod string |
| 136 |
wantRoute string |
| 137 |
}{ |
| 138 |
{ |
| 139 |
name: "user route", |
| 140 |
method: http.MethodGet, |
| 141 |
path: "/user/123", |
| 142 |
reqFunc: nil, |
| 143 |
wantSpanName: "GET /user/{id:[0-9]+}", |
| 144 |
wantMethod: http.MethodGet, |
| 145 |
wantRoute: "/user/{id:[0-9]+}", |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "POST book route", |
| 149 |
method: http.MethodPost, |
| 150 |
path: "/book/foo", |
| 151 |
reqFunc: nil, |
| 152 |
wantSpanName: "POST /book/{title}", |
| 153 |
wantMethod: http.MethodPost, |
| 154 |
wantRoute: "/book/{title}", |
| 155 |
}, |
| 156 |
{ |
| 157 |
name: "book route with custom pattern", |
| 158 |
method: http.MethodGet, |
| 159 |
path: "/book/bar", |
| 160 |
reqFunc: func(r *http.Request) { r.Pattern = "/book/{custom}" }, |
| 161 |
wantSpanName: "GET /book/{custom}", |
| 162 |
wantMethod: http.MethodGet, |
| 163 |
wantRoute: "/book/{custom}", |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "Invalid HTTP Method", |
| 167 |
method: "INVALID", |
| 168 |
path: "/book/bar", |
| 169 |
reqFunc: func(r *http.Request) { r.Pattern = "/book/{custom}" }, |
| 170 |
wantSpanName: "HTTP /book/{custom}", |
| 171 |
wantMethod: http.MethodGet, |
| 172 |
wantRoute: "/book/{custom}", |
| 173 |
}, |
| 174 |
} |
| 175 |
|
| 176 |
for _, tt := range tests { |
| 177 |
t.Run(tt.name, func(t *testing.T) { |
| 178 |
defer sr.Reset() |
| 179 |
|
| 180 |
r := httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, http.NoBody) |
| 181 |
if tt.reqFunc != nil { |
| 182 |
tt.reqFunc(r) |
| 183 |
} |
| 184 |
|
| 185 |
w := httptest.NewRecorder() |
| 186 |
router.ServeHTTP(w, r) |
| 187 |
spans := sr.Ended() |
| 188 |
|
| 189 |
require.Len(t, spans, 1) |
| 190 |
assertSpan(t, sr.Ended()[0], |
| 191 |
tt.wantSpanName, |
| 192 |
trace.SpanKindServer, |
| 193 |
attribute.String("server.address", "foobar"), |
| 194 |
attribute.Int("http.response.status_code", http.StatusOK), |
| 195 |
attribute.String("http.request.method", tt.wantMethod), |
| 196 |
attribute.String("http.route", tt.wantRoute), |
| 197 |
) |
| 198 |
}) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestNotFoundIsNotError(t *testing.T) { |
| 203 |
sr := tracetest.NewSpanRecorder() |
| 204 |
provider := sdktrace.NewTracerProvider() |
| 205 |
provider.RegisterSpanProcessor(sr) |
| 206 |
|
| 207 |
router := mux.NewRouter() |
| 208 |
router.Use(otelmux.Middleware("foobar", otelmux.WithTracerProvider(provider))) |
| 209 |
router.HandleFunc("/does/not/exist", notfound) |
| 210 |
|
| 211 |
r0 := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/does/not/exist", http.NoBody) |
| 212 |
w := httptest.NewRecorder() |
| 213 |
router.ServeHTTP(w, r0) |
| 214 |
|
| 215 |
require.Len(t, sr.Ended(), 1) |
| 216 |
assertSpan(t, sr.Ended()[0], |
| 217 |
"GET /does/not/exist", |
| 218 |
trace.SpanKindServer, |
| 219 |
attribute.String("server.address", "foobar"), |
| 220 |
attribute.Int("http.response.status_code", http.StatusNotFound), |
| 221 |
attribute.String("http.request.method", "GET"), |
| 222 |
attribute.String("http.route", "/does/not/exist"), |
| 223 |
) |
| 224 |
assert.Equal(t, codes.Unset, sr.Ended()[0].Status().Code) |
| 225 |
} |
| 226 |
|
| 227 |
func assertSpan(t *testing.T, span sdktrace.ReadOnlySpan, name string, kind trace.SpanKind, attrs ...attribute.KeyValue) { |
| 228 |
t.Helper() |
| 229 |
|
| 230 |
assert.Equal(t, name, span.Name()) |
| 231 |
assert.Equal(t, kind, span.SpanKind()) |
| 232 |
|
| 233 |
got := make(map[attribute.Key]attribute.Value, len(span.Attributes())) |
| 234 |
for _, a := range span.Attributes() { |
| 235 |
got[a.Key] = a.Value |
| 236 |
} |
| 237 |
for _, want := range attrs { |
| 238 |
if !assert.Contains(t, got, want.Key) { |
| 239 |
continue |
| 240 |
} |
| 241 |
assert.Equal(t, want.Value, got[want.Key]) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestWithPublicEndpoint(t *testing.T) { |
| 246 |
sr := tracetest.NewSpanRecorder() |
| 247 |
provider := sdktrace.NewTracerProvider() |
| 248 |
provider.RegisterSpanProcessor(sr) |
| 249 |
|
| 250 |
remoteSpan := trace.SpanContextConfig{ |
| 251 |
TraceID: trace.TraceID{0x01}, |
| 252 |
SpanID: trace.SpanID{0x01}, |
| 253 |
Remote: true, |
| 254 |
} |
| 255 |
prop := propagation.TraceContext{} |
| 256 |
|
| 257 |
router := mux.NewRouter() |
| 258 |
router.Use(otelmux.Middleware("foobar", |
| 259 |
otelmux.WithPublicEndpoint(), |
| 260 |
otelmux.WithPropagators(prop), |
| 261 |
otelmux.WithTracerProvider(provider), |
| 262 |
)) |
| 263 |
router.HandleFunc("/with/public/endpoint", func(_ http.ResponseWriter, r *http.Request) { |
| 264 |
s := trace.SpanFromContext(r.Context()) |
| 265 |
sc := s.SpanContext() |
| 266 |
|
| 267 |
|
| 268 |
assert.True(t, sc.IsValid()) |
| 269 |
assert.False(t, sc.IsRemote()) |
| 270 |
assert.NotEqual(t, remoteSpan.TraceID, sc.TraceID()) |
| 271 |
}) |
| 272 |
|
| 273 |
r0 := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/with/public/endpoint", http.NoBody) |
| 274 |
w := httptest.NewRecorder() |
| 275 |
|
| 276 |
sc := trace.NewSpanContext(remoteSpan) |
| 277 |
ctx := trace.ContextWithSpanContext(t.Context(), sc) |
| 278 |
prop.Inject(ctx, propagation.HeaderCarrier(r0.Header)) |
| 279 |
|
| 280 |
router.ServeHTTP(w, r0) |
| 281 |
assert.Equal(t, http.StatusOK, w.Result().StatusCode) |
| 282 |
|
| 283 |
|
| 284 |
assert.NoError(t, sr.ForceFlush(ctx)) |
| 285 |
done := sr.Ended() |
| 286 |
require.Len(t, done, 1) |
| 287 |
require.Len(t, done[0].Links(), 1, "should contain link") |
| 288 |
require.True(t, sc.Equal(done[0].Links()[0].SpanContext), "should link incoming span context") |
| 289 |
} |
| 290 |
|
| 291 |
func TestWithPublicEndpointFn(t *testing.T) { |
| 292 |
remoteSpan := trace.SpanContextConfig{ |
| 293 |
TraceID: trace.TraceID{0x01}, |
| 294 |
SpanID: trace.SpanID{0x01}, |
| 295 |
TraceFlags: trace.FlagsSampled, |
| 296 |
Remote: true, |
| 297 |
} |
| 298 |
prop := propagation.TraceContext{} |
| 299 |
|
| 300 |
testdata := []struct { |
| 301 |
name string |
| 302 |
fn func(*http.Request) bool |
| 303 |
handlerAssert func(*testing.T, trace.SpanContext) |
| 304 |
spansAssert func(*testing.T, trace.SpanContext, []sdktrace.ReadOnlySpan) |
| 305 |
}{ |
| 306 |
{ |
| 307 |
name: "with the method returning true", |
| 308 |
fn: func(*http.Request) bool { |
| 309 |
return true |
| 310 |
}, |
| 311 |
handlerAssert: func(t *testing.T, sc trace.SpanContext) { |
| 312 |
|
| 313 |
assert.True(t, sc.IsValid()) |
| 314 |
assert.False(t, sc.IsRemote()) |
| 315 |
assert.NotEqual(t, remoteSpan.TraceID, sc.TraceID()) |
| 316 |
}, |
| 317 |
spansAssert: func(t *testing.T, sc trace.SpanContext, spans []sdktrace.ReadOnlySpan) { |
| 318 |
require.Len(t, spans, 1) |
| 319 |
require.Len(t, spans[0].Links(), 1, "should contain link") |
| 320 |
require.True(t, sc.Equal(spans[0].Links()[0].SpanContext), "should link incoming span context") |
| 321 |
}, |
| 322 |
}, |
| 323 |
{ |
| 324 |
name: "with the method returning false", |
| 325 |
fn: func(*http.Request) bool { |
| 326 |
return false |
| 327 |
}, |
| 328 |
handlerAssert: func(t *testing.T, sc trace.SpanContext) { |
| 329 |
|
| 330 |
assert.True(t, sc.IsValid()) |
| 331 |
assert.False(t, sc.IsRemote()) |
| 332 |
assert.Equal(t, remoteSpan.TraceID, sc.TraceID()) |
| 333 |
}, |
| 334 |
spansAssert: func(t *testing.T, _ trace.SpanContext, spans []sdktrace.ReadOnlySpan) { |
| 335 |
require.Len(t, spans, 1) |
| 336 |
require.Empty(t, spans[0].Links(), "should not contain link") |
| 337 |
}, |
| 338 |
}, |
| 339 |
} |
| 340 |
|
| 341 |
for _, tt := range testdata { |
| 342 |
t.Run(tt.name, func(t *testing.T) { |
| 343 |
sr := tracetest.NewSpanRecorder() |
| 344 |
provider := sdktrace.NewTracerProvider() |
| 345 |
provider.RegisterSpanProcessor(sr) |
| 346 |
|
| 347 |
router := mux.NewRouter() |
| 348 |
router.Use(otelmux.Middleware("foobar", |
| 349 |
otelmux.WithPublicEndpointFn(tt.fn), |
| 350 |
otelmux.WithPropagators(prop), |
| 351 |
otelmux.WithTracerProvider(provider), |
| 352 |
)) |
| 353 |
router.HandleFunc("/with/public/endpointfn", func(_ http.ResponseWriter, r *http.Request) { |
| 354 |
s := trace.SpanFromContext(r.Context()) |
| 355 |
tt.handlerAssert(t, s.SpanContext()) |
| 356 |
}) |
| 357 |
|
| 358 |
r0 := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/with/public/endpointfn", http.NoBody) |
| 359 |
w := httptest.NewRecorder() |
| 360 |
|
| 361 |
sc := trace.NewSpanContext(remoteSpan) |
| 362 |
ctx := trace.ContextWithSpanContext(t.Context(), sc) |
| 363 |
prop.Inject(ctx, propagation.HeaderCarrier(r0.Header)) |
| 364 |
|
| 365 |
router.ServeHTTP(w, r0) |
| 366 |
assert.Equal(t, http.StatusOK, w.Result().StatusCode) |
| 367 |
|
| 368 |
|
| 369 |
assert.NoError(t, sr.ForceFlush(ctx)) |
| 370 |
spans := sr.Ended() |
| 371 |
tt.spansAssert(t, sc, spans) |
| 372 |
}) |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
func TestDefaultMetricAttributes(t *testing.T) { |
| 377 |
defaultMetricAttributes := []attribute.KeyValue{ |
| 378 |
attribute.String("http.route", "/user/{id:[0-9]+}"), |
| 379 |
attribute.String("server.address", "foobar"), |
| 380 |
} |
| 381 |
|
| 382 |
reader := sdkmetric.NewManualReader() |
| 383 |
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 384 |
|
| 385 |
router := mux.NewRouter() |
| 386 |
router.Use(otelmux.Middleware("foobar", |
| 387 |
otelmux.WithMeterProvider(meterProvider), |
| 388 |
)) |
| 389 |
|
| 390 |
router.HandleFunc("/user/{id:[0-9]+}", ok) |
| 391 |
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://localhost/user/123", http.NoBody) |
| 392 |
require.NoError(t, err) |
| 393 |
rr := httptest.NewRecorder() |
| 394 |
router.ServeHTTP(rr, r) |
| 395 |
|
| 396 |
rm := metricdata.ResourceMetrics{} |
| 397 |
err = reader.Collect(t.Context(), &rm) |
| 398 |
require.NoError(t, err) |
| 399 |
require.Len(t, rm.ScopeMetrics, 1) |
| 400 |
assert.Len(t, rm.ScopeMetrics[0].Metrics, 3) |
| 401 |
|
| 402 |
|
| 403 |
for _, m := range rm.ScopeMetrics[0].Metrics { |
| 404 |
switch d := m.Data.(type) { |
| 405 |
case metricdata.Histogram[int64]: |
| 406 |
assert.Len(t, d.DataPoints, 1) |
| 407 |
containsAttributes(t, d.DataPoints[0].Attributes, defaultMetricAttributes) |
| 408 |
case metricdata.Histogram[float64]: |
| 409 |
assert.Len(t, d.DataPoints, 1) |
| 410 |
containsAttributes(t, d.DataPoints[0].Attributes, defaultMetricAttributes) |
| 411 |
default: |
| 412 |
|
| 413 |
t.Errorf("Unexpected metric type") |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
func TestHandlerWithMetricAttributesFn(t *testing.T) { |
| 419 |
const ( |
| 420 |
serverRequestSize = "http.server.request.body.size" |
| 421 |
serverResponseSize = "http.server.response.body.size" |
| 422 |
serverDuration = "http.server.request.duration" |
| 423 |
) |
| 424 |
testCases := []struct { |
| 425 |
name string |
| 426 |
fn func(r *http.Request) []attribute.KeyValue |
| 427 |
wantAdditionalAttribute []attribute.KeyValue |
| 428 |
}{ |
| 429 |
{ |
| 430 |
name: "With a nil function", |
| 431 |
fn: nil, |
| 432 |
wantAdditionalAttribute: []attribute.KeyValue{}, |
| 433 |
}, |
| 434 |
{ |
| 435 |
name: "With a function that returns an additional attribute", |
| 436 |
fn: func(*http.Request) []attribute.KeyValue { |
| 437 |
return []attribute.KeyValue{ |
| 438 |
attribute.String("fooKey", "fooValue"), |
| 439 |
attribute.String("barKey", "barValue"), |
| 440 |
} |
| 441 |
}, |
| 442 |
wantAdditionalAttribute: []attribute.KeyValue{ |
| 443 |
attribute.String("fooKey", "fooValue"), |
| 444 |
attribute.String("barKey", "barValue"), |
| 445 |
}, |
| 446 |
}, |
| 447 |
} |
| 448 |
|
| 449 |
for _, tc := range testCases { |
| 450 |
reader := sdkmetric.NewManualReader() |
| 451 |
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 452 |
|
| 453 |
router := mux.NewRouter() |
| 454 |
router.Use(otelmux.Middleware("foobar", |
| 455 |
otelmux.WithMeterProvider(meterProvider), |
| 456 |
otelmux.WithMetricAttributesFn(tc.fn), |
| 457 |
)) |
| 458 |
|
| 459 |
router.HandleFunc("/user/{id:[0-9]+}", ok) |
| 460 |
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://localhost/user/123", http.NoBody) |
| 461 |
require.NoError(t, err) |
| 462 |
rr := httptest.NewRecorder() |
| 463 |
router.ServeHTTP(rr, r) |
| 464 |
|
| 465 |
rm := metricdata.ResourceMetrics{} |
| 466 |
err = reader.Collect(t.Context(), &rm) |
| 467 |
require.NoError(t, err) |
| 468 |
require.Len(t, rm.ScopeMetrics, 1) |
| 469 |
assert.Len(t, rm.ScopeMetrics[0].Metrics, 3) |
| 470 |
|
| 471 |
|
| 472 |
for _, m := range rm.ScopeMetrics[0].Metrics { |
| 473 |
switch m.Name { |
| 474 |
case serverRequestSize, serverResponseSize: |
| 475 |
d, ok := m.Data.(metricdata.Histogram[int64]) |
| 476 |
assert.True(t, ok) |
| 477 |
assert.Len(t, d.DataPoints, 1) |
| 478 |
containsAttributes(t, d.DataPoints[0].Attributes, testCases[0].wantAdditionalAttribute) |
| 479 |
case serverDuration: |
| 480 |
d, ok := m.Data.(metricdata.Histogram[float64]) |
| 481 |
assert.True(t, ok) |
| 482 |
assert.Len(t, d.DataPoints, 1) |
| 483 |
containsAttributes(t, d.DataPoints[0].Attributes, testCases[0].wantAdditionalAttribute) |
| 484 |
default: |
| 485 |
|
| 486 |
t.Errorf("Unexpected metric name") |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
func containsAttributes(t *testing.T, attrSet attribute.Set, expected []attribute.KeyValue) { |
| 493 |
for _, att := range expected { |
| 494 |
actualValue, ok := attrSet.Value(att.Key) |
| 495 |
assert.True(t, ok) |
| 496 |
assert.Equal(t, att.Value.AsString(), actualValue.AsString()) |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
func setDefaultName(method, path string) string { |
| 501 |
return method + " " + path |
| 502 |
} |
| 503 |
|
| 504 |
func ensurePrefix(prefix, s string) bool { |
| 505 |
return strings.HasPrefix(s, prefix) |
| 506 |
} |