Skip to content

Commit df4020c

Browse files
authored
Extract git tags from embedded git.properties and datadog_git.properties (#8561)
* Extract git tags from embedded git.properties and datadog_git.properties * Prioritize datadog_git.properties even in spring boot context * Add test for ordering * PR review: inline constants
1 parent 473081b commit df4020c

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import datadog.trace.api.config.UsmConfig;
3232
import datadog.trace.api.gateway.RequestContextSlot;
3333
import datadog.trace.api.gateway.SubscriptionService;
34+
import datadog.trace.api.git.EmbeddedGitInfoBuilder;
35+
import datadog.trace.api.git.GitInfoProvider;
3436
import datadog.trace.api.profiling.ProfilingEnablement;
3537
import datadog.trace.api.scopemanager.ScopeListener;
3638
import datadog.trace.bootstrap.benchmark.StaticEventLogger;
@@ -225,6 +227,12 @@ public static void start(
225227
}
226228
}
227229

230+
// Enable automatic fetching of git tags from datadog_git.properties only if CI Visibility is
231+
// not enabled
232+
if (!ciVisibilityEnabled) {
233+
GitInfoProvider.INSTANCE.registerGitInfoBuilder(new EmbeddedGitInfoBuilder());
234+
}
235+
228236
boolean dataJobsEnabled = isFeatureEnabled(AgentFeature.DATA_JOBS);
229237
if (dataJobsEnabled) {
230238
log.info("Data Jobs Monitoring enabled, enabling spark integrations");

internal-api/src/main/java/datadog/trace/api/git/EmbeddedGitInfoBuilder.java

+30-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.util.Arrays;
6+
import java.util.List;
57
import java.util.Properties;
68
import javax.annotation.Nullable;
79
import org.slf4j.Logger;
@@ -11,29 +13,44 @@ public class EmbeddedGitInfoBuilder implements GitInfoBuilder {
1113

1214
private static final Logger log = LoggerFactory.getLogger(EmbeddedGitInfoBuilder.class);
1315

14-
private static final String EMBEDDED_GIT_PROPERTIES_FILE_NAME = "git.properties";
15-
16-
private final String resourceName;
16+
private final List<String> resourceNames;
1717

1818
public EmbeddedGitInfoBuilder() {
19-
this(EMBEDDED_GIT_PROPERTIES_FILE_NAME);
19+
// Order is important here, from the most reliable sources to the least reliable ones
20+
this(
21+
Arrays.asList(
22+
// Spring boot fat jars and wars should have the git.properties file in the following
23+
// specific paths, guaranteeing that it's not coming from a dependency
24+
"BOOT-INF/classes/datadog_git.properties",
25+
"BOOT-INF/classes/git.properties",
26+
"WEB-INF/classes/datadog_git.properties",
27+
"WEB-INF/classes/git.properties",
28+
// If we can't find the files above, probably because we're not in a spring context, we
29+
// can look at the root of the classpath. Since it could be tainted by dependencies,
30+
// we're looking for a specific datadog_git.properties file.
31+
"datadog_git.properties"));
2032
}
2133

22-
EmbeddedGitInfoBuilder(String resourceName) {
23-
this.resourceName = resourceName;
34+
EmbeddedGitInfoBuilder(List<String> resourceNames) {
35+
this.resourceNames = resourceNames;
2436
}
2537

2638
@Override
2739
public GitInfo build(@Nullable String repositoryPath) {
2840
Properties gitProperties = new Properties();
29-
try (InputStream is = ClassLoader.getSystemResourceAsStream(resourceName)) {
30-
if (is != null) {
31-
gitProperties.load(is);
32-
} else {
33-
log.debug("Could not find embedded Git properties resource: {}", resourceName);
41+
42+
for (String resourceName : resourceNames) {
43+
try (InputStream is = ClassLoader.getSystemResourceAsStream(resourceName)) {
44+
if (is != null) {
45+
gitProperties.load(is);
46+
// stop at the first git properties file found
47+
break;
48+
} else {
49+
log.debug("Could not find embedded Git properties resource: {}", resourceName);
50+
}
51+
} catch (IOException e) {
52+
log.error("Error reading embedded Git properties from {}", resourceName, e);
3453
}
35-
} catch (IOException e) {
36-
log.error("Error reading embedded Git properties from {}", resourceName, e);
3754
}
3855

3956
String commitSha = gitProperties.getProperty("git.commit.id");

internal-api/src/test/groovy/datadog/trace/api/git/EmbeddedGitInfoBuilderTest.groovy

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class EmbeddedGitInfoBuilderTest extends Specification {
77

88
def "test no embedded git info"() {
99
when:
10-
def gitInfo = new EmbeddedGitInfoBuilder("non-existent-git.properties").build(null)
10+
def gitInfo = new EmbeddedGitInfoBuilder(["non-existent-git.properties"]).build(null)
1111

1212
then:
1313
gitInfo.isEmpty()
@@ -16,7 +16,7 @@ class EmbeddedGitInfoBuilderTest extends Specification {
1616
def "test maven-plugin-generated git info"() {
1717
when:
1818
def mavenGitProperties = "datadog/trace/bootstrap/git/maven-git.properties"
19-
def gitInfo = new EmbeddedGitInfoBuilder(mavenGitProperties).build(null)
19+
def gitInfo = new EmbeddedGitInfoBuilder([mavenGitProperties]).build(null)
2020

2121
then:
2222
gitInfo.repositoryURL == "[email protected]:DataDog/ciapp-test-resources.git"
@@ -35,7 +35,7 @@ class EmbeddedGitInfoBuilderTest extends Specification {
3535
def "test gradle-plugin-generated git info"() {
3636
when:
3737
def gradleGitProperties = "datadog/trace/bootstrap/git/gradle-git.properties"
38-
def gitInfo = new EmbeddedGitInfoBuilder(gradleGitProperties).build(null)
38+
def gitInfo = new EmbeddedGitInfoBuilder([gradleGitProperties]).build(null)
3939

4040
then:
4141
gitInfo.repositoryURL == "[email protected]:DataDog/ciapp-test-resources.git"
@@ -50,4 +50,13 @@ class EmbeddedGitInfoBuilderTest extends Specification {
5050
gitInfo.commit.committer.email == "[email protected]"
5151
gitInfo.commit.committer.iso8601Date == "2023-03-22T14:43:21+0100"
5252
}
53+
54+
def "test embedded gitinfo has a lower priority than user supplied gitinfo"() {
55+
when:
56+
def embeddedGitInfoBuilder = new EmbeddedGitInfoBuilder()
57+
def userSuppliedGitInfoBuilder = new UserSuppliedGitInfoBuilder()
58+
59+
then:
60+
embeddedGitInfoBuilder.order() > userSuppliedGitInfoBuilder.order()
61+
}
5362
}

0 commit comments

Comments
 (0)