Skip to content

Commit

Permalink
fix: skip metrics when not configured
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Dec 6, 2023
1 parent ceb44a6 commit 49ea432
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/lib/metrics/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Metrics &Metrics::getInstance() {
}

void Metrics::init(Options opts) {
if (!opts.enableOStreamExporter && !opts.enablePrometheusExporter) {
return;
}

auto provider = metrics_sdk::MeterProviderFactory::Create();
auto* p = static_cast<metrics_sdk::MeterProvider*>(provider.get());

Expand Down Expand Up @@ -69,12 +73,7 @@ void Metrics::init(Options opts) {
latencyHistograms[name] = getMeter()->CreateDoubleHistogram(name, "Latency", "us");
}

if (opts.enableOStreamExporter || opts.enablePrometheusExporter) {
metrics_api::Provider::SetMeterProvider(std::move(provider));
} else {
std::shared_ptr<metrics_api::MeterProvider> none;
metrics_api::Provider::SetMeterProvider(none);
}
metrics_api::Provider::SetMeterProvider(std::move(provider));
}

void Metrics::shutdown() {
Expand All @@ -85,7 +84,6 @@ void Metrics::shutdown() {
ScopedLatency::ScopedLatency(std::string_view name, const std::string &histogramName, const std::string &scopeKey) :

Check warning on line 84 in src/lib/metrics/metrics.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/lib/metrics/metrics.cpp#L84

Function parameter 'name' should be passed by const reference.
Raw output
src/lib/metrics/metrics.cpp:84:Function parameter 'name' should be passed by const reference.
ScopedLatency(name, g_metrics().latencyHistograms[histogramName], { { scopeKey, std::string(name) } }, g_metrics().defaultContext) {
if (histogram == nullptr) {
g_logger().debug("ScopedLatency: Histogram {} not found", histogramName);
stopped = true;
return;
}
Expand Down
12 changes: 11 additions & 1 deletion src/lib/metrics/metrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ namespace metrics {

void addCounter(std::string_view name, double value, std::map<std::string, std::string> attrs = {}) {
std::scoped_lock lock(mutex_);
if (!getMeter()) {
return;
}
if (counters.find(name) == counters.end()) {
std::string nameStr(name);
counters[name] = getMeter()->CreateDoubleCounter(nameStr);
Expand All @@ -128,6 +131,9 @@ namespace metrics {

void addUpDownCounter(std::string_view name, int value, std::map<std::string, std::string> attrs = {}) {
std::scoped_lock lock(mutex_);
if (!getMeter()) {
return;
}
if (upDownCounters.find(name) == upDownCounters.end()) {
std::string nameStr(name);
upDownCounters[name] = getMeter()->CreateInt64UpDownCounter(nameStr);
Expand All @@ -145,7 +151,11 @@ namespace metrics {
phmap::flat_hash_map<std::string, Counter<double>> counters;

Meter getMeter() {
return metrics_api::Provider::GetMeterProvider()->GetMeter(meterName, otelVersion);
auto provider = metrics_api::Provider::GetMeterProvider();
if (provider == nullptr) {
return {};
}
return provider->GetMeter(meterName, otelVersion);
}

private:
Expand Down

0 comments on commit 49ea432

Please sign in to comment.