fix(telemetry): resolve phase-7 clang-tidy findings

- OTelCollector::formatName made static (no member state); braces added;
  <cctype> added for std::tolower.
- Telemetry.cpp getMeter(): braces around single-statement if.
- GetMeter.cpp: drop unused <opentelemetry/metrics/meter.h> and
  sync_instruments.h; add <xrpl/beast/utility/Journal.h> and <memory>;
  make sdkProvider const.
- ValidationTracker.h: add <cstddef> for std::size_t.
- detail/ValidationTracker.cpp: use std::views::reverse range-based loop;
  add <ranges>.
- test ValidationTracker.cpp: use auto for cast result.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pratik Mankawde
2026-07-09 15:09:43 +01:00
parent e304d65d63
commit 7ab34d2e37
6 changed files with 19 additions and 10 deletions

View File

@@ -56,6 +56,7 @@
#include <algorithm>
#include <atomic>
#include <cctype>
#include <chrono>
#include <cstdint>
#include <memory>
@@ -490,8 +491,8 @@ public:
* @param name Raw metric name from beast::insight callers.
* @return Fully-qualified metric name.
*/
std::string
formatName(std::string const& name) const;
static std::string
formatName(std::string const& name);
private:
/** Journal for log output. */
@@ -821,7 +822,7 @@ OTelCollectorImp::otelMeter() const
}
std::string
OTelCollectorImp::formatName(std::string const& name) const
OTelCollectorImp::formatName(std::string const& name)
{
// Produce a clean, lowercase, Prometheus-compatible metric name.
// No prefix — the OTel resource (service.name) identifies the service.
@@ -831,9 +832,13 @@ OTelCollectorImp::formatName(std::string const& name) const
for (char const c : name)
{
if (c == '.' || c == ' ')
{
result += '_';
}
else
{
result += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
}
return result;
}

View File

@@ -535,8 +535,10 @@ public:
getMeter(std::string_view name = kMeterName) override
{
if (!meterProvider_)
{
return metrics_api::Provider::GetMeterProvider()->GetMeter(
std::string(name), std::string(kMeterVersion));
}
return meterProvider_->GetMeter(std::string(name), std::string(kMeterVersion));
}

View File

@@ -17,18 +17,18 @@
#ifdef XRPL_ENABLE_TELEMETRY
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/telemetry/Telemetry.h>
#include <gtest/gtest.h>
#include <opentelemetry/metrics/meter.h>
#include <opentelemetry/metrics/meter_provider.h>
#include <opentelemetry/metrics/provider.h>
#include <opentelemetry/metrics/sync_instruments.h>
#include <opentelemetry/nostd/shared_ptr.h>
#include <opentelemetry/sdk/metrics/meter_provider.h>
#include <opentelemetry/sdk/metrics/meter_provider_factory.h>
#include <cstdint>
#include <memory>
#include <string>
using namespace xrpl;
@@ -85,7 +85,7 @@ TEST(GetMeter, global_provider_meter_accepts_updown_counter)
// A views-less SDK MeterProvider with no reader is sufficient to prove the
// API contract: it hands out a real (non-noop) Meter that creates working
// instruments. No exporter/reader means no background threads or network.
std::shared_ptr<metrics_sdk::MeterProvider> sdkProvider =
std::shared_ptr<metrics_sdk::MeterProvider> const sdkProvider =
metrics_sdk::MeterProviderFactory::Create();
metrics_api::Provider::SetMeterProvider(
opentelemetry::nostd::shared_ptr<metrics_api::MeterProvider>(sdkProvider));

View File

@@ -176,7 +176,7 @@ TEST_F(ValidationTrackerTest, MaxPendingEventsTrimming)
for (std::size_t i = 0; i < kCount; ++i)
{
auto const hash = makeHash(i + 1);
LedgerIndex const seq = static_cast<LedgerIndex>(i + 1);
auto const seq = static_cast<LedgerIndex>(i + 1);
tracker_.recordOurValidation(hash, seq);
tracker_.recordNetworkValidation(hash, seq);
}

View File

@@ -10,6 +10,7 @@
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <mutex>

View File

@@ -12,6 +12,7 @@
#include <cstdint>
#include <deque>
#include <mutex>
#include <ranges>
namespace xrpl::telemetry {
@@ -255,11 +256,11 @@ void
ValidationTracker::repairWindowEntry(std::deque<WindowEvent>& window, uint256 const& hash)
{
// Scan backwards since late repairs target recently added entries.
for (auto it = window.rbegin(); it != window.rend(); ++it)
for (auto& event : std::views::reverse(window))
{
if (!it->agreed && it->ledgerHash == hash)
if (!event.agreed && event.ledgerHash == hash)
{
it->agreed = true;
event.agreed = true;
return;
}
}