Skip to content

sci: add git metadata from artifacts support #1646

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 25 commits into from
Feb 1, 2023

Conversation

sashacmc
Copy link
Contributor

@sashacmc sashacmc commented Jan 4, 2023

What does this PR do?

Add three environment variables:

  1. DD_GIT_REPOSITORY_URL string, default: empty
  2. DD_GIT_COMMIT_SHA string, default: empty
  3. DD_TRACE_GIT_METADATA_ENABLED bool, default: true

Add following steps:

  1. Check if DD_TRACE_GIT_METADATA_ENABLED is True.
  2. Check if git metadata is already provided by environment variables:
  • Check DD_GIT_REPOSITORY_URL and DD_GIT_COMMIT_SHA, if they exist, use metadata from them.
  • Check DD_TAGS, if it contains git.repository_url and git.commit.sha, use metadata from them.
  1. Continue with metadata from artifacts.
    Extract git metadata from version information from binary by means of the debug/buildinfo package:
    • vcs.revision for commit hash
    • path for go module path

During trace telemetry sending:
Add metadata tags to the first span of each chunk:

  • _dd.git.repository_url for repository URL
  • _dd.git.commit.sha for commit hash
  • _dd.go_path for go module path

During profile telemetry sending:
Add metadata tags to each profile:

  • git.repository_url for repository URL
  • git.commit.sha to commit hash
  • go_path to go module path

Motivation

Allow customers easily provide git metadata

Describe how to test/QA your changes

  1. Build app from the cloned git repo with tracer/profiler with go version >= 1.18
    Check git.repository_url and git.commit.sha tags on traces/profiles DD pages
  2. Same as 1, but with DD_GIT_REPOSITORY_URL and DD_GIT_COMMIT_SHA with different values (values from env variables must present)
  3. Same as 1, but with DD_TAGS="git.repository_url:some_repo git.commit.sha:some_sha" (values from env variables must present)
  4. Same as 1, but with DD_TRACE_GIT_METADATA_ENABLED=false, tags must not be presented

Reviewer's Checklist

  • If known, an appropriate milestone has been selected; otherwise the Triage milestone is set.
  • Changed code has unit tests for its functionality.
  • If this interacts with the agent in a new way, a system test has been added.

Sorry, something went wrong.

@pr-commenter
Copy link

pr-commenter bot commented Jan 4, 2023

Benchmarks

Comparing candidate commit e4a44a1 in PR branch sashacmc/embed-build-artifacts with baseline commit 03c9bd3 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 6 cases.

@sashacmc sashacmc added this to the Triage milestone Jan 5, 2023
@sashacmc sashacmc changed the title Add git metadata from artifacts support sci: add git metadata from artifacts support Jan 6, 2023
@sashacmc sashacmc marked this pull request as ready for review January 6, 2023 13:41
@sashacmc sashacmc requested a review from a team as a code owner January 6, 2023 13:41
@sashacmc sashacmc requested a review from a team January 6, 2023 13:41
@sashacmc
Copy link
Contributor Author

sashacmc commented Jan 6, 2023

NB: Don't merge until APM Intake team deploy corresponded changes!

Copy link
Contributor

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll have a closer look at the profiler part soon, but I have a thought for a different way to work around needing to support Go 1.17.

@sashacmc sashacmc requested a review from a team as a code owner January 10, 2023 13:59
Copy link
Contributor

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I looked over this more for the profiling part. I like that you've de-duplicated the tag logic. There's one small change we should do for tags (see my comment) and then we should be good to go for the profiling part of this change :)

@sashacmc sashacmc force-pushed the sashacmc/embed-build-artifacts branch from 11adc10 to 26fe827 Compare January 10, 2023 23:05
Copy link
Member

@felixge felixge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some quick feedback. Didn't do a full review.

@sashacmc sashacmc modified the milestone: Triage Jan 11, 2023
@sashacmc sashacmc modified the milestone: Triage Jan 26, 2023
@sashacmc sashacmc requested review from nsrip-dd and felixge January 26, 2023 12:54
@sashacmc sashacmc modified the milestones: Triage, v1.47.0 Jan 26, 2023
nsrip-dd
nsrip-dd previously approved these changes Jan 30, 2023
Copy link
Contributor

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Approved, with one small question about how DD_TAGS is handled an an optional suggestion for making the test code a little shorter.


t.Run("git-metadata-from-dd-tags", func(t *testing.T) {
maininternal.ResetGitMetadataTags()
os.Setenv(maininternal.EnvDDTags, "git.commit.sha:123456789ABCD git.repository_url:github.com/user/repo go_path:somepath")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These os.Setenv calls followed by defer os.Unsetenv can be replaced by t.Setenv calls. This saves you some lines of code, and also handles corner cases better, like re-setting the env var to its previous value if it had one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dianashevchenko dianashevchenko modified the milestones: v1.47.0, v1.48.0 Jan 31, 2023
Copy link
Member

@felixge felixge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. A few nits and one warning, but that can be dealt with in a follow-up PR.

Comment on lines +115 to +122
res := make(map[string]string)
tags := GetGitMetadataTags()

updateTags(res, TraceTagRepositoryURL, tags[TagRepositoryURL])
updateTags(res, TraceTagCommitSha, tags[TagCommitSha])
updateTags(res, TraceTagGoPath, tags[TagGoPath])

return res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: This could be simplified.

Suggested change
res := make(map[string]string)
tags := GetGitMetadataTags()
updateTags(res, TraceTagRepositoryURL, tags[TagRepositoryURL])
updateTags(res, TraceTagCommitSha, tags[TagCommitSha])
updateTags(res, TraceTagGoPath, tags[TagGoPath])
return res
tags := GetGitMetadataTags()
return map[string]string{
TraceTagRepositoryURL: tags[TagRepositoryURL],
TraceTagCommitSha: tags[TagCommitSha],
TraceTagGoPath: tags[TagGoPath],
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but it will not work, updateTags has some checks (e.g. it will not add empty strings)

Comment on lines +44 to +54
func updateTags(tags map[string]string, key string, value string) {
if _, ok := tags[key]; !ok && value != "" {
tags[key] = value
}
}

func updateAllTags(tags map[string]string, newtags map[string]string) {
for k, v := range newtags {
updateTags(tags, k, v)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: This code doesn't really seem to benefit from being split into two functions. The naming of the methods implies that there is tag specific logic happening. But it's really just a simple map merging operation. Maybe a rename and putting it all into a single method would be better?

Suggested change
func updateTags(tags map[string]string, key string, value string) {
if _, ok := tags[key]; !ok && value != "" {
tags[key] = value
}
}
func updateAllTags(tags map[string]string, newtags map[string]string) {
for k, v := range newtags {
updateTags(tags, k, v)
}
}
func mergeNewKeys(dst map[string]string, src map[string]string) {
for k, v := range src {
if _, ok := dst[k]; !ok && v != "" {
dst[k] = v
}
}
}

It's also not clear to me why values from src are ignored if they are ""? Is there a real use case for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also not clear to me why values from src are ignored if they are ""? Is there a real use case for this?

It is for skip empty tags adding, they can appear e.g. because "" is default value for os.Getenv

updateAllTags(gitMetadataTags, getTagsFromBinary())
}

return gitMetadataTags
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARN: The returned map is mutable which is dangerous. The caller might decide to add their own keys or even overwrite keys by accident.

return res
}
res[TagCommitSha] = commitSha
res[TagGoPath] = goPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: No need for extra goPath var.

Suggested change
res[TagGoPath] = goPath
res[TagGoPath] = info.Path

@sashacmc sashacmc modified the milestone: v1.48.0 Feb 1, 2023
@dianashevchenko dianashevchenko merged commit 7f6f95d into main Feb 1, 2023
@dianashevchenko dianashevchenko deleted the sashacmc/embed-build-artifacts branch February 1, 2023 14:36
dianashevchenko pushed a commit that referenced this pull request Feb 9, 2023
[SC-1158] Add git metadata from artifacts support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants