Skip to content

Commit

Permalink
Avoid setting span status to Unknown when no HTTP status is provided;…
Browse files Browse the repository at this point in the history
… stdlib assumes it to be `200 OK`
  • Loading branch information
Aneurysm9 committed Jul 6, 2020
1 parent 27e892a commit 9354304
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- The B3 Single Header name is now correctly `b3` instead of the previous `X-B3`. (#881)
- Ensure span status is not set to `Unknown` when no HTTP status code is provided as it is assumed to be `200 OK`. (#908)

## [0.7.0] - 2020-06-26

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/othttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handler.ServeHTTP(rww, r.WithContext(ctx))

setAfterServeAttributes(span, bw.read, rww.written, rww.statusCode, bw.err, rww.err)
span.SetStatus(standard.SpanStatusFromHTTPStatusCode(rww.statusCode))

// Add request metrics

Expand Down Expand Up @@ -185,6 +184,7 @@ func setAfterServeAttributes(span trace.Span, read, wrote int64, statusCode int,
}
if statusCode > 0 {
kv = append(kv, standard.HTTPAttributesFromHTTPStatusCode(statusCode)...)
span.SetStatus(standard.SpanStatusFromHTTPStatusCode(statusCode))
}
if werr != nil && werr != io.EOF {
kv = append(kv, WriteErrorKey.String(werr.Error()))
Expand Down
43 changes: 42 additions & 1 deletion instrumentation/othttp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"

"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/standard"
"go.opentelemetry.io/otel/api/trace"
mockmeter "go.opentelemetry.io/otel/internal/metric"
mocktrace "go.opentelemetry.io/otel/internal/trace"
)
Expand Down Expand Up @@ -71,7 +73,6 @@ func TestHandlerBasics(t *testing.T) {
standard.HTTPFlavorKey.String(fmt.Sprintf("1.%d", r.ProtoMinor)),
}

standard.HTTPServerMetricAttributesFromHTTPRequest(operation, r)
assertMetricLabels(t, labelsToVerify, meterimpl.MeasurementBatches)

if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected {
Expand All @@ -91,3 +92,43 @@ func TestHandlerBasics(t *testing.T) {
t.Fatalf("got %q, expected %q", got, expected)
}
}

func TestHandlerNoWrite(t *testing.T) {
rr := httptest.NewRecorder()

var id uint64
tracer := mocktrace.MockTracer{StartSpanID: &id}

operation := "test_handler"
var span trace.Span

h := NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span = trace.SpanFromContext(r.Context())
}), operation,
WithTracer(&tracer),
)

r, err := http.NewRequest(http.MethodGet, "http://localhost/", nil)
if err != nil {
t.Fatal(err)
}
h.ServeHTTP(rr, r)

if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected {
t.Fatalf("got %d, expected %d", got, expected)
}
if got := rr.Header().Get("Traceparent"); got != "" {
t.Fatal("expected empty trace header")
}
if got, expected := id, uint64(1); got != expected {
t.Fatalf("got %d, expected %d", got, expected)
}
if mockSpan, ok := span.(*mocktrace.MockSpan); ok {
if got, expected := mockSpan.Status, codes.OK; got != expected {
t.Fatalf("got %q, expected %q", got, expected)
}
} else {
t.Fatalf("Expected *moctrace.MockSpan, got %T", span)
}
}
1 change: 0 additions & 1 deletion instrumentation/othttp/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (w *respWriterWrapper) Header() http.Header {
func (w *respWriterWrapper) Write(p []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
w.wroteHeader = true
}
n, err := w.ResponseWriter.Write(p)
n1 := int64(n)
Expand Down
10 changes: 7 additions & 3 deletions internal/trace/mock_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (

// MockSpan is a mock span used in association with MockTracer for testing purpose only.
type MockSpan struct {
sc apitrace.SpanContext
tracer apitrace.Tracer
Name string
sc apitrace.SpanContext
tracer apitrace.Tracer
Name string
Status codes.Code
StatusMsg string
}

var _ apitrace.Span = (*MockSpan)(nil)
Expand All @@ -49,6 +51,8 @@ func (ms *MockSpan) IsRecording() bool {

// SetStatus does nothing.
func (ms *MockSpan) SetStatus(status codes.Code, msg string) {
ms.Status = status
ms.StatusMsg = msg
}

// SetError does nothing.
Expand Down

0 comments on commit 9354304

Please sign in to comment.