-
Notifications
You must be signed in to change notification settings - Fork 466
feat: add process tags to tracing payloads #3566
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0cd7031
feat: add process tags to tracing payloads
rarguelloF 451947a
use const for tag names
rarguelloF 6948d35
address comments + sort tags before serializing to string
rarguelloF 042661d
change correct env variable name
rarguelloF d057794
add entrypoint.type tag
rarguelloF 9c8a429
Merge branch 'main' into rarguelloF/process-tags-tracing
darccio 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2025 Datadog, Inc. | ||
|
||
package processtags | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/trace/traceutil" | ||
|
||
"github.com/DataDog/dd-trace-go/v2/internal" | ||
"github.com/DataDog/dd-trace-go/v2/internal/log" | ||
) | ||
|
||
const envProcessTagsEnabled = "DD_EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED" | ||
|
||
const ( | ||
tagEntrypointName = "entrypoint.name" | ||
tagEntrypointBasedir = "entrypoint.basedir" | ||
tagEntrypointWorkdir = "entrypoint.workdir" | ||
tagEntrypointType = "entrypoint.type" | ||
) | ||
|
||
const ( | ||
entrypointTypeExecutable = "executable" | ||
) | ||
|
||
var ( | ||
enabled bool | ||
pTags *ProcessTags | ||
) | ||
|
||
func init() { | ||
Reload() | ||
} | ||
|
||
type ProcessTags struct { | ||
mu sync.RWMutex | ||
tags map[string]string | ||
str string | ||
slice []string | ||
} | ||
|
||
// String returns the string representation of the process tags. | ||
func (p *ProcessTags) String() string { | ||
if p == nil { | ||
return "" | ||
} | ||
p.mu.RLock() | ||
defer p.mu.RUnlock() | ||
return p.str | ||
} | ||
|
||
// Slice returns the string slice representation of the process tags. | ||
func (p *ProcessTags) Slice() []string { | ||
if p == nil { | ||
return nil | ||
} | ||
p.mu.RLock() | ||
defer p.mu.RUnlock() | ||
return p.slice | ||
} | ||
|
||
func (p *ProcessTags) merge(newTags map[string]string) { | ||
if len(newTags) == 0 { | ||
return | ||
} | ||
pTags.mu.Lock() | ||
defer pTags.mu.Unlock() | ||
|
||
if p.tags == nil { | ||
p.tags = make(map[string]string) | ||
} | ||
for k, v := range newTags { | ||
p.tags[k] = v | ||
} | ||
|
||
// loop over the sorted map keys so the resulting string and slice versions are created consistently. | ||
keys := make([]string, 0, len(p.tags)) | ||
for k := range p.tags { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
|
||
tagsSlice := make([]string, 0, len(p.tags)) | ||
var b strings.Builder | ||
first := true | ||
for _, k := range keys { | ||
val := p.tags[k] | ||
if !first { | ||
b.WriteByte(',') | ||
} | ||
first = false | ||
keyVal := traceutil.NormalizeTag(k + ":" + val) | ||
b.WriteString(keyVal) | ||
tagsSlice = append(tagsSlice, keyVal) | ||
} | ||
p.slice = tagsSlice | ||
darccio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
p.str = b.String() | ||
} | ||
|
||
// Reload initializes the configuration and process tags collection. This is useful for tests. | ||
func Reload() { | ||
enabled = internal.BoolEnv(envProcessTagsEnabled, false) | ||
if !enabled { | ||
return | ||
} | ||
pTags = &ProcessTags{} | ||
tags := collect() | ||
if len(tags) > 0 { | ||
Add(tags) | ||
} | ||
} | ||
|
||
func collect() map[string]string { | ||
tags := make(map[string]string) | ||
execPath, err := os.Executable() | ||
if err != nil { | ||
log.Debug("failed to get binary path: %v", err) | ||
} else { | ||
baseDirName := filepath.Base(filepath.Dir(execPath)) | ||
tags[tagEntrypointName] = filepath.Base(execPath) | ||
tags[tagEntrypointBasedir] = baseDirName | ||
tags[tagEntrypointType] = entrypointTypeExecutable | ||
} | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
log.Debug("failed to get working directory: %v", err) | ||
} else { | ||
tags[tagEntrypointWorkdir] = filepath.Base(wd) | ||
} | ||
return tags | ||
} | ||
|
||
// GlobalTags returns the global process tags. | ||
func GlobalTags() *ProcessTags { | ||
if !enabled { | ||
return nil | ||
} | ||
return pTags | ||
} | ||
|
||
// Add merges the given tags into the global processTags map. | ||
func Add(tags map[string]string) { | ||
if !enabled { | ||
return | ||
} | ||
pTags.merge(tags) | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2025 Datadog, Inc. | ||
|
||
package processtags | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestProcessTags(t *testing.T) { | ||
t.Run("enabled", func(t *testing.T) { | ||
t.Setenv("DD_EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED", "true") | ||
Reload() | ||
|
||
wantTagsRe := regexp.MustCompile(`^entrypoint\.basedir:[a-zA-Z0-9._-]+,entrypoint\.name:[a-zA-Z0-9._-]+,entrypoint.type:executable,entrypoint\.workdir:[a-zA-Z0-9._-]+$`) | ||
p := GlobalTags() | ||
assert.NotNil(t, p) | ||
assert.NotEmpty(t, p.String()) | ||
assert.Regexp(t, wantTagsRe, p.String(), "wrong string serialized tags") | ||
|
||
assert.NotEmpty(t, p.Slice()) | ||
assert.Regexp(t, wantTagsRe, strings.Join(p.Slice(), ","), "wrong slice serialized tags") | ||
}) | ||
|
||
t.Run("disabled", func(t *testing.T) { | ||
t.Setenv("DD_EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED", "false") | ||
Reload() | ||
|
||
p := GlobalTags() | ||
assert.Nil(t, p) | ||
assert.Empty(t, p.String()) | ||
assert.Empty(t, p.Slice()) | ||
}) | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.