Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added test for Fields in `go.opentelemetry.io/contrib/propagators/jaeger`. (#7119)
- Allow configuring samplers in `go.opentelemetry.io/contrib/otelconf`. (#7148)
- Slog log bridge now sets `SeverityText` attribute using source value in `go.opentelemetry.io/contrib/bridges/otelslog`. (#7198)
- Add `http.route` metric attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#7275)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {

// Record the server-side attributes.
var additionalAttributes []attribute.KeyValue
if c.FullPath() != "" {
additionalAttributes = append(additionalAttributes, sc.Route(c.FullPath()))
}
if cfg.MetricAttributeFn != nil {
additionalAttributes = append(additionalAttributes, cfg.MetricAttributeFn(c.Request)...)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,24 @@ func TestMetrics(t *testing.T) {
name string
metricAttributeExtractor func(*http.Request) []attribute.KeyValue
ginMetricAttributeExtractor func(*gin.Context) []attribute.KeyValue
requestTarget string
wantRouteAttr string
wantStatus int64
}{
{
name: "default",
metricAttributeExtractor: nil,
ginMetricAttributeExtractor: nil,
requestTarget: "/user/123",
wantRouteAttr: "/user/:id",
wantStatus: 200,
},
{
name: "request target not exist",
metricAttributeExtractor: nil,
ginMetricAttributeExtractor: nil,
requestTarget: "/abc/123",
wantStatus: 404,
},
{
name: "with metric attributes callback",
Expand All @@ -481,6 +494,9 @@ func TestMetrics(t *testing.T) {
attribute.String("key3", "value3"),
}
},
requestTarget: "/user/123",
wantRouteAttr: "/user/:id",
wantStatus: 200,
},
}

Expand All @@ -501,7 +517,7 @@ func TestMetrics(t *testing.T) {
_, _ = c.Writer.Write([]byte(id))
})

r := httptest.NewRequest("GET", "/user/123", nil)
r := httptest.NewRequest("GET", tt.requestTarget, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = r
Expand All @@ -518,12 +534,15 @@ func TestMetrics(t *testing.T) {

attrs := []attribute.KeyValue{
attribute.String("http.request.method", "GET"),
attribute.Int64("http.response.status_code", 200),
attribute.Int64("http.response.status_code", tt.wantStatus),
attribute.String("network.protocol.name", "http"),
attribute.String("network.protocol.version", fmt.Sprintf("1.%d", r.ProtoMinor)),
attribute.String("server.address", "foobar"),
attribute.String("url.scheme", "http"),
}
if tt.wantRouteAttr != "" {
attrs = append(attrs, attribute.String("http.route", tt.wantRouteAttr))
}

if tt.metricAttributeExtractor != nil {
attrs = append(attrs, tt.metricAttributeExtractor(r)...)
Expand Down
Loading