From 277187006bc7853240473207522e52f42417ad79 Mon Sep 17 00:00:00 2001 From: jean-philippe bempel Date: Mon, 13 Nov 2023 10:25:31 +0100 Subject: [PATCH 1/6] Fix span line probe smoke test --- .../java/datadog/smoketest/SpanProbesIntegrationTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dd-smoke-tests/debugger-integration-tests/src/test/java/datadog/smoketest/SpanProbesIntegrationTest.java b/dd-smoke-tests/debugger-integration-tests/src/test/java/datadog/smoketest/SpanProbesIntegrationTest.java index 16d8874dc74..8a63c6c1ea3 100644 --- a/dd-smoke-tests/debugger-integration-tests/src/test/java/datadog/smoketest/SpanProbesIntegrationTest.java +++ b/dd-smoke-tests/debugger-integration-tests/src/test/java/datadog/smoketest/SpanProbesIntegrationTest.java @@ -58,7 +58,12 @@ void testLineRangeSpan() throws Exception { void testSingleLineSpan() throws Exception { final String METHOD_NAME = "fullMethod"; final String EXPECTED_UPLOADS = "2"; // 2 probe statuses: RECEIVED + ERROR - SpanProbe spanProbe = SpanProbe.builder().probeId(PROBE_ID).where(MAIN_CLASS_NAME, 80).build(); + SpanProbe spanProbe = + SpanProbe.builder() + .probeId(PROBE_ID) + // on line: System.out.println("fullMethod"); + .where(MAIN_CLASS_NAME, 88) + .build(); setCurrentConfiguration(createSpanConfig(spanProbe)); targetProcess = createProcessBuilder(logFilePath, METHOD_NAME, EXPECTED_UPLOADS).start(); AtomicBoolean received = new AtomicBoolean(false); From 17734ae5fa179e2a62f76d51baef8daf8cda1824 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Mon, 13 Nov 2023 11:45:30 +0100 Subject: [PATCH 2/6] Update system-tests to 6998b307ce0e8d206ca6eb99045651dcc8bc853f (#6199) --- .circleci/config.continue.yml.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.continue.yml.j2 b/.circleci/config.continue.yml.j2 index 6009f29fdbf..5d1486aa3c6 100644 --- a/.circleci/config.continue.yml.j2 +++ b/.circleci/config.continue.yml.j2 @@ -36,7 +36,7 @@ instrumentation_modules: &instrumentation_modules "dd-java-agent/instrumentation debugger_modules: &debugger_modules "dd-java-agent/agent-debugger|dd-java-agent/agent-bootstrap|dd-java-agent/agent-builder|internal-api|communication|dd-trace-core" profiling_modules: &profiling_modules "dd-java-agent/agent-profiling" -default_system_tests_commit: &default_system_tests_commit 2487cea5160a398549743d2cfd927a863792e3bd +default_system_tests_commit: &default_system_tests_commit 6998b307ce0e8d206ca6eb99045651dcc8bc853f parameters: nightly: From 6382bb4601302d3500dbb60093820a2af7d356ba Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko <121111529+nikita-tkachenko-datadog@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:55:23 +0100 Subject: [PATCH 3/6] Fix error when trying to get class stream for Mockito mocks (#6183) --- .../agent-ci-visibility/build.gradle | 1 + .../trace/civisibility/source/Utils.java | 23 +++++++++++++++++-- .../civisibility/source/index/RepoIndex.java | 15 ++++-------- .../RepoIndexSourcePathResolverTest.groovy | 12 ++++++++++ 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/dd-java-agent/agent-ci-visibility/build.gradle b/dd-java-agent/agent-ci-visibility/build.gradle index 9917f17cbdb..d3d0ad668cb 100644 --- a/dd-java-agent/agent-ci-visibility/build.gradle +++ b/dd-java-agent/agent-ci-visibility/build.gradle @@ -73,6 +73,7 @@ excludedClassesCoverage += [ "datadog.trace.civisibility.source.index.RepoIndexBuilder.RepoIndexingFileVisitor", "datadog.trace.civisibility.source.index.RepoIndexFetcher", "datadog.trace.civisibility.source.index.RepoIndexSourcePathResolver", + "datadog.trace.civisibility.source.Utils", "datadog.trace.civisibility.utils.ShellCommandExecutor", "datadog.trace.civisibility.utils.ShellCommandExecutor.OutputParser", "datadog.trace.civisibility.utils.SpanUtils" diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/Utils.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/Utils.java index f9023ab5b60..8c826776656 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/Utils.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/Utils.java @@ -7,7 +7,26 @@ public abstract class Utils { public static InputStream getClassStream(Class clazz) throws IOException { String className = clazz.getName(); - String classPath = "/" + className.replace('.', '/') + ".class"; - return clazz.getResourceAsStream(classPath); + InputStream classStream = clazz.getResourceAsStream(toResourceName(className)); + if (classStream != null) { + return classStream; + } else { + // might be auto-generated inner class (e.g. Mockito mock) + String topLevelClassName = stripNestedClassNames(clazz.getName()); + return clazz.getResourceAsStream(toResourceName(topLevelClassName)); + } + } + + private static String toResourceName(String className) { + return "/" + className.replace('.', '/') + ".class"; + } + + public static String stripNestedClassNames(String className) { + int innerClassNameIdx = className.indexOf('$'); + if (innerClassNameIdx >= 0) { + return className.substring(0, innerClassNameIdx); + } else { + return className; + } } } diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index/RepoIndex.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index/RepoIndex.java index d8010aeb072..54537b18810 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index/RepoIndex.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index/RepoIndex.java @@ -50,7 +50,7 @@ public List getRootPackages() { @Nullable public String getSourcePath(@Nonnull Class c) { - String topLevelClassName = stripNestedClassNames(c.getName()); + String topLevelClassName = Utils.stripNestedClassNames(c.getName()); SourceType sourceType = detectSourceType(c); String extension = sourceType.getExtension(); String classNameWithExtension = topLevelClassName + extension; @@ -107,15 +107,6 @@ private SourceType detectSourceType(Class c) { return SourceType.JAVA; } - private String stripNestedClassNames(String className) { - int innerClassNameIdx = className.indexOf('$'); - if (innerClassNameIdx >= 0) { - return className.substring(0, innerClassNameIdx); - } else { - return className; - } - } - /** * Names of package-private classes do not have to correspond to the names of their source code * files. For such classes filename is extracted from SourceFile attribute that is available in @@ -126,6 +117,10 @@ private String getSourcePathForPackagePrivateOrNonJavaClass(Class c) { SourceFileAttributeVisitor sourceFileAttributeVisitor = new SourceFileAttributeVisitor(); try (InputStream classStream = Utils.getClassStream(c)) { + if (classStream == null) { + log.debug("Could not get input stream for class {}", c.getName()); + return null; + } ClassReader classReader = new ClassReader(classStream); classReader.accept( sourceFileAttributeVisitor, ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES); diff --git a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/source/index/RepoIndexSourcePathResolverTest.groovy b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/source/index/RepoIndexSourcePathResolverTest.groovy index 59a29662254..57b6b0ad229 100644 --- a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/source/index/RepoIndexSourcePathResolverTest.groovy +++ b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/source/index/RepoIndexSourcePathResolverTest.groovy @@ -72,6 +72,17 @@ class RepoIndexSourcePathResolverTest extends Specification { sourcePathResolver.getSourcePath(PackagePrivateClass) == expectedSourcePath } + def "test source path resolution for class nested into package-private class"() { + setup: + def expectedSourcePath = givenSourceFile(RepoIndexSourcePathResolverTest, repoRoot + "/src") + + when: + def sourcePathResolver = new RepoIndexSourcePathResolver(repoRoot, packageResolver, fileSystem) + + then: + sourcePathResolver.getSourcePath(PackagePrivateClass.NestedIntoPackagePrivateClass) == expectedSourcePath + } + def "test source path resolution for non-java class whose file name is different from class name"() { setup: def expectedSourcePath = givenSourceFile(RepoIndexSourcePathResolverTest, repoRoot + "/src") @@ -173,6 +184,7 @@ class RepoIndexSourcePathResolverTest extends Specification { @PackageScope class PackagePrivateClass { + class NestedIntoPackagePrivateClass {} } class PublicClassWhoseNameDoesNotCorrespondToFileName { From a37fb7ec2977ae0a154e985f326000140e672121 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko <121111529+nikita-tkachenko-datadog@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:55:58 +0100 Subject: [PATCH 4/6] Fix error when serializing skippable tests whose names contain non-ASCII characters (#6182) --- .../config/SkippableTestsSerializer.java | 22 +++++++++++-------- .../SkippableTestsSerializerTest.groovy | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-) rename dd-java-agent/agent-ci-visibility/src/{main/java => test/groovy}/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy (90%) diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializer.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializer.java index 3250f3a6389..e01e56785c9 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializer.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializer.java @@ -26,10 +26,10 @@ public static ByteBuffer serialize(Collection skippableTests) { String name = test.getName(); String parameters = test.getParameters(); - length += suite.length(); - length += name.length(); + length += suite.getBytes(CHARSET).length; + length += name.getBytes(CHARSET).length; if (parameters != null) { - length += parameters.length(); + length += parameters.getBytes(CHARSET).length; } } @@ -41,14 +41,18 @@ public static ByteBuffer serialize(Collection skippableTests) { String name = test.getName(); String parameters = test.getParameters(); - buffer.putInt(suite.length()); - buffer.put(suite.getBytes(CHARSET)); - buffer.putInt(name.length()); - buffer.put(name.getBytes(CHARSET)); + byte[] suiteBytes = suite.getBytes(CHARSET); + buffer.putInt(suiteBytes.length); + buffer.put(suiteBytes); + + byte[] nameBytes = name.getBytes(CHARSET); + buffer.putInt(nameBytes.length); + buffer.put(nameBytes); if (parameters != null) { - buffer.putInt(parameters.length()); - buffer.put(parameters.getBytes(CHARSET)); + byte[] parametersBytes = parameters.getBytes(CHARSET); + buffer.putInt(parametersBytes.length); + buffer.put(parametersBytes); } else { buffer.putInt(-1); } diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy similarity index 90% rename from dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy rename to dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy index db382fc92a1..76494f2c43b 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy +++ b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/SkippableTestsSerializerTest.groovy @@ -22,6 +22,8 @@ class SkippableTestsSerializerTest extends Specification { tests << [ // single test [["suite", "name", null]], + [["suite", "𝕄 add user properties 𝕎 addUserProperties()", null]], + // non-ASCII characters [["suite", "name", "parameters"]], [["suite", "name", "{\"metadata\":{\"test_name\":\"test display name with #a #b #c\"}}"]], // multiple tests From 3fb888b81b6546784736580fc772c598297869d7 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko <121111529+nikita-tkachenko-datadog@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:56:12 +0100 Subject: [PATCH 5/6] Fail fast when tracer versions do not match in parent and child processes (#6168) --- .../trace/civisibility/CiVisibilitySystem.java | 13 +++++++++++++ .../config/ModuleExecutionSettingsFactoryImpl.java | 6 ++++++ .../trace/api/config/CiVisibilityConfig.java | 2 ++ .../src/main/java/datadog/trace/api/Config.java | 8 ++++++++ 4 files changed, 29 insertions(+) diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java index 1c3000726fa..4f4a29255b6 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java @@ -1,6 +1,7 @@ package datadog.trace.civisibility; import datadog.communication.ddagent.SharedCommunicationObjects; +import datadog.communication.ddagent.TracerVersion; import datadog.trace.api.Config; import datadog.trace.api.civisibility.CIVisibility; import datadog.trace.api.civisibility.InstrumentationBridge; @@ -52,6 +53,7 @@ import datadog.trace.civisibility.source.index.RepoIndexProvider; import datadog.trace.civisibility.source.index.RepoIndexSourcePathResolver; import datadog.trace.util.Strings; +import datadog.trace.util.throwable.FatalAgentMisconfigurationError; import java.net.InetSocketAddress; import java.nio.file.FileSystems; import java.nio.file.Path; @@ -74,6 +76,17 @@ public static void start(SharedCommunicationObjects sco) { return; } + String injectedTracerVersion = config.getCiVisibilityInjectedTracerVersion(); + if (injectedTracerVersion != null + && !injectedTracerVersion.equals(TracerVersion.TRACER_VERSION)) { + throw new FatalAgentMisconfigurationError( + "Running JVM with tracer version " + + TracerVersion.TRACER_VERSION + + " however parent process attempted to inject " + + injectedTracerVersion + + ". Do not inject the tracer into the forked JVMs manually, or ensure the manually injected version is the same as the one injected automatically"); + } + sco.createRemaining(config); GitClient.Factory gitClientFactory = buildGitClientFactory(config); diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ModuleExecutionSettingsFactoryImpl.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ModuleExecutionSettingsFactoryImpl.java index 6122ac1aabe..6e4c4b29de4 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ModuleExecutionSettingsFactoryImpl.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/config/ModuleExecutionSettingsFactoryImpl.java @@ -1,5 +1,6 @@ package datadog.trace.civisibility.config; +import datadog.communication.ddagent.TracerVersion; import datadog.trace.api.Config; import datadog.trace.api.civisibility.config.Configurations; import datadog.trace.api.civisibility.config.ModuleExecutionSettings; @@ -156,6 +157,11 @@ private Map getPropertiesPropagatedToChildProcess( CiVisibilityConfig.CIVISIBILITY_BUILD_INSTRUMENTATION_ENABLED), Boolean.toString(false)); + propagatedSystemProperties.put( + Strings.propertyNameToSystemPropertyName( + CiVisibilityConfig.CIVISIBILITY_INJECTED_TRACER_VERSION), + TracerVersion.TRACER_VERSION); + return propagatedSystemProperties; } diff --git a/dd-trace-api/src/main/java/datadog/trace/api/config/CiVisibilityConfig.java b/dd-trace-api/src/main/java/datadog/trace/api/config/CiVisibilityConfig.java index 1a3d861c3ac..b23ce0fd206 100644 --- a/dd-trace-api/src/main/java/datadog/trace/api/config/CiVisibilityConfig.java +++ b/dd-trace-api/src/main/java/datadog/trace/api/config/CiVisibilityConfig.java @@ -60,6 +60,8 @@ public final class CiVisibilityConfig { "civisibility.module.execution.settings.cache.size"; public static final String CIVISIBILITY_COVERAGE_SEGMENTS_ENABLED = "civisibility.coverage.segments.enabled"; + public static final String CIVISIBILITY_INJECTED_TRACER_VERSION = + "civisibility.injected.tracer.version"; private CiVisibilityConfig() {} } diff --git a/internal-api/src/main/java/datadog/trace/api/Config.java b/internal-api/src/main/java/datadog/trace/api/Config.java index a322a4c8f1d..b5b898012fa 100644 --- a/internal-api/src/main/java/datadog/trace/api/Config.java +++ b/internal-api/src/main/java/datadog/trace/api/Config.java @@ -155,6 +155,7 @@ import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_GIT_UNSHALLOW_ENABLED; import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_GIT_UPLOAD_ENABLED; import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_GIT_UPLOAD_TIMEOUT_MILLIS; +import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_INJECTED_TRACER_VERSION; import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_ITR_ENABLED; import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_JACOCO_GRADLE_SOURCE_SETS; import static datadog.trace.api.config.CiVisibilityConfig.CIVISIBILITY_JACOCO_PLUGIN_EXCLUDES; @@ -716,6 +717,7 @@ static class HostNameHolder { private final boolean ciVisibilityRepoIndexSharingEnabled; private final int ciVisibilityModuleExecutionSettingsCacheSize; private final boolean ciVisibilityCoverageSegmentsEnabled; + private final String ciVisibilityInjectedTracerVersion; private final boolean remoteConfigEnabled; private final boolean remoteConfigIntegrityCheckEnabled; @@ -1651,6 +1653,8 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins configProvider.getInteger(CIVISIBILITY_MODULE_EXECUTION_SETTINGS_CACHE_SIZE, 16); ciVisibilityCoverageSegmentsEnabled = configProvider.getBoolean(CIVISIBILITY_COVERAGE_SEGMENTS_ENABLED, false); + ciVisibilityInjectedTracerVersion = + configProvider.getString(CIVISIBILITY_INJECTED_TRACER_VERSION); remoteConfigEnabled = configProvider.getBoolean(REMOTE_CONFIG_ENABLED, DEFAULT_REMOTE_CONFIG_ENABLED); @@ -2733,6 +2737,10 @@ public boolean isCiVisibilityCoverageSegmentsEnabled() { return ciVisibilityCoverageSegmentsEnabled; } + public String getCiVisibilityInjectedTracerVersion() { + return ciVisibilityInjectedTracerVersion; + } + public String getAppSecRulesFile() { return appSecRulesFile; } From 5dce4fdae63a4f4f1000d022179df0bdf6d94580 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko <121111529+nikita-tkachenko-datadog@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:28:45 +0100 Subject: [PATCH 6/6] Add x-datadog-trace-id header to ITR backend requests (#6189) --- .../trace/civisibility/CiVisibilitySystem.java | 6 ++---- .../communication/BackendApiFactory.java | 6 ++++-- .../civisibility/communication/EvpProxyApi.java | 11 ++++++++++- .../civisibility/communication/IntakeApi.java | 17 +++++++++++++++-- .../config/ConfigurationApiImplTest.groovy | 3 ++- .../civisibility/git/tree/GitDataApiTest.groovy | 3 ++- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java index 4f4a29255b6..51ba05d9709 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/CiVisibilitySystem.java @@ -147,8 +147,6 @@ private static DDBuildSystemSession.Factory buildSystemSessionFactory( CoverageProbeStoreFactory coverageProbeStoreFactory, GitClient.Factory gitClientFactory) { BackendApiFactory backendApiFactory = new BackendApiFactory(config, sco); - BackendApi backendApi = backendApiFactory.createBackendApi(); - return (String projectName, Path projectRoot, String startCommand, @@ -178,6 +176,7 @@ private static DDBuildSystemSession.Factory buildSystemSessionFactory( TestDecorator testDecorator = new TestDecoratorImpl(buildSystemName, ciTags); TestModuleRegistry testModuleRegistry = new TestModuleRegistry(); + BackendApi backendApi = backendApiFactory.createBackendApi(); GitDataUploader gitDataUploader = buildGitDataUploader(config, gitInfoProvider, gitClientFactory, backendApi, repoRoot); ModuleExecutionSettingsFactory moduleExecutionSettingsFactory = @@ -241,8 +240,6 @@ private static DDTestFrameworkSession.Factory testFrameworkSessionFactory( CoverageProbeStoreFactory coverageProbeStoreFactory, GitClient.Factory gitClientFactory) { BackendApiFactory backendApiFactory = new BackendApiFactory(config, sco); - BackendApi backendApi = backendApiFactory.createBackendApi(); - return (String projectName, Path projectRoot, String component, Long startTime) -> { CIProviderInfoFactory ciProviderInfoFactory = new CIProviderInfoFactory(config); CIProviderInfo ciProviderInfo = ciProviderInfoFactory.createCIProviderInfo(projectRoot); @@ -288,6 +285,7 @@ private static DDTestFrameworkSession.Factory testFrameworkSessionFactory( // either we are in the build system // or we are in the tests JVM and the build system is not instrumented if (parentProcessSessionId == null || parentProcessModuleId == null) { + BackendApi backendApi = backendApiFactory.createBackendApi(); GitDataUploader gitDataUploader = buildGitDataUploader(config, gitInfoProvider, gitClientFactory, backendApi, repoRoot); RepoIndexProvider indexProvider = new RepoIndexBuilder(repoRoot, FileSystems.getDefault()); diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/BackendApiFactory.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/BackendApiFactory.java index e0f6c512b61..934e68868ee 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/BackendApiFactory.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/BackendApiFactory.java @@ -33,17 +33,19 @@ public BackendApiFactory(Config config, SharedCommunicationObjects sharedCommuni "Agentless mode is enabled and api key is not set. Please set application key"); } long timeoutMillis = config.getCiVisibilityBackendApiTimeoutMillis(); - return new IntakeApi(site, apiKey, timeoutMillis, retryPolicyFactory); + String traceId = config.getIdGenerationStrategy().generateTraceId().toString(); + return new IntakeApi(site, apiKey, traceId, timeoutMillis, retryPolicyFactory); } DDAgentFeaturesDiscovery featuresDiscovery = sharedCommunicationObjects.featuresDiscovery(config); featuresDiscovery.discoverIfOutdated(); if (featuresDiscovery.supportsEvpProxy()) { + String traceId = config.getIdGenerationStrategy().generateTraceId().toString(); String evpProxyEndpoint = featuresDiscovery.getEvpProxyEndpoint(); HttpUrl evpProxyUrl = sharedCommunicationObjects.agentUrl.resolve(evpProxyEndpoint); return new EvpProxyApi( - evpProxyUrl, retryPolicyFactory, sharedCommunicationObjects.okHttpClient); + traceId, evpProxyUrl, retryPolicyFactory, sharedCommunicationObjects.okHttpClient); } log.warn( diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/EvpProxyApi.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/EvpProxyApi.java index 0353cd80fc7..2329b86b061 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/EvpProxyApi.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/EvpProxyApi.java @@ -19,14 +19,21 @@ public class EvpProxyApi implements BackendApi { private static final String API_VERSION = "v2"; private static final String X_DATADOG_EVP_SUBDOMAIN_HEADER = "X-Datadog-EVP-Subdomain"; + private static final String X_DATADOG_TRACE_ID_HEADER = "x-datadog-trace-id"; + private static final String X_DATADOG_PARENT_ID_HEADER = "x-datadog-parent-id"; private static final String API_SUBDOMAIN = "api"; + private final String traceId; private final HttpRetryPolicy.Factory retryPolicyFactory; private final HttpUrl evpProxyUrl; private final OkHttpClient httpClient; public EvpProxyApi( - HttpUrl evpProxyUrl, HttpRetryPolicy.Factory retryPolicyFactory, OkHttpClient httpClient) { + String traceId, + HttpUrl evpProxyUrl, + HttpRetryPolicy.Factory retryPolicyFactory, + OkHttpClient httpClient) { + this.traceId = traceId; this.evpProxyUrl = evpProxyUrl.resolve(String.format("api/%s/", API_VERSION)); this.retryPolicyFactory = retryPolicyFactory; this.httpClient = httpClient; @@ -41,6 +48,8 @@ public T post( new Request.Builder() .url(url) .addHeader(X_DATADOG_EVP_SUBDOMAIN_HEADER, API_SUBDOMAIN) + .addHeader(X_DATADOG_TRACE_ID_HEADER, traceId) + .addHeader(X_DATADOG_PARENT_ID_HEADER, traceId) .post(requestBody) .build(); diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/IntakeApi.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/IntakeApi.java index d8617376b9c..55728a277d2 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/IntakeApi.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/communication/IntakeApi.java @@ -20,15 +20,23 @@ public class IntakeApi implements BackendApi { private static final String API_VERSION = "v2"; private static final String DD_API_KEY_HEADER = "dd-api-key"; + private static final String X_DATADOG_TRACE_ID_HEADER = "x-datadog-trace-id"; + private static final String X_DATADOG_PARENT_ID_HEADER = "x-datadog-parent-id"; private final String apiKey; + private final String traceId; private final HttpRetryPolicy.Factory retryPolicyFactory; private final HttpUrl hostUrl; private final OkHttpClient httpClient; public IntakeApi( - String site, String apiKey, long timeoutMillis, HttpRetryPolicy.Factory retryPolicyFactory) { + String site, + String apiKey, + String traceId, + long timeoutMillis, + HttpRetryPolicy.Factory retryPolicyFactory) { this.apiKey = apiKey; + this.traceId = traceId; this.retryPolicyFactory = retryPolicyFactory; final String ciVisibilityAgentlessUrlStr = Config.get().getCiVisibilityAgentlessUrl(); @@ -47,7 +55,12 @@ public T post( throws IOException { HttpUrl url = hostUrl.resolve(uri); Request.Builder requestBuilder = - new Request.Builder().url(url).post(requestBody).addHeader(DD_API_KEY_HEADER, apiKey); + new Request.Builder() + .url(url) + .post(requestBody) + .addHeader(DD_API_KEY_HEADER, apiKey) + .addHeader(X_DATADOG_TRACE_ID_HEADER, traceId) + .addHeader(X_DATADOG_PARENT_ID_HEADER, traceId); Request request = requestBuilder.build(); HttpRetryPolicy retryPolicy = retryPolicyFactory.create(); diff --git a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/ConfigurationApiImplTest.groovy b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/ConfigurationApiImplTest.groovy index 53c0c31ee0a..41fcbdd54b0 100644 --- a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/ConfigurationApiImplTest.groovy +++ b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/config/ConfigurationApiImplTest.groovy @@ -135,10 +135,11 @@ class ConfigurationApiImplTest extends Specification { } private BackendApi givenEvpProxy() { + String traceId = "a-trace-id" HttpUrl proxyUrl = HttpUrl.get(intakeServer.address) HttpRetryPolicy.Factory retryPolicyFactory = new HttpRetryPolicy.Factory(5, 100, 2.0) OkHttpClient client = OkHttpUtils.buildHttpClient(proxyUrl, REQUEST_TIMEOUT_MILLIS) - return new EvpProxyApi(proxyUrl, retryPolicyFactory, client) + return new EvpProxyApi(traceId, proxyUrl, retryPolicyFactory, client) } private static TracerEnvironment givenTracerEnvironment() { diff --git a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/git/tree/GitDataApiTest.groovy b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/git/tree/GitDataApiTest.groovy index f0c6edfdca3..c280fe51f76 100644 --- a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/git/tree/GitDataApiTest.groovy +++ b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/git/tree/GitDataApiTest.groovy @@ -114,10 +114,11 @@ class GitDataApiTest extends Specification { } private BackendApi givenEvpProxy() { + String traceId = "a-trace-id" HttpUrl proxyUrl = HttpUrl.get(intakeServer.address) HttpRetryPolicy.Factory retryPolicyFactory = new HttpRetryPolicy.Factory(5, 100, 2.0) OkHttpClient client = OkHttpUtils.buildHttpClient(proxyUrl, REQUEST_TIMEOUT_MILLIS) - return new EvpProxyApi(proxyUrl, retryPolicyFactory, client) + return new EvpProxyApi(traceId, proxyUrl, retryPolicyFactory, client) } private Path givenPackFile() {