Skip to content

Commit ea2ced3

Browse files
Solve concurrency issue when telemetry endpoint is unreachable
1 parent e14a5a4 commit ea2ced3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/datadog/telemetry/telemetry_impl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ void Telemetry::schedule_tasks() {
226226
}
227227

228228
Telemetry::~Telemetry() {
229+
shutting_down_.store(true, std::memory_order_release);
229230
if (!tasks_.empty()) {
230231
cancel_tasks(tasks_);
231232
capture_metrics();
@@ -736,6 +737,9 @@ void Telemetry::capture_metrics() {
736737

737738
void Telemetry::log(std::string message, telemetry::LogLevel level,
738739
Optional<std::string> stacktrace) {
740+
if (shutting_down_.load(std::memory_order_acquire)) {
741+
return;
742+
}
739743
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(
740744
clock_().wall.time_since_epoch())
741745
.count();
@@ -750,6 +754,9 @@ void Telemetry::increment_counter(const Counter& id) {
750754

751755
void Telemetry::increment_counter(const Counter& id,
752756
const std::vector<std::string>& tags) {
757+
if (shutting_down_.load(std::memory_order_acquire)) {
758+
return;
759+
}
753760
std::lock_guard l{counter_mutex_};
754761
counters_[{id, tags}] += 1;
755762
}
@@ -760,6 +767,9 @@ void Telemetry::decrement_counter(const Counter& id) {
760767

761768
void Telemetry::decrement_counter(const Counter& id,
762769
const std::vector<std::string>& tags) {
770+
if (shutting_down_.load(std::memory_order_acquire)) {
771+
return;
772+
}
763773
std::lock_guard l{counter_mutex_};
764774
auto& v = counters_[{id, tags}];
765775
if (v > 0) v -= 1;
@@ -772,6 +782,9 @@ void Telemetry::set_counter(const Counter& id, uint64_t value) {
772782
void Telemetry::set_counter(const Counter& id,
773783
const std::vector<std::string>& tags,
774784
uint64_t value) {
785+
if (shutting_down_.load(std::memory_order_acquire)) {
786+
return;
787+
}
775788
std::lock_guard l{counter_mutex_};
776789
counters_[{id, tags}] = value;
777790
}
@@ -782,6 +795,9 @@ void Telemetry::set_rate(const Rate& id, uint64_t value) {
782795

783796
void Telemetry::set_rate(const Rate& id, const std::vector<std::string>& tags,
784797
uint64_t value) {
798+
if (shutting_down_.load(std::memory_order_acquire)) {
799+
return;
800+
}
785801
std::lock_guard l{rate_mutex_};
786802
rates_[{id, tags}] = value;
787803
}
@@ -793,6 +809,9 @@ void Telemetry::add_datapoint(const Distribution& id, uint64_t value) {
793809
void Telemetry::add_datapoint(const Distribution& id,
794810
const std::vector<std::string>& tags,
795811
uint64_t value) {
812+
if (shutting_down_.load(std::memory_order_acquire)) {
813+
return;
814+
}
796815
std::lock_guard l{distributions_mutex_};
797816
distributions_[{id, tags}].emplace_back(value);
798817
}

src/datadog/telemetry/telemetry_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <datadog/telemetry/metrics.h>
1010
#include <datadog/tracer_signature.h>
1111

12+
#include <atomic>
1213
#include <mutex>
1314

1415
#include "json.hpp"
@@ -39,6 +40,7 @@ class Telemetry final {
3940
std::shared_ptr<tracing::HTTPClient> http_client_;
4041
tracing::Clock clock_;
4142
std::shared_ptr<tracing::EventScheduler> scheduler_;
43+
std::atomic<bool> shutting_down_{false};
4244

4345
/// Counter
4446
std::mutex counter_mutex_;

0 commit comments

Comments
 (0)