Skip to content

Commit

Permalink
ddtrace/tracer: review patch
Browse files Browse the repository at this point in the history
- Change UserTagOption to UserMonitoringOption
- Options now return a closure that sets the tag directly
- Improve comments
- SetUser new sets the tags even if span type assertion fails
  • Loading branch information
Hellzy committed Mar 3, 2022
1 parent 33c9ba2 commit 2cacd2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
45 changes: 22 additions & 23 deletions ddtrace/tracer/option.go
Expand Up @@ -830,41 +830,40 @@ func StackFrames(n, skip uint) FinishOption {
}
}

// UserTagOption represents a function that can be provided as a parameter to SetUser.
// This is used to normalize user tags key/value pairs.
type UserTagOption func() (key string, val interface{})
// UserMonitoringOption represents a function that can be provided as a parameter to SetUser.
type UserMonitoringOption func(Span)

// WithUserEmailTag emits the normalized key/value UserTagOption holding user email information.
func WithUserEmailTag(email string) UserTagOption {
return func() (key string, val interface{}) {
return "usr.email", email
// WithUserEmail returns the option setting the email of the authenticated user.
func WithUserEmail(email string) UserMonitoringOption {
return func(s Span) {
s.SetTag("usr.email", email)
}
}

// WithUserNameTag emits the normalized key/value UserTagOption holding user name information.
func WithUserNameTag(name string) UserTagOption {
return func() (key string, val interface{}) {
return "usr.name", name
// WithUserName returns the option setting the name of the authenticated user.
func WithUserName(name string) UserMonitoringOption {
return func(s Span) {
s.SetTag("usr.name", name)
}
}

// WithUserSessionIDTag emits the normalized key/value UserTagOption holding user session id information.
func WithUserSessionIDTag(sessionID string) UserTagOption {
return func() (key string, val interface{}) {
return "usr.session_id", sessionID
// WithUserSessionID returns the option setting the session ID of the authenticated user.
func WithUserSessionID(sessionID string) UserMonitoringOption {
return func(s Span) {
s.SetTag("usr.session_id", sessionID)
}
}

// WithUserRoleTag emits the normalized key/value UserTagOption holding user role information.
func WithUserRoleTag(role string) UserTagOption {
return func() (key string, val interface{}) {
return "usr.role", role
// WithUserRole returns the option setting the role of the authenticated user.
func WithUserRole(role string) UserMonitoringOption {
return func(s Span) {
s.SetTag("usr.role", role)
}
}

// WithUserScopeTag emits the normalized key/value UserTagOption holding user scope information.
func WithUserScopeTag(scope string) UserTagOption {
return func() (key string, val interface{}) {
return "usr.scope", scope
// WithUserScope returns the option setting the scope (authorizations) of the authenticated user
func WithUserScope(scope string) UserMonitoringOption {
return func(s Span) {
s.SetTag("usr.scope", scope)
}
}
23 changes: 11 additions & 12 deletions ddtrace/tracer/tracer.go
Expand Up @@ -153,21 +153,20 @@ func Inject(ctx ddtrace.SpanContext, carrier interface{}) error {
return internal.GetGlobalTracer().Inject(ctx, carrier)
}

// SetUser tags the root span of s with the provided user id as well as the
// optional tags provided as userTagOption parameters. This allows user identification
// across all spans of a trace.
func SetUser(s Span, id string, opts ...UserTagOption) {
// SetUser associates user information to the current trace which the
// provided span belongs to. The options can be used to tune which user
// bit of information gets monitored.
func SetUser(s Span, id string, opts ...UserMonitoringOption) {
if s == nil {
return
}
if span, ok := s.(*span); ok {
if span.context != nil {
span = span.context.trace.root
}
span.SetTag("usr.id", id)
for _, fn := range opts {
span.SetTag(fn())
}
if span, ok := s.(*span); ok && span.context != nil {
span = span.context.trace.root
s = span
}
s.SetTag("usr.id", id)
for _, fn := range opts {
fn(s)
}
}

Expand Down

0 comments on commit 2cacd2c

Please sign in to comment.