Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for disablement of tracing via remote config #6827

Merged
merged 7 commits into from
Mar 27, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ abstract class AgentTestRunner extends DDSpecification implements AgentBuilder.L
@SuppressWarnings('PropertyName')
@Shared
TraceConfig MOCK_DSM_TRACE_CONFIG = new TraceConfig() {
@Override
boolean isTraceEnabled() {
return true
}
@Override
boolean isRuntimeMetricsEnabled() {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ private CoreTracer(

this.dynamicConfig =
DynamicConfig.create(ConfigSnapshot::new)
.setTracingEnabled(config.isTraceEnabled())
.setRuntimeMetricsEnabled(config.isRuntimeMetricsEnabled())
.setLogsInjectionEnabled(config.isLogsInjectionEnabled())
.setDataStreamsEnabled(config.isDataStreamsEnabled())
Expand Down Expand Up @@ -725,6 +726,7 @@ private CoreTracer(
public void rebuildTraceConfig(Config config) {
dynamicConfig
.initial()
.setTracingEnabled(config.isTraceEnabled())
.setRuntimeMetricsEnabled(config.isRuntimeMetricsEnabled())
.setLogsInjectionEnabled(config.isLogsInjectionEnabled())
.setDataStreamsEnabled(config.isDataStreamsEnabled())
Expand Down Expand Up @@ -927,6 +929,9 @@ public AgentDataStreamsMonitoring getDataStreamsMonitoring() {
* @param trace a list of the spans related to the same trace
*/
void write(final List<DDSpan> trace) {
if (trace.isEmpty() || !trace.get(0).traceConfig().isTraceEnabled()) {
return;
}
List<DDSpan> writtenTrace = interceptCompleteTrace(trace);
if (writtenTrace.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static datadog.remoteconfig.tuf.RemoteConfigRequest.ClientInfo.CAPABILITY_APM_LOGS_INJECTION;
import static datadog.remoteconfig.tuf.RemoteConfigRequest.ClientInfo.CAPABILITY_APM_TRACING_DATA_STREAMS_ENABLED;
import static datadog.remoteconfig.tuf.RemoteConfigRequest.ClientInfo.CAPABILITY_APM_TRACING_SAMPLE_RATE;
import static datadog.remoteconfig.tuf.RemoteConfigRequest.ClientInfo.CAPABILITY_APM_TRACING_TRACING_ENABLED;

import com.squareup.moshi.Json;
import com.squareup.moshi.JsonAdapter;
Expand Down Expand Up @@ -46,11 +47,11 @@ public TracingConfigPoller(DynamicConfig<?> dynamicConfig) {
public void start(Config config, SharedCommunicationObjects sco) {
this.startupLogsEnabled = config.isStartupLogsEnabled();
ConfigurationPoller configPoller = sco.configurationPoller(config);
// TODO: Add CAPABILITY_APM_TRACING_TRACING_ENABLED flag when tracing_enabled is implemented for
// Remote config

if (configPoller != null) {
configPoller.addCapabilities(
CAPABILITY_APM_TRACING_SAMPLE_RATE
CAPABILITY_APM_TRACING_TRACING_ENABLED
| CAPABILITY_APM_TRACING_SAMPLE_RATE
| CAPABILITY_APM_LOGS_INJECTION
| CAPABILITY_APM_HTTP_HEADER_TAGS
| CAPABILITY_APM_CUSTOM_TAGS
Expand Down Expand Up @@ -111,7 +112,6 @@ public void accept(ParsedConfigKey configKey, byte[] content, PollingRateHinter
throw new IllegalArgumentException("env mismatch");
}
}

receivedOverrides = true;
applyConfigOverrides(overrides.libConfig);
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -156,7 +156,7 @@ void applyConfigOverrides(LibConfig libConfig) {
} else {
GlobalLogLevelSwitcher.get().restore();
}

maybeOverride(builder::setTracingEnabled, libConfig.tracingEnabled);
maybeOverride(builder::setRuntimeMetricsEnabled, libConfig.runtimeMetricsEnabled);
maybeOverride(builder::setLogsInjectionEnabled, libConfig.logsInjectionEnabled);
maybeOverride(builder::setDataStreamsEnabled, libConfig.dataStreamsEnabled);
Expand Down Expand Up @@ -218,6 +218,9 @@ static final class ServiceTarget {
}

static final class LibConfig {
@Json(name = "tracing_enabled")
public Boolean tracingEnabled;

@Json(name = "tracing_debug")
public Boolean debugEnabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,51 @@ class CoreTracerTest extends DDCoreSpecification {
"""{"lib_config":{"tracing_tags": []}}""" | [:]
}

def "verify configuration polling with tracing_enabled"() {
setup:
def key = ParsedConfigKey.parse("datadog/2/APM_TRACING/config_overrides/config")
def poller = Mock(ConfigurationPoller)
def sco = new SharedCommunicationObjects(
okHttpClient: Mock(OkHttpClient),
monitoring: Mock(Monitoring),
agentUrl: HttpUrl.get('https://example.com'),
featuresDiscovery: Mock(DDAgentFeaturesDiscovery),
configurationPoller: poller
)

def updater

when:
def tracer = CoreTracer.builder()
.sharedCommunicationObjects(sco)
.pollForTracingConfiguration()
.build()

then:

1 * poller.addListener(Product.APM_TRACING, _ as ProductListener) >> {
updater = it[1] // capture config updater for further testing
}
and:
tracer.captureTraceConfig().traceEnabled == true

when:
updater.accept(key, value.getBytes(StandardCharsets.UTF_8), null)
updater.commit()

then:
tracer.captureTraceConfig().traceEnabled == expectedValue

cleanup:
tracer?.close()

where:
value | expectedValue
"""{"lib_config":{"tracing_enabled": false } } """ | false
"""{"lib_config":{"tracing_enabled": true } } """ | true
"""{"action": "enable", "lib_config": {"tracing_sampling_rate": null, "log_injection_enabled": null, "tracing_header_tags": null, "runtime_metrics_enabled": null, "tracing_debug": null, "tracing_service_mapping": null, "tracing_sampling_rules": null, "span_sampling_rules": null, "data_streams_enabled": null, "tracing_enabled": false}}""" | false
}

def "test local root service name override"() {
setup:
def tracer = tracerBuilder().writer(new ListWriter()).serviceName("test").build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public String toString() {
}

public final class Builder {

boolean tracingEnabled;
boolean runtimeMetricsEnabled;
boolean logsInjectionEnabled;
boolean dataStreamsEnabled;
Expand All @@ -114,6 +114,7 @@ public final class Builder {

Builder(Snapshot snapshot) {

this.tracingEnabled = snapshot.tracingEnabled;
this.runtimeMetricsEnabled = snapshot.runtimeMetricsEnabled;
this.logsInjectionEnabled = snapshot.logsInjectionEnabled;
this.dataStreamsEnabled = snapshot.dataStreamsEnabled;
Expand Down Expand Up @@ -202,6 +203,11 @@ public Builder setTracingTags(Map<String, String> tracingTags) {
return this;
}

public Builder setTracingEnabled(boolean tracingEnabled) {
this.tracingEnabled = tracingEnabled;
return this;
}

public Builder setPreferredServiceName(String preferredServiceName) {
this.preferredServiceName = preferredServiceName;
return this;
Expand Down Expand Up @@ -290,6 +296,7 @@ private static void maybePut(Map<String, Object> update, String key, Object valu

/** Immutable snapshot of the configuration. */
public static class Snapshot implements TraceConfig {
final boolean tracingEnabled;
final boolean runtimeMetricsEnabled;
final boolean logsInjectionEnabled;
final boolean dataStreamsEnabled;
Expand All @@ -309,6 +316,7 @@ public static class Snapshot implements TraceConfig {

protected Snapshot(DynamicConfig<?>.Builder builder, Snapshot oldSnapshot) {

this.tracingEnabled = builder.tracingEnabled;
this.runtimeMetricsEnabled = builder.runtimeMetricsEnabled;
this.logsInjectionEnabled = builder.logsInjectionEnabled;
this.dataStreamsEnabled = builder.dataStreamsEnabled;
Expand All @@ -330,6 +338,11 @@ private static <K, V> Map<K, V> nullToEmpty(Map<K, V> mapping) {
return null != mapping ? mapping : Collections.emptyMap();
}

@Override
public boolean isTraceEnabled() {
return tracingEnabled;
}

@Override
public boolean isRuntimeMetricsEnabled() {
return runtimeMetricsEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/** Snapshot of dynamic configuration; valid for the duration of a trace. */
public interface TraceConfig {
boolean isTraceEnabled();

boolean isRuntimeMetricsEnabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,11 @@ public ByteBuffer serialize() {
public static final class NoopTraceConfig implements TraceConfig {
public static final NoopTraceConfig INSTANCE = new NoopTraceConfig();

@Override
public boolean isTraceEnabled() {
return false;
}

@Override
public boolean isRuntimeMetricsEnabled() {
return false;
Expand Down