Skip to content

Commit 3444245

Browse files
committed
Code review 2
1 parent f105c8f commit 3444245

17 files changed

+148
-72
lines changed

BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ cc_library(
102102
"src/datadog/tags.h",
103103
"src/datadog/threaded_event_scheduler.h",
104104
"src/datadog/tracer_config.h",
105+
"src/datadog/tracer_signature.h",
105106
"src/datadog/tracer_telemetry.h",
106107
"src/datadog/tracer.h",
107108
"src/datadog/trace_id.h",
108-
"src/datadog/tracer_id.h",
109109
"src/datadog/trace_sampler_config.h",
110110
"src/datadog/trace_sampler.h",
111111
"src/datadog/trace_segment.h",

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ target_sources(dd_trace_cpp-objects PUBLIC
208208
src/datadog/tags.h
209209
src/datadog/threaded_event_scheduler.h
210210
src/datadog/tracer_config.h
211+
src/datadog/tracer_signature.h
211212
src/datadog/tracer_telemetry.h
212213
src/datadog/tracer.h
213214
src/datadog/trace_id.h
214-
src/datadog/tracer_id.h
215215
src/datadog/trace_sampler_config.h
216216
src/datadog/trace_sampler.h
217217
src/datadog/trace_segment.h

src/datadog/config_manager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "config_manager.h"
22

3+
#include "trace_sampler.h"
4+
35
namespace datadog {
46
namespace tracing {
57

src/datadog/config_manager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3-
// TODO: Document
3+
// The `ConfigManager` class is designed to handle configuration update
4+
// and provide access to the current configuration.
5+
// It utilizes a mutex to ensure thread safety when updating or accessing
6+
// the configuration.
47

58
#include <mutex>
69

src/datadog/config_update.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
#pragma once
2+
13
#include "optional"
2-
#include "trace_sampler.h"
4+
#include "trace_sampler_config.h"
35

46
namespace datadog {
57
namespace tracing {
68

7-
// TODO: Document
9+
// The `ConfigUpdate` struct serves as a container for configuration that can
10+
// exclusively be changed remotely.
11+
//
12+
// Configurations can be `nullopt` to signal the absence of a value from the
13+
// remote configuration value.
814
struct ConfigUpdate {
915
Optional<TraceSamplerConfig> trace_sampler;
1016
};

src/datadog/datadog_agent.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ std::variant<CollectorResponse, std::string> parse_agent_traces_response(
149149
DatadogAgent::DatadogAgent(
150150
const FinalizedDatadogAgentConfig& config,
151151
const std::shared_ptr<TracerTelemetry>& tracer_telemetry,
152-
const std::shared_ptr<Logger>& logger, const TracerID& tracer_id,
153-
ConfigManager& config_manager)
152+
const std::shared_ptr<Logger>& logger,
153+
const TracerSignature& tracer_signature, ConfigManager& config_manager)
154154
: tracer_telemetry_(tracer_telemetry),
155155
clock_(config.clock),
156156
logger_(logger),
@@ -164,7 +164,7 @@ DatadogAgent::DatadogAgent(
164164
flush_interval_(config.flush_interval),
165165
request_timeout_(config.request_timeout),
166166
shutdown_timeout_(config.shutdown_timeout),
167-
remote_config_(tracer_id, config_manager) {
167+
remote_config_(tracer_signature, config_manager) {
168168
assert(logger_);
169169
assert(tracer_telemetry_);
170170
if (tracer_telemetry_->enabled()) {

src/datadog/datadog_agent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FinalizedDatadogAgentConfig;
2626
class Logger;
2727
struct SpanData;
2828
class TraceSampler;
29-
struct TracerID;
29+
struct TracerSignature;
3030

3131
class DatadogAgent : public Collector {
3232
public:
@@ -65,7 +65,7 @@ class DatadogAgent : public Collector {
6565
public:
6666
DatadogAgent(const FinalizedDatadogAgentConfig&,
6767
const std::shared_ptr<TracerTelemetry>&,
68-
const std::shared_ptr<Logger>&, const TracerID& id,
68+
const std::shared_ptr<Logger>&, const TracerSignature& id,
6969
ConfigManager& config_manager);
7070
~DatadogAgent();
7171

src/datadog/remote_config.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ ConfigUpdate parse_dynamic_config(const nlohmann::json& j) {
6464
} // namespace
6565

6666
RemoteConfigurationManager::RemoteConfigurationManager(
67-
const TracerID& tracer_id, ConfigManager& config_manager)
68-
: tracer_id_(tracer_id),
67+
const TracerSignature& tracer_signature, ConfigManager& config_manager)
68+
: tracer_signature_(tracer_signature),
6969
config_manager_(config_manager),
7070
client_id_(uuid()) {}
7171

@@ -87,11 +87,11 @@ nlohmann::json RemoteConfigurationManager::make_request_payload() {
8787
{"is_tracer", true},
8888
{"capabilities", k_apm_capabilities},
8989
{"client_tracer", {
90-
{"runtime_id", tracer_id_.runtime_id.string()},
91-
{"language", "cpp"},
92-
{"tracer_version", tracer_version},
93-
{"service", tracer_id_.service},
94-
{"env", tracer_id_.environment}
90+
{"runtime_id", tracer_signature_.runtime_id.string()},
91+
{"language", tracer_signature_.library_language},
92+
{"tracer_version", tracer_signature_.library_version},
93+
{"service", tracer_signature_.default_service},
94+
{"env", tracer_signature_.default_environment}
9595
}},
9696
{"state", {
9797
{"root_version", 1},
@@ -178,9 +178,9 @@ void RemoteConfigurationManager::process_response(const nlohmann::json& json) {
178178

179179
const auto& targeted_service = config_json.at("service_target");
180180
if (targeted_service.at("service").get<StringView>() !=
181-
tracer_id_.service ||
181+
tracer_signature_.default_service ||
182182
targeted_service.at("env").get<StringView>() !=
183-
tracer_id_.environment) {
183+
tracer_signature_.default_environment) {
184184
continue;
185185
}
186186

src/datadog/remote_config.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#pragma once
22

3-
// TODO: Document
3+
// Remote Configuration is a Datadog capability that allows a user to remotely
4+
// configure and change the behaviour of the tracing library.
5+
// The current implementation is restricted to Application Performance
6+
// Monitoring features.
7+
//
8+
// The `RemoteConfigurationManager` class implement the protocol to query,
9+
// process and verify configuration from a remote source. It is also
10+
// responsible for handling configuration updates received from a remote source
11+
// and maintains the state of applied configuration.
12+
// It interacts with the `ConfigManager` to seamlessly apply or revert
13+
// configurations based on responses received from the remote source.
414

515
#include <string>
616

@@ -10,44 +20,56 @@
1020
#include "runtime_id.h"
1121
#include "string_view.h"
1222
#include "trace_sampler_config.h"
13-
#include "tracer_id.h"
23+
#include "tracer_signature.h"
1424

1525
namespace datadog {
1626
namespace tracing {
1727

1828
class RemoteConfigurationManager {
29+
// Represents the *current* state of the RemoteConfigurationManager.
30+
// It is also used to report errors to the remote source.
1931
struct State {
2032
uint64_t targets_version = 1;
2133
std::string opaque_backend_state;
2234
Optional<std::string> error_message;
2335
};
2436

37+
// Holds information about a specific configuration update,
38+
// including its identifier, hash value, version number and the content.
2539
struct Configuration {
2640
std::string id;
2741
std::string hash;
2842
std::size_t version;
2943
ConfigUpdate content;
3044
};
3145

32-
TracerID tracer_id_;
46+
TracerSignature tracer_signature_;
3347
ConfigManager& config_manager_;
34-
std::string client_id_;
48+
std::string client_id_; ///< Identifier a `RemoteConfigurationManager`
3549

3650
State state_;
3751
std::unordered_map<std::string, Configuration> applied_config_;
3852

3953
public:
40-
RemoteConfigurationManager(const TracerID& tracer_id,
54+
RemoteConfigurationManager(const TracerSignature& tracer_signature,
4155
ConfigManager& config_manager);
4256

57+
// Construct a JSON object representing the payload to be sent in a remote
58+
// configuration request.
4359
nlohmann::json make_request_payload();
4460

61+
// Handles the response received from a remote source and udates the internal
62+
// state accordingly.
4563
void process_response(const nlohmann::json& json);
4664

4765
private:
66+
// Tell if a `config_path` is a new configuration update.
4867
bool is_new_config(StringView config_path, const nlohmann::json& config_meta);
4968

69+
// Apply a remote configuration.
5070
void apply_config(Configuration config);
71+
72+
// Revert a remote configuration.
5173
void revert_config(Configuration config);
5274
};
5375

src/datadog/tracer.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "tags.h"
2424
#include "trace_sampler.h"
2525
#include "trace_segment.h"
26-
#include "tracer_id.h"
26+
#include "tracer_signature.h"
2727
#include "version.h"
2828
#include "w3c_propagation.h"
2929

@@ -40,9 +40,10 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
4040
defaults_(std::make_shared<SpanDefaults>(config.defaults)),
4141
runtime_id_(config.runtime_id ? *config.runtime_id
4242
: RuntimeID::generate()),
43-
id_{runtime_id_, config.defaults.service, config.defaults.environment},
43+
signature_{runtime_id_, config.defaults.service,
44+
config.defaults.environment},
4445
tracer_telemetry_(std::make_shared<TracerTelemetry>(
45-
config.report_telemetry, config.clock, logger_, id_)),
46+
config.report_telemetry, config.clock, logger_, signature_)),
4647
span_sampler_(
4748
std::make_shared<SpanSampler>(config.span_sampler, config.clock)),
4849
generator_(generator),
@@ -59,8 +60,9 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
5960
auto& agent_config =
6061
std::get<FinalizedDatadogAgentConfig>(config.collector);
6162

62-
auto agent = std::make_shared<DatadogAgent>(
63-
agent_config, tracer_telemetry_, config.logger, id_, config_manager_);
63+
auto agent = std::make_shared<DatadogAgent>(agent_config, tracer_telemetry_,
64+
config.logger, signature_,
65+
config_manager_);
6466
collector_ = agent;
6567

6668
if (tracer_telemetry_->enabled()) {

src/datadog/tracer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "optional.h"
2020
#include "span.h"
2121
#include "tracer_config.h"
22+
#include "tracer_signature.h"
2223
#include "tracer_telemetry.h"
2324

2425
namespace datadog {
@@ -28,14 +29,13 @@ class DictReader;
2829
struct SpanConfig;
2930
class TraceSampler;
3031
class SpanSampler;
31-
struct TracerID;
3232

3333
class Tracer {
3434
std::shared_ptr<Logger> logger_;
3535
std::shared_ptr<Collector> collector_;
3636
std::shared_ptr<const SpanDefaults> defaults_;
3737
RuntimeID runtime_id_;
38-
TracerID id_;
38+
TracerSignature signature_;
3939
std::shared_ptr<TracerTelemetry> tracer_telemetry_;
4040
std::shared_ptr<SpanSampler> span_sampler_;
4141
std::shared_ptr<const IDGenerator> generator_;

src/datadog/tracer_id.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/datadog/tracer_signature.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
// This component provides a class, `TracerSignature`, that contains the parts
4+
// of a tracer's configuration that are used to refer to the tracer in Datadog's
5+
// telemetry and remote configuration APIs.
6+
//
7+
// `TracerSignature` is used in three contexts:
8+
//
9+
// 1. When telemetry is sent to the Datadog Agent, the tracer signature is
10+
// included in the request payload. See
11+
// `TracerTelemetry::generate_telemetry_body` in `tracer_telemetry.cpp`.
12+
// 2. When the Datadog Agent is polled for configuration updates, part of the
13+
// tracer signature (all but the language version) is included in the request
14+
// payload. See `RemoteConfigurationManager::make_request_payload` in
15+
// `remote_config.h`.
16+
// 3. When the Datadog Agent responds with configuration updates, the service
17+
// and environment of the tracer signature are used to determine whether the
18+
// updates are relevant to the `Tracer` that created the collector that is
19+
// polling the Datadog Agent. See
20+
// `RemoteConfigurationManager::process_response` in `remote_config.h`.
21+
22+
#include <string>
23+
24+
#include "runtime_id.h"
25+
#include "string_view.h"
26+
#include "version.h"
27+
28+
#define DD_TRACE_STRINGIFY(ARG) DD_TRACE_STRINGIFY_HELPER(ARG)
29+
#define DD_TRACE_STRINGIFY_HELPER(ARG) #ARG
30+
31+
namespace datadog {
32+
namespace tracing {
33+
34+
struct TracerSignature {
35+
RuntimeID runtime_id;
36+
std::string default_service;
37+
std::string default_environment;
38+
std::string library_version;
39+
StringView library_language;
40+
StringView library_language_version;
41+
42+
TracerSignature() = delete;
43+
TracerSignature(RuntimeID id, std::string service, std::string environment)
44+
: runtime_id(id),
45+
default_service(std::move(service)),
46+
default_environment(std::move(environment)),
47+
library_version(tracer_version),
48+
library_language("cpp"),
49+
library_language_version(DD_TRACE_STRINGIFY(__cplusplus), 6) {}
50+
};
51+
52+
} // namespace tracing
53+
} // namespace datadog

src/datadog/tracer_telemetry.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace tracing {
1010

1111
TracerTelemetry::TracerTelemetry(bool enabled, const Clock& clock,
1212
const std::shared_ptr<Logger>& logger,
13-
const TracerID& tracer_id)
13+
const TracerSignature& tracer_signature)
1414
: enabled_(enabled),
1515
clock_(clock),
1616
logger_(logger),
17-
tracer_id_(tracer_id),
17+
tracer_signature_(tracer_signature),
1818
hostname_(get_hostname().value_or("hostname-unavailable")) {
1919
if (enabled_) {
2020
// Register all the metrics that we're tracking by adding them to the
@@ -62,15 +62,16 @@ nlohmann::json TracerTelemetry::generate_telemetry_body(
6262
{"seq_id", seq_id_},
6363
{"request_type", request_type},
6464
{"tracer_time", tracer_time},
65-
{"runtime_id", tracer_id_.runtime_id.string()},
65+
{"runtime_id", tracer_signature_.runtime_id.string()},
6666
{"debug", debug_},
67-
{"application", nlohmann::json::object({
68-
{"service_name", tracer_id_.service},
69-
{"env", tracer_id_.environment},
70-
{"tracer_version", tracer_version},
71-
{"language_name", "cpp"},
72-
{"language_version", std::to_string(__cplusplus)},
73-
})},
67+
{"application",
68+
nlohmann::json::object({
69+
{"service_name", tracer_signature_.default_service},
70+
{"env", tracer_signature_.default_environment},
71+
{"tracer_version", tracer_signature_.library_version},
72+
{"language_name", tracer_signature_.library_language},
73+
{"language_version", tracer_signature_.library_language_version},
74+
})},
7475
// TODO: host information (os, os_version, kernel, etc)
7576
{"host", nlohmann::json::object({
7677
{"hostname", hostname_},

0 commit comments

Comments
 (0)