[StatsPlugin] Add API to check if an instrument is enabled (#36757)

Closes #36757

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36757 from yashykt:CheckIfMetricsAreEnabled 7755e98e60
PiperOrigin-RevId: 639067118
pull/36787/head
Yash Tibrewal 6 months ago committed by Copybara-Service
parent 8faac767da
commit 7ccb51e2ea
  1. 14
      src/core/telemetry/metrics.h
  2. 6
      src/cpp/ext/otel/otel_plugin.cc
  3. 3
      src/cpp/ext/otel/otel_plugin.h
  4. 10
      test/core/test_util/fake_stats_plugin.h
  5. 35
      test/cpp/ext/otel/otel_plugin_test.cc

@ -329,6 +329,9 @@ class StatsPlugin {
// Removes a callback previously added via AddCallback(). The stats
// plugin may not use the callback after this method returns.
virtual void RemoveCallback(RegisteredMetricCallback* callback) = 0;
// Returns true if instrument \a handle is enabled.
virtual bool IsInstrumentEnabled(
GlobalInstrumentsRegistry::GlobalInstrumentHandle handle) = 0;
// Gets a ClientCallTracer associated with this stats plugin which can be used
// in a call.
@ -424,6 +427,17 @@ class GlobalStatsPluginRegistry {
optional_values);
}
}
// Returns true if any of the stats plugins in the group have enabled \a
// handle.
bool IsInstrumentEnabled(
GlobalInstrumentsRegistry::GlobalInstrumentHandle handle) {
for (auto& state : plugins_state_) {
if (state.plugin->IsInstrumentEnabled(handle)) {
return true;
}
}
return false;
}
// Registers a callback to be used to populate callback metrics.
// The callback will update the specified metrics. The callback

@ -913,6 +913,12 @@ grpc_core::ServerCallTracer* OpenTelemetryPlugin::GetServerCallTracer(
scope_config));
}
bool OpenTelemetryPlugin::IsInstrumentEnabled(
grpc_core::GlobalInstrumentsRegistry::GlobalInstrumentHandle handle) {
return !absl::holds_alternative<Disabled>(
instruments_data_.at(handle.index).instrument);
}
} // namespace internal
constexpr absl::string_view

@ -404,6 +404,9 @@ class OpenTelemetryPlugin : public grpc_core::StatsPlugin {
grpc_core::ServerCallTracer* GetServerCallTracer(
std::shared_ptr<grpc_core::StatsPlugin::ScopeConfig> scope_config)
override;
bool IsInstrumentEnabled(
grpc_core::GlobalInstrumentsRegistry::GlobalInstrumentHandle handle)
override;
const absl::AnyInvocable<bool(const grpc_core::ChannelArgs& /*args*/) const>&
server_selector() const {

@ -210,7 +210,8 @@ class FakeStatsPlugin : public StatsPlugin {
bool(const experimental::StatsPluginChannelScope& /*scope*/) const>
channel_filter = nullptr,
bool use_disabled_by_default_metrics = false)
: channel_filter_(std::move(channel_filter)) {
: channel_filter_(std::move(channel_filter)),
use_disabled_by_default_metrics_(use_disabled_by_default_metrics) {
GlobalInstrumentsRegistry::ForEach(
[&](const GlobalInstrumentsRegistry::GlobalInstrumentDescriptor&
descriptor) {
@ -359,6 +360,12 @@ class FakeStatsPlugin : public StatsPlugin {
std::shared_ptr<StatsPlugin::ScopeConfig> /*scope_config*/) override {
return nullptr;
}
bool IsInstrumentEnabled(
GlobalInstrumentsRegistry::GlobalInstrumentHandle handle) override {
const auto& descriptor =
GlobalInstrumentsRegistry::GetInstrumentDescriptor(handle);
return use_disabled_by_default_metrics_ || descriptor.enable_by_default;
}
absl::optional<uint64_t> GetUInt64CounterValue(
GlobalInstrumentsRegistry::GlobalInstrumentHandle handle,
@ -600,6 +607,7 @@ class FakeStatsPlugin : public StatsPlugin {
absl::AnyInvocable<bool(
const experimental::StatsPluginChannelScope& /*scope*/) const>
channel_filter_;
bool use_disabled_by_default_metrics_;
// Instruments.
Mutex mu_;
absl::flat_hash_map<uint32_t, Counter<uint64_t>> uint64_counters_

@ -39,6 +39,7 @@
#include "src/core/lib/config/core_configuration.h"
#include "src/core/telemetry/call_tracer.h"
#include "test/core/test_util/fake_stats_plugin.h"
#include "test/core/test_util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h"
#include "test/cpp/ext/otel/otel_test_library.h"
@ -1281,7 +1282,17 @@ TEST_F(OpenTelemetryPluginOptionEnd2EndTest,
EXPECT_EQ(absl::get<std::string>(server_attributes.at("key5")), "value5");
}
using OpenTelemetryPluginNPCMetricsTest = OpenTelemetryPluginEnd2EndTest;
class OpenTelemetryPluginNPCMetricsTest
: public OpenTelemetryPluginEnd2EndTest {
protected:
void TearDown() override {
// We are tearing down OpenTelemetryPluginEnd2EndTest first to ensure that
// gRPC has shutdown before we reset the instruments registry.
OpenTelemetryPluginEnd2EndTest::TearDown();
grpc_core::GlobalInstrumentsRegistryTestPeer::
ResetGlobalInstrumentsRegistry();
}
};
TEST_F(OpenTelemetryPluginNPCMetricsTest, RecordUInt64Counter) {
constexpr absl::string_view kMetricName = "uint64_counter";
@ -1677,6 +1688,28 @@ TEST_F(OpenTelemetryPluginNPCMetricsTest,
::testing::DoubleEq(kMax), kCount))))));
}
TEST_F(OpenTelemetryPluginNPCMetricsTest, InstrumentsEnabledTest) {
constexpr absl::string_view kDoubleHistogramMetricName =
"yet_another_yet_another_double_histogram";
constexpr absl::string_view kUnit64CounterMetricName = "uint64_counter";
auto histogram_handle =
grpc_core::GlobalInstrumentsRegistry::RegisterDoubleHistogram(
kDoubleHistogramMetricName, "A simple double histogram.", "unit",
/*enable_by_default=*/false)
.Build();
auto counter_handle =
grpc_core::GlobalInstrumentsRegistry::RegisterUInt64Counter(
kUnit64CounterMetricName, "A simple unit64 counter.", "unit",
/*enable_by_default=*/false)
.Build();
Init(std::move(Options().set_metric_names({kDoubleHistogramMetricName})));
auto stats_plugins =
grpc_core::GlobalStatsPluginRegistry::GetStatsPluginsForServer(
grpc_core::ChannelArgs());
EXPECT_TRUE(stats_plugins.IsInstrumentEnabled(histogram_handle));
EXPECT_FALSE(stats_plugins.IsInstrumentEnabled(counter_handle));
}
using OpenTelemetryPluginCallbackMetricsTest = OpenTelemetryPluginEnd2EndTest;
// The callback minimal interval is longer than the OT reporting interval, so we

Loading…
Cancel
Save