Skip to content

Commit

Permalink
Merge branch 'master' into alejandro.gonzalez/session_rewriting_detec…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jandro996 authored Feb 16, 2024
2 parents 11583a1 + 167e8f7 commit 7c37c84
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 155 deletions.
1 change: 1 addition & 0 deletions dd-java-agent/agent-ci-visibility/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ excludedClassesCoverage += [
"datadog.trace.civisibility.config.CachingModuleExecutionSettingsFactory",
"datadog.trace.civisibility.config.CachingModuleExecutionSettingsFactory.Key",
"datadog.trace.civisibility.config.CiVisibilitySettings",
"datadog.trace.civisibility.config.ConfigurationApiImpl",
"datadog.trace.civisibility.config.ConfigurationApiImpl.MultiEnvelopeDto",
"datadog.trace.civisibility.config.ConfigurationApi.1",
"datadog.trace.civisibility.config.ModuleExecutionSettingsFactoryImpl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datadog.trace.api.Config;
import datadog.trace.api.civisibility.config.ModuleExecutionSettings;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
import datadog.trace.api.git.GitInfoProvider;
import datadog.trace.civisibility.ci.CIInfo;
import datadog.trace.civisibility.ci.CIProviderInfo;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class CiVisibilityRepoServices {
gitDataUploader =
buildGitDataUploader(
services.config,
services.metricCollector,
services.gitInfoProvider,
services.gitClientFactory,
services.backendApi,
Expand All @@ -75,7 +77,12 @@ public class CiVisibilityRepoServices {
} else {
moduleExecutionSettingsFactory =
buildModuleExecutionSettingsFactory(
services.config, services.backendApi, gitDataUploader, repoIndexProvider, repoRoot);
services.config,
services.metricCollector,
services.backendApi,
gitDataUploader,
repoIndexProvider,
repoRoot);
}
}

Expand Down Expand Up @@ -112,6 +119,7 @@ private static ModuleExecutionSettingsFactory buildModuleExecutionSettingsFetche

private static ModuleExecutionSettingsFactory buildModuleExecutionSettingsFactory(
Config config,
CiVisibilityMetricCollector metricCollector,
BackendApi backendApi,
GitDataUploader gitDataUploader,
RepoIndexProvider repoIndexProvider,
Expand All @@ -122,7 +130,7 @@ private static ModuleExecutionSettingsFactory buildModuleExecutionSettingsFactor
"Remote config and skippable tests requests will be skipped since backend API client could not be created");
configurationApi = ConfigurationApi.NO_OP;
} else {
configurationApi = new ConfigurationApiImpl(backendApi);
configurationApi = new ConfigurationApiImpl(backendApi, metricCollector);
}
return new CachingModuleExecutionSettingsFactory(
config,
Expand All @@ -132,6 +140,7 @@ private static ModuleExecutionSettingsFactory buildModuleExecutionSettingsFactor

private static GitDataUploader buildGitDataUploader(
Config config,
CiVisibilityMetricCollector metricCollector,
GitInfoProvider gitInfoProvider,
GitClient.Factory gitClientFactory,
BackendApi backendApi,
Expand All @@ -153,10 +162,10 @@ private static GitDataUploader buildGitDataUploader(
}

String remoteName = config.getCiVisibilityGitRemoteName();
GitDataApi gitDataApi = new GitDataApi(backendApi);
GitDataApi gitDataApi = new GitDataApi(backendApi, metricCollector);
GitClient gitClient = gitClientFactory.create(repoRoot);
return new GitDataUploaderImpl(
config, gitDataApi, gitClient, gitInfoProvider, repoRoot, remoteName);
config, metricCollector, gitDataApi, gitClient, gitInfoProvider, repoRoot, remoteName);
}

private static SourcePathResolver buildSourcePathResolver(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package datadog.trace.civisibility.communication;

import datadog.communication.http.OkHttpUtils;
import datadog.trace.civisibility.utils.IOThrowingFunction;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nullable;
import okhttp3.RequestBody;

/** API for posting HTTP requests to backend */
public interface BackendApi {

<T> T post(String uri, RequestBody requestBody, IOThrowingFunction<InputStream, T> responseParser)
<T> T post(
String uri,
RequestBody requestBody,
IOThrowingFunction<InputStream, T> responseParser,
@Nullable OkHttpUtils.CustomListener requestListener)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import javax.annotation.Nullable;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -56,7 +57,10 @@ public EvpProxyApi(

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

Expand All @@ -67,6 +71,10 @@ public <T> T post(
.addHeader(X_DATADOG_TRACE_ID_HEADER, traceId)
.addHeader(X_DATADOG_PARENT_ID_HEADER, traceId);

if (requestListener != null) {
requestBuilder.tag(OkHttpUtils.CustomListener.class, requestListener);
}

if (gzipEnabled) {
requestBuilder.addHeader(ACCEPT_ENCODING_HEADER, GZIP_ENCODING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import javax.annotation.Nullable;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -61,7 +62,10 @@ public IntakeApi(

@Override
public <T> T post(
String uri, RequestBody requestBody, IOThrowingFunction<InputStream, T> responseParser)
String uri,
RequestBody requestBody,
IOThrowingFunction<InputStream, T> responseParser,
@Nullable OkHttpUtils.CustomListener requestListener)
throws IOException {
HttpUrl url = hostUrl.resolve(uri);
Request.Builder requestBuilder =
Expand All @@ -72,6 +76,10 @@ public <T> T post(
.addHeader(X_DATADOG_TRACE_ID_HEADER, traceId)
.addHeader(X_DATADOG_PARENT_ID_HEADER, traceId);

if (requestListener != null) {
requestBuilder.tag(OkHttpUtils.CustomListener.class, requestListener);
}

if (gzipEnabled) {
requestBuilder.addHeader(ACCEPT_ENCODING_HEADER, GZIP_ENCODING);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package datadog.trace.civisibility.communication;

import datadog.communication.http.OkHttpUtils;
import datadog.trace.api.civisibility.telemetry.CiVisibilityCountMetric;
import datadog.trace.api.civisibility.telemetry.CiVisibilityDistributionMetric;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
import datadog.trace.api.civisibility.telemetry.tag.ErrorType;
import java.io.IOException;
import javax.annotation.Nullable;
import okhttp3.Call;
import okhttp3.Response;

public class TelemetryListener extends OkHttpUtils.CustomListener {

private final CiVisibilityMetricCollector metricCollector;
private final @Nullable CiVisibilityCountMetric requestCountMetric;
private final @Nullable CiVisibilityCountMetric requestErrorsMetric;
private final @Nullable CiVisibilityDistributionMetric requestBytesMetric;
private final @Nullable CiVisibilityDistributionMetric requestDurationMetric;
private final @Nullable CiVisibilityDistributionMetric responseBytesMetric;
private long callStartTimestamp;

private TelemetryListener(
CiVisibilityMetricCollector metricCollector,
@Nullable CiVisibilityCountMetric requestCountMetric,
@Nullable CiVisibilityCountMetric requestErrorsMetric,
@Nullable CiVisibilityDistributionMetric requestBytesMetric,
@Nullable CiVisibilityDistributionMetric requestDurationMetric,
@Nullable CiVisibilityDistributionMetric responseBytesMetric) {
this.metricCollector = metricCollector;
this.requestCountMetric = requestCountMetric;
this.requestErrorsMetric = requestErrorsMetric;
this.requestBytesMetric = requestBytesMetric;
this.requestDurationMetric = requestDurationMetric;
this.responseBytesMetric = responseBytesMetric;
}

public void callStart(Call call) {
callStartTimestamp = System.currentTimeMillis();
if (requestCountMetric != null) {
metricCollector.add(requestCountMetric, 1);
}
}

public void requestBodyEnd(Call call, long byteCount) {
if (requestBytesMetric != null) {
metricCollector.add(requestBytesMetric, (int) byteCount);
}
}

public void responseHeadersEnd(Call call, Response response) {
if (requestErrorsMetric != null) {
if (!response.isSuccessful()) {
int responseCode = response.code();
metricCollector.add(requestErrorsMetric, 1, ErrorType.from(responseCode));
}
}
}

@Override
public void responseBodyEnd(Call call, long byteCount) {
if (responseBytesMetric != null) {
metricCollector.add(responseBytesMetric, (int) byteCount);
}
}

public void callEnd(Call call) {
if (requestDurationMetric != null) {
int durationMillis = (int) (System.currentTimeMillis() - callStartTimestamp);
metricCollector.add(requestDurationMetric, durationMillis);
}
}

public void callFailed(Call call, IOException ioe) {
if (requestDurationMetric != null) {
int durationMillis = (int) (System.currentTimeMillis() - callStartTimestamp);
metricCollector.add(requestDurationMetric, durationMillis);
}

if (requestErrorsMetric != null) {
metricCollector.add(requestErrorsMetric, 1, ErrorType.NETWORK);
}
}

public static final class Builder {
private final CiVisibilityMetricCollector metricCollector;
private @Nullable CiVisibilityCountMetric requestCountMetric;
private @Nullable CiVisibilityCountMetric requestErrorsMetric;
private @Nullable CiVisibilityDistributionMetric requestBytesMetric;
private @Nullable CiVisibilityDistributionMetric requestDurationMetric;
private @Nullable CiVisibilityDistributionMetric responseBytesMetric;

public Builder(CiVisibilityMetricCollector metricCollector) {
this.metricCollector = metricCollector;
}

public Builder requestCount(@Nullable CiVisibilityCountMetric requestCountMetric) {
this.requestCountMetric = requestCountMetric;
return this;
}

public Builder requestErrors(@Nullable CiVisibilityCountMetric requestErrorsMetric) {
this.requestErrorsMetric = requestErrorsMetric;
return this;
}

public Builder requestBytes(@Nullable CiVisibilityDistributionMetric requestBytesMetric) {
this.requestBytesMetric = requestBytesMetric;
return this;
}

public Builder requestDuration(@Nullable CiVisibilityDistributionMetric requestDurationMetric) {
this.requestDurationMetric = requestDurationMetric;
return this;
}

public Builder responseBytes(@Nullable CiVisibilityDistributionMetric responseBytesMetric) {
this.responseBytesMetric = responseBytesMetric;
return this;
}

public OkHttpUtils.CustomListener build() {
return new TelemetryListener(
metricCollector,
requestCountMetric,
requestErrorsMetric,
requestBytesMetric,
requestDurationMetric,
responseBytesMetric);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@
public class CiVisibilitySettings {

public static final CiVisibilitySettings DEFAULT =
new CiVisibilitySettings(false, false, false, false);
new CiVisibilitySettings(false, false, false, false, false);

private final boolean itr_enabled;
private final boolean code_coverage;
private final boolean tests_skipping;
private final boolean require_git;
private final boolean flaky_test_retries_enabled;

public CiVisibilitySettings(
boolean itr_enabled,
boolean code_coverage,
boolean tests_skipping,
boolean require_git,
boolean flaky_test_retries_enabled) {
this.itr_enabled = itr_enabled;
this.code_coverage = code_coverage;
this.tests_skipping = tests_skipping;
this.require_git = require_git;
this.flaky_test_retries_enabled = flaky_test_retries_enabled;
}

public boolean isItrEnabled() {
return itr_enabled;
}

public boolean isCodeCoverageEnabled() {
return code_coverage;
}
Expand Down
Loading

0 comments on commit 7c37c84

Please sign in to comment.