34
34
35
35
namespace couchbase ::core
36
36
{
37
+ struct node_labels {
38
+ std::string node;
39
+ std::optional<std::string> alt_node;
40
+ };
41
+
37
42
class app_telemetry_meter_impl
38
43
{
39
44
public:
40
45
app_telemetry_meter_impl () = default ;
41
- app_telemetry_meter_impl (app_telemetry_meter_impl&&) = default ;
46
+ app_telemetry_meter_impl (app_telemetry_meter_impl&&) = delete ;
42
47
app_telemetry_meter_impl (const app_telemetry_meter_impl&) = delete ;
43
- auto operator =(app_telemetry_meter_impl&&) -> app_telemetry_meter_impl& = default ;
48
+ auto operator =(app_telemetry_meter_impl&&) -> app_telemetry_meter_impl& = delete ;
44
49
auto operator =(const app_telemetry_meter_impl&) -> app_telemetry_meter_impl& = delete ;
45
50
virtual ~app_telemetry_meter_impl () = default ;
46
51
47
52
virtual auto enabled () -> bool = 0;
53
+ [[nodiscard]] virtual auto current_node_labels () const -> std::map<std::string, node_labels> = 0;
54
+ virtual void set_node_labels_cache (std::map<std::string, node_labels>) = 0;
48
55
virtual auto nothing_to_report () -> bool = 0;
49
56
virtual void update_config (const topology::configuration& config) = 0;
50
57
virtual auto value_recorder (const std::string& node_uuid, const std::string& bucket_name)
@@ -101,11 +108,6 @@ namespace couchbase::core
101
108
{
102
109
namespace
103
110
{
104
- struct node_labels {
105
- std::string node;
106
- std::optional<std::string> alt_node;
107
- };
108
-
109
111
struct kv_non_durable_histogram {
110
112
const char * name;
111
113
std::atomic_uint64_t le_1ms{};
@@ -349,6 +351,16 @@ class null_app_telemetry_meter_impl : public app_telemetry_meter_impl
349
351
{
350
352
/* do nothing */
351
353
}
354
+
355
+ [[nodiscard]] auto current_node_labels () const -> std::map<std::string, node_labels> override
356
+ {
357
+ return {};
358
+ }
359
+
360
+ void set_node_labels_cache (std::map<std::string, node_labels>) override
361
+ {
362
+ /* do nothing */
363
+ }
352
364
};
353
365
354
366
class default_app_telemetry_value_recorder : public app_telemetry_value_recorder
@@ -600,6 +612,7 @@ class default_app_telemetry_meter_impl : public app_telemetry_meter_impl
600
612
601
613
void update_config (const topology::configuration& config) override
602
614
{
615
+ node_uuids_in_last_config_.clear ();
603
616
for (const auto & node : config.nodes ) {
604
617
std::optional<std::string> alt_node{};
605
618
if (auto it = node.alt .find (" external" ); it != node.alt .end ()) {
@@ -611,6 +624,7 @@ class default_app_telemetry_meter_impl : public app_telemetry_meter_impl
611
624
node.hostname ,
612
625
alt_node,
613
626
};
627
+ node_uuids_in_last_config_.push_back (node.node_uuid );
614
628
}
615
629
}
616
630
@@ -625,6 +639,23 @@ class default_app_telemetry_meter_impl : public app_telemetry_meter_impl
625
639
return recorders_.empty ();
626
640
}
627
641
642
+ [[nodiscard]] auto current_node_labels () const -> std::map<std::string, node_labels> override
643
+ {
644
+ std::map<std::string, node_labels> labels{};
645
+ for (const auto & node_uuid : node_uuids_in_last_config_) {
646
+ labels[node_uuid] = labels_cache_.at (node_uuid);
647
+ }
648
+ return labels;
649
+ }
650
+
651
+ void set_node_labels_cache (std::map<std::string, node_labels> labels) override
652
+ {
653
+ for (const auto & [node_uuid, _] : labels) {
654
+ node_uuids_in_last_config_.push_back (node_uuid);
655
+ }
656
+ labels_cache_ = std::move (labels);
657
+ }
658
+
628
659
void generate_to (std::vector<std::byte>& buffer, const std::string& agent) override
629
660
{
630
661
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(
@@ -677,6 +708,7 @@ class default_app_telemetry_meter_impl : public app_telemetry_meter_impl
677
708
std::map<std::string, std::shared_ptr<default_app_telemetry_value_recorder>>>
678
709
recorders_{};
679
710
std::map<std::string, node_labels> labels_cache_{};
711
+ std::vector<std::string> node_uuids_in_last_config_{};
680
712
};
681
713
682
714
auto
@@ -699,6 +731,7 @@ app_telemetry_meter::app_telemetry_meter()
699
731
void
700
732
app_telemetry_meter::disable ()
701
733
{
734
+ const std::scoped_lock lock{ impl_mutex_ };
702
735
if (!impl_->enabled ()) {
703
736
return ;
704
737
}
@@ -712,6 +745,7 @@ app_telemetry_meter::disable()
712
745
void
713
746
app_telemetry_meter::enable ()
714
747
{
748
+ const std::scoped_lock lock{ impl_mutex_ };
715
749
if (impl_->enabled ()) {
716
750
return ;
717
751
}
@@ -730,24 +764,28 @@ app_telemetry_meter::~app_telemetry_meter() = default;
730
764
void
731
765
app_telemetry_meter::update_config (const topology::configuration& config)
732
766
{
767
+ const std::shared_lock lock{ impl_mutex_ };
733
768
return impl_->update_config (config);
734
769
}
735
770
736
771
auto
737
772
app_telemetry_meter::value_recorder (const std::string& node_uuid, const std::string& bucket_name)
738
773
-> std::shared_ptr<app_telemetry_value_recorder>
739
774
{
775
+ const std::shared_lock lock{ impl_mutex_ };
740
776
return impl_->value_recorder (node_uuid, bucket_name);
741
777
}
742
778
743
779
void
744
780
app_telemetry_meter::generate_report (std::vector<std::byte>& output_buffer)
745
781
{
782
+ const std::scoped_lock lock{ impl_mutex_ };
746
783
if (impl_->nothing_to_report ()) {
747
784
return ;
748
785
}
749
- auto old_impl = std::move (impl_);
786
+ const auto old_impl = std::move (impl_);
750
787
impl_ = std::make_unique<default_app_telemetry_meter_impl>();
788
+ impl_->set_node_labels_cache (old_impl->current_node_labels ());
751
789
old_impl->generate_to (output_buffer, agent_);
752
790
}
753
791
} // namespace couchbase::core
0 commit comments