Skip to content

Commit

Permalink
OPA: Add response status to control plane traces (#3118)
Browse files Browse the repository at this point in the history
Signed-off-by: Magnus Jungsbluth <[email protected]>
  • Loading branch information
mjungsbluth authored Jun 19, 2024
1 parent 7da6fc6 commit ff44b6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
29 changes: 20 additions & 9 deletions filters/openpolicyagent/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,32 @@ func (tr *transport) RoundTrip(req *http.Request) (*http.Response, error) {
span = tr.tracer.StartSpan(spanNameHttpOut, spanOpts...)
}

if span != nil {
defer span.Finish()
if span == nil {
return tr.wrapped.RoundTrip(req)
}

setSpanTags(span, tr.bundleName, tr.manager)
req = req.WithContext(opentracing.ContextWithSpan(ctx, span))
defer span.Finish()

carrier := opentracing.HTTPHeadersCarrier(req.Header)
span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, carrier)
}
setSpanTags(span, tr.bundleName, tr.manager)
req = req.WithContext(opentracing.ContextWithSpan(ctx, span))

carrier := opentracing.HTTPHeadersCarrier(req.Header)
span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, carrier)

resp, err := tr.wrapped.RoundTrip(req)
if err != nil && span != nil {

if err != nil {
span.SetTag("error", true)
span.LogKV("event", "error", "message", err.Error())
return resp, err
}

span.SetTag(proxy.HTTPStatusCodeTag, resp.StatusCode)

if resp.StatusCode > 399 {
span.SetTag("error", true)
span.LogKV("event", "error", "message", resp.Status)
}

return resp, err
return resp, nil
}
20 changes: 20 additions & 0 deletions filters/openpolicyagent/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/open-policy-agent/opa/plugins"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/tracing/tracingtest"
)

Expand Down Expand Up @@ -43,6 +44,7 @@ func TestTracingFactory(t *testing.T) {
},
tracer: nil,
parentSpan: tracer.StartSpan("open-policy-agent"),
resp: &http.Response{StatusCode: http.StatusOK},
},
{
name: "Sub-span created with parent span without tracer set",
Expand All @@ -54,6 +56,7 @@ func TestTracingFactory(t *testing.T) {
},
tracer: tracer,
parentSpan: tracer.StartSpan("open-policy-agent"),
resp: &http.Response{StatusCode: http.StatusOK},
},
{
name: "Sub-span created without parent span",
Expand All @@ -64,6 +67,7 @@ func TestTracingFactory(t *testing.T) {
URL: &url.URL{Path: "/test", Scheme: "http", Host: "example.com"},
},
tracer: tracer,
resp: &http.Response{StatusCode: http.StatusOK},
},
{
name: "Span contains error information",
Expand All @@ -76,6 +80,17 @@ func TestTracingFactory(t *testing.T) {
tracer: tracer,
resperr: assert.AnError,
},
{
name: "Response has a 4xx response",
req: &http.Request{
Header: map[string][]string{},
Method: "GET",
Host: "example.com",
URL: &url.URL{Path: "/test", Scheme: "http", Host: "example.com"},
},
tracer: tracer,
resp: &http.Response{StatusCode: http.StatusUnauthorized},
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -105,6 +120,11 @@ func TestTracingFactory(t *testing.T) {

if tc.resperr == nil {
assert.NoError(t, err)
if tc.resp.StatusCode > 399 {
assert.Equal(t, true, createdSpan.Tags["error"], "Error tag was not set")
}

assert.Equal(t, tc.resp.StatusCode, createdSpan.Tags[proxy.HTTPStatusCodeTag], "http status tag was not set")
} else {
assert.Equal(t, true, createdSpan.Tags["error"], "Error tag was not set")
assert.Equal(t, tc.resperr, err, "Error was not propagated")
Expand Down

0 comments on commit ff44b6d

Please sign in to comment.