1414
1515#include " tpu_sync/telemetry/buffered_metrics_exporter.h"
1616
17+ #include < algorithm>
1718#include < cstdint>
1819#include < map>
1920#include < memory>
2021#include < string>
2122#include < utility>
2223#include < vector>
2324
25+ #include " absl/container/inlined_vector.h"
2426#include " absl/strings/str_cat.h"
2527#include " absl/strings/string_view.h"
2628#include " absl/types/span.h"
@@ -32,21 +34,82 @@ namespace {
3234
3335constexpr absl::string_view kMetricPrefix = " tpu_raiden_" ;
3436
37+ void AppendEscapedLabelValue (
38+ absl::string_view value,
39+ absl::InlinedVector<char , kDefaultInlinedLabelBufferSize >* out) {
40+ for (char c : value) {
41+ switch (c) {
42+ case ' \\ ' :
43+ out->push_back (' \\ ' );
44+ out->push_back (' \\ ' );
45+ break ;
46+ case ' "' :
47+ out->push_back (' \\ ' );
48+ out->push_back (' "' );
49+ break ;
50+ case ' \n ' :
51+ out->push_back (' \\ ' );
52+ out->push_back (' n' );
53+ break ;
54+ default :
55+ out->push_back (c);
56+ break ;
57+ }
58+ }
59+ }
60+
3561} // namespace
3662
63+ void FormatCanonicalLabels (
64+ LabelSpan labels,
65+ absl::InlinedVector<char , kDefaultInlinedLabelBufferSize >* out) {
66+ out->clear ();
67+ if (labels.empty ()) {
68+ return ;
69+ }
70+ absl::InlinedVector<MetricLabel, kDefaultInlinedLabelCapacity > sorted_labels (
71+ labels.begin (), labels.end ());
72+ std::sort (sorted_labels.begin (), sorted_labels.end ());
73+
74+ out->push_back (' {' );
75+ bool first = true ;
76+ for (const auto & [key, value] : sorted_labels) {
77+ if (!first) {
78+ out->push_back (' ,' );
79+ }
80+ first = false ;
81+ out->insert (out->end (), key.begin (), key.end ());
82+ out->push_back (' =' );
83+ out->push_back (' "' );
84+ AppendEscapedLabelValue (value, out);
85+ out->push_back (' "' );
86+ }
87+ out->push_back (' }' );
88+ }
89+
90+ std::string FormatCanonicalLabels (LabelSpan labels) {
91+ absl::InlinedVector<char , kDefaultInlinedLabelBufferSize > buf;
92+ FormatCanonicalLabels (labels, &buf);
93+ return std::string (buf.data (), buf.size ());
94+ }
95+
3796BufferedMetricsExporter::BufferedMetricsExporter (
3897 absl::Span<const MetricMetadata> metrics) {
3998 for (const auto & meta : metrics) {
4099 switch (meta.type ) {
41100 case MetricType::kCounter :
42- counters_.emplace (meta.name ,
43- std::make_unique<LockFreeCounterAccumulator>());
101+ counters_.emplace (
102+ meta.name ,
103+ std::make_unique<
104+ MetricFamilyBuffer<LockFreeCounterAccumulator>>());
44105 break ;
45106 case MetricType::kGauge :
46- gauges_.emplace (meta.name , std::make_unique<QueueBuffer<>>());
107+ gauges_.emplace (
108+ meta.name , std::make_unique<MetricFamilyBuffer<QueueBuffer<>>>());
47109 break ;
48110 case MetricType::kHistogram :
49- histograms_.emplace (meta.name , std::make_unique<QueueBuffer<>>());
111+ histograms_.emplace (
112+ meta.name , std::make_unique<MetricFamilyBuffer<QueueBuffer<>>>());
50113 break ;
51114 }
52115 }
@@ -57,15 +120,19 @@ void BufferedMetricsExporter::IncrementCounter(absl::string_view name,
57120 uint64_t val) const {
58121 auto it = counters_.find (name);
59122 if (it != counters_.end ()) {
60- it->second ->Add (val);
123+ if (auto * acc = it->second ->GetOrCreate (labels)) {
124+ acc->Add (val);
125+ }
61126 }
62127}
63128
64129void BufferedMetricsExporter::SetGauge (absl::string_view name, LabelSpan labels,
65130 double val) const {
66131 auto it = gauges_.find (name);
67132 if (it != gauges_.end ()) {
68- it->second ->Push (val);
133+ if (auto * buf = it->second ->GetOrCreate (labels)) {
134+ buf->Push (val);
135+ }
69136 }
70137}
71138
@@ -74,36 +141,51 @@ void BufferedMetricsExporter::ObserveHistogram(absl::string_view name,
74141 double val) const {
75142 auto it = histograms_.find (name);
76143 if (it != histograms_.end ()) {
77- it->second ->Push (val);
144+ if (auto * buf = it->second ->GetOrCreate (labels)) {
145+ buf->Push (val);
146+ }
78147 }
79148}
80149
81150std::map<std::string, std::vector<double >>
82151BufferedMetricsExporter::GetAndResetMetricSamples () {
83152 std::map<std::string, std::vector<double >> result;
84153
85- for (const auto & [name, counter] : counters_) {
86- uint64_t delta = counter->ExchangeAndReset ();
87- if (delta > 0 ) {
88- std::string full_name = absl::StrCat (kMetricPrefix , name);
89- result[full_name].push_back (static_cast <double >(delta));
90- }
154+ for (const auto & [name, family_buffer] : counters_) {
155+ family_buffer->ForEachAccumulator (
156+ [&](absl::string_view canonical_labels,
157+ LockFreeCounterAccumulator* counter) {
158+ uint64_t delta = counter->ExchangeAndReset ();
159+ if (delta > 0 ) {
160+ std::string full_name =
161+ absl::StrCat (kMetricPrefix , name, canonical_labels);
162+ result[full_name].push_back (static_cast <double >(delta));
163+ }
164+ });
91165 }
92166
93- for (const auto & [name, gauge] : gauges_) {
94- std::vector<double > samples = gauge->ExtractAndReset ();
95- if (!samples.empty ()) {
96- std::string full_name = absl::StrCat (kMetricPrefix , name);
97- result[full_name] = std::move (samples);
98- }
167+ for (const auto & [name, family_buffer] : gauges_) {
168+ family_buffer->ForEachAccumulator (
169+ [&](absl::string_view canonical_labels, QueueBuffer<>* gauge) {
170+ std::vector<double > samples = gauge->ExtractAndReset ();
171+ if (!samples.empty ()) {
172+ std::string full_name =
173+ absl::StrCat (kMetricPrefix , name, canonical_labels);
174+ result[full_name] = std::move (samples);
175+ }
176+ });
99177 }
100178
101- for (const auto & [name, histogram] : histograms_) {
102- std::vector<double > samples = histogram->ExtractAndReset ();
103- if (!samples.empty ()) {
104- std::string full_name = absl::StrCat (kMetricPrefix , name);
105- result[full_name] = std::move (samples);
106- }
179+ for (const auto & [name, family_buffer] : histograms_) {
180+ family_buffer->ForEachAccumulator (
181+ [&](absl::string_view canonical_labels, QueueBuffer<>* histogram) {
182+ std::vector<double > samples = histogram->ExtractAndReset ();
183+ if (!samples.empty ()) {
184+ std::string full_name =
185+ absl::StrCat (kMetricPrefix , name, canonical_labels);
186+ result[full_name] = std::move (samples);
187+ }
188+ });
107189 }
108190
109191 return result;
0 commit comments