From 216bbb4cd3486bdec81229b1cd16e34e51d0dcf6 Mon Sep 17 00:00:00 2001 From: Stanislas Lange Date: Tue, 12 Nov 2024 11:31:17 +0100 Subject: [PATCH] Update semconv, migrate to httpconv when needed --- common.go | 2 +- semconv.go | 1 - trace_client.go | 7 +++---- trace_server.go | 8 ++++++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/common.go b/common.go index c581880..b64f99c 100644 --- a/common.go +++ b/common.go @@ -6,7 +6,7 @@ import ( "github.com/twitchtv/twirp" "go.opentelemetry.io/otel/attribute" - semconv "go.opentelemetry.io/otel/semconv/v1.7.0" + semconv "go.opentelemetry.io/otel/semconv/v1.20.0" ) // spanInfo returns a span name and all appropriate attributes from the context diff --git a/semconv.go b/semconv.go index abb3078..92586df 100644 --- a/semconv.go +++ b/semconv.go @@ -2,7 +2,6 @@ package oteltwirp import ( "go.opentelemetry.io/otel/attribute" - semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) const instrumentationName = "github.com/qonto/twirp-otel" diff --git a/trace_client.go b/trace_client.go index 4065ecb..e6aed48 100644 --- a/trace_client.go +++ b/trace_client.go @@ -6,7 +6,7 @@ import ( "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/propagation" - semconv "go.opentelemetry.io/otel/semconv/v1.7.0" + "go.opentelemetry.io/otel/semconv/v1.20.0/httpconv" "go.opentelemetry.io/otel/trace" ) @@ -46,7 +46,7 @@ func (c *TraceHTTPClient) configure(cfg *config) { func (c *TraceHTTPClient) Do(r *http.Request) (*http.Response, error) { ctx := r.Context() name, attr := spanInfo(ctx) - attr = append(attr, semconv.HTTPClientAttributesFromHTTPRequest(r)...) + attr = append(attr, httpconv.ClientRequest(r)...) ctx, span := c.tracer.Start( ctx, name, @@ -59,7 +59,6 @@ func (c *TraceHTTPClient) Do(r *http.Request) (*http.Response, error) { span.AddEvent(rpcEventName, trace.WithAttributes(RPCMessageTypeSent)) resp, err := c.client.Do(r) - if err != nil { span.RecordError(err) span.SetStatus(codes.Error, err.Error()) @@ -72,7 +71,7 @@ func (c *TraceHTTPClient) Do(r *http.Request) (*http.Response, error) { if resp.StatusCode >= 400 && c.includeClientErrors || resp.StatusCode >= 500 { span.SetStatus(codes.Error, "") } - span.SetAttributes(semconv.HTTPAttributesFromHTTPStatusCode(resp.StatusCode)...) + span.SetAttributes(httpconv.ClientResponse(resp)...) // We want to track when the body is closed, meaning the server is done with // the response. diff --git a/trace_server.go b/trace_server.go index 7c50068..6155c4d 100644 --- a/trace_server.go +++ b/trace_server.go @@ -6,9 +6,10 @@ import ( "strconv" "github.com/twitchtv/twirp" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/propagation" - semconv "go.opentelemetry.io/otel/semconv/v1.7.0" + semconv "go.opentelemetry.io/otel/semconv/v1.20.0" "go.opentelemetry.io/otel/trace" ) @@ -64,7 +65,10 @@ func (t *TraceServerHooks) finishTrace(ctx context.Context) { status, haveStatus := twirp.StatusCode(ctx) code, err := strconv.Atoi(status) if haveStatus && err != nil { - span.SetAttributes(semconv.HTTPAttributesFromHTTPStatusCode(code)...) + span.SetAttributes(attribute.KeyValue{ + Key: semconv.HTTPStatusCodeKey, + Value: attribute.IntValue(code), + }) } span.AddEvent(rpcEventName, trace.WithAttributes(RPCMessageTypeSent)) span.End()