11#pragma once
22
3+ #include < datadog/clock.h>
4+ #include < datadog/config.h>
5+ #include < datadog/event_scheduler.h>
6+ #include < datadog/http_client.h>
37#include < datadog/logger.h>
48#include < datadog/telemetry/configuration.h>
59#include < datadog/telemetry/metrics.h>
10+ #include < datadog/tracer_signature.h>
611
712#include < memory>
13+ #include < unordered_map>
814#include < vector>
915
1016namespace datadog {
1117
1218namespace tracing {
13- class DatadogAgent ;
1419class TracerTelemetry ;
1520} // namespace tracing
1621
@@ -21,13 +26,74 @@ namespace telemetry {
2126// /
2227// / IMPORTANT: This is intended for use only by Datadog Engineers.
2328class Telemetry final {
29+ // This structure contains all the metrics that are exposed by tracer
30+ // telemetry.
31+ struct {
32+ struct {
33+ telemetry::CounterMetric spans_created = {
34+ " spans_created" , " tracers" , {}, true };
35+ telemetry::CounterMetric spans_finished = {
36+ " spans_finished" , " tracers" , {}, true };
37+
38+ telemetry::CounterMetric trace_segments_created_new = {
39+ " trace_segments_created" , " tracers" , {" new_continued:new" }, true };
40+ telemetry::CounterMetric trace_segments_created_continued = {
41+ " trace_segments_created" ,
42+ " tracers" ,
43+ {" new_continued:continued" },
44+ true };
45+ telemetry::CounterMetric trace_segments_closed = {
46+ " trace_segments_closed" , " tracers" , {}, true };
47+ telemetry::CounterMetric baggage_items_exceeded = {
48+ " context_header.truncated" ,
49+ " tracers" ,
50+ {{" truncation_reason:baggage_item_count_exceeded" }},
51+ true ,
52+ };
53+ telemetry::CounterMetric baggage_bytes_exceeded = {
54+ " context_header.truncated" ,
55+ " tracers" ,
56+ {{" truncation_reason:baggage_byte_count_exceeded" }},
57+ true ,
58+ };
59+ } tracer;
60+ struct {
61+ telemetry::CounterMetric requests = {
62+ " trace_api.requests" , " tracers" , {}, true };
63+
64+ telemetry::CounterMetric responses_1xx = {
65+ " trace_api.responses" , " tracers" , {" status_code:1xx" }, true };
66+ telemetry::CounterMetric responses_2xx = {
67+ " trace_api.responses" , " tracers" , {" status_code:2xx" }, true };
68+ telemetry::CounterMetric responses_3xx = {
69+ " trace_api.responses" , " tracers" , {" status_code:3xx" }, true };
70+ telemetry::CounterMetric responses_4xx = {
71+ " trace_api.responses" , " tracers" , {" status_code:4xx" }, true };
72+ telemetry::CounterMetric responses_5xx = {
73+ " trace_api.responses" , " tracers" , {" status_code:5xx" }, true };
74+
75+ telemetry::CounterMetric errors_timeout = {
76+ " trace_api.errors" , " tracers" , {" type:timeout" }, true };
77+ telemetry::CounterMetric errors_network = {
78+ " trace_api.errors" , " tracers" , {" type:network" }, true };
79+ telemetry::CounterMetric errors_status_code = {
80+ " trace_api.errors" , " tracers" , {" type:status_code" }, true };
81+
82+ } trace_api;
83+ } metrics_;
84+
2485 // / Configuration object containing the validated settings for telemetry
2586 FinalizedConfiguration config_;
2687 // / Shared pointer to the user logger instance.
2788 std::shared_ptr<tracing::Logger> logger_;
28- // / TODO(@dmehala): Legacy dependency.
29- std::shared_ptr<tracing::DatadogAgent> datadog_agent_;
3089 std::shared_ptr<tracing::TracerTelemetry> tracer_telemetry_;
90+ std::vector<tracing::EventScheduler::Cancel> tasks_;
91+ tracing::HTTPClient::ResponseHandler telemetry_on_response_;
92+ tracing::HTTPClient::ErrorHandler telemetry_on_error_;
93+ tracing::HTTPClient::URL telemetry_endpoint_;
94+ tracing::TracerSignature tracer_signature_;
95+ std::shared_ptr<tracing::HTTPClient> http_client_;
96+ tracing::Clock clock_;
3197
3298 public:
3399 // / Constructor for the Telemetry class
@@ -37,9 +103,20 @@ class Telemetry final {
37103 // / @param metrics A vector user metrics to report.
38104 Telemetry (FinalizedConfiguration configuration,
39105 std::shared_ptr<tracing::Logger> logger,
40- std::vector<std::shared_ptr<Metric>> metrics);
106+ std::shared_ptr<tracing::HTTPClient> client,
107+ std::vector<std::shared_ptr<Metric>> metrics,
108+ tracing::EventScheduler& scheduler,
109+ tracing::HTTPClient::URL agent_url,
110+ tracing::Clock clock = tracing::default_clock);
111+
112+ // / Destructor
113+ // /
114+ // / Send last metrics snapshot and `app-closing` event.
115+ ~Telemetry ();
41116
42- ~Telemetry () = default ;
117+ // Provides access to the telemetry metrics for updating the values.
118+ // This value should not be stored.
119+ inline auto & metrics () { return metrics_; }
43120
44121 // / Capture and report internal error message to Datadog.
45122 // /
@@ -50,6 +127,22 @@ class Telemetry final {
50127 // /
51128 // / @param message The warning message to log.
52129 void log_warning (std::string message);
130+
131+ void send_app_started (
132+ const std::unordered_map<tracing::ConfigName, tracing::ConfigMetadata>&
133+ config_metadata);
134+
135+ void send_configuration_change ();
136+
137+ void capture_configuration_change (
138+ const std::vector<tracing::ConfigMetadata>& new_configuration);
139+
140+ void send_app_closing ();
141+
142+ private:
143+ void send_telemetry (tracing::StringView request_type, std::string payload);
144+
145+ void send_heartbeat_and_telemetry ();
53146};
54147
55148} // namespace telemetry
0 commit comments