Skip to content

Commit

Permalink
refactor!: Deprecated OTEL attributes replaced (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
dadrus authored Jul 26, 2024
1 parent dd75306 commit e5ed3a5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 46 deletions.
10 changes: 5 additions & 5 deletions internal/handler/middleware/grpc/otelmetrics/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"

Expand Down Expand Up @@ -131,10 +131,10 @@ func peerAttr(addr string) []attribute.KeyValue {
}

if ip := net.ParseIP(host); ip != nil {
return []attribute.KeyValue{semconv.NetSockPeerAddr(host), semconv.NetSockPeerPort(port)} // nolint: staticcheck
return []attribute.KeyValue{semconv.NetworkPeerAddress(host), semconv.NetworkPeerPort(port)}
}

return []attribute.KeyValue{semconv.NetPeerName(host), semconv.NetPeerPort(port)} // nolint: staticcheck
return []attribute.KeyValue{semconv.ClientAddress(host), semconv.ClientPort(port)}
}

func parseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
Expand Down Expand Up @@ -187,7 +187,7 @@ func serverAttr(addr string) []attribute.KeyValue {
}

return []attribute.KeyValue{
attribute.Key("server.address").String(host),
attribute.Key("server.port").Int(port),
semconv.ServerAddress(host),
semconv.ServerPort(port),
}
}
18 changes: 9 additions & 9 deletions internal/handler/middleware/grpc/otelmetrics/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
rpc_status "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -101,8 +101,8 @@ func TestHandlerObserveKnownRequests(t *testing.T) {
assert.Equal(t, "heimdall.local",
attributeValue(activeRequests.DataPoints[0].Attributes, "server.address").AsString())
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue("server.port"))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerAddrKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerPortKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerAddressKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerPortKey))
},
},
{
Expand Down Expand Up @@ -152,8 +152,8 @@ func TestHandlerObserveKnownRequests(t *testing.T) {
assert.Equal(t, "heimdall.local",
attributeValue(activeRequests.DataPoints[0].Attributes, "server.address").AsString())
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue("server.port"))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerAddrKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerPortKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerAddressKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerPortKey))
},
},
{
Expand Down Expand Up @@ -197,8 +197,8 @@ func TestHandlerObserveKnownRequests(t *testing.T) {
assert.Equal(t, "heimdall.local",
attributeValue(activeRequests.DataPoints[0].Attributes, "server.address").AsString())
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue("server.port"))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerAddrKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerPortKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerAddressKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerPortKey))
},
},
} {
Expand Down Expand Up @@ -347,6 +347,6 @@ func TestHandlerObserveUnknownRequests(t *testing.T) {
assert.Equal(t, "127.0.0.1",
attributeValue(activeRequests.DataPoints[0].Attributes, "server.address").AsString())
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue("server.port"))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerAddrKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetSockPeerPortKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerAddressKey))
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue(semconv.NetworkPeerAddressKey))
}
33 changes: 9 additions & 24 deletions internal/handler/middleware/http/otelmetrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package otelmetrics

import (
"net/http"
"strconv"
"strings"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"

"github.com/dadrus/heimdall/internal/x"
"github.com/dadrus/heimdall/internal/x/httpx"
Expand Down Expand Up @@ -104,14 +105,13 @@ func serverRequestMetrics(server string, req *http.Request) []attribute.KeyValue

attrs := make([]attribute.KeyValue, 0, attrsCount)
attrs = append(attrs, methodMetric(req.Method))
attrs = append(attrs, x.IfThenElse(req.TLS != nil,
semconv.HTTPSchemeKey.String("https"), // nolint: staticcheck
semconv.HTTPSchemeKey.String("http"))) // nolint: staticcheck
attrs = append(attrs, flavor(req.Proto))
attrs = append(attrs, semconv.NetHostNameKey.String(host)) // nolint: staticcheck
attrs = append(attrs, x.IfThenElse(req.TLS != nil, semconv.URLScheme("https"), semconv.URLScheme("http")))
attrs = append(attrs, semconv.NetworkProtocolName("http"))
attrs = append(attrs, semconv.NetworkProtocolVersion(strconv.Itoa(req.ProtoMajor)+"."+strconv.Itoa(req.ProtoMinor)))
attrs = append(attrs, semconv.ServerAddress(host))

if hostPort > 0 {
attrs = append(attrs, semconv.NetHostPortKey.Int(hostPort)) // nolint: staticcheck
attrs = append(attrs, semconv.ServerPort(hostPort))
}

return attrs
Expand All @@ -133,25 +133,10 @@ func methodMetric(method string) attribute.KeyValue {
method = "_OTHER"
}

return semconv.HTTPMethodKey.String(method)
return semconv.HTTPRequestMethodKey.String(method)
}

func flavor(proto string) attribute.KeyValue {
switch proto {
case "HTTP/1.0":
return semconv.HTTPFlavorHTTP10 // nolint: staticcheck
case "HTTP/1.1":
return semconv.HTTPFlavorHTTP11 // nolint: staticcheck
case "HTTP/2":
return semconv.HTTPFlavorHTTP20 // nolint: staticcheck
case "HTTP/3":
return semconv.HTTPFlavorHTTP30 // nolint: staticcheck
default:
return semconv.HTTPFlavorKey.String(proto) // nolint: staticcheck
}
}

func requiredHTTPPort(https bool, port int) int { // nolint:revive
func requiredHTTPPort(https bool, port int) int {
if https {
if port > 0 && port != 443 {
return port
Expand Down
14 changes: 8 additions & 6 deletions internal/handler/middleware/http/otelmetrics/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,22 @@ func TestHandlerExecution(t *testing.T) {
assert.False(t, activeRequests.IsMonotonic)
require.Len(t, activeRequests.DataPoints, 1)
require.InDelta(t, float64(0), activeRequests.DataPoints[0].Value, 0.00)
require.Equal(t, 7, activeRequests.DataPoints[0].Attributes.Len())
require.Equal(t, 8, activeRequests.DataPoints[0].Attributes.Len())
assert.Equal(t, "foobar",
attributeValue(activeRequests.DataPoints[0].Attributes, "service.subsystem").AsString())
assert.Equal(t, "zab",
attributeValue(activeRequests.DataPoints[0].Attributes, "baz").AsString())
assert.Equal(t, "http",
attributeValue(activeRequests.DataPoints[0].Attributes, "network.protocol.name").AsString())
assert.Equal(t, "1.1",
attributeValue(activeRequests.DataPoints[0].Attributes, "http.flavor").AsString())
attributeValue(activeRequests.DataPoints[0].Attributes, "network.protocol.version").AsString())
assert.Equal(t, http.MethodGet,
attributeValue(activeRequests.DataPoints[0].Attributes, "http.method").AsString())
attributeValue(activeRequests.DataPoints[0].Attributes, "http.request.method").AsString())
assert.Equal(t, "http",
attributeValue(activeRequests.DataPoints[0].Attributes, "http.scheme").AsString())
attributeValue(activeRequests.DataPoints[0].Attributes, "url.scheme").AsString())
assert.Equal(t, "127.0.0.1",
attributeValue(activeRequests.DataPoints[0].Attributes, "net.host.name").AsString())
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue("net.host.port"))
attributeValue(activeRequests.DataPoints[0].Attributes, "server.address").AsString())
assert.True(t, activeRequests.DataPoints[0].Attributes.HasValue("server.port"))
},
},
} {
Expand Down
2 changes: 1 addition & 1 deletion internal/otel/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package otel

import (
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"

"github.com/dadrus/heimdall/version"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/x/opentelemetry/mocks/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/bridge/opentracing/migration"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/embedded"
"go.opentelemetry.io/otel/trace/noop"
Expand Down

0 comments on commit e5ed3a5

Please sign in to comment.