Skip to content

Commit 890490d

Browse files
committed
Redesign to support tagged metrics
1 parent 6618c75 commit 890490d

File tree

9 files changed

+211
-146
lines changed

9 files changed

+211
-146
lines changed

src/datadog/datadog_agent.cpp

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,26 @@ DatadogAgent::DatadogAgent(
144144
event_scheduler_(config.event_scheduler),
145145
cancel_scheduled_flush_(event_scheduler_->schedule_recurring_event(
146146
config.flush_interval, [this]() { flush(); })),
147-
cancel_heartbeat_timer_(event_scheduler_->schedule_recurring_event(
148-
std::chrono::seconds(10), [this, n=0]() mutable {
147+
cancel_telemetry_timer_(event_scheduler_->schedule_recurring_event(
148+
std::chrono::seconds(10),
149+
[this, n = 0]() mutable {
149150
n++;
150-
tracer_telemetry_->captureMetrics();
151-
if (n%6 == 0) {
152-
sendHeartbeatAndTelemetry();
151+
tracer_telemetry_->capture_metrics();
152+
if (n % 6 == 0) {
153+
sendHeartbeatAndTelemetry();
153154
}
154-
})),
155+
})),
155156
flush_interval_(config.flush_interval) {
156157
assert(logger_);
158+
assert(tracer_telemetry_);
157159
sendAppStarted();
158160
}
159161

160162
DatadogAgent::~DatadogAgent() {
161163
const auto deadline = clock_().tick + std::chrono::seconds(2);
162164
cancel_scheduled_flush_();
163165
flush();
166+
cancel_telemetry_timer_();
164167
http_client_->drain(deadline);
165168
}
166169

@@ -230,10 +233,21 @@ void DatadogAgent::flush() {
230233

231234
// This is the callback for the HTTP response. It's invoked
232235
// asynchronously.
233-
auto on_response = [samplers = std::move(response_handlers),
236+
auto on_response = [this, samplers = std::move(response_handlers),
234237
logger = logger_](int response_status,
235238
const DictReader& /*response_headers*/,
236239
std::string response_body) {
240+
if (response_status >= 500) {
241+
tracer_telemetry_->metrics().trace_api.responses_5xx.inc();
242+
} else if (response_status >= 400) {
243+
tracer_telemetry_->metrics().trace_api.responses_4xx.inc();
244+
} else if (response_status >= 300) {
245+
tracer_telemetry_->metrics().trace_api.responses_3xx.inc();
246+
} else if (response_status >= 200) {
247+
tracer_telemetry_->metrics().trace_api.responses_2xx.inc();
248+
} else if (response_status >= 100) {
249+
tracer_telemetry_->metrics().trace_api.responses_1xx.inc();
250+
}
237251
if (response_status != 200) {
238252
logger->log_error([&](auto& stream) {
239253
stream << "Unexpected response status " << response_status
@@ -269,12 +283,13 @@ void DatadogAgent::flush() {
269283
// This is the callback for if something goes wrong sending the
270284
// request or retrieving the response. It's invoked
271285
// asynchronously.
272-
auto on_error = [logger = logger_](Error error) {
286+
auto on_error = [this, logger = logger_](Error error) {
287+
tracer_telemetry_->metrics().trace_api.errors_network.inc();
273288
logger->log_error(error.with_prefix(
274289
"Error occurred during HTTP request for submitting traces: "));
275290
};
276291

277-
tracer_telemetry_->trace_api_requests().inc();
292+
tracer_telemetry_->metrics().trace_api.requests.inc();
278293
auto post_result = http_client_->post(
279294
traces_endpoint_, std::move(set_request_headers), std::move(body),
280295
std::move(on_response), std::move(on_error));
@@ -284,7 +299,7 @@ void DatadogAgent::flush() {
284299
}
285300

286301
void DatadogAgent::sendAppStarted() {
287-
auto payload = tracer_telemetry_->appStarted();
302+
auto payload = tracer_telemetry_->app_started();
288303
auto set_request_headers = [&](DictWriter& headers) {
289304
headers.set("Content-Type", "application/json");
290305
};
@@ -295,19 +310,18 @@ void DatadogAgent::sendAppStarted() {
295310
std::string response_body) {
296311
if (response_status < 200 || response_status >= 300) {
297312
logger->log_error([&](auto& stream) {
298-
stream << "Unexpected telemetry response status " << response_status
299-
<< " with body (starts on next line):\n"
300-
<< response_body;
301-
});
313+
stream << "Unexpected telemetry response status " << response_status
314+
<< " with body (starts on next line):\n"
315+
<< response_body;
316+
});
302317
return;
303318
} else {
304319
logger->log_error([&](auto& stream) {
305-
stream << "Successful telemetry submission with response status " << response_status
306-
<< " and body (starts on next line):\n"
307-
<< response_body;
308-
});
320+
stream << "Successful telemetry submission with response status "
321+
<< response_status << " and body (starts on next line):\n"
322+
<< response_body;
323+
});
309324
}
310-
311325
};
312326

313327
// Callback for unsuccessful HTTP request.
@@ -325,7 +339,7 @@ void DatadogAgent::sendAppStarted() {
325339
}
326340

327341
void DatadogAgent::sendHeartbeatAndTelemetry() {
328-
auto payload = tracer_telemetry_->heartbeatAndTelemetry();
342+
auto payload = tracer_telemetry_->heartbeat_and_telemetry();
329343
auto set_request_headers = [&](DictWriter& headers) {
330344
headers.set("Content-Type", "application/json");
331345
};
@@ -336,19 +350,18 @@ void DatadogAgent::sendHeartbeatAndTelemetry() {
336350
std::string response_body) {
337351
if (response_status < 200 || response_status >= 300) {
338352
logger->log_error([&](auto& stream) {
339-
stream << "Unexpected telemetry response status " << response_status
340-
<< " with body (starts on next line):\n"
341-
<< response_body;
342-
});
353+
stream << "Unexpected telemetry response status " << response_status
354+
<< " with body (starts on next line):\n"
355+
<< response_body;
356+
});
343357
return;
344358
} else {
345359
logger->log_error([&](auto& stream) {
346-
stream << "Successful telemetry submission with response status " << response_status
347-
<< " and body (starts on next line):\n"
348-
<< response_body;
349-
});
360+
stream << "Successful telemetry submission with response status "
361+
<< response_status << " and body (starts on next line):\n"
362+
<< response_body;
363+
});
350364
}
351-
352365
};
353366

354367
// Callback for unsuccessful HTTP request.

src/datadog/datadog_agent.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "collector.h"
1515
#include "event_scheduler.h"
1616
#include "http_client.h"
17+
#include "metrics.h"
1718
#include "tracer_telemetry.h"
1819

1920
namespace datadog {
@@ -42,7 +43,7 @@ class DatadogAgent : public Collector {
4243
std::shared_ptr<HTTPClient> http_client_;
4344
std::shared_ptr<EventScheduler> event_scheduler_;
4445
EventScheduler::Cancel cancel_scheduled_flush_;
45-
EventScheduler::Cancel cancel_heartbeat_timer_;
46+
EventScheduler::Cancel cancel_telemetry_timer_;
4647
std::chrono::steady_clock::duration flush_interval_;
4748

4849
void flush();

src/datadog/metrics.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
namespace datadog {
66
namespace tracing {
77

8-
Metric::Metric(std::string name, std::string type, bool common) : name_(name), type_(type), common_(common) {}
9-
std::string Metric::name() { return name_; }
10-
std::string Metric::type() { return type_; }
8+
Metric::Metric(const std::string name, std::string type,
9+
const std::vector<std::string> tags, bool common)
10+
: name_(name), type_(type), tags_(tags), common_(common) {}
11+
const std::string Metric::name() { return name_; }
12+
const std::string Metric::type() { return type_; }
13+
const std::vector<std::string> Metric::tags() { return tags_; }
1114
bool Metric::common() { return common_; }
1215
uint64_t Metric::value() { return value_; }
1316

14-
CounterMetric::CounterMetric(std::string name, bool common) : Metric(name, "count", common) {}
17+
CounterMetric::CounterMetric(const std::string name,
18+
const std::vector<std::string> tags, bool common)
19+
: Metric(name, "count", tags, common) {}
1520
void CounterMetric::inc() { add(1); }
1621
void CounterMetric::add(uint64_t amount) { value_ += amount; }
1722

18-
GaugeMetric::GaugeMetric(std::string name, bool common) : Metric(name, "gauge", common) {}
23+
GaugeMetric::GaugeMetric(const std::string name,
24+
const std::vector<std::string> tags, bool common)
25+
: Metric(name, "gauge", tags, common) {}
1926
void GaugeMetric::set(uint64_t value) { value_ = value; }
2027
void GaugeMetric::inc() { add(1); }
2128
void GaugeMetric::add(uint64_t amount) { value_ += amount; }

src/datadog/metrics.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
#pragma once
22

33
#include <atomic>
4-
5-
#include "json_fwd.hpp"
6-
#include "string_view.h"
4+
#include <string>
5+
#include <vector>
76

87
namespace datadog {
98
namespace tracing {
109

1110
class Metric {
12-
std::string name_;
13-
std::string type_;
11+
const std::string name_;
12+
const std::string type_;
13+
const std::vector<std::string> tags_;
1414
bool common_;
15-
protected:
15+
16+
protected:
1617
std::atomic<uint64_t> value_ = 0;
17-
Metric(std::string name, std::string type, bool common);
18+
Metric(const std::string name, std::string type,
19+
const std::vector<std::string> tags, bool common);
20+
1821
public:
19-
std::string name();
20-
std::string type();
22+
const std::string name();
23+
const std::string type();
24+
const std::vector<std::string> tags();
2125
bool common();
2226
uint64_t value();
2327
};
2428

2529
class CounterMetric : public Metric {
2630
public:
27-
CounterMetric(std::string name, bool common);
31+
CounterMetric(const std::string name, const std::vector<std::string> tags,
32+
bool common);
2833
void inc();
2934
void add(uint64_t amount);
3035
};
3136

3237
class GaugeMetric : public Metric {
3338
public:
34-
GaugeMetric(std::string name, bool common);
39+
GaugeMetric(const std::string name, const std::vector<std::string> tags,
40+
bool common);
3541
void set(uint64_t value);
3642
void inc();
3743
void add(uint64_t amount);

src/datadog/trace_segment.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ TraceSegment::TraceSegment(
118118
assert(span_sampler_);
119119
assert(defaults_);
120120

121-
tracer_telemetry_->traces_started().inc();
122-
tracer_telemetry_->active_traces().inc();
123-
124121
register_span(std::move(local_root));
125122
}
126123

@@ -141,19 +138,16 @@ Optional<SamplingDecision> TraceSegment::sampling_decision() const {
141138
Logger& TraceSegment::logger() const { return *logger_; }
142139

143140
void TraceSegment::register_span(std::unique_ptr<SpanData> span) {
144-
tracer_telemetry_->spans_started().inc();
145-
tracer_telemetry_->active_spans().inc();
141+
tracer_telemetry_->metrics().tracer.spans_created.inc();
146142

147143
std::lock_guard<std::mutex> lock(mutex_);
148144
assert(spans_.empty() || num_finished_spans_ < spans_.size());
149145
spans_.emplace_back(std::move(span));
150146
}
151147

152148
void TraceSegment::span_finished() {
153-
tracer_telemetry_->spans_finished().inc();
154-
tracer_telemetry_->active_spans().dec();
155-
156149
{
150+
tracer_telemetry_->metrics().tracer.spans_finished.inc();
157151
std::lock_guard<std::mutex> lock(mutex_);
158152
++num_finished_spans_;
159153
assert(num_finished_spans_ <= spans_.size());
@@ -233,8 +227,7 @@ void TraceSegment::span_finished() {
233227
error->with_prefix("Error sending spans to collector: "));
234228
}
235229

236-
tracer_telemetry_->traces_finished().inc();
237-
tracer_telemetry_->active_traces().dec();
230+
tracer_telemetry_->metrics().tracer.trace_segments_closed.inc();
238231
}
239232

240233
void TraceSegment::override_sampling_priority(int priority) {

src/datadog/trace_segment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <vector>
3434

3535
#include "expected.h"
36+
#include "metrics.h"
3637
#include "optional.h"
3738
#include "propagation_style.h"
3839
#include "sampling_decision.h"

src/datadog/tracer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ Span Tracer::create_span(const SpanConfig& config) {
293293
span_data->parent_id = 0;
294294

295295
const auto span_data_ptr = span_data.get();
296+
tracer_telemetry_->metrics().tracer.trace_segments_created_new.inc();
296297
const auto segment = std::make_shared<TraceSegment>(
297298
logger_, collector_, tracer_telemetry_, trace_sampler_, span_sampler_,
298299
defaults_, injection_styles_, hostname_, nullopt /* origin */,
@@ -456,6 +457,7 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
456457
}
457458

458459
const auto span_data_ptr = span_data.get();
460+
tracer_telemetry_->metrics().tracer.trace_segments_created_continued.inc();
459461
const auto segment = std::make_shared<TraceSegment>(
460462
logger_, collector_, tracer_telemetry_, trace_sampler_, span_sampler_,
461463
defaults_, injection_styles_, hostname_, std::move(origin),

0 commit comments

Comments
 (0)