Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/datadog/null_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class NullCollector : public Collector {
std::string config() const override {
// clang-format off
return R"({
{"type", "datadog::tracing::NullCollector"},
{"config", {}}
"type": "datadog::tracing::NullCollector",
"config": {}
})";
// clang-format on
};
Expand Down
15 changes: 15 additions & 0 deletions src/datadog/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ ConfigManager::Update parse_dynamic_config(const nlohmann::json& j) {
namespace rc = datadog::remote_config;

ConfigManager::ConfigManager(const FinalizedTracerConfig& config,
const TracerSignature& tracer_signature,
const std::shared_ptr<TracerTelemetry>& telemetry)
: clock_(config.clock),
default_metadata_(config.metadata),
Expand All @@ -117,6 +118,7 @@ ConfigManager::ConfigManager(const FinalizedTracerConfig& config,
rules_(config.trace_sampler.rules),
span_defaults_(std::make_shared<SpanDefaults>(config.defaults)),
report_traces_(config.report_traces),
tracer_signature_(tracer_signature),
telemetry_(telemetry) {}

rc::Products ConfigManager::get_products() { return rc::product::APM_TRACING; }
Expand All @@ -128,7 +130,20 @@ rc::Capabilities ConfigManager::get_capabilities() {
}

Optional<std::string> ConfigManager::on_update(const Configuration& config) {
if (config.product != rc::product::Flag::APM_TRACING) {
return nullopt;
}

const auto config_json = nlohmann::json::parse(config.content);

const auto& targeted_service = config_json.at("service_target");
if (targeted_service.at("service").get<StringView>() !=
tracer_signature_.default_service ||
targeted_service.at("env").get<StringView>() !=
tracer_signature_.default_environment) {
return "Wrong service targeted";
}

auto config_update = parse_dynamic_config(config_json.at("lib_config"));

auto config_metadata = apply_update(config_update);
Expand Down
2 changes: 2 additions & 0 deletions src/datadog/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ConfigManager : public remote_config::Listener {
DynamicConfig<std::shared_ptr<const SpanDefaults>> span_defaults_;
DynamicConfig<bool> report_traces_;

const TracerSignature& tracer_signature_;
std::shared_ptr<TracerTelemetry> telemetry_;

private:
Expand All @@ -85,6 +86,7 @@ class ConfigManager : public remote_config::Listener {

public:
ConfigManager(const FinalizedTracerConfig& config,
const TracerSignature& signature,
const std::shared_ptr<TracerTelemetry>& telemetry);
~ConfigManager() override{};

Expand Down
4 changes: 2 additions & 2 deletions src/datadog/datadog_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ std::string DatadogAgent::config() const {
{"flush_interval_milliseconds", std::chrono::duration_cast<std::chrono::milliseconds>(flush_interval_).count() },
{"request_timeout_milliseconds", std::chrono::duration_cast<std::chrono::milliseconds>(request_timeout_).count() },
{"shutdown_timeout_milliseconds", std::chrono::duration_cast<std::chrono::milliseconds>(shutdown_timeout_).count() },
{"http_client", http_client_->config()},
{"event_scheduler", event_scheduler_->config()},
{"http_client", nlohmann::json::parse(http_client_->config())},
{"event_scheduler", nlohmann::json::parse(event_scheduler_->config())},
})},
}).dump();
// clang-format on
Expand Down
64 changes: 31 additions & 33 deletions src/datadog/remote_config/remote_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ constexpr std::array<uint8_t, sizeof(uint64_t)> capabilities_byte_array(
return res;
}

Optional<product::Flag> parse_config_path(StringView config_path) {
struct ConfigKeyMetadata final {
product::Flag product;
StringView config_id;
};

Optional<ConfigKeyMetadata> parse_config_path(StringView config_path) {
static const std::regex path_reg(
"^(datadog/\\d+|employee)/([^/]+)/[^/]+/[^/]+$");
"^(datadog/\\d+|employee)/([^/]+)/([^/]+)/[^/]+$");

std::cmatch match;
if (!std::regex_match(config_path.data(),
Expand All @@ -42,12 +47,14 @@ Optional<product::Flag> parse_config_path(StringView config_path) {
}

assert(match.ready());
assert(match.size() == 3);
assert(match.size() == 4);

StringView product_sv(config_path.data() + match.position(2),
std::size_t(match.length(2)));
StringView config_id_sv{config_path.data() + match.position(3),
static_cast<std::size_t>(match.length(3))};

return parse_product(product_sv);
return {{parse_product(product_sv), config_id_sv}};
}

} // namespace
Expand Down Expand Up @@ -199,15 +206,17 @@ void Manager::process_response(const nlohmann::json& json) {
auto config_path = client_config.get<StringView>();
visited_config.emplace(config_path);

const auto product = parse_config_path(config_path);
if (!product) {
const auto config_key_metadata = parse_config_path(config_path);
if (!config_key_metadata) {
std::string reason{config_path};
reason += " is an invalid configuration path";

error(reason);
return;
}

const auto product = config_key_metadata->product;

const auto& config_metadata =
targets.at("/signed/targets"_json_pointer).at(config_path);

Expand All @@ -231,38 +240,27 @@ void Manager::process_response(const nlohmann::json& json) {
return;
}

auto decoded_config =
base64_decode(target_it.value().at("raw").get<StringView>());

const auto config_json = nlohmann::json::parse(decoded_config);
auto raw_data = target_it->at("raw").get<StringView>();
auto decoded_config = base64_decode(raw_data);

Configuration new_config;
new_config.id = config_json.at("id");
new_config.id = std::string{config_key_metadata->config_id};
new_config.path = std::string{config_path};
new_config.hash = config_metadata.at("/hashes/sha256"_json_pointer);
new_config.content = std::move(decoded_config);
new_config.version = config_json.at("revision");
new_config.product = *product;

const auto& targeted_service = config_json.at("service_target");
if (targeted_service.at("service").get<StringView>() !=
tracer_signature_.default_service ||
targeted_service.at("env").get<StringView>() !=
tracer_signature_.default_environment) {
new_config.state = Configuration::State::error;
new_config.error_message = "Wrong service targeted";
} else {
for (const auto& listener : listeners_per_product_[*product]) {
// Q: Two listeners on the same product. What should be the behaviour
// if one of the listeners report an error?
// R(@dmehala): Unspecified. For now, the config is marked with an
// error.
if (auto error_msg = listener->on_update(new_config)) {
new_config.state = Configuration::State::error;
new_config.error_message = std::move(*error_msg);
} else {
new_config.state = Configuration::State::acknowledged;
}
new_config.version = config_metadata.at("/custom/v"_json_pointer);
new_config.product = product;

for (const auto& listener : listeners_per_product_[product]) {
// Q: Two listeners on the same product. What should be the behaviour
// if one of the listeners report an error?
// R(@dmehala): Unspecified. For now, the config is marked with an
// error.
if (auto error_msg = listener->on_update(new_config)) {
new_config.state = Configuration::State::error;
new_config.error_message = std::move(*error_msg);
} else {
new_config.state = Configuration::State::acknowledged;
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/datadog/span_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ nlohmann::json SpanSampler::config_json() const {
}

return nlohmann::json::object({
{"rules", rules},
})
.dump();
{"rules", rules},
});
}

} // namespace tracing
Expand Down
14 changes: 9 additions & 5 deletions src/datadog/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
namespace datadog {
namespace tracing {

void to_json(nlohmann::json& j, const PropagationStyle& style) {
j = to_string_view(style);
}

Tracer::Tracer(const FinalizedTracerConfig& config)
: Tracer(config, default_id_generator(config.generate_128bit_trace_ids)) {}

Expand All @@ -43,8 +47,8 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
tracer_telemetry_(std::make_shared<TracerTelemetry>(
config.report_telemetry, config.clock, logger_, signature_,
config.integration_name, config.integration_version)),
config_manager_(
std::make_shared<ConfigManager>(config, tracer_telemetry_)),
config_manager_(std::make_shared<ConfigManager>(config, signature_,
tracer_telemetry_)),
collector_(/* see constructor body */),
span_sampler_(
std::make_shared<SpanSampler>(config.span_sampler, config.clock)),
Expand Down Expand Up @@ -88,10 +92,10 @@ std::string Tracer::config() const {
auto config = nlohmann::json::object({
{"version", tracer_version_string},
{"runtime_id", runtime_id_.string()},
{"collector", collector_->config()},
{"collector", nlohmann::json::parse(collector_->config())},
{"span_sampler", span_sampler_->config_json()},
{"injection_styles", join_propagation_styles(injection_styles_)},
{"extraction_styles", join_propagation_styles(extraction_styles_)},
{"injection_styles", injection_styles_},
{"extraction_styles", extraction_styles_},
{"tags_header_size", tags_header_max_size_},
{"environment_variables", environment::to_json()},
});
Expand Down
Loading