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
2 changes: 0 additions & 2 deletions include/datadog/datadog_agent_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ struct DatadogAgentConfig {
// How often, in seconds, to query the Datadog Agent for remote configuration
// updates.
Optional<double> remote_configuration_poll_interval_seconds;

static Expected<HTTPClient::URL> parse(StringView);
};

class FinalizedDatadogAgentConfig {
Expand Down
103 changes: 98 additions & 5 deletions include/datadog/telemetry/telemetry.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#pragma once

#include <datadog/clock.h>
#include <datadog/config.h>
#include <datadog/event_scheduler.h>
#include <datadog/http_client.h>
#include <datadog/logger.h>
#include <datadog/telemetry/configuration.h>
#include <datadog/telemetry/metrics.h>
#include <datadog/tracer_signature.h>

#include <memory>
#include <unordered_map>
#include <vector>

namespace datadog {

namespace tracing {
class DatadogAgent;
class TracerTelemetry;
} // namespace tracing

Expand All @@ -21,13 +26,74 @@ namespace telemetry {
///
/// IMPORTANT: This is intended for use only by Datadog Engineers.
class Telemetry final {
// This structure contains all the metrics that are exposed by tracer
// telemetry.
struct {
struct {
telemetry::CounterMetric spans_created = {
"spans_created", "tracers", {}, true};
telemetry::CounterMetric spans_finished = {
"spans_finished", "tracers", {}, true};

telemetry::CounterMetric trace_segments_created_new = {
"trace_segments_created", "tracers", {"new_continued:new"}, true};
telemetry::CounterMetric trace_segments_created_continued = {
"trace_segments_created",
"tracers",
{"new_continued:continued"},
true};
telemetry::CounterMetric trace_segments_closed = {
"trace_segments_closed", "tracers", {}, true};
telemetry::CounterMetric baggage_items_exceeded = {
"context_header.truncated",
"tracers",
{{"truncation_reason:baggage_item_count_exceeded"}},
true,
};
telemetry::CounterMetric baggage_bytes_exceeded = {
"context_header.truncated",
"tracers",
{{"truncation_reason:baggage_byte_count_exceeded"}},
true,
};
} tracer;
struct {
telemetry::CounterMetric requests = {
"trace_api.requests", "tracers", {}, true};

telemetry::CounterMetric responses_1xx = {
"trace_api.responses", "tracers", {"status_code:1xx"}, true};
telemetry::CounterMetric responses_2xx = {
"trace_api.responses", "tracers", {"status_code:2xx"}, true};
telemetry::CounterMetric responses_3xx = {
"trace_api.responses", "tracers", {"status_code:3xx"}, true};
telemetry::CounterMetric responses_4xx = {
"trace_api.responses", "tracers", {"status_code:4xx"}, true};
telemetry::CounterMetric responses_5xx = {
"trace_api.responses", "tracers", {"status_code:5xx"}, true};

telemetry::CounterMetric errors_timeout = {
"trace_api.errors", "tracers", {"type:timeout"}, true};
telemetry::CounterMetric errors_network = {
"trace_api.errors", "tracers", {"type:network"}, true};
telemetry::CounterMetric errors_status_code = {
"trace_api.errors", "tracers", {"type:status_code"}, true};

} trace_api;
} metrics_;

/// Configuration object containing the validated settings for telemetry
FinalizedConfiguration config_;
/// Shared pointer to the user logger instance.
std::shared_ptr<tracing::Logger> logger_;
/// TODO(@dmehala): Legacy dependency.
std::shared_ptr<tracing::DatadogAgent> datadog_agent_;
std::shared_ptr<tracing::TracerTelemetry> tracer_telemetry_;
std::vector<tracing::EventScheduler::Cancel> tasks_;
tracing::HTTPClient::ResponseHandler telemetry_on_response_;
tracing::HTTPClient::ErrorHandler telemetry_on_error_;
tracing::HTTPClient::URL telemetry_endpoint_;
tracing::TracerSignature tracer_signature_;
std::shared_ptr<tracing::HTTPClient> http_client_;
tracing::Clock clock_;

public:
/// Constructor for the Telemetry class
Expand All @@ -37,9 +103,20 @@ class Telemetry final {
/// @param metrics A vector user metrics to report.
Telemetry(FinalizedConfiguration configuration,
std::shared_ptr<tracing::Logger> logger,
std::vector<std::shared_ptr<Metric>> metrics);
std::shared_ptr<tracing::HTTPClient> client,
std::vector<std::shared_ptr<Metric>> metrics,
tracing::EventScheduler& scheduler,
tracing::HTTPClient::URL agent_url,
tracing::Clock clock = tracing::default_clock);

/// Destructor
///
/// Send last metrics snapshot and `app-closing` event.
~Telemetry();

~Telemetry() = default;
// Provides access to the telemetry metrics for updating the values.
// This value should not be stored.
inline auto& metrics() { return metrics_; }

/// Capture and report internal error message to Datadog.
///
Expand All @@ -50,6 +127,22 @@ class Telemetry final {
///
/// @param message The warning message to log.
void log_warning(std::string message);

void send_app_started(
const std::unordered_map<tracing::ConfigName, tracing::ConfigMetadata>&
config_metadata);

void send_configuration_change();

void capture_configuration_change(
const std::vector<tracing::ConfigMetadata>& new_configuration);

void send_app_closing();

private:
void send_telemetry(tracing::StringView request_type, std::string payload);

void send_heartbeat_and_telemetry();
};

} // namespace telemetry
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 @@ -40,6 +40,9 @@
#include "sampling_priority.h"

namespace datadog {
namespace telemetry {
class Telemetry;
}
namespace tracing {

class Collector;
Expand All @@ -52,14 +55,13 @@ struct SpanDefaults;
class SpanSampler;
class TraceSampler;
class ConfigManager;
class TracerTelemetry;

class TraceSegment {
mutable std::mutex mutex_;

std::shared_ptr<Logger> logger_;
std::shared_ptr<Collector> collector_;
std::shared_ptr<TracerTelemetry> tracer_telemetry_;
std::shared_ptr<telemetry::Telemetry> telemetry_;
std::shared_ptr<TraceSampler> trace_sampler_;
std::shared_ptr<SpanSampler> span_sampler_;

Expand All @@ -82,7 +84,7 @@ class TraceSegment {
public:
TraceSegment(const std::shared_ptr<Logger>& logger,
const std::shared_ptr<Collector>& collector,
const std::shared_ptr<TracerTelemetry>& tracer_telemetry,
const std::shared_ptr<telemetry::Telemetry>& telemetry,
const std::shared_ptr<TraceSampler>& trace_sampler,
const std::shared_ptr<SpanSampler>& span_sampler,
const std::shared_ptr<const SpanDefaults>& defaults,
Expand Down
4 changes: 3 additions & 1 deletion include/datadog/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// obtained from a `TracerConfig` via the `finalize_config` function. See
// `tracer_config.h`.

#include <datadog/telemetry/telemetry.h>

#include <cstddef>
#include <memory>

Expand Down Expand Up @@ -39,7 +41,7 @@ class Tracer {
std::shared_ptr<Logger> logger_;
RuntimeID runtime_id_;
TracerSignature signature_;
std::shared_ptr<TracerTelemetry> tracer_telemetry_;
std::shared_ptr<telemetry::Telemetry> telemetry_;
std::shared_ptr<ConfigManager> config_manager_;
std::shared_ptr<Collector> collector_;
std::shared_ptr<SpanSampler> span_sampler_;
Expand Down
10 changes: 10 additions & 0 deletions include/datadog/tracer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,18 @@ struct TracerConfig {
// programmatic value in Datadog's Active Configuration, whereas it is
// actually the default value for the integration.
Optional<bool> report_service_as_default;

/// The maximum number of baggage items that can be stored or propagated.
Optional<std::size_t> baggage_max_items;

/// The maximum amount of bytes allowed to be written during tracing context
/// injection.
Optional<std::size_t> baggage_max_bytes;

/// The event scheduler used for scheduling recurring tasks.
/// By default, it uses `ThreadedEventScheduler`, which runs tasks on a
/// separate thread.
std::shared_ptr<EventScheduler> event_scheduler;
};

// `FinalizedTracerConfig` contains `Tracer` implementation details derived from
Expand Down Expand Up @@ -197,6 +204,9 @@ class FinalizedTracerConfig final {
bool report_traces;
std::unordered_map<ConfigName, ConfigMetadata> metadata;
Baggage::Options baggage_opts;
HTTPClient::URL agent_url;
std::shared_ptr<EventScheduler> event_scheduler;
std::shared_ptr<HTTPClient> http_client;
};

// Return a `FinalizedTracerConfig` from the specified `config` and from any
Expand Down
5 changes: 3 additions & 2 deletions src/datadog/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ ConfigManager::Update parse_dynamic_config(const nlohmann::json& j) {

namespace rc = datadog::remote_config;

ConfigManager::ConfigManager(const FinalizedTracerConfig& config,
const std::shared_ptr<TracerTelemetry>& telemetry)
ConfigManager::ConfigManager(
const FinalizedTracerConfig& config,
const std::shared_ptr<telemetry::Telemetry>& telemetry)
: clock_(config.clock),
default_metadata_(config.metadata),
trace_sampler_(
Expand Down
6 changes: 3 additions & 3 deletions src/datadog/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <datadog/optional.h>
#include <datadog/remote_config/listener.h>
#include <datadog/span_defaults.h>
#include <datadog/telemetry/telemetry.h>
#include <datadog/tracer_config.h>

#include <mutex>

#include "json.hpp"
#include "tracer_telemetry.h"

namespace datadog {
namespace tracing {
Expand Down Expand Up @@ -76,7 +76,7 @@ class ConfigManager : public remote_config::Listener {
DynamicConfig<std::shared_ptr<const SpanDefaults>> span_defaults_;
DynamicConfig<bool> report_traces_;

std::shared_ptr<TracerTelemetry> telemetry_;
std::shared_ptr<telemetry::Telemetry> telemetry_;

private:
template <typename T>
Expand All @@ -85,7 +85,7 @@ class ConfigManager : public remote_config::Listener {

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

remote_config::Products get_products() override;
Expand Down
Loading