Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Drop support for [Go 1.23]. (#7831)
- Remove deprecated `go.opentelemetry.io/contrib/detectors/aws/ec2` module, please use `go.opentelemetry.io/contrib/detectors/aws/ec2/v2` instead. (#7841)
- Remove the deprecated `Extract` and `Inject` functions from `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#7952)
- Add the `http.route` metric attribute to `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#7966)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (tw traceware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
metricAttributes := semconv.MetricAttributes{
Req: r,
StatusCode: statusCode,
Route: routeStr,
AdditionalAttributes: tw.metricAttributesFromRequest(r),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,53 @@ func TestWithPublicEndpointFn(t *testing.T) {
}
}

func TestDefaultMetricAttributes(t *testing.T) {
defaultMetricAttributes := []attribute.KeyValue{
attribute.String("http.route", "/user/{id:[0-9]+}"),
attribute.String("server.address", "foobar"),
}

reader := sdkmetric.NewManualReader()
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))

router := mux.NewRouter()
router.Use(otelmux.Middleware("foobar",
otelmux.WithMeterProvider(meterProvider),
))

router.HandleFunc("/user/{id:[0-9]+}", ok)
r, err := http.NewRequest(http.MethodGet, "http://localhost/user/123", http.NoBody)
require.NoError(t, err)
rr := httptest.NewRecorder()
router.ServeHTTP(rr, r)

rm := metricdata.ResourceMetrics{}
err = reader.Collect(t.Context(), &rm)
require.NoError(t, err)
require.Len(t, rm.ScopeMetrics, 1)
assert.Len(t, rm.ScopeMetrics[0].Metrics, 3)

// Verify that the additional attribute is present in the metrics.
for _, m := range rm.ScopeMetrics[0].Metrics {
switch d := m.Data.(type) {
case metricdata.Histogram[int64]:
assert.Len(t, d.DataPoints, 1)
containsAttributes(t, d.DataPoints[0].Attributes, defaultMetricAttributes)
case metricdata.Histogram[float64]:
assert.Len(t, d.DataPoints, 1)
containsAttributes(t, d.DataPoints[0].Attributes, defaultMetricAttributes)
default:
// Intentional failure to keep the test updated with changes in metrics
t.Errorf("Unexpected metric type")
}
}
}

func TestHandlerWithMetricAttributesFn(t *testing.T) {
const (
serverRequestSize = "http.server.request.size"
serverResponseSize = "http.server.response.size"
serverDuration = "http.server.duration"
serverRequestSize = "http.server.request.body.size"
serverResponseSize = "http.server.response.body.size"
serverDuration = "http.server.request.duration"
)
testCases := []struct {
name string
Expand Down Expand Up @@ -430,7 +472,7 @@ func TestHandlerWithMetricAttributesFn(t *testing.T) {
for _, m := range rm.ScopeMetrics[0].Metrics {
switch m.Name {
case serverRequestSize, serverResponseSize:
d, ok := m.Data.(metricdata.Sum[int64])
d, ok := m.Data.(metricdata.Histogram[int64])
assert.True(t, ok)
assert.Len(t, d.DataPoints, 1)
containsAttributes(t, d.DataPoints[0].Attributes, testCases[0].wantAdditionalAttribute)
Expand All @@ -439,6 +481,9 @@ func TestHandlerWithMetricAttributesFn(t *testing.T) {
assert.True(t, ok)
assert.Len(t, d.DataPoints, 1)
containsAttributes(t, d.DataPoints[0].Attributes, testCases[0].wantAdditionalAttribute)
default:
// Intentional failure to keep the test updated with changes in metrics
t.Errorf("Unexpected metric name")
}
}
}
Expand Down
Loading