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/database/sql: add db service name tag and _dd.dbm_trace_injected span tag #1448
Changes from 2 commits
12cbdbd
23ec6e0
29cd203
75a1e93
d1791d5
1dc064a
9cd4841
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
) | ||
|
||
// SQLCommentInjectionMode represents the mode of SQL comment injection. | ||
|
@@ -34,6 +35,7 @@ const ( | |
sqlCommentTraceID = "ddtid" | ||
sqlCommentSpanID = "ddsid" | ||
sqlCommentService = "ddsn" | ||
sqlCommentDBService = "dddbs" | ||
sqlCommentVersion = "ddsv" | ||
sqlCommentEnv = "dde" | ||
) | ||
|
@@ -42,9 +44,10 @@ const ( | |
// of a sqlcommenter formatted comment prepended to the original query text. | ||
// See https://google.github.io/sqlcommenter/spec/ for more details. | ||
type SQLCommentCarrier struct { | ||
Query string | ||
Mode SQLCommentInjectionMode | ||
SpanID uint64 | ||
Query string | ||
Mode SQLCommentInjectionMode | ||
DBServiceName string | ||
SpanID uint64 | ||
} | ||
|
||
// Inject injects a span context in the carrier's Query field as a comment. | ||
|
@@ -83,6 +86,9 @@ func (c *SQLCommentCarrier) Inject(spanCtx ddtrace.SpanContext) error { | |
if v, ok := ctx.meta(ext.Version); ok { | ||
version = v | ||
} | ||
if s := ctx.span; s != nil { | ||
tags[sqlCommentDBService] = ctx.span.Service | ||
} | ||
} | ||
if globalconfig.ServiceName() != "" { | ||
tags[sqlCommentService] = globalconfig.ServiceName() | ||
|
@@ -93,6 +99,7 @@ func (c *SQLCommentCarrier) Inject(spanCtx ddtrace.SpanContext) error { | |
if version != "" { | ||
tags[sqlCommentVersion] = version | ||
} | ||
tags[sqlCommentDBService] = c.DBServiceName | ||
} | ||
c.Query = commentQuery(c.Query, tags) | ||
return nil | ||
|
@@ -113,7 +120,7 @@ func commentQuery(query string, tags map[string]string) string { | |
var b strings.Builder | ||
// the sqlcommenter specification dictates that tags should be sorted. Since we know all injected keys, | ||
// we skip a sorting operation by specifying the order of keys statically | ||
orderedKeys := []string{sqlCommentEnv, sqlCommentSpanID, sqlCommentService, sqlCommentKeySamplingPriority, sqlCommentVersion, sqlCommentTraceID} | ||
orderedKeys := []string{sqlCommentDBService, sqlCommentEnv, sqlCommentSpanID, sqlCommentService, sqlCommentKeySamplingPriority, sqlCommentVersion, sqlCommentTraceID} | ||
first := true | ||
for _, k := range orderedKeys { | ||
if v, ok := tags[k]; ok { | ||
|
@@ -141,6 +148,9 @@ func commentQuery(query string, tags map[string]string) string { | |
if query == "" { | ||
return b.String() | ||
} | ||
if log.DebugEnabled() { | ||
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 don't think you need to check if DebugEnabled, you can just call log.Debug and it will not log if debug is disabled 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. Yeah, I was wondering about that. The comment on that DebugEnabled said it was for things in "hot paths" to avoid the interface{} allocations. I wasn't sure if this would be considered a hot path or not. I'm fine to just switch to log.Debug. 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. Updated ✅ |
||
log.Debug("Injected sql comment %s", b.String()) | ||
} | ||
b.WriteRune(' ') | ||
b.WriteString(query) | ||
return b.String() | ||
|
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.
Any reason to create
s
here? Perhaps justif ctx.span != nil
?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.
Good catch. Updated ✅