-
Notifications
You must be signed in to change notification settings - Fork 419
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
contrib: Adding WithCustomTag
to various integrations
#1359
Changes from 15 commits
f01e184
f8c0559
97b98bd
ce2377c
44f1af4
b34c1ef
7791857
fa1a32d
be8d52d
d3e6207
52921ec
ba1c464
0cfc41f
3d28837
4f31706
4be53e6
7830af8
1e2d3f9
bbd4673
a4a48ed
37fef06
b4a432a
2847535
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -277,3 +277,49 @@ func TestDeprecatedContext(t *testing.T) { | |||||
<-c.Events() | ||||||
|
||||||
} | ||||||
|
||||||
func TestCustomTags(t *testing.T) { | ||||||
mt := mocktracer.Start() | ||||||
defer mt.Stop() | ||||||
|
||||||
c, err := NewConsumer(&kafka.ConfigMap{ | ||||||
"go.events.channel.enable": true, // required for the events channel to be turned on | ||||||
"group.id": testGroupID, | ||||||
"socket.timeout.ms": 10, | ||||||
"session.timeout.ms": 10, | ||||||
"enable.auto.offset.store": false, | ||||||
}, WithCustomTag("foo", func(msg *kafka.Message) interface{} { | ||||||
return "bar" | ||||||
}), WithCustomTag("key", func(msg *kafka.Message) interface{} { | ||||||
return msg.Key | ||||||
})) | ||||||
assert.NoError(t, err) | ||||||
|
||||||
err = c.Subscribe(testTopic, nil) | ||||||
assert.NoError(t, err) | ||||||
|
||||||
go func() { | ||||||
c.Consumer.Events() <- &kafka.Message{ | ||||||
TopicPartition: kafka.TopicPartition{ | ||||||
Topic: &testTopic, | ||||||
Partition: 1, | ||||||
Offset: 1, | ||||||
}, | ||||||
Key: []byte("key1"), | ||||||
Value: []byte("value1"), | ||||||
} | ||||||
}() | ||||||
|
||||||
_ = (<-c.Events()).(*kafka.Message) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why does it need a cast and a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the object from the channel is not consumed and what you suggested is preferred. |
||||||
|
||||||
c.Close() | ||||||
// wait for the events channel to be closed | ||||||
<-c.Events() | ||||||
|
||||||
spans := mt.FinishedSpans() | ||||||
assert.Len(t, spans, 1) | ||||||
s := spans[0] | ||||||
|
||||||
assert.Equal(t, "bar", s.Tag("foo")) | ||||||
assert.Equal(t, []byte("key1"), s.Tag("key")) | ||||||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -216,6 +216,13 @@ func (tp *traceParams) tryTrace(ctx context.Context, qtype queryType, query stri | |||
tracer.SpanType(ext.SpanTypeSQL), | ||||
tracer.StartTime(startTime), | ||||
) | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here. |
||||
if tp.cfg.tags != nil { | ||||
for key, tag := range tp.cfg.tags { | ||||
opts = append(opts, tracer.Tag(key, tag)) | ||||
} | ||||
} | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
if !math.IsNaN(tp.cfg.analyticsRate) { | ||||
opts = append(opts, tracer.Tag(ext.EventSampleRate, tp.cfg.analyticsRate)) | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,6 +18,7 @@ type config struct { | |||||
analyticsRate float64 | ||||||
dsn string | ||||||
childSpansOnly bool | ||||||
tags map[string]interface{} | ||||||
commentInjectionMode tracer.SQLCommentInjectionMode | ||||||
} | ||||||
|
||||||
|
@@ -88,6 +89,16 @@ func WithChildSpansOnly() Option { | |||||
} | ||||||
} | ||||||
|
||||||
// WithCustomTag will attach the value to the span tagged by the key. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is this correct? For some reason the current explanation is a bit confusing. |
||||||
func WithCustomTag(key string, value interface{}) Option { | ||||||
gbbr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return func(cfg *config) { | ||||||
if cfg.tags == nil { | ||||||
cfg.tags = make(map[string]interface{}) | ||||||
} | ||||||
cfg.tags[key] = value | ||||||
} | ||||||
} | ||||||
|
||||||
// WithSQLCommentInjection enables injection of tags as sql comments on traced queries. | ||||||
// This includes dynamic values like span id, trace id and sampling priority which can make queries | ||||||
// unique for some cache implementations. Use WithStaticTagsCommentInjection if this is a concern. | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,24 @@ import ( | |||||||||||||||||||||||||||||||||||||
"google.golang.org/grpc/status" | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func addCustomTags(cfg *config, opts ...tracer.StartSpanOption) []tracer.StartSpanOption { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is better suited as a method on // appendOpts creates a new set of StartSpanOptions by appending all options defined by configuration
// to opts.
func (cfg *config) appendOpts(opts ...tracer.StartSpanOption) []tracer.StartSpanOption { You can probably include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed |
||||||||||||||||||||||||||||||||||||||
if cfg == nil || len(cfg.tags) == 0 { | ||||||||||||||||||||||||||||||||||||||
return opts | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
ret := make([]tracer.StartSpanOption, len(cfg.tags)+len(opts)) | ||||||||||||||||||||||||||||||||||||||
i := 0 | ||||||||||||||||||||||||||||||||||||||
for _, opt := range opts { | ||||||||||||||||||||||||||||||||||||||
ret[i] = opt | ||||||||||||||||||||||||||||||||||||||
i++ | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
for key, tag := range cfg.tags { | ||||||||||||||||||||||||||||||||||||||
ret[i] = tracer.Tag(key, tag) | ||||||||||||||||||||||||||||||||||||||
i++ | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why not |
||||||||||||||||||||||||||||||||||||||
return ret | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func startSpanFromContext( | ||||||||||||||||||||||||||||||||||||||
ctx context.Context, method, operation, service string, opts ...tracer.StartSpanOption, | ||||||||||||||||||||||||||||||||||||||
) (ddtrace.Span, context.Context) { | ||||||||||||||||||||||||||||||||||||||
|
@@ -31,6 +49,7 @@ func startSpanFromContext( | |||||||||||||||||||||||||||||||||||||
tracer.Tag(tagMethodName, method), | ||||||||||||||||||||||||||||||||||||||
tracer.SpanType(ext.AppTypeRPC), | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't check the style guidance prior to submitting the PR. |
||||||||||||||||||||||||||||||||||||||
md, _ := metadata.FromIncomingContext(ctx) // nil is ok | ||||||||||||||||||||||||||||||||||||||
if sctx, err := tracer.Extract(grpcutil.MDCarrier(md)); err == nil { | ||||||||||||||||||||||||||||||||||||||
opts = append(opts, tracer.ChildOf(sctx)) | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see point 3 in https://github.com/DataDog/dd-trace-go/wiki/Style-guidelines#general-guidelines. Happy to discuss these guidelines at any time and happy to change them if we see any issues.