Line data Source code
1 : #pragma once
2 :
3 : // This component provides a class, TracerTelemetry, that is used to collect
4 : // data from the activity of the tracer implementation, and encode messages that
5 : // can be submitted to the Datadog Agent.
6 : //
7 : // Counter metrics are updated in other parts of the tracers, with the values
8 : // being managed by this class.
9 : //
10 : // The messages that TracerTelemetry produces are
11 : // - `app-started`
12 : // - `message-batch`
13 : // - `app-heartbeat`
14 : // - `generate-metrics`
15 : // - `app-closing`
16 : //
17 : // `app-started` messages are sent as part of initializing the tracer.
18 : //
19 : // At 60 second intervals, a `message-batch` message is sent containing an
20 : // `app-heartbeat` message, and if metrics have changed during that interval, a
21 : // `generate-metrics` message is also included in the batch.
22 : //
23 : // `app-closing` messages are sent as part of terminating the tracer. These are
24 : // sent as a `message-batch` message , and if metrics have changed since the
25 : // last `app-heartbeat` event, a `generate-metrics` message is also included in
26 : // the batch.
27 : //
28 : #include <vector>
29 :
30 : #include "clock.h"
31 : #include "json.hpp"
32 : #include "metrics.h"
33 : #include "runtime_id.h"
34 : #include "tracer_signature.h"
35 :
36 : namespace datadog {
37 : namespace tracing {
38 :
39 : class Logger;
40 : struct SpanDefaults;
41 :
42 : class TracerTelemetry {
43 : bool enabled_ = false;
44 : bool debug_ = false;
45 : Clock clock_;
46 : std::shared_ptr<Logger> logger_;
47 : TracerSignature tracer_signature_;
48 : std::string hostname_;
49 : uint64_t seq_id_ = 0;
50 : // This structure contains all the metrics that are exposed by tracer
51 : // telemetry.
52 : struct {
53 : struct {
54 : CounterMetric spans_created = {
55 : "spans_created", {"integration_name:datadog"}, true};
56 : CounterMetric spans_finished = {
57 : "spans_finished", {"integration_name:datadog"}, true};
58 :
59 : CounterMetric trace_segments_created_new = {
60 : "trace_segments_created", {"new_continued:new"}, true};
61 : CounterMetric trace_segments_created_continued = {
62 : "trace_segments_created", {"new_continued:continued"}, true};
63 : CounterMetric trace_segments_closed = {
64 : "trace_segments_closed", {"integration_name:datadog"}, true};
65 : } tracer;
66 : struct {
67 : CounterMetric requests = {"trace_api.requests", {}, true};
68 :
69 : CounterMetric responses_1xx = {
70 : "trace_api.responses", {"status_code:1xx"}, true};
71 : CounterMetric responses_2xx = {
72 : "trace_api.responses", {"status_code:2xx"}, true};
73 : CounterMetric responses_3xx = {
74 : "trace_api.responses", {"status_code:3xx"}, true};
75 : CounterMetric responses_4xx = {
76 : "trace_api.responses", {"status_code:4xx"}, true};
77 : CounterMetric responses_5xx = {
78 : "trace_api.responses", {"status_code:5xx"}, true};
79 :
80 : CounterMetric errors_timeout = {
81 : "trace_api.errors", {"type:timeout"}, true};
82 : CounterMetric errors_network = {
83 : "trace_api.errors", {"type:network"}, true};
84 : CounterMetric errors_status_code = {
85 : "trace_api.errors", {"type:status_code"}, true};
86 :
87 : } trace_api;
88 : } metrics_;
89 : // Each metric has an associated MetricSnapshot that contains the data points,
90 : // represented as a timestamp and the value of that metric.
91 : using MetricSnapshot = std::vector<std::pair<std::time_t, uint64_t>>;
92 : // This uses a reference_wrapper so references to internal metric values can
93 : // be captured, and be iterated trivially when the values need to be
94 : // snapshotted and published in telemetry messages.
95 : std::vector<std::pair<std::reference_wrapper<Metric>, MetricSnapshot>>
96 : metrics_snapshots_;
97 :
98 : nlohmann::json generate_telemetry_body(std::string request_type);
99 :
100 : public:
101 : TracerTelemetry(bool enabled, const Clock& clock,
102 : const std::shared_ptr<Logger>& logger,
103 : const TracerSignature& tracer_signature);
104 1287 : bool enabled() { return enabled_; };
105 : // Provides access to the telemetry metrics for updating the values.
106 : // This value should not be stored.
107 332420 : auto& metrics() { return metrics_; };
108 : // Constructs an `app-started` message using information provided when
109 : // constructed and the tracer_config value passed in.
110 : std::string app_started();
111 : // This is used to take a snapshot of the current state of metrics and collect
112 : // timestamped "points" of values. These values are later submitted in
113 : // `generate-metrics` messages.
114 : void capture_metrics();
115 : // Constructs a messsage-batch containing `app-heartbeat`, and if metrics have
116 : // been modified, a `generate-metrics` message.
117 : std::string heartbeat_and_telemetry();
118 : // Constructs a message-batch containing `app-closing`, and if metrics have
119 : // been modified, a `generate-metrics` message.
120 : std::string app_closing();
121 : };
122 :
123 : } // namespace tracing
124 : } // namespace datadog
|