Skip to content

Commit

Permalink
Merge pull request #4 from DataDog/david.goffredo/startup-banner
Browse files Browse the repository at this point in the history
print configuration as JSON on Tracer startup
  • Loading branch information
dgoffredo authored Oct 28, 2022
2 parents 32497d4 + cda2230 commit 51cd7ba
Show file tree
Hide file tree
Showing 28 changed files with 265 additions and 33 deletions.
4 changes: 0 additions & 4 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ Code Component Relationships
----------------------------
![another diagram](includes.svg)

Example Usage
-------------
TODO

Objects
-------
- _Span_ has a beginning, end, and tags. It is associated with a _TraceSegment_.
Expand Down
11 changes: 11 additions & 0 deletions src/datadog/collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>

#include "expected.h"
#include "json_fwd.hpp"

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

// Assign to the specified `destination` a JSON representation of this
// object's configuration. The JSON representation is an object with
// the following properties:
//
// - "type" is the unmangled, unqualified name of the most-derived class, e.g.
// "DatadogAgent".
// - "config" is an object containing this object's configuration. "config"
// may be omitted if the derived class has no configuration.
virtual void config_json(nlohmann::json& destination) const = 0;

virtual ~Collector() {}
};

Expand Down
23 changes: 22 additions & 1 deletion src/datadog/datadog_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cassert>
#include <chrono>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>

Expand Down Expand Up @@ -142,7 +143,8 @@ DatadogAgent::DatadogAgent(const FinalizedDatadogAgentConfig& config,
http_client_(config.http_client),
event_scheduler_(config.event_scheduler),
cancel_scheduled_flush_(event_scheduler_->schedule_recurring_event(
config.flush_interval, [this]() { flush(); })) {
config.flush_interval, [this]() { flush(); })),
flush_interval_(config.flush_interval) {
assert(logger_);
}

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

void DatadogAgent::config_json(nlohmann::json& destination) const {
const auto& url = traces_endpoint_; // brevity
const auto flush_interval_milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(flush_interval_)
.count();

// clang-format off
destination = nlohmann::json::object({
{"type", "DatadogAgent"},
{"config", nlohmann::json::object({
{"url", (url.scheme + "://" + url.authority + url.path)},
{"flush_interval_milliseconds", flush_interval_milliseconds},
{"http_client_typeid", typeid(*http_client_).name()},
{"event_scheduler_typeid", typeid(*event_scheduler_).name()},
})},
});
// clang-format on
}

void DatadogAgent::flush() {
outgoing_trace_chunks_.clear();
{
Expand Down
5 changes: 4 additions & 1 deletion src/datadog/datadog_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DatadogAgent : public Collector {
std::shared_ptr<HTTPClient> http_client_;
std::shared_ptr<EventScheduler> event_scheduler_;
EventScheduler::Cancel cancel_scheduled_flush_;
std::chrono::steady_clock::duration flush_interval_;

void flush();

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

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

void config_json(nlohmann::json& destination) const override;
};

} // namespace tracing
Expand Down
11 changes: 11 additions & 0 deletions src/datadog/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <cstdlib>

#include "json.hpp"

namespace datadog {
namespace tracing {
namespace environment {
Expand All @@ -17,6 +19,15 @@ std::optional<std::string_view> lookup(Variable variable) {
return std::string_view{value};
}

void to_json(nlohmann::json &destination) {
destination = nlohmann::json::object({});
for (const char *name : variable_names) {
if (const char *value = std::getenv(name)) {
destination[name] = value;
}
}
}

} // namespace environment
} // namespace tracing
} // namespace datadog
6 changes: 5 additions & 1 deletion src/datadog/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <optional>
#include <string_view>

#include "json_fwd.hpp"

namespace datadog {
namespace tracing {
namespace environment {
Expand Down Expand Up @@ -54,7 +56,7 @@ enum Variable { LIST_ENVIRONMENT_VARIABLES(WITH_COMMA) };

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

inline const char *const variable_names[] = {
inline const char* const variable_names[] = {
LIST_ENVIRONMENT_VARIABLES(QUOTED_WITH_COMMA)};

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

void to_json(nlohmann::json& destination);

} // namespace environment
} // namespace tracing
} // namespace datadog
2 changes: 1 addition & 1 deletion src/datadog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// Errors, when they occur, are typically returned as `Error` values (often as
// part of an `Expected` value). However, in "async" contexts where there is
// nowhere to return a value, the logger is used instead.
// nowhere to return a value, the logger is used instead.
//
// `Logger`'s pure virtual member functions accept a callback function that is
// either invoked immediately or not invoked at all, depending on the
Expand Down
17 changes: 17 additions & 0 deletions src/datadog/null_collector.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
#include "null_collector.h"

#include "json.hpp"

namespace datadog {
namespace tracing {

void NullCollector::config_json(nlohmann::json& destination) const {
// clang-format off
destination = nlohmann::json::object({
{"type", "NullCollector"},
{"config", nlohmann::json::object({})},
});
// clang-format on
}

} // namespace tracing
} // namespace datadog
2 changes: 2 additions & 0 deletions src/datadog/null_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class NullCollector : public Collector {
const std::shared_ptr<TraceSampler>&) override {
return {};
}

void config_json(nlohmann::json& destination) const override;
};

} // namespace tracing
Expand Down
4 changes: 2 additions & 2 deletions src/datadog/parse_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ std::string_view strip(std::string_view input);
Expected<std::uint64_t> parse_uint64(std::string_view input, int base);
Expected<int> parse_int(std::string_view input, int base);

// Return a floating point number parsed from the specified `input`, or return an
// `Error` if not such number can be parsed. It is an error unless all of
// Return a floating point number parsed from the specified `input`, or return
// an `Error` if not such number can be parsed. It is an error unless all of
// `input` is consumed by the parse. Leading and trailing whitespace are not
// ignored.
Expected<double> parse_double(std::string_view input);
Expand Down
22 changes: 22 additions & 0 deletions src/datadog/propagation_styles.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
#include "propagation_styles.h"

#include <string>
#include <vector>

#include "json.hpp"

namespace datadog {
namespace tracing {

void to_json(nlohmann::json& destination, const PropagationStyles& styles) {
std::vector<std::string> selected_names;
if (styles.datadog) {
selected_names.emplace_back("datadog");
}
if (styles.b3) {
selected_names.emplace_back("B3");
}
destination = selected_names;
}

} // namespace tracing
} // namespace datadog
4 changes: 4 additions & 0 deletions src/datadog/propagation_styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// has one `PropagationStyles` for extraction and another for injection. See
// `tracer_config.h`.

#include "json_fwd.hpp"

namespace datadog {
namespace tracing {

Expand All @@ -13,5 +15,7 @@ struct PropagationStyles {
bool b3 = false;
};

void to_json(nlohmann::json& destination, const PropagationStyles&);

} // namespace tracing
} // namespace datadog
15 changes: 15 additions & 0 deletions src/datadog/span_defaults.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "span_defaults.h"

#include "json.hpp"

namespace datadog {
namespace tracing {

Expand All @@ -10,5 +12,18 @@ bool operator==(const SpanDefaults& left, const SpanDefaults& right) {
#undef EQ
}

void to_json(nlohmann::json& destination, const SpanDefaults& defaults) {
destination = nlohmann::json::object({});
#define TO_JSON(FIELD) \
if (!defaults.FIELD.empty()) destination[#FIELD] = defaults.FIELD
TO_JSON(service);
TO_JSON(service_type);
TO_JSON(environment);
TO_JSON(version);
TO_JSON(name);
TO_JSON(tags);
#undef TO_JSON
}

} // namespace tracing
} // namespace datadog
4 changes: 4 additions & 0 deletions src/datadog/span_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string>
#include <unordered_map>

#include "json_fwd.hpp"

namespace datadog {
namespace tracing {

Expand All @@ -19,6 +21,8 @@ struct SpanDefaults {
std::unordered_map<std::string, std::string> tags;
};

void to_json(nlohmann::json& destination, const SpanDefaults&);

bool operator==(const SpanDefaults&, const SpanDefaults&);

} // namespace tracing
Expand Down
13 changes: 13 additions & 0 deletions src/datadog/span_sampler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "span_sampler.h"

#include "json.hpp"
#include "sampling_mechanism.h"
#include "sampling_priority.h"
#include "sampling_util.h"
Expand Down Expand Up @@ -65,5 +66,17 @@ SpanSampler::Rule* SpanSampler::match(const SpanData& span) {
return nullptr;
}

void SpanSampler::config_json(nlohmann::json& destination) const {
std::vector<nlohmann::json> rules;
for (const auto& rule : rules_) {
rules.emplace_back();
to_json(rules.back(), rule);
}

destination = nlohmann::json::object({
{"rules", rules},
});
}

} // namespace tracing
} // namespace datadog
3 changes: 3 additions & 0 deletions src/datadog/span_sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <mutex>

#include "clock.h"
#include "json_fwd.hpp"
#include "limiter.h"
#include "sampling_decision.h"
#include "span_sampler_config.h"
Expand Down Expand Up @@ -58,6 +59,8 @@ class SpanSampler {
// Return a pointer to the first `Rule` that the specified span matches, or
// return null if there is no match.
Rule* match(const SpanData&);

void config_json(nlohmann::json& destination) const;
};

} // namespace tracing
Expand Down
14 changes: 14 additions & 0 deletions src/datadog/span_sampler_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,19 @@ Expected<FinalizedSpanSamplerConfig> finalize_config(
return result;
}

void to_json(nlohmann::json &destination,
const FinalizedSpanSamplerConfig::Rule &rule) {
destination = nlohmann::json::object({
{"service", rule.service},
{"name", rule.name},
{"resource", rule.resource},
{"sample_rate", double(rule.sample_rate)},
});

if (rule.max_per_second) {
destination["max_per_second"] = *rule.max_per_second;
}
}

} // namespace tracing
} // namespace datadog
6 changes: 5 additions & 1 deletion src/datadog/span_sampler_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>

#include "expected.h"
#include "json_fwd.hpp"
#include "rate.h"
#include "span_matcher.h"

Expand All @@ -30,7 +31,7 @@ struct SpanSamplerConfig {

// Can be overriden by the `DD_TRACE_SAMPLING_RULES` environment variable.
// Also, the `DD_TRACE_SAMPLE_RATE` environment variable, if present, causes a
// corresponding `Rule` to be appended to `rules`.
// corresponding `Rule` to be appended to `rules`.
std::vector<Rule> rules;
};

Expand All @@ -53,5 +54,8 @@ class FinalizedSpanSamplerConfig {
Expected<FinalizedSpanSamplerConfig> finalize_config(const SpanSamplerConfig&,
Logger&);

void to_json(nlohmann::json& destination,
const FinalizedSpanSamplerConfig::Rule&);

} // namespace tracing
} // namespace datadog
Loading

0 comments on commit 51cd7ba

Please sign in to comment.