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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- `http.route` attribute to otelhttp server request spans, when `net/http.Request.Pattern` is set in `go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful`, `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`, `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`, `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho` and `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#6905)

### Changed

- Jaeger remote sampler's probabilistic strategy now uses the same sampling algorithm as `trace.TraceIDRatioBased` in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#6892)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type CurrentHTTPServer struct{}

// TraceRequest returns trace attributes for an HTTP request received by a
// RequestTraceAttrs returns trace attributes for an HTTP request received by a
// server.
//
// The server must be the primary server name if it is known. For example this
Expand Down Expand Up @@ -96,6 +96,11 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
count++
}

httpRoute := req.Pattern
if httpRoute != "" {
count++
}

attrs := make([]attribute.KeyValue, 0, count)
attrs = append(attrs,
semconvNew.ServerAddress(host),
Expand Down Expand Up @@ -138,6 +143,10 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
attrs = append(attrs, semconvNew.NetworkProtocolVersion(protoVersion))
}

if httpRoute != "" {
attrs = append(attrs, n.Route(httpRoute))
}

return attrs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package semconv

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

Expand Down Expand Up @@ -256,3 +257,18 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
})
}
}

func TestRequestTraceAttrs_HTTPRoute(t *testing.T) {
req := httptest.NewRequest("GET", "/high/cardinality/path/abc123", nil)
req.Pattern = "/high/cardinality/path/{id}"

var found bool
for _, attr := range (CurrentHTTPServer{}).RequestTraceAttrs("", req) {
if attr.Key != "http.route" {
continue
}
found = true
assert.Equal(t, req.Pattern, attr.Value.AsString())
}
require.True(t, found)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type CurrentHTTPServer struct{}

// TraceRequest returns trace attributes for an HTTP request received by a
// RequestTraceAttrs returns trace attributes for an HTTP request received by a
// server.
//
// The server must be the primary server name if it is known. For example this
Expand Down Expand Up @@ -96,6 +96,11 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
count++
}

httpRoute := req.Pattern
if httpRoute != "" {
count++
}

attrs := make([]attribute.KeyValue, 0, count)
attrs = append(attrs,
semconvNew.ServerAddress(host),
Expand Down Expand Up @@ -138,6 +143,10 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
attrs = append(attrs, semconvNew.NetworkProtocolVersion(protoVersion))
}

if httpRoute != "" {
attrs = append(attrs, n.Route(httpRoute))
}

return attrs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package semconv

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

Expand Down Expand Up @@ -256,3 +257,18 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
})
}
}

func TestRequestTraceAttrs_HTTPRoute(t *testing.T) {
req := httptest.NewRequest("GET", "/high/cardinality/path/abc123", nil)
req.Pattern = "/high/cardinality/path/{id}"

var found bool
for _, attr := range (CurrentHTTPServer{}).RequestTraceAttrs("", req) {
if attr.Key != "http.route" {
continue
}
found = true
assert.Equal(t, req.Pattern, attr.Value.AsString())
}
require.True(t, found)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type CurrentHTTPServer struct{}

// TraceRequest returns trace attributes for an HTTP request received by a
// RequestTraceAttrs returns trace attributes for an HTTP request received by a
// server.
//
// The server must be the primary server name if it is known. For example this
Expand Down Expand Up @@ -96,6 +96,11 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
count++
}

httpRoute := req.Pattern
if httpRoute != "" {
count++
}

attrs := make([]attribute.KeyValue, 0, count)
attrs = append(attrs,
semconvNew.ServerAddress(host),
Expand Down Expand Up @@ -138,6 +143,10 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
attrs = append(attrs, semconvNew.NetworkProtocolVersion(protoVersion))
}

if httpRoute != "" {
attrs = append(attrs, n.Route(httpRoute))
}

return attrs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package semconv

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

Expand Down Expand Up @@ -256,3 +257,18 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
})
}
}

func TestRequestTraceAttrs_HTTPRoute(t *testing.T) {
req := httptest.NewRequest("GET", "/high/cardinality/path/abc123", nil)
req.Pattern = "/high/cardinality/path/{id}"

var found bool
for _, attr := range (CurrentHTTPServer{}).RequestTraceAttrs("", req) {
if attr.Key != "http.route" {
continue
}
found = true
assert.Equal(t, req.Pattern, attr.Value.AsString())
}
require.True(t, found)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type CurrentHTTPServer struct{}

// TraceRequest returns trace attributes for an HTTP request received by a
// RequestTraceAttrs returns trace attributes for an HTTP request received by a
// server.
//
// The server must be the primary server name if it is known. For example this
Expand Down Expand Up @@ -96,6 +96,11 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
count++
}

httpRoute := req.Pattern
if httpRoute != "" {
count++
}

attrs := make([]attribute.KeyValue, 0, count)
attrs = append(attrs,
semconvNew.ServerAddress(host),
Expand Down Expand Up @@ -138,6 +143,10 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
attrs = append(attrs, semconvNew.NetworkProtocolVersion(protoVersion))
}

if httpRoute != "" {
attrs = append(attrs, n.Route(httpRoute))
}

return attrs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package semconv

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

Expand Down Expand Up @@ -256,3 +257,18 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
})
}
}

func TestRequestTraceAttrs_HTTPRoute(t *testing.T) {
req := httptest.NewRequest("GET", "/high/cardinality/path/abc123", nil)
req.Pattern = "/high/cardinality/path/{id}"

var found bool
for _, attr := range (CurrentHTTPServer{}).RequestTraceAttrs("", req) {
if attr.Key != "http.route" {
continue
}
found = true
assert.Equal(t, req.Pattern, attr.Value.AsString())
}
require.True(t, found)
}
11 changes: 10 additions & 1 deletion instrumentation/net/http/otelhttp/internal/semconv/httpconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type CurrentHTTPServer struct{}

// TraceRequest returns trace attributes for an HTTP request received by a
// RequestTraceAttrs returns trace attributes for an HTTP request received by a
// server.
//
// The server must be the primary server name if it is known. For example this
Expand Down Expand Up @@ -96,6 +96,11 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
count++
}

httpRoute := req.Pattern
if httpRoute != "" {
count++
}

attrs := make([]attribute.KeyValue, 0, count)
attrs = append(attrs,
semconvNew.ServerAddress(host),
Expand Down Expand Up @@ -138,6 +143,10 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
attrs = append(attrs, semconvNew.NetworkProtocolVersion(protoVersion))
}

if httpRoute != "" {
attrs = append(attrs, n.Route(httpRoute))
}

return attrs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package semconv

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

Expand Down Expand Up @@ -256,3 +257,18 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
})
}
}

func TestRequestTraceAttrs_HTTPRoute(t *testing.T) {
req := httptest.NewRequest("GET", "/high/cardinality/path/abc123", nil)
req.Pattern = "/high/cardinality/path/{id}"

var found bool
for _, attr := range (CurrentHTTPServer{}).RequestTraceAttrs("", req) {
if attr.Key != "http.route" {
continue
}
found = true
assert.Equal(t, req.Pattern, attr.Value.AsString())
}
require.True(t, found)
}
11 changes: 10 additions & 1 deletion internal/shared/semconv/httpconv.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type CurrentHTTPServer struct{}

// TraceRequest returns trace attributes for an HTTP request received by a
// RequestTraceAttrs returns trace attributes for an HTTP request received by a
// server.
//
// The server must be the primary server name if it is known. For example this
Expand Down Expand Up @@ -96,6 +96,11 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
count++
}

httpRoute := req.Pattern
if httpRoute != "" {
count++
}

attrs := make([]attribute.KeyValue, 0, count)
attrs = append(attrs,
semconvNew.ServerAddress(host),
Expand Down Expand Up @@ -138,6 +143,10 @@ func (n CurrentHTTPServer) RequestTraceAttrs(server string, req *http.Request) [
attrs = append(attrs, semconvNew.NetworkProtocolVersion(protoVersion))
}

if httpRoute != "" {
attrs = append(attrs, n.Route(httpRoute))
}

return attrs
}

Expand Down
16 changes: 16 additions & 0 deletions internal/shared/semconv/httpconv_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package semconv

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

Expand Down Expand Up @@ -256,3 +257,18 @@ func TestCurrentHttpClient_MetricAttributes(t *testing.T) {
})
}
}

func TestRequestTraceAttrs_HTTPRoute(t *testing.T) {
req := httptest.NewRequest("GET", "/high/cardinality/path/abc123", nil)
req.Pattern = "/high/cardinality/path/{id}"

var found bool
for _, attr := range (CurrentHTTPServer{}).RequestTraceAttrs("", req) {
if attr.Key != "http.route" {
continue
}
found = true
assert.Equal(t, req.Pattern, attr.Value.AsString())
}
require.True(t, found)
}
Loading