Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import datadog.trace.api.config.UsmConfig;
import datadog.trace.api.gateway.RequestContextSlot;
import datadog.trace.api.gateway.SubscriptionService;
import datadog.trace.api.git.EmbeddedGitInfoBuilder;
import datadog.trace.api.git.GitInfoProvider;
import datadog.trace.api.profiling.ProfilingEnablement;
import datadog.trace.api.scopemanager.ScopeListener;
import datadog.trace.bootstrap.benchmark.StaticEventLogger;
Expand Down Expand Up @@ -225,6 +227,12 @@ public static void start(
}
}

// Enable automatic fetching of git tags from datadog_git.properties only if CI Visibility is
// not enabled
if (!ciVisibilityEnabled) {
GitInfoProvider.INSTANCE.registerGitInfoBuilder(new EmbeddedGitInfoBuilder());
}

boolean dataJobsEnabled = isFeatureEnabled(AgentFeature.DATA_JOBS);
if (dataJobsEnabled) {
log.info("Data Jobs Monitoring enabled, enabling spark integrations");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.annotation.Nullable;
import org.slf4j.Logger;
Expand All @@ -11,29 +13,44 @@ public class EmbeddedGitInfoBuilder implements GitInfoBuilder {

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

private static final String EMBEDDED_GIT_PROPERTIES_FILE_NAME = "git.properties";

private final String resourceName;
private final List<String> resourceNames;

public EmbeddedGitInfoBuilder() {
this(EMBEDDED_GIT_PROPERTIES_FILE_NAME);
// Order is important here, from the most reliable sources to the least reliable ones
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if the constants were inline right here, along with useful comments and ordering, it would be easier to read and maintain. WDYT?

Copy link
Contributor Author

@wmouchere wmouchere Mar 27, 2025

Choose a reason for hiding this comment

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

I agree 👍

this(
Arrays.asList(
// Spring boot fat jars and wars should have the git.properties file in the following
// specific paths, guaranteeing that it's not coming from a dependency
"BOOT-INF/classes/datadog_git.properties",
"BOOT-INF/classes/git.properties",
"WEB-INF/classes/datadog_git.properties",
"WEB-INF/classes/git.properties",
// If we can't find the files above, probably because we're not in a spring context, we
// can look at the root of the classpath. Since it could be tainted by dependencies,
// we're looking for a specific datadog_git.properties file.
"datadog_git.properties"));
}

EmbeddedGitInfoBuilder(String resourceName) {
this.resourceName = resourceName;
EmbeddedGitInfoBuilder(List<String> resourceNames) {
this.resourceNames = resourceNames;
}

@Override
public GitInfo build(@Nullable String repositoryPath) {
Properties gitProperties = new Properties();
try (InputStream is = ClassLoader.getSystemResourceAsStream(resourceName)) {
if (is != null) {
gitProperties.load(is);
} else {
log.debug("Could not find embedded Git properties resource: {}", resourceName);

for (String resourceName : resourceNames) {
try (InputStream is = ClassLoader.getSystemResourceAsStream(resourceName)) {
if (is != null) {
gitProperties.load(is);
// stop at the first git properties file found
break;
} else {
log.debug("Could not find embedded Git properties resource: {}", resourceName);
}
} catch (IOException e) {
log.error("Error reading embedded Git properties from {}", resourceName, e);
}
} catch (IOException e) {
log.error("Error reading embedded Git properties from {}", resourceName, e);
}

String commitSha = gitProperties.getProperty("git.commit.id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EmbeddedGitInfoBuilderTest extends Specification {

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

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

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

then:
gitInfo.repositoryURL == "[email protected]:DataDog/ciapp-test-resources.git"
Expand All @@ -50,4 +50,13 @@ class EmbeddedGitInfoBuilderTest extends Specification {
gitInfo.commit.committer.email == "[email protected]"
gitInfo.commit.committer.iso8601Date == "2023-03-22T14:43:21+0100"
}

def "test embedded gitinfo has a lower priority than user supplied gitinfo"() {
when:
def embeddedGitInfoBuilder = new EmbeddedGitInfoBuilder()
def userSuppliedGitInfoBuilder = new UserSuppliedGitInfoBuilder()

then:
embeddedGitInfoBuilder.order() > userSuppliedGitInfoBuilder.order()
}
}