Skip to content

Commit

Permalink
Remove hostIPNamePort from span info
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Nov 12, 2024
1 parent 90083f6 commit 7539769
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 58 deletions.
44 changes: 1 addition & 43 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 2 additions & 11 deletions semconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion trace_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions trace_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 7539769

Please sign in to comment.