Skip to content

Commit

Permalink
Merge branch 'master' into joey/aws-sns
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyzhao2018 authored May 30, 2024
2 parents c255ef1 + c2fa83f commit 7e2774e
Show file tree
Hide file tree
Showing 70 changed files with 1,407 additions and 463 deletions.
19 changes: 18 additions & 1 deletion communication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,29 @@ ext {
'datadog.communication.monitor.DDAgentStatsDConnection',
'datadog.communication.monitor.DDAgentStatsDConnection.*',
'datadog.communication.monitor.LoggingStatsDClient',
'datadog.communication.BackendApiFactory',
'datadog.communication.BackendApiFactory.Intake',
'datadog.communication.EvpProxyApi',
'datadog.communication.IntakeApi',
'datadog.communication.util.IOUtils',
'datadog.communication.util.IOUtils.1',
]
excludedClassesBranchCoverage = [
'datadog.communication.ddagent.TracerVersion',
'datadog.communication.BackendApiFactory',
'datadog.communication.EvpProxyApi',
'datadog.communication.IntakeApi',
]
excludedClassesBranchCoverage = ['datadog.communication.ddagent.TracerVersion',]
excludedClassesInstructionCoverage = [
// can't reach the error condition now
'datadog.communication.fleet.FleetServiceImpl',
'datadog.communication.ddagent.SharedCommunicationObjects',
'datadog.communication.ddagent.TracerVersion',
'datadog.communication.BackendApiFactory',
'datadog.communication.BackendApiFactory.Intake',
'datadog.communication.EvpProxyApi',
'datadog.communication.IntakeApi',
'datadog.communication.util.IOUtils',
'datadog.communication.util.IOUtils.1',
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package datadog.trace.civisibility.communication;
package datadog.communication;

import datadog.communication.http.OkHttpUtils;
import datadog.trace.civisibility.utils.IOThrowingFunction;
import datadog.communication.util.IOThrowingFunction;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nullable;
Expand All @@ -14,6 +14,7 @@ <T> T post(
String uri,
RequestBody requestBody,
IOThrowingFunction<InputStream, T> responseParser,
@Nullable OkHttpUtils.CustomListener requestListener)
@Nullable OkHttpUtils.CustomListener requestListener,
boolean requestCompression)
throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package datadog.trace.civisibility.communication;
package datadog.communication;

import datadog.communication.ddagent.DDAgentFeaturesDiscovery;
import datadog.communication.ddagent.SharedCommunicationObjects;
import datadog.communication.http.HttpRetryPolicy;
import datadog.trace.api.Config;
import datadog.trace.util.throwable.FatalAgentMisconfigurationError;
import java.util.function.Function;
import javax.annotation.Nullable;
import okhttp3.HttpUrl;
import org.slf4j.Logger;
Expand All @@ -22,19 +23,19 @@ public BackendApiFactory(Config config, SharedCommunicationObjects sharedCommuni
this.sharedCommunicationObjects = sharedCommunicationObjects;
}

public @Nullable BackendApi createBackendApi() {
public @Nullable BackendApi createBackendApi(Intake intake) {
HttpRetryPolicy.Factory retryPolicyFactory = new HttpRetryPolicy.Factory(5, 100, 2.0, true);

if (config.isCiVisibilityAgentlessEnabled()) {
HttpUrl agentlessUrl = getAgentlessUrl();
if (intake.agentlessModeEnabled.apply(config)) {
HttpUrl agentlessUrl = getAgentlessUrl(intake);
String apiKey = config.getApiKey();
if (apiKey == null || apiKey.isEmpty()) {
throw new FatalAgentMisconfigurationError(
"Agentless mode is enabled and api key is not set. Please set application key");
}
String traceId = config.getIdGenerationStrategy().generateTraceId().toString();
long timeoutMillis = config.getCiVisibilityBackendApiTimeoutMillis();
return new IntakeApi(agentlessUrl, apiKey, traceId, timeoutMillis, retryPolicyFactory);
return new IntakeApi(agentlessUrl, apiKey, traceId, timeoutMillis, retryPolicyFactory, true);
}

DDAgentFeaturesDiscovery featuresDiscovery =
Expand All @@ -45,7 +46,7 @@ public BackendApiFactory(Config config, SharedCommunicationObjects sharedCommuni
String evpProxyEndpoint = featuresDiscovery.getEvpProxyEndpoint();
HttpUrl evpProxyUrl = sharedCommunicationObjects.agentUrl.resolve(evpProxyEndpoint);
return new EvpProxyApi(
traceId, evpProxyUrl, retryPolicyFactory, sharedCommunicationObjects.okHttpClient);
traceId, evpProxyUrl, retryPolicyFactory, sharedCommunicationObjects.okHttpClient, true);
}

log.warn(
Expand All @@ -54,14 +55,34 @@ public BackendApiFactory(Config config, SharedCommunicationObjects sharedCommuni
return null;
}

private HttpUrl getAgentlessUrl() {
final String ciVisibilityAgentlessUrlStr = config.getCiVisibilityAgentlessUrl();
if (ciVisibilityAgentlessUrlStr != null && !ciVisibilityAgentlessUrlStr.isEmpty()) {
return HttpUrl.get(
String.format("%s/api/%s/", ciVisibilityAgentlessUrlStr, IntakeApi.API_VERSION));
private HttpUrl getAgentlessUrl(Intake intake) {
String customUrl = intake.customUrl.apply(config);
if (customUrl != null && !customUrl.isEmpty()) {
return HttpUrl.get(String.format("%s/api/%s/", customUrl, intake.version));
} else {
String site = config.getSite();
return HttpUrl.get(String.format("https://api.%s/api/%s/", site, IntakeApi.API_VERSION));
return HttpUrl.get(
String.format("https://%s.%s/api/%s/", intake.urlPrefix, site, intake.version));
}
}

public enum Intake {
API("api", "v2", Config::isCiVisibilityAgentlessEnabled, Config::getCiVisibilityAgentlessUrl);

public final String urlPrefix;
public final String version;
public final Function<Config, Boolean> agentlessModeEnabled;
public final Function<Config, String> customUrl;

Intake(
String urlPrefix,
String version,
Function<Config, Boolean> agentlessModeEnabled,
Function<Config, String> customUrl) {
this.urlPrefix = urlPrefix;
this.version = version;
this.agentlessModeEnabled = agentlessModeEnabled;
this.customUrl = customUrl;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.civisibility.communication;
package datadog.communication;

import datadog.communication.http.HttpRetryPolicy;
import datadog.communication.http.OkHttpUtils;
import datadog.trace.civisibility.utils.IOThrowingFunction;
import datadog.communication.util.IOThrowingFunction;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -32,35 +32,28 @@ public class EvpProxyApi implements BackendApi {
private final HttpRetryPolicy.Factory retryPolicyFactory;
private final HttpUrl evpProxyUrl;
private final OkHttpClient httpClient;
private final boolean gzipEnabled;

public EvpProxyApi(
String traceId,
HttpUrl evpProxyUrl,
HttpRetryPolicy.Factory retryPolicyFactory,
OkHttpClient httpClient) {
this(traceId, evpProxyUrl, retryPolicyFactory, httpClient, true);
}
private final boolean responseCompression;

public EvpProxyApi(
String traceId,
HttpUrl evpProxyUrl,
HttpRetryPolicy.Factory retryPolicyFactory,
OkHttpClient httpClient,
boolean gzipEnabled) {
boolean responseCompression) {
this.traceId = traceId;
this.evpProxyUrl = evpProxyUrl.resolve(String.format("api/%s/", API_VERSION));
this.retryPolicyFactory = retryPolicyFactory;
this.httpClient = httpClient;
this.gzipEnabled = gzipEnabled;
this.responseCompression = responseCompression;
}

@Override
public <T> T post(
String uri,
RequestBody requestBody,
IOThrowingFunction<InputStream, T> responseParser,
@Nullable OkHttpUtils.CustomListener requestListener)
@Nullable OkHttpUtils.CustomListener requestListener,
boolean requestCompression)
throws IOException {
final HttpUrl url = evpProxyUrl.resolve(uri);

Expand All @@ -75,7 +68,11 @@ public <T> T post(
requestBuilder.tag(OkHttpUtils.CustomListener.class, requestListener);
}

if (gzipEnabled) {
if (requestCompression) {
requestBuilder.addHeader(CONTENT_ENCODING_HEADER, GZIP_ENCODING);
}

if (responseCompression) {
requestBuilder.addHeader(ACCEPT_ENCODING_HEADER, GZIP_ENCODING);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.civisibility.communication;
package datadog.communication;

import datadog.communication.http.HttpRetryPolicy;
import datadog.communication.http.OkHttpUtils;
import datadog.trace.civisibility.utils.IOThrowingFunction;
import datadog.communication.util.IOThrowingFunction;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
Expand All @@ -19,8 +19,6 @@ public class IntakeApi implements BackendApi {

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

public 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";
Expand All @@ -31,31 +29,22 @@ public class IntakeApi implements BackendApi {
private final String apiKey;
private final String traceId;
private final HttpRetryPolicy.Factory retryPolicyFactory;
private final boolean gzipEnabled;
private final boolean responseCompression;
private final HttpUrl hostUrl;
private final OkHttpClient httpClient;

public IntakeApi(
HttpUrl hostUrl,
String apiKey,
String traceId,
long timeoutMillis,
HttpRetryPolicy.Factory retryPolicyFactory) {
this(hostUrl, apiKey, traceId, timeoutMillis, retryPolicyFactory, true);
}

public IntakeApi(
HttpUrl hostUrl,
String apiKey,
String traceId,
long timeoutMillis,
HttpRetryPolicy.Factory retryPolicyFactory,
boolean gzipEnabled) {
boolean responseCompression) {
this.hostUrl = hostUrl;
this.apiKey = apiKey;
this.traceId = traceId;
this.retryPolicyFactory = retryPolicyFactory;
this.gzipEnabled = gzipEnabled;
this.responseCompression = responseCompression;

httpClient = OkHttpUtils.buildHttpClient(hostUrl, timeoutMillis);
}
Expand All @@ -65,7 +54,8 @@ public <T> T post(
String uri,
RequestBody requestBody,
IOThrowingFunction<InputStream, T> responseParser,
@Nullable OkHttpUtils.CustomListener requestListener)
@Nullable OkHttpUtils.CustomListener requestListener,
boolean requestCompression)
throws IOException {
HttpUrl url = hostUrl.resolve(uri);
Request.Builder requestBuilder =
Expand All @@ -80,7 +70,11 @@ public <T> T post(
requestBuilder.tag(OkHttpUtils.CustomListener.class, requestListener);
}

if (gzipEnabled) {
if (requestCompression) {
requestBuilder.addHeader(CONTENT_ENCODING_HEADER, GZIP_ENCODING);
}

if (responseCompression) {
requestBuilder.addHeader(ACCEPT_ENCODING_HEADER, GZIP_ENCODING);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void createRemaining(Config config) {
monitoring = Monitoring.DISABLED;
}
if (agentUrl == null) {
agentUrl = HttpUrl.parse(config.getAgentUrl());
agentUrl = parseAgentUrl(config);
if (agentUrl == null) {
throw new IllegalArgumentException("Bad agent URL: " + config.getAgentUrl());
}
Expand All @@ -43,6 +43,15 @@ public void createRemaining(Config config) {
}
}

private static HttpUrl parseAgentUrl(Config config) {
String agentUrl = config.getAgentUrl();
if (agentUrl.startsWith("unix:")) {
// provide placeholder agent URL, in practice we'll be tunnelling over UDS
agentUrl = "http://" + config.getAgentHost() + ":" + config.getAgentPort();
}
return HttpUrl.parse(agentUrl);
}

private static long getHttpClientTimeout(Config config) {
if (!config.isCiVisibilityEnabled()) {
return TimeUnit.SECONDS.toMillis(config.getAgentTimeout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import com.timgroup.statsd.NoOpStatsDClient;
import com.timgroup.statsd.NoOpDirectStatsDClient;
import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
import com.timgroup.statsd.StatsDClientErrorHandler;
import datadog.trace.api.Config;
Expand All @@ -23,7 +23,7 @@ final class DDAgentStatsDConnection implements StatsDClientErrorHandler {
private static final Logger log = LoggerFactory.getLogger(DDAgentStatsDConnection.class);
private static final IOLogger ioLogger = new IOLogger(log);

private static final com.timgroup.statsd.StatsDClient NO_OP = new NoOpStatsDClient();
private static final com.timgroup.statsd.StatsDClient NO_OP = new NoOpDirectStatsDClient();

private static final String UNIX_DOMAIN_SOCKET_PREFIX = "unix://";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datadog.trace.civisibility.utils;
package datadog.communication.util;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datadog.trace.civisibility.utils;
package datadog.communication.util;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.BufferedReader;
Expand Down
2 changes: 2 additions & 0 deletions dd-java-agent/agent-builder/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ excludedClassesCoverage += ['datadog.trace.agent.tooling.*']
dependencies {
api project(':dd-java-agent:agent-tooling')

implementation project(':dd-java-agent:agent-otel:otel-tooling')

testImplementation project(':dd-java-agent:testing')
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.agent.tooling;

import static datadog.opentelemetry.tooling.OtelExtensionHandler.OPENTELEMETRY;
import static datadog.trace.agent.tooling.ExtensionHandler.DATADOG;

import de.thetaphi.forbiddenapis.SuppressForbidden;
Expand All @@ -23,7 +24,7 @@
public final class ExtensionFinder {
private static final Logger log = LoggerFactory.getLogger(ExtensionFinder.class);

private static final ExtensionHandler[] handlers = {DATADOG};
private static final ExtensionHandler[] handlers = {OPENTELEMETRY, DATADOG};

/**
* Discovers extensions on the configured path and creates a classloader for each extension.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.civisibility;

import datadog.communication.BackendApi;
import datadog.trace.api.Config;
import datadog.trace.api.civisibility.config.ModuleExecutionSettings;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
Expand All @@ -10,7 +11,6 @@
import datadog.trace.civisibility.codeowners.Codeowners;
import datadog.trace.civisibility.codeowners.CodeownersProvider;
import datadog.trace.civisibility.codeowners.NoCodeowners;
import datadog.trace.civisibility.communication.BackendApi;
import datadog.trace.civisibility.config.CachingModuleExecutionSettingsFactory;
import datadog.trace.civisibility.config.ConfigurationApi;
import datadog.trace.civisibility.config.ConfigurationApiImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package datadog.trace.civisibility;

import datadog.communication.BackendApi;
import datadog.communication.BackendApiFactory;
import datadog.communication.ddagent.SharedCommunicationObjects;
import datadog.trace.api.Config;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
import datadog.trace.api.git.GitInfoProvider;
import datadog.trace.civisibility.ci.CIProviderInfoFactory;
import datadog.trace.civisibility.communication.BackendApi;
import datadog.trace.civisibility.communication.BackendApiFactory;
import datadog.trace.civisibility.config.CachingJvmInfoFactory;
import datadog.trace.civisibility.config.JvmInfoFactory;
import datadog.trace.civisibility.config.JvmInfoFactoryImpl;
Expand Down Expand Up @@ -61,7 +61,8 @@ public class CiVisibilityServices {
GitInfoProvider gitInfoProvider) {
this.config = config;
this.metricCollector = metricCollector;
this.backendApi = new BackendApiFactory(config, sco).createBackendApi();
this.backendApi =
new BackendApiFactory(config, sco).createBackendApi(BackendApiFactory.Intake.API);
this.jvmInfoFactory = new CachingJvmInfoFactory(config, new JvmInfoFactoryImpl());
this.gitClientFactory = new GitClient.Factory(config, metricCollector);
this.ciProviderInfoFactory = new CIProviderInfoFactory(config);
Expand Down
Loading

0 comments on commit 7e2774e

Please sign in to comment.