|
7 | 7 | #include <datadog/logger.h> |
8 | 8 | #include <datadog/telemetry/configuration.h> |
9 | 9 | #include <datadog/telemetry/metrics.h> |
10 | | -#include <datadog/tracer_signature.h> |
11 | 10 |
|
12 | 11 | #include <memory> |
13 | 12 | #include <unordered_map> |
14 | 13 | #include <vector> |
15 | 14 |
|
16 | | -namespace datadog { |
17 | | - |
18 | | -namespace tracing { |
19 | | -class TracerTelemetry; |
20 | | -} // namespace tracing |
21 | | - |
22 | | -namespace telemetry { |
23 | | - |
24 | | -/// The telemetry class is responsible for handling internal telemetry data to |
| 15 | +/// Telemetry functions are responsibles for handling internal telemetry data to |
25 | 16 | /// track Datadog product usage. It _can_ collect and report logs and metrics. |
26 | 17 | /// |
27 | 18 | /// IMPORTANT: This is intended for use only by Datadog Engineers. |
28 | | -class 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 | | - |
85 | | - /// Configuration object containing the validated settings for telemetry |
86 | | - FinalizedConfiguration config_; |
87 | | - /// Shared pointer to the user logger instance. |
88 | | - std::shared_ptr<tracing::Logger> logger_; |
89 | | - 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_; |
97 | | - |
98 | | - public: |
99 | | - /// Constructor for the Telemetry class |
100 | | - /// |
101 | | - /// @param configuration The finalized configuration settings. |
102 | | - /// @param logger User logger instance. |
103 | | - /// @param metrics A vector user metrics to report. |
104 | | - Telemetry(FinalizedConfiguration configuration, |
105 | | - std::shared_ptr<tracing::Logger> logger, |
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(); |
116 | | - |
117 | | - // Provides access to the telemetry metrics for updating the values. |
118 | | - // This value should not be stored. |
119 | | - inline auto& metrics() { return metrics_; } |
120 | | - |
121 | | - /// Capture and report internal error message to Datadog. |
122 | | - /// |
123 | | - /// @param message The error message. |
124 | | - void log_error(std::string message); |
125 | | - |
126 | | - /// capture and report internal warning message to Datadog. |
127 | | - /// |
128 | | - /// @param message The warning message to log. |
129 | | - void log_warning(std::string message); |
| 19 | +namespace datadog::telemetry { |
130 | 20 |
|
131 | | - void send_app_started( |
132 | | - const std::unordered_map<tracing::ConfigName, tracing::ConfigMetadata>& |
133 | | - config_metadata); |
| 21 | +/// Initialize the telemetry module |
| 22 | +/// Once initialized, the telemetry module is running for the entier lifecycle |
| 23 | +/// of the application. |
| 24 | +/// |
| 25 | +/// @param configuration The finalized configuration settings. |
| 26 | +/// @param logger User logger instance. |
| 27 | +/// @param metrics A vector user metrics to report. |
| 28 | +/// |
| 29 | +/// NOTE: Make sure to call `init` before calling any of the other telemetry |
| 30 | +/// functions. |
| 31 | +void init(FinalizedConfiguration configuration, |
| 32 | + std::shared_ptr<tracing::Logger> logger, |
| 33 | + std::shared_ptr<tracing::HTTPClient> client, |
| 34 | + std::vector<std::shared_ptr<Metric>> metrics, |
| 35 | + std::shared_ptr<tracing::EventScheduler> event_scheduler, |
| 36 | + tracing::HTTPClient::URL agent_url, |
| 37 | + tracing::Clock clock = tracing::default_clock); |
| 38 | + |
| 39 | +/// Sends a notification indicating that the application has started. |
| 40 | +/// |
| 41 | +/// This function is responsible for reporting the application has successfully |
| 42 | +/// started. It takes a configuration map as a parameter, which contains various |
| 43 | +/// configuration settings helping to understand how our product are used. |
| 44 | +/// |
| 45 | +/// @param conf A map containing configuration names and their corresponding |
| 46 | +/// metadata. |
| 47 | +/// |
| 48 | +/// @note This function should be called after the application has completed its |
| 49 | +/// initialization process to ensure that all components are aware of the |
| 50 | +/// application's startup status. |
| 51 | +void send_app_started(const std::unordered_map<tracing::ConfigName, |
| 52 | + tracing::ConfigMetadata>& conf); |
134 | 53 |
|
135 | | - void send_configuration_change(); |
| 54 | +/// Sends configuration changes. |
| 55 | +/// |
| 56 | +/// This function is responsible for sending reported configuration changes |
| 57 | +/// reported by `capture_configuration_change`. |
| 58 | +/// |
| 59 | +/// @note This function should be called _AFTER_ all configuration changes are |
| 60 | +/// captures by `capture_configuration_change`. |
| 61 | +void send_configuration_change(); |
136 | 62 |
|
137 | | - void capture_configuration_change( |
138 | | - const std::vector<tracing::ConfigMetadata>& new_configuration); |
| 63 | +/// Captures a change in the application's configuration. |
| 64 | +/// |
| 65 | +/// This function is called to report updates to the application's |
| 66 | +/// configuration. It takes a vector of new configuration metadata as a |
| 67 | +/// parameter, which contains the updated settings. |
| 68 | +/// |
| 69 | +/// @param new_configuration A vector containing the new configuration metadata. |
| 70 | +/// |
| 71 | +/// @note This function should be invoked whenever there is a change in the |
| 72 | +/// configuration. |
| 73 | +void capture_configuration_change( |
| 74 | + const std::vector<tracing::ConfigMetadata>& new_configuration); |
139 | 75 |
|
140 | | - void send_app_closing(); |
| 76 | +/// Provides access to the telemetry metrics for updating the values. |
| 77 | +/// This value should not be stored. |
| 78 | +DefaultMetrics& metrics(); |
141 | 79 |
|
142 | | - private: |
143 | | - void send_telemetry(tracing::StringView request_type, std::string payload); |
| 80 | +/// Report internal warning message to Datadog. |
| 81 | +/// |
| 82 | +/// @param message The warning message to log. |
| 83 | +void report_warning_log(std::string message); |
144 | 84 |
|
145 | | - void send_heartbeat_and_telemetry(); |
146 | | -}; |
| 85 | +/// Report internal error message to Datadog. |
| 86 | +/// |
| 87 | +/// @param message The error message. |
| 88 | +void report_error_log(std::string message); |
147 | 89 |
|
148 | | -} // namespace telemetry |
149 | | -} // namespace datadog |
| 90 | +} // namespace datadog::telemetry |
0 commit comments