Skip to content

Commit

Permalink
fix(spans): adhere attribute name to otel semver
Browse files Browse the repository at this point in the history
  • Loading branch information
Strazz1337 committed Oct 12, 2024
1 parent 5beb0cf commit d641c90
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
7 changes: 4 additions & 3 deletions compression_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
abstractions "github.com/microsoft/kiota-abstractions-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -104,7 +105,7 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r
req.ContentLength = int64(size)

if span != nil {
span.SetAttributes(attribute.Int64("http.request_content_length", req.ContentLength))
span.SetAttributes(semconv.HTTPRequestBodySize(int(req.ContentLength)))
}

// Sending request with compressed body
Expand All @@ -120,8 +121,8 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r
req.ContentLength = unCompressedContentLength

if span != nil {
span.SetAttributes(attribute.Int64("http.request_content_length", req.ContentLength),
attribute.Int("http.request_content_length", 415))
span.SetAttributes(semconv.HTTPRequestBodySize(int(req.ContentLength)),
semconv.HTTPResponseStatusCode(415))
}

return pipeline.Next(req, middlewareIndex)
Expand Down
23 changes: 12 additions & 11 deletions nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -148,15 +149,15 @@ func (a *NetHttpRequestAdapter) getHttpResponseMessage(ctx context.Context, requ
contentLenHeader := response.Header.Get("Content-Length")
if contentLenHeader != "" {
contentLen, _ := strconv.Atoi(contentLenHeader)
spanForAttributes.SetAttributes(attribute.Int("http.response_content_length", contentLen))
spanForAttributes.SetAttributes(semconv.HTTPResponseBodySize(contentLen))
}
contentTypeHeader := response.Header.Get("Content-Type")
if contentTypeHeader != "" {
spanForAttributes.SetAttributes(attribute.String("http.response_content_type", contentTypeHeader))
spanForAttributes.SetAttributes(attribute.String("http.response.header.content-type", contentTypeHeader))
}
spanForAttributes.SetAttributes(
attribute.Int("http.status_code", response.StatusCode),
attribute.String("http.flavor", response.Proto),
semconv.HTTPResponseStatusCode(response.StatusCode),
semconv.NetworkProtocolName(response.Proto),
)
}
return a.retryCAEResponseIfRequired(ctx, response, requestInfo, claims, spanForAttributes)
Expand Down Expand Up @@ -253,19 +254,19 @@ func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(ctx context.Con
if spanForAttributes == nil {
spanForAttributes = span
}
spanForAttributes.SetAttributes(attribute.String("http.method", requestInfo.Method.String()))
spanForAttributes.SetAttributes(semconv.HTTPRequestMethodKey.String(requestInfo.Method.String()))
uri, err := requestInfo.GetUri()
if err != nil {
spanForAttributes.RecordError(err)
return nil, err
}
spanForAttributes.SetAttributes(
attribute.String("http.scheme", uri.Scheme),
attribute.String("http.host", uri.Host),
semconv.ServerAddress(uri.Scheme),
semconv.URLScheme(uri.Host),
)

if a.observabilityOptions.IncludeEUIIAttributes {
spanForAttributes.SetAttributes(attribute.String("http.uri", uri.String()))
spanForAttributes.SetAttributes(semconv.URLFull(uri.String()))
}

request, err := nethttp.NewRequestWithContext(ctx, requestInfo.Method.String(), uri.String(), nil)
Expand All @@ -290,14 +291,14 @@ func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(ctx context.Con
}
if request.Header.Get("Content-Type") != "" {
spanForAttributes.SetAttributes(
attribute.String("http.request_content_type", request.Header.Get("Content-Type")),
attribute.String("http.request.header.content-type", request.Header.Get("Content-Type")),
)
}
if request.Header.Get("Content-Length") != "" {
contentLenVal, _ := strconv.Atoi(request.Header.Get("Content-Length"))
request.ContentLength = int64(contentLenVal)
spanForAttributes.SetAttributes(
attribute.Int("http.request_content_length", contentLenVal),
semconv.HTTPRequestBodySize(contentLenVal),
)
}
}
Expand All @@ -313,7 +314,7 @@ func (a *NetHttpRequestAdapter) startTracingSpan(ctx context.Context, requestInf
decodedUriTemplate := decodeUriEncodedString(requestInfo.UrlTemplate, []byte{'-', '.', '~', '$'})
telemetryPathValue := queryParametersCleanupRegex.ReplaceAll([]byte(decodedUriTemplate), []byte(""))
ctx, span := otel.GetTracerProvider().Tracer(a.observabilityOptions.GetTracerInstrumentationName()).Start(ctx, methodName+" - "+string(telemetryPathValue))
span.SetAttributes(attribute.String("http.uri_template", decodedUriTemplate))
span.SetAttributes(attribute.String("url.uri_template", decodedUriTemplate))
return ctx, span
}

Expand Down
3 changes: 2 additions & 1 deletion redirect_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
abs "github.com/microsoft/kiota-abstractions-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -120,7 +121,7 @@ func (middleware RedirectHandler) redirectRequest(ctx context.Context, pipeline
if observabilityName != "" {
ctx, span := otel.GetTracerProvider().Tracer(observabilityName).Start(ctx, "RedirectHandler_Intercept - redirect "+fmt.Sprint(redirectCount))
span.SetAttributes(attribute.Int("com.microsoft.kiota.handler.redirect.count", redirectCount),
attribute.Int("http.status_code", response.StatusCode),
semconv.HTTPResponseStatusCode(response.StatusCode),
)
defer span.End()
redirectRequest = redirectRequest.WithContext(ctx)
Expand Down
4 changes: 2 additions & 2 deletions retry_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
abs "github.com/microsoft/kiota-abstractions-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -144,9 +145,8 @@ func (middleware RetryHandler) retryRequest(ctx context.Context, pipeline Pipeli
ctx, span := otel.GetTracerProvider().Tracer(observabilityName).Start(ctx, "RetryHandler_Intercept - attempt "+fmt.Sprint(executionCount))
span.SetAttributes(attribute.Int("http.request.resend_count", executionCount),

attribute.Int("http.status_code", resp.StatusCode),
semconv.HTTPResponseStatusCode(resp.StatusCode),
attribute.Float64("http.request.resend_delay", delay.Seconds()),

)
defer span.End()
req = req.WithContext(ctx)
Expand Down

0 comments on commit d641c90

Please sign in to comment.