Skip to content

Commit

Permalink
tracing: add WithTags() to define default tags (#11)
Browse files Browse the repository at this point in the history
This PR adds the new `WithTags` TraceOption setting to allow users to
define custom tags that will be added by default to each span.
  • Loading branch information
Fatih Arslan authored Sep 15, 2020
1 parent 0526fd8 commit 7d5402b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
24 changes: 24 additions & 0 deletions tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ type TraceServerHooks struct {

type TraceOptions struct {
includeClientErrors bool
tags []TraceTag
}

// TraceTag represents a single span tag.
type TraceTag struct {
Key string

// Value defines the span's tag value. Values can be numeric types, strings, or
// bools.
Value interface{}
}

type TraceOption func(opts *TraceOptions)
Expand All @@ -39,6 +49,14 @@ func IncludeClientErrors(includeClientErrors bool) TraceOption {
}
}

// WithTags defines tags to be added to each outoing span by default. If there
// is a pre-existing tag set for `key`, it is overwritten.
func WithTags(tags ...TraceTag) TraceOption {
return func(opts *TraceOptions) {
opts.tags = tags
}
}

// NewOpenTracingHooks provides a twirp.ServerHooks struct which records
// OpenTracing spans.
func NewOpenTracingHooks(tracer ot.Tracer, opts ...TraceOption) *twirp.ServerHooks {
Expand Down Expand Up @@ -86,6 +104,12 @@ func (t *TraceServerHooks) startTraceSpan(ctx context.Context) (context.Context,
if serviceName, ok := twirp.ServiceName(ctx); ok {
span.SetTag("service", serviceName)
}

if len(t.opts.tags) != 0 {
for _, tag := range t.opts.tags {
span.SetTag(tag.Key, tag.Value)
}
}
}

return ctx, nil
Expand Down
26 changes: 23 additions & 3 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestTracingHooks(t *testing.T) {
tests := []struct {
desc string
service twirptest.Haberdasher
traceOpts []TraceOption
traceOpts []TraceOption
expectedTags map[string]interface{}
expectedLogs []mocktracer.MockLogRecord
errExpected bool
Expand All @@ -38,6 +38,26 @@ func TestTracingHooks(t *testing.T) {
},
expectedLogs: []mocktracer.MockLogRecord{},
},
{
desc: "set tags and logs with additional tags",
service: twirptest.NoopHatmaker(),
traceOpts: []TraceOption{
WithTags(
TraceTag{"foo", "bar"},
TraceTag{"city", "tokyo"},
),
},
expectedTags: map[string]interface{}{
"package": "twirptest",
"component": "twirp",
"service": "Haberdasher",
"span.kind": serverType,
"http.status_code": int64(200),
"foo": "bar",
"city": "tokyo",
},
expectedLogs: []mocktracer.MockLogRecord{},
},
{
desc: "set tags and logs with operation name for an errored request",
service: twirptest.ErroringHatmaker(errors.New("test")),
Expand Down Expand Up @@ -68,8 +88,8 @@ func TestTracingHooks(t *testing.T) {
errExpected: true,
},
{
desc: "user error should be not reported as an erroneous span when correct option is set",
service: twirptest.ErroringHatmaker(twirp.NotFoundError("not found")),
desc: "user error should be not reported as an erroneous span when correct option is set",
service: twirptest.ErroringHatmaker(twirp.NotFoundError("not found")),
traceOpts: []TraceOption{IncludeClientErrors(false)},
expectedTags: map[string]interface{}{
"package": "twirptest",
Expand Down

0 comments on commit 7d5402b

Please sign in to comment.