Skip to content

Commit

Permalink
OPA: Add response status to control plane traces (zalando#3118)
Browse files Browse the repository at this point in the history
Signed-off-by: Magnus Jungsbluth <[email protected]>
  • Loading branch information
mjungsbluth authored and Janardhan Sharma committed Jul 19, 2024
1 parent 2c91aac commit 63e499e
Showing 1 changed file with 20 additions and 0 deletions.
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 63e499e

Please sign in to comment.