From 2c3d72d52ef1f0555e08dd7354c511ef46e8a410 Mon Sep 17 00:00:00 2001 From: Manu Gupta Date: Sat, 22 Sep 2018 00:28:18 -0400 Subject: [PATCH] Save http status in the outermost span (#694) * Save http status in the outermost span Co-authored-by: Federico Paolinelli * Use ochttp to use span status and store http status code as a tag * Make the conditional oneliner --- pkg/observ/observ.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/observ/observ.go b/pkg/observ/observ.go index 5eeed59d..5e685eca 100644 --- a/pkg/observ/observ.go +++ b/pkg/observ/observ.go @@ -7,6 +7,7 @@ import ( "github.com/gobuffalo/buffalo" "github.com/gomods/athens/pkg/errors" "go.opencensus.io/exporter/jaeger" + "go.opencensus.io/plugin/ochttp" "go.opencensus.io/trace" ) @@ -61,11 +62,20 @@ func Tracer(service string) buffalo.MiddlewareFunc { trace.WithSpanKind(trace.SpanKindClient)) defer span.End() + handler := next(&observabilityContext{Context: ctx, spanCtx: spanCtx}) + + // Add request attributes span.AddAttributes( requestAttrs(ctx.Request())..., ) - return next(&observabilityContext{Context: ctx, spanCtx: spanCtx}) + // SetSpan Status from response + if resp, ok := ctx.Response().(*buffalo.Response); ok { + span.SetStatus(ochttp.TraceStatus(resp.Status, "")) + span.AddAttributes(trace.Int64Attribute("http.status_code", int64(resp.Status))) + } + + return handler } } }