Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(middleware/tracing): upgrade otel semconv #3416

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
46 changes: 30 additions & 16 deletions middleware/tracing/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"net"
"net/url"
"strconv"
"strings"

"github.com/go-kratos/kratos/v2/metadata"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/peer"
"google.golang.org/protobuf/proto"
Expand All @@ -31,12 +32,16 @@ func setClientSpan(ctx context.Context, span trace.Span, m interface{}) {
switch tr.Kind() {
case transport.KindHTTP:
if ht, ok := tr.(http.Transporter); ok {
method := ht.Request().Method
route := ht.PathTemplate()
path := ht.Request().URL.Path
attrs = append(attrs, semconv.HTTPMethodKey.String(method))
attrs = append(attrs, semconv.HTTPRouteKey.String(route))
attrs = append(attrs, semconv.HTTPTargetKey.String(path))
attrs = append(attrs,
semconv.HTTPRequestMethodKey.String(ht.Request().Method),
semconv.HTTPRouteKey.String(ht.PathTemplate()),
semconv.URLFragment(ht.Request().URL.Fragment),
semconv.URLPath(ht.Request().URL.Path),
semconv.URLFull(ht.Request().URL.String()),
semconv.URLQuery(ht.Request().URL.RawQuery),
semconv.URLScheme(ht.Request().URL.Scheme),
semconv.UserAgentOriginal(ht.Request().UserAgent()),
)
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
remote = ht.Request().Host
}
case transport.KindGRPC:
Expand Down Expand Up @@ -70,12 +75,16 @@ func setServerSpan(ctx context.Context, span trace.Span, m interface{}) {
switch tr.Kind() {
case transport.KindHTTP:
if ht, ok := tr.(http.Transporter); ok {
method := ht.Request().Method
route := ht.PathTemplate()
path := ht.Request().URL.Path
attrs = append(attrs, semconv.HTTPMethodKey.String(method))
attrs = append(attrs, semconv.HTTPRouteKey.String(route))
attrs = append(attrs, semconv.HTTPTargetKey.String(path))
attrs = append(attrs,
semconv.HTTPRequestMethodKey.String(ht.Request().Method),
semconv.HTTPRouteKey.String(ht.PathTemplate()),
semconv.URLFragment(ht.Request().URL.Fragment),
semconv.URLPath(ht.Request().URL.Path),
semconv.URLFull(ht.Request().URL.String()),
semconv.URLQuery(ht.Request().URL.RawQuery),
semconv.URLScheme(ht.Request().URL.Scheme),
semconv.UserAgentOriginal(ht.Request().UserAgent()),
)
remote = ht.Request().RemoteAddr
}
case transport.KindGRPC:
Expand Down Expand Up @@ -130,10 +139,15 @@ func peerAttr(addr string) []attribute.KeyValue {
host = "127.0.0.1"
}

return []attribute.KeyValue{
semconv.NetPeerIPKey.String(host),
semconv.NetPeerPortKey.String(port),
attrs := []attribute.KeyValue{
semconv.NetworkPeerAddress(host),
}

if pi, err := strconv.Atoi(port); err == nil {
attrs = append(attrs, semconv.NetworkPeerPort(pi))
}

return attrs
}

func parseTarget(endpoint string) (address string, err error) {
Expand Down
14 changes: 7 additions & 7 deletions middleware/tracing/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

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

"go.opentelemetry.io/otel/trace/noop"
Expand Down Expand Up @@ -74,8 +74,8 @@ func Test_peerAttr(t *testing.T) {
name: "nil addr",
addr: ":8080",
want: []attribute.KeyValue{
semconv.NetPeerIPKey.String("127.0.0.1"),
semconv.NetPeerPortKey.String("8080"),
semconv.NetworkPeerAddress("127.0.0.1"),
semconv.NetworkPeerPort(8080),
},
},
{
Expand All @@ -87,16 +87,16 @@ func Test_peerAttr(t *testing.T) {
name: "normal addr with port",
addr: "192.168.0.1:8080",
want: []attribute.KeyValue{
semconv.NetPeerIPKey.String("192.168.0.1"),
semconv.NetPeerPortKey.String("8080"),
semconv.NetworkPeerAddress("192.168.0.1"),
semconv.NetworkPeerPort(8080),
},
},
{
name: "dns addr",
addr: "foo:8080",
want: []attribute.KeyValue{
semconv.NetPeerIPKey.String("foo"),
semconv.NetPeerPortKey.String("8080"),
semconv.NetworkPeerAddress("foo"),
semconv.NetworkPeerPort(8080),
},
},
}
Expand Down
Loading