Skip to content

Commit

Permalink
Update semconv, migrate to httpconv when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Nov 12, 2024
1 parent 7539769 commit 216bbb4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion semconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions trace_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand All @@ -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())
Expand All @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions trace_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 216bbb4

Please sign in to comment.