11#pragma once
2- #include < memory>
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+ //
328#include < vector>
429
530#include " clock.h"
@@ -20,12 +45,16 @@ class TracerTelemetry {
2045 std::shared_ptr<const SpanDefaults> span_defaults_;
2146 std::string hostname_;
2247 uint64_t seq_id = 0 ;
48+ // Each metric has an associated MetricSnapshot that contains the data points,
49+ // represented as a timestamp and the value of that metric.
2350 using MetricSnapshot = std::vector<std::pair<time_t , uint64_t >>;
2451 // This uses a reference_wrapper so references to internal metric values can
2552 // be captured, and be iterated trivially when the values need to be
2653 // snapshotted and published in telemetry messages.
2754 std::vector<std::pair<std::reference_wrapper<Metric>, MetricSnapshot>>
2855 metrics_snapshots_;
56+ // This structure contains all the metrics that are exposed by tracer
57+ // telemetry.
2958 struct {
3059 struct {
3160 CounterMetric spans_created = {
@@ -69,10 +98,21 @@ class TracerTelemetry {
6998 const std::shared_ptr<Logger>& logger,
7099 const std::shared_ptr<const SpanDefaults>& span_defaults);
71100 bool enabled () { return enabled_; };
101+ // Provides access to the telemetry metrics for updating the values.
102+ // This value should not be stored.
72103 auto & metrics () { return metrics_; };
104+ // Constructs an `app-started` message using information provided when
105+ // constructed and the tracer_config value passed in.
73106 std::string app_started (nlohmann::json&& tracer_config);
107+ // This is used to take a snapshot of the current state of metrics and collect
108+ // timestamped "points" of values. These values are later submitted in
109+ // `generate-metrics` messages.
74110 void capture_metrics ();
111+ // Constructs a messsage-batch containing `app-heartbeat`, and if metrics have
112+ // been modified, a `generate-metrics` message.
75113 std::string heartbeat_and_telemetry ();
114+ // Constructs a message-batch containing `app-closing`, and if metrics have
115+ // been modified, a `generate-metrics` message.
76116 std::string app_closing ();
77117};
78118
0 commit comments