From 753976940ba5e047980e9dbc99adad379ab15ac0 Mon Sep 17 00:00:00 2001 From: Stanislas Lange Date: Tue, 12 Nov 2024 11:31:00 +0100 Subject: [PATCH] Remove hostIPNamePort from span info --- common.go | 44 +------------------------------------------- semconv.go | 13 ++----------- trace_client.go | 2 +- trace_server.go | 4 +--- 4 files changed, 5 insertions(+), 58 deletions(-) diff --git a/common.go b/common.go index f546056..c581880 100644 --- a/common.go +++ b/common.go @@ -3,64 +3,22 @@ package oteltwirp import ( "context" "fmt" - "net" - "strconv" "github.com/twitchtv/twirp" "go.opentelemetry.io/otel/attribute" semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) -type oteltwirpKey string - -const keyRemoteAddr oteltwirpKey = "OteltwirpRemoteAddr" - -// hostIPNamePort extracts the IP address, name and (optional) port from hostWithPort. -// It handles both IPv4 and IPv6 addresses. If the host portion is not recognized -// as a valid IPv4 or IPv6 address, the `ip` result will be empty and the -// host portion will instead be returned in `name`. -func hostIPNamePort(hostWithPort string) (ip string, name string, port int) { - var ( - hostPart, portPart string - parsedPort uint64 - err error - ) - if hostPart, portPart, err = net.SplitHostPort(hostWithPort); err != nil { - hostPart, portPart = hostWithPort, "" - } - if parsedIP := net.ParseIP(hostPart); parsedIP != nil { - ip = parsedIP.String() - } else { - name = hostPart - } - if parsedPort, err = strconv.ParseUint(portPart, 10, 16); err == nil { - port = int(parsedPort) - } - return -} - // spanInfo returns a span name and all appropriate attributes from the context -func spanInfo(ctx context.Context, addr string) (string, []attribute.KeyValue) { +func spanInfo(ctx context.Context) (string, []attribute.KeyValue) { packageName, _ := twirp.PackageName(ctx) serviceName, _ := twirp.ServiceName(ctx) methodName, _ := twirp.MethodName(ctx) attr := []attribute.KeyValue{ semconv.RPCServiceKey.String(serviceName), semconv.RPCMethodKey.String(methodName), - semconv.NetTransportTCP, } spanName := fmt.Sprintf("%s.%s/%s", packageName, serviceName, methodName) - ip, name, port := hostIPNamePort(addr) - if ip != "" { - attr = append(attr, semconv.NetPeerIPKey.String(ip)) - } - if name != "" { - attr = append(attr, semconv.NetPeerNameKey.String(name)) - } - if port != 0 { - attr = append(attr, semconv.NetPeerPortKey.Int(port)) - } - return spanName, attr } diff --git a/semconv.go b/semconv.go index d640c8c..abb3078 100644 --- a/semconv.go +++ b/semconv.go @@ -9,22 +9,13 @@ const instrumentationName = "github.com/qonto/twirp-otel" // Semantic conventions for attribute keys for twirp. const ( - // Name of message transmitted or received. - RPCNameKey = attribute.Key("name") - // Type of message transmitted or received. RPCMessageTypeKey = attribute.Key("message.type") ) // Semantic conventions for common RPC attributes. var ( - // Semantic convention for gRPC as the remoting system. - RPCSystemTwirp = semconv.RPCSystemKey.String("twrip") - - // Semantic convention for a message named message. - RPCNameMessage = RPCNameKey.String("message") - // Semantic conventions for RPC message types. - RPCMessageTypeSent = RPCMessageTypeKey.String("SENT") - RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED") + RPCMessageTypeSent = RPCMessageTypeKey.String("SENT") //nolint:gochecknoglobals + RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED") //nolint:gochecknoglobals ) diff --git a/trace_client.go b/trace_client.go index b312013..4065ecb 100644 --- a/trace_client.go +++ b/trace_client.go @@ -45,7 +45,7 @@ func (c *TraceHTTPClient) configure(cfg *config) { // making the actual request. func (c *TraceHTTPClient) Do(r *http.Request) (*http.Response, error) { ctx := r.Context() - name, attr := spanInfo(ctx, r.RemoteAddr) + name, attr := spanInfo(ctx) attr = append(attr, semconv.HTTPClientAttributesFromHTTPRequest(r)...) ctx, span := c.tracer.Start( ctx, diff --git a/trace_server.go b/trace_server.go index 8400800..7c50068 100644 --- a/trace_server.go +++ b/trace_server.go @@ -52,8 +52,7 @@ func (t *TraceServerHooks) startTraceSpan(ctx context.Context) (context.Context, // handleRequestRouted sets the operation name and attributes because we won't // know what it is until the RequestRouted hook. func (t *TraceServerHooks) handleRequestRouted(ctx context.Context) (context.Context, error) { - remoteAddr := ctx.Value(keyRemoteAddr).(string) - name, attr := spanInfo(ctx, remoteAddr) + name, attr := spanInfo(ctx) span := trace.SpanFromContext(ctx) span.SetName(name) span.SetAttributes(attr...) @@ -87,7 +86,6 @@ func WithTraceContext(handler http.Handler, opts ...Option) http.Handler { cfg := newConfig(opts) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := cfg.propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header)) - ctx = context.WithValue(ctx, keyRemoteAddr, r.RemoteAddr) r = r.WithContext(ctx) handler.ServeHTTP(w, r) })