Skip to content

Commit

Permalink
impr/tracing: selective trace reporting
Browse files Browse the repository at this point in the history
Adding an option to only reports traces if they are in error.

Author:    CyrilPeponnet <[email protected]>
  • Loading branch information
CyrilPeponnet authored and Antoine Mercadal committed Mar 21, 2023
1 parent 853bf31 commit 82c61eb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type config struct {
opentracing struct {
tracer opentracing.Tracer
excludedIdentities map[string]struct{}
onlyErrors bool
traceCleaner TraceCleaner
}

Expand Down
6 changes: 5 additions & 1 deletion opentracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,16 @@ func traceRequest(ctx context.Context, r *elemental.Request, tracer opentracing.
return trackingCtx
}

func finishTracing(ctx context.Context) {
func finishTracing(ctx context.Context, onlyErrors bool) {

span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}

if span.BaggageItem(errorsOnlyFlagBaggageItem) == "" && onlyErrors {
return
}

span.Finish()
}
42 changes: 40 additions & 2 deletions opentracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package bahamut
import (
"bytes"
"context"
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -320,7 +321,44 @@ func TestTracing_finishTracing(t *testing.T) {

Convey("When I call finishTracing", func() {

finishTracing(ctx)
finishTracing(ctx, false)

Convey("Then my span should be finished", func() {
So(ts.finished, ShouldBeTrue)
})
})

Convey("When I call finishTracing for error only", func() {

finishTracing(ctx, true)

Convey("Then my span should not be finished", func() {
So(ts.finished, ShouldBeFalse)
})
})
})

Convey("Given I have a context with a span in error", t, func() {

tracer := &mockTracer{}
ts := newMockSpan(tracer)

ctx := opentracing.ContextWithSpan(context.Background(), ts)

_ = processError(ctx, fmt.Errorf("an error"))

Convey("When I call finishTracing", func() {

finishTracing(ctx, false)

Convey("Then my span should be finished", func() {
So(ts.finished, ShouldBeTrue)
})
})

Convey("When I call finishTracing for error only", func() {

finishTracing(ctx, false)

Convey("Then my span should be finished", func() {
So(ts.finished, ShouldBeTrue)
Expand All @@ -335,7 +373,7 @@ func TestTracing_finishTracing(t *testing.T) {
Convey("When I call finishTracing", func() {

Convey("Then it should not panic", func() {
So(func() { finishTracing(ctx) }, ShouldNotPanic)
So(func() { finishTracing(ctx, false) }, ShouldNotPanic)
})
})
})
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ func OptOpentracingExcludedIdentities(identities []elemental.Identity) Option {
}
}

// OptOpentracingTraceOnlyErrors only send trace on errors.
func OptOpentracingTraceOnlyErrors(onlyErrors bool) Option {
return func(c *config) {
c.opentracing.onlyErrors = onlyErrors
}
}

// OptPostStartHook registers a function that will be executed right after the server is started.
func OptPostStartHook(hook func(Server) error) Option {
return func(c *config) {
Expand Down
2 changes: 1 addition & 1 deletion rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (a *restServer) makeHandler(handler handlerFunc) http.HandlerFunc {
}

ctx := traceRequest(req.Context(), request, a.cfg.opentracing.tracer, a.cfg.opentracing.excludedIdentities, a.cfg.opentracing.traceCleaner)
defer finishTracing(ctx)
defer finishTracing(ctx, a.cfg.opentracing.onlyErrors)

// Global rate limiting
if a.cfg.rateLimiting.rateLimiter != nil {
Expand Down
3 changes: 3 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"go.aporeto.io/elemental"
)

const errorsOnlyFlagBaggageItem = "onlyErrors"

func handleRecoveredPanic(ctx context.Context, r any, disablePanicRecovery bool) error {

if r == nil {
Expand Down Expand Up @@ -77,6 +79,7 @@ func processError(ctx context.Context, err error) (outError elemental.Errors) {
span.SetTag("error", true)
span.SetTag("status.code", outError.Code())
span.LogFields(log.Object("elemental.error", outError))
span.SetBaggageItem(errorsOnlyFlagBaggageItem, "true")
}

return outError
Expand Down

0 comments on commit 82c61eb

Please sign in to comment.