Skip to content

Commit 32dbe19

Browse files
committed
Extract git tags from embedded git.properties and datadog_git.properties
1 parent 1dff23c commit 32dbe19

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import datadog.trace.api.config.UsmConfig;
3131
import datadog.trace.api.gateway.RequestContextSlot;
3232
import datadog.trace.api.gateway.SubscriptionService;
33+
import datadog.trace.api.git.EmbeddedGitInfoBuilder;
34+
import datadog.trace.api.git.GitInfoProvider;
3335
import datadog.trace.api.profiling.ProfilingEnablement;
3436
import datadog.trace.api.scopemanager.ScopeListener;
3537
import datadog.trace.bootstrap.benchmark.StaticEventLogger;
@@ -223,6 +225,12 @@ public static void start(
223225
}
224226
}
225227

228+
// Enable automatic fetching of git tags from datadog_git.properties only if CI Visibility is
229+
// not enabled
230+
if (!ciVisibilityEnabled) {
231+
GitInfoProvider.INSTANCE.registerGitInfoBuilder(new EmbeddedGitInfoBuilder());
232+
}
233+
226234
boolean dataJobsEnabled = isFeatureEnabled(AgentFeature.DATA_JOBS);
227235
if (dataJobsEnabled) {
228236
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");

0 commit comments

Comments
 (0)