Use snake_case
This commit is contained in:
@@ -126,11 +126,11 @@ template <> struct Family<Counter>::State {
|
|||||||
struct PerThreadState {
|
struct PerThreadState {
|
||||||
std::unordered_map<LabelsKey, std::unique_ptr<Counter::State>> instances;
|
std::unordered_map<LabelsKey, std::unique_ptr<Counter::State>> instances;
|
||||||
};
|
};
|
||||||
std::unordered_map<std::thread::id, PerThreadState> perThreadState;
|
std::unordered_map<std::thread::id, PerThreadState> per_thread_state;
|
||||||
|
|
||||||
// Global accumulation state for destroyed threads
|
// Global accumulation state for destroyed threads
|
||||||
std::unordered_map<LabelsKey, std::unique_ptr<Counter::State>>
|
std::unordered_map<LabelsKey, std::unique_ptr<Counter::State>>
|
||||||
globalAccumulatedValues;
|
global_accumulated_values;
|
||||||
|
|
||||||
// Callback-based metrics (global, not per-thread)
|
// Callback-based metrics (global, not per-thread)
|
||||||
std::unordered_map<LabelsKey, MetricCallback<Counter>> callbacks;
|
std::unordered_map<LabelsKey, MetricCallback<Counter>> callbacks;
|
||||||
@@ -153,11 +153,11 @@ template <> struct Family<Histogram>::State {
|
|||||||
struct PerThreadState {
|
struct PerThreadState {
|
||||||
std::unordered_map<LabelsKey, std::unique_ptr<Histogram::State>> instances;
|
std::unordered_map<LabelsKey, std::unique_ptr<Histogram::State>> instances;
|
||||||
};
|
};
|
||||||
std::unordered_map<std::thread::id, PerThreadState> perThreadState;
|
std::unordered_map<std::thread::id, PerThreadState> per_thread_state;
|
||||||
|
|
||||||
// Global accumulation state for destroyed threads
|
// Global accumulation state for destroyed threads
|
||||||
std::unordered_map<LabelsKey, std::unique_ptr<Histogram::State>>
|
std::unordered_map<LabelsKey, std::unique_ptr<Histogram::State>>
|
||||||
globalAccumulatedValues;
|
global_accumulated_values;
|
||||||
|
|
||||||
// Note: No callbacks map - histograms don't support callback-based metrics
|
// Note: No callbacks map - histograms don't support callback-based metrics
|
||||||
};
|
};
|
||||||
@@ -225,14 +225,14 @@ struct Metric {
|
|||||||
|
|
||||||
// Accumulate counter families
|
// Accumulate counter families
|
||||||
for (auto &[name, family] : Metric::get_counter_families()) {
|
for (auto &[name, family] : Metric::get_counter_families()) {
|
||||||
auto thread_it = family->perThreadState.find(thread_id);
|
auto thread_it = family->per_thread_state.find(thread_id);
|
||||||
if (thread_it != family->perThreadState.end()) {
|
if (thread_it != family->per_thread_state.end()) {
|
||||||
for (auto &[labels_key, instance] : thread_it->second.instances) {
|
for (auto &[labels_key, instance] : thread_it->second.instances) {
|
||||||
// Get current thread-local value
|
// Get current thread-local value
|
||||||
double current_value = instance->value;
|
double current_value = instance->value;
|
||||||
|
|
||||||
// Ensure global accumulator exists
|
// Ensure global accumulator exists
|
||||||
auto &global_state = family->globalAccumulatedValues[labels_key];
|
auto &global_state = family->global_accumulated_values[labels_key];
|
||||||
if (!global_state) {
|
if (!global_state) {
|
||||||
global_state = std::make_unique<Counter::State>();
|
global_state = std::make_unique<Counter::State>();
|
||||||
global_state->value = 0.0;
|
global_state->value = 0.0;
|
||||||
@@ -241,20 +241,20 @@ struct Metric {
|
|||||||
// Add thread-local value to global accumulator (mutex already held)
|
// Add thread-local value to global accumulator (mutex already held)
|
||||||
global_state->value += current_value;
|
global_state->value += current_value;
|
||||||
}
|
}
|
||||||
family->perThreadState.erase(thread_it);
|
family->per_thread_state.erase(thread_it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accumulate histogram families
|
// Accumulate histogram families
|
||||||
for (auto &[name, family] : Metric::get_histogram_families()) {
|
for (auto &[name, family] : Metric::get_histogram_families()) {
|
||||||
auto thread_it = family->perThreadState.find(thread_id);
|
auto thread_it = family->per_thread_state.find(thread_id);
|
||||||
if (thread_it != family->perThreadState.end()) {
|
if (thread_it != family->per_thread_state.end()) {
|
||||||
for (auto &[labels_key, instance] : thread_it->second.instances) {
|
for (auto &[labels_key, instance] : thread_it->second.instances) {
|
||||||
// Acquire lock to get consistent snapshot
|
// Acquire lock to get consistent snapshot
|
||||||
std::lock_guard<std::mutex> lock(instance->mutex);
|
std::lock_guard<std::mutex> lock(instance->mutex);
|
||||||
|
|
||||||
// Ensure global accumulator exists
|
// Ensure global accumulator exists
|
||||||
auto &global_state = family->globalAccumulatedValues[labels_key];
|
auto &global_state = family->global_accumulated_values[labels_key];
|
||||||
if (!global_state) {
|
if (!global_state) {
|
||||||
global_state = std::make_unique<Histogram::State>();
|
global_state = std::make_unique<Histogram::State>();
|
||||||
global_state->thresholds = instance->thresholds;
|
global_state->thresholds = instance->thresholds;
|
||||||
@@ -273,7 +273,7 @@ struct Metric {
|
|||||||
global_state->sum += instance->sum;
|
global_state->sum += instance->sum;
|
||||||
global_state->observations += instance->observations;
|
global_state->observations += instance->observations;
|
||||||
}
|
}
|
||||||
family->perThreadState.erase(thread_it);
|
family->per_thread_state.erase(thread_it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,13 +300,13 @@ struct Metric {
|
|||||||
key.labels.empty() ? "(no labels)" : key.labels[0].first.c_str());
|
key.labels.empty() ? "(no labels)" : key.labels[0].first.c_str());
|
||||||
|
|
||||||
auto &ptr =
|
auto &ptr =
|
||||||
family->p->perThreadState[std::this_thread::get_id()].instances[key];
|
family->p->per_thread_state[std::this_thread::get_id()].instances[key];
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
ptr = std::make_unique<Counter::State>();
|
ptr = std::make_unique<Counter::State>();
|
||||||
ptr->value = 0.0;
|
ptr->value = 0.0;
|
||||||
|
|
||||||
// Ensure global accumulator exists for this label set
|
// Ensure global accumulator exists for this label set
|
||||||
auto &global_state = family->p->globalAccumulatedValues[key];
|
auto &global_state = family->p->global_accumulated_values[key];
|
||||||
if (!global_state) {
|
if (!global_state) {
|
||||||
global_state = std::make_unique<Counter::State>();
|
global_state = std::make_unique<Counter::State>();
|
||||||
global_state->value = 0.0;
|
global_state->value = 0.0;
|
||||||
@@ -348,7 +348,7 @@ struct Metric {
|
|||||||
std::unique_lock<std::mutex> _{mutex};
|
std::unique_lock<std::mutex> _{mutex};
|
||||||
LabelsKey key{labels};
|
LabelsKey key{labels};
|
||||||
auto &ptr =
|
auto &ptr =
|
||||||
family->p->perThreadState[std::this_thread::get_id()].instances[key];
|
family->p->per_thread_state[std::this_thread::get_id()].instances[key];
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
ptr = std::make_unique<Histogram::State>();
|
ptr = std::make_unique<Histogram::State>();
|
||||||
// DESIGN: Prometheus-compatible histogram buckets
|
// DESIGN: Prometheus-compatible histogram buckets
|
||||||
@@ -361,7 +361,7 @@ struct Metric {
|
|||||||
ptr->observations = 0;
|
ptr->observations = 0;
|
||||||
|
|
||||||
// Ensure global accumulator exists for this label set
|
// Ensure global accumulator exists for this label set
|
||||||
auto &global_state = family->p->globalAccumulatedValues[key];
|
auto &global_state = family->p->global_accumulated_values[key];
|
||||||
if (!global_state) {
|
if (!global_state) {
|
||||||
global_state = std::make_unique<Histogram::State>();
|
global_state = std::make_unique<Histogram::State>();
|
||||||
global_state->thresholds = ptr->thresholds;
|
global_state->thresholds = ptr->thresholds;
|
||||||
@@ -756,7 +756,7 @@ std::span<std::string_view> render(ArenaAllocator &arena) {
|
|||||||
std::unordered_map<LabelsKey, double> aggregated_values;
|
std::unordered_map<LabelsKey, double> aggregated_values;
|
||||||
|
|
||||||
// First, add thread-local values
|
// First, add thread-local values
|
||||||
for (const auto &[thread_id, per_thread] : family->perThreadState) {
|
for (const auto &[thread_id, per_thread] : family->per_thread_state) {
|
||||||
for (const auto &[labels_key, instance] : per_thread.instances) {
|
for (const auto &[labels_key, instance] : per_thread.instances) {
|
||||||
// Atomic read to match atomic store in Counter::inc()
|
// Atomic read to match atomic store in Counter::inc()
|
||||||
double value;
|
double value;
|
||||||
@@ -767,7 +767,7 @@ std::span<std::string_view> render(ArenaAllocator &arena) {
|
|||||||
|
|
||||||
// Then, add globally accumulated values from destroyed threads
|
// Then, add globally accumulated values from destroyed threads
|
||||||
for (const auto &[labels_key, global_state] :
|
for (const auto &[labels_key, global_state] :
|
||||||
family->globalAccumulatedValues) {
|
family->global_accumulated_values) {
|
||||||
if (global_state) {
|
if (global_state) {
|
||||||
aggregated_values[labels_key] += global_state->value;
|
aggregated_values[labels_key] += global_state->value;
|
||||||
}
|
}
|
||||||
@@ -834,7 +834,7 @@ std::span<std::string_view> render(ArenaAllocator &arena) {
|
|||||||
std::vector<std::pair<std::string_view, std::string_view>> bucket_labels_sv;
|
std::vector<std::pair<std::string_view, std::string_view>> bucket_labels_sv;
|
||||||
|
|
||||||
// First, collect thread-local histogram data
|
// First, collect thread-local histogram data
|
||||||
for (const auto &[thread_id, per_thread] : family->perThreadState) {
|
for (const auto &[thread_id, per_thread] : family->per_thread_state) {
|
||||||
for (const auto &[labels_key, instance] : per_thread.instances) {
|
for (const auto &[labels_key, instance] : per_thread.instances) {
|
||||||
// Extract data under lock - minimize critical section
|
// Extract data under lock - minimize critical section
|
||||||
// Pre-allocate vectors to avoid malloc inside critical section
|
// Pre-allocate vectors to avoid malloc inside critical section
|
||||||
@@ -881,7 +881,7 @@ std::span<std::string_view> render(ArenaAllocator &arena) {
|
|||||||
|
|
||||||
// Then, add globally accumulated values from destroyed threads
|
// Then, add globally accumulated values from destroyed threads
|
||||||
for (const auto &[labels_key, global_state] :
|
for (const auto &[labels_key, global_state] :
|
||||||
family->globalAccumulatedValues) {
|
family->global_accumulated_values) {
|
||||||
if (global_state) {
|
if (global_state) {
|
||||||
auto &[thresholds, counts, sum, observations] =
|
auto &[thresholds, counts, sum, observations] =
|
||||||
aggregated_histograms[labels_key];
|
aggregated_histograms[labels_key];
|
||||||
@@ -963,7 +963,7 @@ void Family<Counter>::register_callback(
|
|||||||
LabelsKey key{std::move(labels)};
|
LabelsKey key{std::move(labels)};
|
||||||
|
|
||||||
// Validate that labels aren't already in use by create() calls
|
// Validate that labels aren't already in use by create() calls
|
||||||
for (const auto &[thread_id, per_thread] : p->perThreadState) {
|
for (const auto &[thread_id, per_thread] : p->per_thread_state) {
|
||||||
validate_or_abort(
|
validate_or_abort(
|
||||||
per_thread.instances.find(key) == per_thread.instances.end(),
|
per_thread.instances.find(key) == per_thread.instances.end(),
|
||||||
"labels already registered as static instance",
|
"labels already registered as static instance",
|
||||||
|
|||||||
Reference in New Issue
Block a user