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

OPA: Add response status to control plane traces #3118

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading