Skip to content

Commit 22b45f2

Browse files
committed
Extract git tags from embedded git.properties and datadog_git.properties
1 parent 5fb00dc commit 22b45f2

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
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;
@@ -224,6 +226,12 @@ public static void start(
224226
}
225227
}
226228

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

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

+40-12
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,55 @@ 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";
16+
// Spring boot fat jars and wars should have the git.properties file in a specific path,
17+
// guaranteeing that it's not coming from a dependency
18+
private static final String SPRING_BOOT_JAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME =
19+
"BOOT-INF/classes/git.properties";
20+
private static final String SPRING_BOOT_JAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME =
21+
"BOOT-INF/classes/datadog_git.properties";
22+
private static final String WAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME =
23+
"WEB-INF/classes/git.properties";
24+
private static final String WAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME =
25+
"WEB-INF/classes/datadog_git.properties";
26+
// If we can't find the files above, probably because we're not in a spring context, we can look
27+
// at the root of the classpath.
28+
// Since it could be tainted by dependencies, we're looking for a specific datadog_git.properties
29+
// file.
30+
private static final String EMBEDDED_DATADOOG_GIT_PROPERTIES_FILE_NAME = "datadog_git.properties";
1531

16-
private final String resourceName;
32+
private final List<String> resourceNames;
1733

1834
public EmbeddedGitInfoBuilder() {
19-
this(EMBEDDED_GIT_PROPERTIES_FILE_NAME);
35+
// Order is important here, from the most reliable sources to the least reliable ones
36+
this(
37+
Arrays.asList(
38+
SPRING_BOOT_JAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME,
39+
SPRING_BOOT_JAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME,
40+
WAR_EMBEDDED_GIT_PROPERTIES_FILE_NAME,
41+
WAR_EMBEDDED_DATADOG_GIT_PROPERTIES_FILE_NAME,
42+
EMBEDDED_DATADOOG_GIT_PROPERTIES_FILE_NAME));
2043
}
2144

22-
EmbeddedGitInfoBuilder(String resourceName) {
23-
this.resourceName = resourceName;
45+
EmbeddedGitInfoBuilder(List<String> resourceNames) {
46+
this.resourceNames = resourceNames;
2447
}
2548

2649
@Override
2750
public GitInfo build(@Nullable String repositoryPath) {
2851
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);
52+
53+
for (String resourceName : resourceNames) {
54+
try (InputStream is = ClassLoader.getSystemResourceAsStream(resourceName)) {
55+
if (is != null) {
56+
gitProperties.load(is);
57+
// stop at the first git properties file found
58+
break;
59+
} else {
60+
log.debug("Could not find embedded Git properties resource: {}", resourceName);
61+
}
62+
} catch (IOException e) {
63+
log.error("Error reading embedded Git properties from {}", resourceName, e);
3464
}
35-
} catch (IOException e) {
36-
log.error("Error reading embedded Git properties from {}", resourceName, e);
3765
}
3866

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

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

+3-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"

0 commit comments

Comments
 (0)