Skip to content

Commit d3808ef

Browse files
committed
print config as JSON on Tracer startup
1 parent 8a9375c commit d3808ef

27 files changed

+259
-30
lines changed

doc/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ Code Component Relationships
1414
----------------------------
1515
![another diagram](includes.svg)
1616

17-
Example Usage
18-
-------------
19-
TODO
20-
2117
Objects
2218
-------
2319
- _Span_ has a beginning, end, and tags. It is associated with a _TraceSegment_.

src/datadog/collector.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <vector>
1616

1717
#include "expected.h"
18+
#include "json_fwd.hpp"
1819

1920
namespace datadog {
2021
namespace tracing {
@@ -32,6 +33,16 @@ class Collector {
3233
std::vector<std::unique_ptr<SpanData>>&& spans,
3334
const std::shared_ptr<TraceSampler>& response_handler) = 0;
3435

36+
// Assign to the specified `destination` a JSON representation of this
37+
// object's configuration. The JSON representation is an object with
38+
// the following properties:
39+
//
40+
// - "type" is the unmangled, unqualified name of the most-derived class, e.g.
41+
// "DatadogAgent".
42+
// - "config" is an object containing this object's configuration. "config"
43+
// may be omitted if the derived class has no configuration.
44+
virtual void config_json(nlohmann::json& destination) const = 0;
45+
3546
virtual ~Collector() {}
3647
};
3748

src/datadog/datadog_agent.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cassert>
44
#include <chrono>
55
#include <string>
6+
#include <typeinfo>
67
#include <unordered_map>
78
#include <unordered_set>
89

@@ -142,7 +143,8 @@ DatadogAgent::DatadogAgent(const FinalizedDatadogAgentConfig& config,
142143
http_client_(config.http_client),
143144
event_scheduler_(config.event_scheduler),
144145
cancel_scheduled_flush_(event_scheduler_->schedule_recurring_event(
145-
config.flush_interval, [this]() { flush(); })) {
146+
config.flush_interval, [this]() { flush(); })),
147+
flush_interval_(config.flush_interval) {
146148
assert(logger_);
147149
}
148150

@@ -162,6 +164,25 @@ Expected<void> DatadogAgent::send(
162164
return std::nullopt;
163165
}
164166

167+
void DatadogAgent::config_json(nlohmann::json& destination) const {
168+
const auto& url = traces_endpoint_; // brevity
169+
const auto flush_interval_milliseconds =
170+
std::chrono::duration_cast<std::chrono::milliseconds>(flush_interval_)
171+
.count();
172+
173+
// clang-format off
174+
destination = nlohmann::json::object({
175+
{"type", "DatadogAgent"},
176+
{"config", nlohmann::json::object({
177+
{"url", (url.scheme + "://" + url.authority + url.path)},
178+
{"flush_interval_milliseconds", flush_interval_milliseconds},
179+
{"http_client_typeid", typeid(*http_client_).name()},
180+
{"event_scheduler_typeid", typeid(*event_scheduler_).name()},
181+
})},
182+
});
183+
// clang-format on
184+
}
185+
165186
void DatadogAgent::flush() {
166187
outgoing_trace_chunks_.clear();
167188
{

src/datadog/datadog_agent.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class DatadogAgent : public Collector {
4242
std::shared_ptr<HTTPClient> http_client_;
4343
std::shared_ptr<EventScheduler> event_scheduler_;
4444
EventScheduler::Cancel cancel_scheduled_flush_;
45+
std::chrono::steady_clock::duration flush_interval_;
4546

4647
void flush();
4748

@@ -50,9 +51,11 @@ class DatadogAgent : public Collector {
5051
const std::shared_ptr<Logger>&);
5152
~DatadogAgent();
5253

53-
virtual Expected<void> send(
54+
Expected<void> send(
5455
std::vector<std::unique_ptr<SpanData>>&& spans,
5556
const std::shared_ptr<TraceSampler>& response_handler) override;
57+
58+
void config_json(nlohmann::json& destination) const override;
5659
};
5760

5861
} // namespace tracing

src/datadog/environment.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <cstdlib>
44

5+
#include "json.hpp"
6+
57
namespace datadog {
68
namespace tracing {
79
namespace environment {
@@ -17,6 +19,15 @@ std::optional<std::string_view> lookup(Variable variable) {
1719
return std::string_view{value};
1820
}
1921

22+
void environment_to_json(nlohmann::json &destination) {
23+
destination = nlohmann::json::object({});
24+
for (const char *name : variable_names) {
25+
if (const char *value = std::getenv(name)) {
26+
destination[name] = value;
27+
}
28+
}
29+
}
30+
2031
} // namespace environment
2132
} // namespace tracing
2233
} // namespace datadog

src/datadog/environment.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <optional>
1818
#include <string_view>
1919

20+
#include "json_fwd.hpp"
21+
2022
namespace datadog {
2123
namespace tracing {
2224
namespace environment {
@@ -54,7 +56,7 @@ enum Variable { LIST_ENVIRONMENT_VARIABLES(WITH_COMMA) };
5456

5557
#define QUOTED_WITH_COMMA(ARG) WITH_COMMA(QUOTED(ARG))
5658

57-
inline const char *const variable_names[] = {
59+
inline const char* const variable_names[] = {
5860
LIST_ENVIRONMENT_VARIABLES(QUOTED_WITH_COMMA)};
5961

6062
#undef QUOTED_WITH_COMMA
@@ -70,6 +72,8 @@ std::string_view name(Variable variable);
7072
// `std::nullptr` if that variable is not set in the environment.
7173
std::optional<std::string_view> lookup(Variable variable);
7274

75+
void environment_to_json(nlohmann::json& destination);
76+
7377
} // namespace environment
7478
} // namespace tracing
7579
} // namespace datadog

src/datadog/logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//
66
// Errors, when they occur, are typically returned as `Error` values (often as
77
// part of an `Expected` value). However, in "async" contexts where there is
8-
// nowhere to return a value, the logger is used instead.
8+
// nowhere to return a value, the logger is used instead.
99
//
1010
// `Logger`'s pure virtual member functions accept a callback function that is
1111
// either invoked immediately or not invoked at all, depending on the

src/datadog/null_collector.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11
#include "null_collector.h"
2+
3+
#include "json.hpp"
4+
5+
namespace datadog {
6+
namespace tracing {
7+
8+
void NullCollector::config_json(nlohmann::json& destination) const {
9+
// clang-format off
10+
destination = nlohmann::json::object({
11+
{"type", "NullCollector"},
12+
{"config", nlohmann::json::object({})},
13+
});
14+
// clang-format on
15+
}
16+
17+
} // namespace tracing
18+
} // namespace datadog

src/datadog/null_collector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class NullCollector : public Collector {
1515
const std::shared_ptr<TraceSampler>&) override {
1616
return {};
1717
}
18+
19+
void config_json(nlohmann::json& destination) const override;
1820
};
1921

2022
} // namespace tracing

src/datadog/parse_util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ std::string_view strip(std::string_view input);
2727
Expected<std::uint64_t> parse_uint64(std::string_view input, int base);
2828
Expected<int> parse_int(std::string_view input, int base);
2929

30-
// Return a floating point number parsed from the specified `input`, or return an
31-
// `Error` if not such number can be parsed. It is an error unless all of
30+
// Return a floating point number parsed from the specified `input`, or return
31+
// an `Error` if not such number can be parsed. It is an error unless all of
3232
// `input` is consumed by the parse. Leading and trailing whitespace are not
3333
// ignored.
3434
Expected<double> parse_double(std::string_view input);

0 commit comments

Comments
 (0)