@@ -226,6 +226,7 @@ void Telemetry::schedule_tasks() {
226226}
227227
228228Telemetry::~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
737738void 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
751755void 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
761768void 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) {
772782void 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
783796void 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) {
793809void 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}
0 commit comments