Skip to content

Commit

Permalink
[sc-12075] request: add span on ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
rotta-f committed Jan 5, 2024
1 parent 0d91d05 commit bfda012
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
18 changes: 16 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package goyavetrace

import "goyave.dev/goyave/v4"
import (
"net/http"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"goyave.dev/goyave/v4"
)

// Middleware substitutes the original response writer with a trace writer wrapper.
type Middleware struct {
Expand All @@ -15,6 +20,15 @@ func (m Middleware) Handle(next goyave.Handler) goyave.Handler {
return func(resp *goyave.Response, req *goyave.Request) {
traceWriter := NewWriter(resp, req, m.SpanOption)
resp.SetWriter(traceWriter)
next(resp, req)

ctx := req.Request().Context()
ctx = tracer.ContextWithSpan(ctx, traceWriter.span)

goyave.NativeMiddleware(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
})(next)(resp, req)
}
}
58 changes: 58 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,64 @@ func (suite *MiddlewareTestSuite) TestWriterWithError() {
})
}

func (suite *MiddlewareTestSuite) TestSpanContext() {
err := config.LoadJSON(`{"app": {"environment": "test", "datadog": {"service": "service-name"}}}`)
if !suite.NoError(err) {
return
}

mt := mocktracer.Start()
defer mt.Stop()
suite.RunServer(func(r *goyave.Router) {
r.GlobalMiddleware((&Middleware{
SpanOption: func(s tracer.Span, resp *goyave.Response, req *goyave.Request) {
suite.NotNil(resp)
suite.NotNil(req)
s.SetTag(ext.ManualKeep, true)
},
}).Handle)
r.Get("/test", func(response *goyave.Response, request *goyave.Request) {
ctx := request.Request().Context()

span, _ := tracer.StartSpanFromContext(ctx, "test-span")
span.Finish()

suite.NoError(response.String(http.StatusNoContent, ""))
}).Name("test-route")
}, func() {
result, err := suite.Get("/test", nil)

if !suite.NoError(err) {
return
}
suite.Equal(http.StatusNoContent, result.StatusCode)

spans := mt.FinishedSpans()
if !suite.Len(spans, 2) {
return
}

{
span := spans[0]
suite.Equal("test-span", span.OperationName())
suite.NotZero(span.ParentID())
}

{
span := spans[1]
suite.Equal("web.request", span.OperationName())
suite.Equal("goyave.dev/goyave", span.Tag(ext.Component))
suite.Equal("service-name", span.Tag(ext.ServiceName))
suite.Equal("test", span.Tag(ext.Environment))
suite.Equal(ext.SpanTypeWeb, span.Tag(ext.SpanType))
suite.Equal(ext.SpanKindServer, span.Tag(ext.SpanKind))
suite.Equal("/test", span.Tag(ext.HTTPURL))
suite.Equal(http.MethodGet, span.Tag(ext.HTTPMethod))
suite.Equal("test-route", span.Tag(ext.HTTPRoute))
}
})
}

func TestWriterSuite(t *testing.T) {
err := config.LoadJSON(`{"app": {"environment": "test", "datadog": {"service": "service-name"}}}`)
if !assert.NoError(t, err) {
Expand Down

0 comments on commit bfda012

Please sign in to comment.