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

Use server span when determining status from code #1973

Merged
merged 7 commits into from
Apr 7, 2022
Merged
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- Fix the `otelmux` middleware by using `SpanKindServer` when deciding the `SpanStatus`.
This makes `4xx` response codes to not be an error anymore. (#1973)

### Changed

- Upgraded packages using otel/metrics v0.27.0 to v0.28.0. (#1977)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (tw traceware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer putRRW(rrw)
tw.handler.ServeHTTP(rrw.writer, r2)
attrs := semconv.HTTPAttributesFromHTTPStatusCode(rrw.status)
spanStatus, spanMessage := semconv.SpanStatusFromHTTPStatusCode(rrw.status)
spanStatus, spanMessage := semconv.SpanStatusFromHTTPStatusCodeAndSpanKind(rrw.status, oteltrace.SpanKindServer)
span.SetAttributes(attrs...)
span.SetStatus(spanStatus, spanMessage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import (

"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)

func ok(w http.ResponseWriter, _ *http.Request) {}
func notfound(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "not found", http.StatusNotFound)
}

func TestSDKIntegration(t *testing.T) {
sr := tracetest.NewSpanRecorder()
Expand Down Expand Up @@ -69,6 +73,33 @@ func TestSDKIntegration(t *testing.T) {
)
}

func TestNotFoundIsNotError(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider()
provider.RegisterSpanProcessor(sr)

router := mux.NewRouter()
router.Use(otelmux.Middleware("foobar", otelmux.WithTracerProvider(provider)))
router.HandleFunc("/does/not/exist", notfound)

r0 := httptest.NewRequest("GET", "/does/not/exist", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r0)

require.Len(t, sr.Ended(), 1)
assertSpan(t, sr.Ended()[0],
"/does/not/exist",
trace.SpanKindServer,
attribute.String("http.server_name", "foobar"),
attribute.Int("http.status_code", http.StatusNotFound),
attribute.String("http.method", "GET"),
attribute.String("http.target", "/does/not/exist"),
attribute.String("http.route", "/does/not/exist"),
)
assert.NotEqual(t, sr.Ended()[0].Status().Code, codes.Error)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

}

func assertSpan(t *testing.T, span sdktrace.ReadOnlySpan, name string, kind trace.SpanKind, attrs ...attribute.KeyValue) {
assert.Equal(t, name, span.Name())
assert.Equal(t, trace.SpanKindServer, span.SpanKind())
Expand Down