Skip to content

Commit

Permalink
Always add http status code to initial span (#1719)
Browse files Browse the repository at this point in the history
* Always add http status code to initial span, fix #1145

Signed-off-by: Ruben Barilani <[email protected]>

* Refactor use ctx.initialSpan also on proxy.errorResponse

Signed-off-by: Ruben Barilani <[email protected]>
  • Loading branch information
rbarilani authored Feb 22, 2021
1 parent fbde0f7 commit de70c2b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions proxy/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type context struct {
startServe time.Time
metrics *filterMetrics
tracer opentracing.Tracer
initialSpan opentracing.Span
proxySpan opentracing.Span
parentSpan opentracing.Span
proxy *Proxy
Expand Down
14 changes: 7 additions & 7 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,8 @@ func (p *Proxy) serveResponse(ctx *context) {
p.tracing.setTag(ctx.proxySpan, ClientRequestStateTag, ClientRequestCanceled)
}

p.tracing.setTag(ctx.initialSpan, HTTPStatusCodeTag, uint16(ctx.response.StatusCode))

ctx.responseWriter.WriteHeader(ctx.response.StatusCode)
ctx.responseWriter.Flush()
n, err := copyStream(ctx.responseWriter, ctx.response.Body)
Expand Down Expand Up @@ -1224,13 +1226,10 @@ func (p *Proxy) errorResponse(ctx *context, err error) {
code = perr.code
}

if span := ot.SpanFromContext(ctx.Request().Context()); span != nil {
p.tracing.setTag(span, ErrorTag, true)
p.tracing.setTag(span, HTTPStatusCodeTag, uint16(code))
if err == errRouteLookupFailed {
span.LogKV("event", "error", "message", errRouteLookup.Error())
}

p.tracing.setTag(ctx.initialSpan, ErrorTag, true)
p.tracing.setTag(ctx.initialSpan, HTTPStatusCodeTag, uint16(code))
if err == errRouteLookupFailed {
ctx.initialSpan.LogKV("event", "error", "message", errRouteLookup.Error())
}

if p.flags.Debug() {
Expand Down Expand Up @@ -1415,6 +1414,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx = newContext(lw, r, p)
ctx.startServe = time.Now()
ctx.tracer = p.tracing.tracer
ctx.initialSpan = span

defer func() {
if ctx.response != nil && ctx.response.Body != nil {
Expand Down
2 changes: 1 addition & 1 deletion proxy/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestTracingIngressSpan(t *testing.T) {
verifyTag(t, span, HTTPPathTag, "/hello")
verifyTag(t, span, HTTPHostTag, ps.Listener.Addr().String())
verifyTag(t, span, FlowIDTag, "test-flow-id")
verifyTag(t, span, HTTPStatusCodeTag, nil) // status tag is not set on ingress span
verifyTag(t, span, HTTPStatusCodeTag, uint16(200))
}

func TestTracingSpanName(t *testing.T) {
Expand Down

0 comments on commit de70c2b

Please sign in to comment.