Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add ClientErrorsAsSpanErrors config to otelecho middleware #6973

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions instrumentation/github.com/labstack/echo/otelecho/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (

// config is used to configure the mux middleware.
type config struct {
TracerProvider oteltrace.TracerProvider
Propagators propagation.TextMapPropagator
Skipper middleware.Skipper
TracerProvider oteltrace.TracerProvider
Propagators propagation.TextMapPropagator
Skipper middleware.Skipper
ClientErrorsAsSpanErrors bool
}

// Option specifies instrumentation configuration options.
Expand Down Expand Up @@ -55,3 +56,11 @@ func WithSkipper(skipper middleware.Skipper) Option {
cfg.Skipper = skipper
})
}

// WithClientErrorsAsSpanErrors specifies whether all HTTP error codes (4xx and 5xx)
// should be treated as errors in spans. By default, only 5xx are treated as errors.
func WithClientErrorsAsSpanErrors(enabled bool) Option {
return optionFunc(func(cfg *config) {
cfg.ClientErrorsAsSpanErrors = enabled
})
}
10 changes: 9 additions & 1 deletion instrumentation/github.com/labstack/echo/otelecho/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package otelecho // import "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"

import (
"fmt"
"net/http"
"slices"
"strings"
Expand All @@ -14,6 +15,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho/internal/semconvutil"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
oteltrace "go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -85,7 +87,13 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc {
}

status := c.Response().Status
span.SetStatus(semconvutil.HTTPServerStatus(status))
if cfg.ClientErrorsAsSpanErrors {
if status >= 400 && status < 600 {
span.SetStatus(codes.Error, fmt.Sprintf("HTTP %d", status))
}
} else {
span.SetStatus(semconvutil.HTTPServerStatus(status))
}
if status > 0 {
span.SetAttributes(semconv.HTTPStatusCode(status))
}
Expand Down