Skip to content

Implement DD_APM_TRACING_ENABLED=false #217

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions include/datadog/collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace datadog {
namespace tracing {

struct SpanData;
class TraceSampler;
class ErasedTraceSampler;

class Collector {
public:
Expand All @@ -29,7 +29,7 @@ class Collector {
// occurs.
virtual Expected<void> send(
std::vector<std::unique_ptr<SpanData>>&& spans,
const std::shared_ptr<TraceSampler>& response_handler) = 0;
const std::shared_ptr<ErasedTraceSampler>& response_handler) = 0;

// Return a JSON representation of this object's configuration. The JSON
// representation is an object with the following properties:
Expand Down
1 change: 1 addition & 0 deletions include/datadog/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum class ConfigName : char {
SPAN_SAMPLING_RULES,
TRACE_BAGGAGE_MAX_BYTES,
TRACE_BAGGAGE_MAX_ITEMS,
APM_TRACING_ENABLED,
};

// Represents metadata for configuration parameters
Expand Down
4 changes: 4 additions & 0 deletions include/datadog/datadog_agent_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ struct DatadogAgentConfig {
// How often, in seconds, to query the Datadog Agent for remote configuration
// updates.
Optional<double> remote_configuration_poll_interval_seconds;
// Whether APM tracing is enabled. This affects whether the
// "Datadog-Client-Computed-Stats: yes" header is sent with trace requests.
Optional<bool> apm_tracing_enabled;
};

class FinalizedDatadogAgentConfig {
Expand All @@ -87,6 +90,7 @@ class FinalizedDatadogAgentConfig {
std::chrono::steady_clock::duration shutdown_timeout;
std::chrono::steady_clock::duration remote_configuration_poll_interval;
std::unordered_map<ConfigName, ConfigMetadata> metadata;
bool apm_tracing_enabled;

// Origin detection
Optional<std::string> admission_controller_uid;
Expand Down
1 change: 1 addition & 0 deletions include/datadog/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace environment {
MACRO(DD_INSTRUMENTATION_INSTALL_ID) \
MACRO(DD_INSTRUMENTATION_INSTALL_TYPE) \
MACRO(DD_INSTRUMENTATION_INSTALL_TIME) \
MACRO(DD_APM_TRACING_ENABLED) \
MACRO(DD_EXTERNAL_ENV)

#define WITH_COMMA(ARG) ARG,
Expand Down
11 changes: 10 additions & 1 deletion include/datadog/injection_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
// parameters to `Span::inject` that alter the behavior of trace context
// propagation.

#include <array>

#include "optional.h"

namespace datadog {
namespace tracing {

struct InjectionOptions {};
struct InjectionOptions {
// If DD_APM_TRACING_ENABLED=false and what we're injecting is not an APM
// trace, then the code for the trace source (e.g. 02 for Appsec) can be
// set here.
Optional<std::array<char, 2>> trace_source{};
};

} // namespace tracing
} // namespace datadog
2 changes: 1 addition & 1 deletion include/datadog/null_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace tracing {
class NullCollector : public Collector {
public:
Expected<void> send(std::vector<std::unique_ptr<SpanData>>&&,
const std::shared_ptr<TraceSampler>&) override {
const std::shared_ptr<ErasedTraceSampler>&) override {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion include/datadog/sampling_mechanism.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum class SamplingMechanism {
// The sampling decision was made explicitly by the user, who set a sampling
// priority.
MANUAL = 4,
// Reserved for future use.
// Trace was kept because of AppSec event.
APP_SEC = 5,
// Reserved for future use.
REMOTE_RATE_USER_DEFINED = 6,
Expand Down
8 changes: 5 additions & 3 deletions include/datadog/trace_segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ class Logger;
struct SpanData;
struct SpanDefaults;
class SpanSampler;
class TraceSampler;
class ErasedTraceSampler;
class ConfigManager;

class TraceSegment {
mutable std::mutex mutex_;

std::shared_ptr<Logger> logger_;
std::shared_ptr<Collector> collector_;
std::shared_ptr<TraceSampler> trace_sampler_;
std::shared_ptr<ErasedTraceSampler> trace_sampler_;
bool apm_tracing_enabled_;
std::shared_ptr<SpanSampler> span_sampler_;

std::shared_ptr<const SpanDefaults> defaults_;
Expand All @@ -83,7 +84,8 @@ class TraceSegment {
public:
TraceSegment(const std::shared_ptr<Logger>& logger,
const std::shared_ptr<Collector>& collector,
const std::shared_ptr<TraceSampler>& trace_sampler,
std::shared_ptr<ErasedTraceSampler> trace_sampler,
bool apm_tracing_enabled,
const std::shared_ptr<SpanSampler>& span_sampler,
const std::shared_ptr<const SpanDefaults>& defaults,
const std::shared_ptr<ConfigManager>& config_manager,
Expand Down
8 changes: 7 additions & 1 deletion include/datadog/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace tracing {
class ConfigManager;
class DictReader;
struct SpanConfig;
class TraceSampler;
class ErasedTraceSampler;
class SpanSampler;
class IDGenerator;
class InMemoryFile;
Expand All @@ -39,6 +39,8 @@ class Tracer {
RuntimeID runtime_id_;
TracerSignature signature_;
std::shared_ptr<ConfigManager> config_manager_;
std::shared_ptr<ErasedTraceSampler>
apm_tracing_disabled_sampler_; // empty if enabled
std::shared_ptr<Collector> collector_;
std::shared_ptr<SpanSampler> span_sampler_;
std::shared_ptr<const IDGenerator> generator_;
Expand Down Expand Up @@ -104,6 +106,10 @@ class Tracer {
// same JSON object that was logged when this Tracer was created.
std::string config() const;

bool is_apm_tracing_enabled() const {
return apm_tracing_disabled_sampler_ == nullptr;
}

private:
void store_config();
};
Expand Down
12 changes: 12 additions & 0 deletions include/datadog/tracer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Collector;
class Logger;
class SpanSampler;
class TraceSampler;
class ErasedTraceSampler;

struct TracerConfig {
// Set the service name.
Expand Down Expand Up @@ -80,6 +81,16 @@ struct TracerConfig {
// `telemetry/configuration.h` By default, the telemetry module is enabled.
telemetry::Configuration telemetry;

// `apm_tracing_enabled` indicates whether APM traces and APM trace metrics
// are enabled. If `false`, APM-specific traces are dropped and APM trace
// metrics are not computed. This allows other products (e.g., AppSec) to
// operate independently.
// This is distinct from `report_traces`, which controls whether any traces
// are sent at all.
// `apm_tracing_enabled` is overridden by the `DD_APM_TRACING_ENABLED`
// environment variable. Defaults to `true`.
Optional<bool> apm_tracing_enabled;

// `trace_sampler` configures trace sampling. Trace sampling determines which
// traces are sent to Datadog. See `trace_sampler_config.h`.
TraceSamplerConfig trace_sampler;
Expand Down Expand Up @@ -197,6 +208,7 @@ class FinalizedTracerConfig final {
std::shared_ptr<Logger> logger;
bool log_on_startup;
bool generate_128bit_trace_ids;
bool apm_tracing_enabled;
Optional<RuntimeID> runtime_id;
Clock clock;
std::string integration_name;
Expand Down
6 changes: 4 additions & 2 deletions src/datadog/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ ConfigManager::ConfigManager(const FinalizedTracerConfig& config)
default_metadata_(config.metadata),
trace_sampler_(
std::make_shared<TraceSampler>(config.trace_sampler, clock_)),
erased_trace_sampler_(
std::make_shared<ErasedTraceSampler>(trace_sampler_)),
rules_(config.trace_sampler.rules),
span_defaults_(std::make_shared<SpanDefaults>(config.defaults)),
report_traces_(config.report_traces) {}
Expand Down Expand Up @@ -163,9 +165,9 @@ void ConfigManager::on_revert(const Configuration&) {
telemetry::capture_configuration_change(config_metadata);
}

std::shared_ptr<TraceSampler> ConfigManager::trace_sampler() {
std::shared_ptr<ErasedTraceSampler> ConfigManager::trace_sampler() {
std::lock_guard<std::mutex> lock(mutex_);
return trace_sampler_;
return erased_trace_sampler_;
}

std::shared_ptr<const SpanDefaults> ConfigManager::span_defaults() {
Expand Down
5 changes: 4 additions & 1 deletion src/datadog/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class ConfigManager : public remote_config::Listener {
std::unordered_map<ConfigName, ConfigMetadata> default_metadata_;

std::shared_ptr<TraceSampler> trace_sampler_;
// wraps trace_sampler_ and should be changed if trace_sampler_ is changed
// Could be created every time, but that would be a waste
std::shared_ptr<ErasedTraceSampler> erased_trace_sampler_;
std::vector<TraceSamplerRule> rules_;

DynamicConfig<std::shared_ptr<const SpanDefaults>> span_defaults_;
Expand All @@ -93,7 +96,7 @@ class ConfigManager : public remote_config::Listener {
void on_post_process() override{};

// Return the `TraceSampler` consistent with the most recent configuration.
std::shared_ptr<TraceSampler> trace_sampler();
std::shared_ptr<ErasedTraceSampler> trace_sampler();

// Return the `SpanDefaults` consistent with the most recent configuration.
std::shared_ptr<const SpanDefaults> span_defaults();
Expand Down
10 changes: 7 additions & 3 deletions src/datadog/datadog_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ DatadogAgent::DatadogAgent(
flush_interval_(config.flush_interval),
request_timeout_(config.request_timeout),
shutdown_timeout_(config.shutdown_timeout),
remote_config_(tracer_signature, rc_listeners, logger) {
remote_config_(tracer_signature, rc_listeners, logger),
apm_tracing_enabled_(config.apm_tracing_enabled) {
assert(logger_);

// Set HTTP headers
Expand Down Expand Up @@ -211,7 +212,7 @@ DatadogAgent::~DatadogAgent() {

Expected<void> DatadogAgent::send(
std::vector<std::unique_ptr<SpanData>>&& spans,
const std::shared_ptr<TraceSampler>& response_handler) {
const std::shared_ptr<ErasedTraceSampler>& response_handler) {
std::lock_guard<std::mutex> lock(mutex_);
trace_chunks_.push_back(TraceChunk{std::move(spans), response_handler});
return nullopt;
Expand Down Expand Up @@ -272,7 +273,7 @@ void DatadogAgent::flush() {
// One HTTP request to the Agent could possibly involve trace chunks from
// multiple tracers, and thus multiple trace samplers might need to have
// their rates updated. Unlikely, but possible.
std::unordered_set<std::shared_ptr<TraceSampler>> response_handlers;
std::unordered_set<std::shared_ptr<ErasedTraceSampler>> response_handlers;
for (auto& chunk : trace_chunks) {
response_handlers.insert(std::move(chunk.response_handler));
}
Expand All @@ -284,6 +285,9 @@ void DatadogAgent::flush() {
for (const auto& [key, value] : headers_) {
writer.set(key, value);
}
if (!apm_tracing_enabled_) {
writer.set("Datadog-Client-Computed-Stats", "yes");
}
};

// This is the callback for the HTTP response. It's invoked
Expand Down
7 changes: 4 additions & 3 deletions src/datadog/datadog_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
namespace datadog {
namespace tracing {

class ErasedTraceSampler;
class FinalizedDatadogAgentConfig;
class Logger;
struct SpanData;
class TraceSampler;
struct TracerSignature;

class DatadogAgent : public Collector {
public:
struct TraceChunk {
std::vector<std::unique_ptr<SpanData>> spans;
std::shared_ptr<TraceSampler> response_handler;
std::shared_ptr<ErasedTraceSampler> response_handler;
};

private:
Expand All @@ -51,6 +51,7 @@ class DatadogAgent : public Collector {
remote_config::Manager remote_config_;

std::unordered_map<std::string, std::string> headers_;
bool apm_tracing_enabled_;

void flush();

Expand All @@ -63,7 +64,7 @@ class DatadogAgent : public Collector {

Expected<void> send(
std::vector<std::unique_ptr<SpanData>>&& spans,
const std::shared_ptr<TraceSampler>& response_handler) override;
const std::shared_ptr<ErasedTraceSampler>& response_handler) override;

void get_and_apply_remote_configuration_updates();

Expand Down
7 changes: 7 additions & 0 deletions src/datadog/datadog_agent_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Expected<DatadogAgentConfig> load_datadog_agent_env_config() {
env_config.remote_configuration_poll_interval_seconds = *res;
}

if (auto apm_enabled_env = lookup(environment::DD_APM_TRACING_ENABLED)) {
env_config.apm_tracing_enabled = !falsy(*apm_enabled_env);
}

auto env_host = lookup(environment::DD_AGENT_HOST);
auto env_port = lookup(environment::DD_TRACE_AGENT_PORT);

Expand Down Expand Up @@ -135,6 +139,9 @@ Expected<FinalizedDatadogAgentConfig> finalize_config(
value_or(env_config->remote_configuration_enabled,
user_config.remote_configuration_enabled, true);

result.apm_tracing_enabled = value_or(env_config->apm_tracing_enabled,
user_config.apm_tracing_enabled, true);

const auto [origin, url] =
pick(env_config->url, user_config.url, "http://localhost:8126");
auto parsed_url = HTTPClient::URL::parse(url);
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/extraction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void handle_trace_tags(StringView trace_tags, ExtractedData& result,
}

for (auto& [key, value] : *maybe_trace_tags) {
if (!starts_with(key, "_dd.p.")) {
if (!starts_with(key, "_dd.p.") && key != "_dd.p.ts") {
continue;
}

Expand Down
2 changes: 2 additions & 0 deletions src/datadog/tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const std::string language = "language";
const std::string runtime_id = "runtime-id";
const std::string sampling_decider = "_dd.is_sampling_decider";
const std::string w3c_parent_id = "_dd.parent_id";
const std::string trace_source = "_dd.p.ts";
const std::string apm_enabled = "_dd.apm.enabled";

} // namespace internal

Expand Down
2 changes: 2 additions & 0 deletions src/datadog/tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern const std::string language;
extern const std::string runtime_id;
extern const std::string sampling_decider;
extern const std::string w3c_parent_id;
extern const std::string trace_source; // _dd.p.ts
extern const std::string apm_enabled; // _dd.apm.enabled
} // namespace internal

// Return whether the specified `tag_name` is reserved for use internal to this
Expand Down
2 changes: 2 additions & 0 deletions src/datadog/telemetry/telemetry_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ std::string to_string(datadog::tracing::ConfigName name) {
return "trace_baggage_max_bytes";
case ConfigName::TRACE_BAGGAGE_MAX_ITEMS:
return "trace_baggage_max_items";
case ConfigName::APM_TRACING_ENABLED:
return "apm_tracing_enabled";
}

std::abort();
Expand Down
Loading