This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add ability to set Global tags using code and environment variables #30
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23259d3
Add ability to set Global tags using code and environment variables
ccaraman c23a7ee
Pull in Owais' circle ci changes
ccaraman 880e66d
Forgot a file
ccaraman 9d614bd
Address PR comments
ccaraman 1c4da66
Merge branch 'master' into add_global_span_tags
ccaraman 618a9d9
Missed merge remnant
ccaraman 6d1efa0
Out of practice merging code bases
ccaraman 8f7bdc4
It wasn't just a nit
ccaraman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,14 @@ import ( | |
"github.com/signalfx/signalfx-go-tracing/ddtrace/opentracer" | ||
"github.com/signalfx/signalfx-go-tracing/ddtrace/tracer" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
signalfxServiceName = "SIGNALFX_SERVICE_NAME" | ||
signalfxEndpointURL = "SIGNALFX_ENDPOINT_URL" | ||
signalfxAccessToken = "SIGNALFX_ACCESS_TOKEN" | ||
signalfxSpanTags = "SIGNALFX_SPAN_TAGS" | ||
) | ||
|
||
var defaults = map[string]string{ | ||
|
@@ -23,6 +25,10 @@ type config struct { | |
serviceName string | ||
accessToken string | ||
url string | ||
// Because there can be multiple global tags added via environment variable | ||
// or calls to WithGlobalTag, store them in the required StartOption format to | ||
// call tracer.Start() in the variadic format. | ||
globalTags []tracer.StartOption | ||
} | ||
|
||
// StartOption is a function that configures an option for Start | ||
|
@@ -33,9 +39,41 @@ func defaultConfig() *config { | |
serviceName: envOrDefault(signalfxServiceName), | ||
accessToken: envOrDefault(signalfxAccessToken), | ||
url: envOrDefault(signalfxEndpointURL), | ||
globalTags: envGlobalTags(), | ||
} | ||
} | ||
|
||
// envGlobalTags extract global tags from the environment variable and parses the value in the expected format | ||
// key1:value1, | ||
func envGlobalTags() []tracer.StartOption { | ||
var globalTags []tracer.StartOption | ||
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. This would be a good opportunity to tag the library and version by default: 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'll do that in a separate PR. |
||
var val string | ||
|
||
if val = os.Getenv(signalfxSpanTags); val == "" { | ||
return globalTags | ||
} | ||
|
||
tags := strings.Split(val, ",") | ||
for _, tag := range tags { | ||
// TODO: Currently this assumes "<stringb>" where "<stringa>:<stringb>" has no ":" in the | ||
// string. The TODO is to fix this logic to allow for "<stringb> to have colons, ":', in it. | ||
pair :=strings.Split(tag, ":") | ||
if len(pair) == 2 { | ||
key := strings.TrimSpace(pair[0]) | ||
value := strings.TrimSpace(pair[1]) | ||
// Empty keys aren't valid in Zipkin. | ||
// https://github.com/openzipkin/zipkin-api/blob/d3324ac79d1aa8f5c6f0ea4febb299402e50480f/zipkin-jsonv2.proto#L50-L51 | ||
if key == "" { | ||
continue | ||
} | ||
globalTag := tracer.WithGlobalTag(key, value) | ||
globalTags = append(globalTags, globalTag) | ||
} | ||
} | ||
|
||
return globalTags | ||
} | ||
|
||
// envOrDefault gets the given environment variable if set otherwise a default value. | ||
func envOrDefault(envVar string) string { | ||
if val := os.Getenv(envVar); val != "" { | ||
|
@@ -65,16 +103,30 @@ func WithEndpointURL(url string) StartOption { | |
} | ||
} | ||
|
||
// WithGlobalTag sets a tag with the given key/value on all spans created by the | ||
// tracer. This option may be used multiple times. | ||
// Note: Since the underlying transport is Zipkin, only values with strings | ||
// are accepted. | ||
func WithGlobalTag(k string, v string) StartOption { | ||
return func(c *config) { | ||
globalTag := tracer.WithGlobalTag(k, v) | ||
c.globalTags = append(c.globalTags, globalTag) | ||
} | ||
} | ||
|
||
// Start tracing globally | ||
func Start(opts ...StartOption) { | ||
c := defaultConfig() | ||
for _, fn := range opts { | ||
fn(c) | ||
} | ||
|
||
startOptions := append(c.globalTags, tracer.WithServiceName(c.serviceName)) | ||
startOptions = append(startOptions, tracer.WithZipkin(c.serviceName, c.url, c.accessToken)) | ||
tracer.Start( | ||
tracer.WithServiceName(c.serviceName), | ||
tracer.WithZipkin(c.serviceName, c.url, c.accessToken)) | ||
startOptions... | ||
pjanotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
opentracing.SetGlobalTracer(opentracer.New()) | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If this is for
SIGNALFX_SPAN_TAGS
, let's move it to the note for that config value.