// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 #pragma once #include "opentelemetry/metrics/async_instruments.h" #include "opentelemetry/metrics/meter.h" #include "opentelemetry/metrics/meter_provider.h" #include "opentelemetry/metrics/observer_result.h" #include "opentelemetry/metrics/sync_instruments.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { template class NoopCounter : public Counter { public: NoopCounter(nostd::string_view /* name */, nostd::string_view /* description */, nostd::string_view /* unit */) noexcept {} void Add(T /* value */) noexcept override {} void Add(T /* value */, const context::Context & /* context */) noexcept override {} void Add(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {} void Add(T /* value */, const common::KeyValueIterable & /* attributes */, const context::Context & /* context */) noexcept override {} }; template class NoopHistogram : public Histogram { public: NoopHistogram(nostd::string_view /* name */, nostd::string_view /* description */, nostd::string_view /* unit */) noexcept {} void Record(T /* value */, const context::Context & /* context */) noexcept override {} void Record(T /* value */, const common::KeyValueIterable & /* attributes */, const context::Context & /* context */) noexcept override {} #if OPENTELEMETRY_ABI_VERSION_NO >= 2 void Record(T /*value*/, const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override {} void Record(T /*value*/) noexcept override {} #endif }; template class NoopUpDownCounter : public UpDownCounter { public: NoopUpDownCounter(nostd::string_view /* name */, nostd::string_view /* description */, nostd::string_view /* unit */) noexcept {} ~NoopUpDownCounter() override = default; void Add(T /* value */) noexcept override {} void Add(T /* value */, const context::Context & /* context */) noexcept override {} void Add(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {} void Add(T /* value */, const common::KeyValueIterable & /* attributes */, const context::Context & /* context */) noexcept override {} }; #if OPENTELEMETRY_ABI_VERSION_NO >= 2 template class NoopGauge : public Gauge { public: NoopGauge(nostd::string_view /* name */, nostd::string_view /* description */, nostd::string_view /* unit */) noexcept {} ~NoopGauge() override = default; void Record(T /* value */) noexcept override {} void Record(T /* value */, const context::Context & /* context */) noexcept override {} void Record(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {} void Record(T /* value */, const common::KeyValueIterable & /* attributes */, const context::Context & /* context */) noexcept override {} }; #endif class NoopObservableInstrument : public ObservableInstrument { public: NoopObservableInstrument(nostd::string_view /* name */, nostd::string_view /* description */, nostd::string_view /* unit */) noexcept {} void AddCallback(ObservableCallbackPtr, void * /* state */) noexcept override {} void RemoveCallback(ObservableCallbackPtr, void * /* state */) noexcept override {} }; /** * No-op implementation of Meter. */ class NoopMeter final : public Meter { public: nostd::unique_ptr> CreateUInt64Counter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopCounter(name, description, unit)}; } nostd::unique_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopCounter(name, description, unit)}; } nostd::shared_ptr CreateInt64ObservableCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::shared_ptr( new NoopObservableInstrument(name, description, unit)); } nostd::shared_ptr CreateDoubleObservableCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::shared_ptr( new NoopObservableInstrument(name, description, unit)); } nostd::unique_ptr> CreateUInt64Histogram( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{ new NoopHistogram(name, description, unit)}; } nostd::unique_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopHistogram(name, description, unit)}; } #if OPENTELEMETRY_ABI_VERSION_NO >= 2 nostd::unique_ptr> CreateInt64Gauge(nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; } nostd::unique_ptr> CreateDoubleGauge(nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; } #endif nostd::shared_ptr CreateInt64ObservableGauge( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::shared_ptr( new NoopObservableInstrument(name, description, unit)); } nostd::shared_ptr CreateDoubleObservableGauge( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::shared_ptr( new NoopObservableInstrument(name, description, unit)); } nostd::unique_ptr> CreateInt64UpDownCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{ new NoopUpDownCounter(name, description, unit)}; } nostd::unique_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{ new NoopUpDownCounter(name, description, unit)}; } nostd::shared_ptr CreateInt64ObservableUpDownCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::shared_ptr( new NoopObservableInstrument(name, description, unit)); } nostd::shared_ptr CreateDoubleObservableUpDownCounter( nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override { return nostd::shared_ptr( new NoopObservableInstrument(name, description, unit)); } }; /** * No-op implementation of a MeterProvider. */ class NoopMeterProvider final : public MeterProvider { public: NoopMeterProvider() : meter_{nostd::shared_ptr(new NoopMeter)} {} #if OPENTELEMETRY_ABI_VERSION_NO >= 2 nostd::shared_ptr GetMeter( nostd::string_view /* name */, nostd::string_view /* version */, nostd::string_view /* schema_url */, const common::KeyValueIterable * /* attributes */) noexcept override { return meter_; } #else nostd::shared_ptr GetMeter(nostd::string_view /* name */, nostd::string_view /* version */, nostd::string_view /* schema_url */) noexcept override { return meter_; } #endif #if OPENTELEMETRY_ABI_VERSION_NO >= 2 void RemoveMeter(nostd::string_view /* name */, nostd::string_view /* version */, nostd::string_view /* schema_url */) noexcept override {} #endif private: nostd::shared_ptr meter_; }; } // namespace metrics OPENTELEMETRY_END_NAMESPACE