From bc38af61c789483f6b4e74dd6a18e6fbaf3ce3ad Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Mon, 29 Jun 2020 15:43:23 -0400 Subject: [PATCH 01/11] first pass at metrics api --- .../opentelemetry/metrics/async_instruments.h | 136 +++++++++ .../opentelemetry/metrics/instrument.h | 148 ++++++++++ api/include/opentelemetry/metrics/noop.h | 185 +++++++++++- .../opentelemetry/metrics/sync_instruments.h | 278 ++++++++++++++++++ 4 files changed, 745 insertions(+), 2 deletions(-) create mode 100644 api/include/opentelemetry/metrics/async_instruments.h create mode 100644 api/include/opentelemetry/metrics/instrument.h create mode 100644 api/include/opentelemetry/metrics/sync_instruments.h diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h new file mode 100644 index 0000000000..8451e41f1f --- /dev/null +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -0,0 +1,136 @@ +#pragma once + +#include "instrument.h" +#include "opentelemetry/common/attribute_value.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +class Result {}; + +class IntObserverResult: public Result { + +public: + + IntObserverResult() = default; + + IntObserverResult(*AsynchronousInstrument instrument, int value); + +} + +class DoubleObserverResult: public Result { + +public: + + DoubleObserverResult() = default; + + DoubleObserverResult(*AsynchronousInstrument instrument, double value); + +} + +class IntValueObserver: public AsynchronousInstrument{ + +public: + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void observe(int value, std::string_view &labels) override; + +private: + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback)(IntObserverResult); + +} + +class DoubleValueObserver: public AsynchronousInstrument{ + +public: + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void observe(int value, std::string_view &labels) override; + +private: + void (*callback)(DoubleObserverResult); + +} + +class IntSumObserver: public AsynchronousInstrument{ + +public: + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void observe(int value, std::string_view &labels) override; + +private: + void (*callback)(IntObserverResult); + +} + +class DoubleSumObserver: public AsynchronousInstrument{ + +public: + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void observe(int value, std::string_view &labels) override; + +private: + void (*callback)(DoubleObserverResult); + +} + +class IntUpDownSumObserver: public AsynchronousInstrument{ + +public: + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void observe(int value, std::string_view &labels) override; + +private: + void (*callback)(IntObserverResult); + +} + +class DoubleUpDownSumObserver: public AsynchronousInstrument{ + +public: + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void observe(int value, std::string_view &labels) override; + +private: + void (*callback)(DoubleObserverResult); + +} + +} +OPENTELEMETRY_BEGIN_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h new file mode 100644 index 0000000000..2dd1f0f7bd --- /dev/null +++ b/api/include/opentelemetry/metrics/instrument.h @@ -0,0 +1,148 @@ +#pragma once + +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/common/attribute_value.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +enum class InstrumentKind +{ + Counter, + UpDownCounter, + ValueRecorder, + ValueObserver, + SumObserver, + UpDownSumObserver +}; + +enum class BoundInstrumentKind +{ + BoundCounter, + BoundUpDownCounter, + BoundValueRecorder, + BoundValueObserver, + BoundSumObserver, + BoundUpDownSumObserver +}; + +//Do not need the getters virtual because each base class should have access to them -- will not be overriden. Also, I removed all private variables and will add them in when writing the SDK because I have more flexibility with types and they are not necessary for a minimal noop implementation. +class Instrument { + +public: + + // Note that Instruments should be created using the Meter class. + // Please refer to meter.h for documentation. + Instrument() = default; + + + /** + * Base class constructor for all other instrument types. Whether or not + * an instrument is synchronous or bound, it requires a name, description, + * unit, and enabled flag. + * + * @param name is the identifier of the instrumenting library + * @param description explains what the metric captures + * @param unit specified the data type held in the instrument + * @param enabled determins if the metric is currently capturing data + * @return Instrument type with the specified attirbutes + */ + Instrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + // Returns true if the instrument is enabled and collecting data + bool IsEnabled(); + + // Return the instrument name + nostd::string_view GetName(); + + // Return the instrument description + nostd::string_view GetDescription(); + + // Return the insrument's units of measurement + nostd::string_view GetUnits(); + +}; + + +class BoundSynchronousInstrument: public Instrument { + +public: + + BoundSynchronousInstrument() = default; + + BoundSynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /** + * Frees the resources associated with this Bound Instrument. + * The Metric from which this instrument was created is not impacted. + * + * @param none + * @return void + */ + void unbind(); + + /** + * Records a single synchronous metric event. //Call to aggregator + * Since this is a bound synchronous instrument, labels are notrequired in * metric capture calls. + * + * @param value the numerical representation of the metric being captured + * @return void + */ + void update(common::AttributeValue value); //don't want this virtual because the base boundsynchronousinstrument will call the aggregator in the sdk +}; + + +// Thinking ahead, I think I want to change this to a shared pointer so I can store pointers to instruments and the meter class has access to the ref counts + +class SynchronousInstrument: public Instrument {// need update virtual or not? + +public: + + SynchronousInstrument() = default; + + SynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /** + * Returns a Bound Instrument associated with the specified labels. * Multiples requests with the same set of labels may return the same + * Bound Instrument instance. + * + * It is recommended that callers keep a reference to the Bound Instrument instead of always + * calling this method for every operation. + * + * @param labels the set of labels, as key-value pairs. + * @return a Bound Instrument + */ + BoundSynchronousInstrument bind(nostd::string_view & labels); + + /** + * Records a single synchronous metric event. + * Since this is an unbound synchronous instrument, labels are required in * metric capture calls. + * + * + * @param labels the set of labels, as key-value pairs. + * @param value the numerical representation of the metric being captured + * @return void + */ + void update(common::AttributeValue value, nostd::string_view &labels); //add or record +}; + +class AsynchronousInstrument: public Instrument{ + +public: + + AsynchronousInstrument() = default; + + /** + * Captures data by activating the callback function associated with the + * instrument and storing its return value. Callbacks for asychronous + * instruments are defined during construction. + * + * @param none + * @return none + */ + void observe(); +}; + +} +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index a53a591232..b23af52a44 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -4,11 +4,13 @@ // This file is part of the internal implementation of OpenTelemetry. Nothing in this file should be // used directly. Please refer to meter.h for documentation on these interfaces. +#include "opentelemetry/version.h" +#include "opentelemetry/metrics/instrument.h" #include "opentelemetry/metrics/meter.h" #include "opentelemetry/metrics/meter_provider.h" +#include "opentelemetry/metrics/sync_instruments.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/unique_ptr.h" -#include "opentelemetry/version.h" #include @@ -38,5 +40,184 @@ class NoopMeterProvider final : public opentelemetry::metrics::MeterProvider private: nostd::shared_ptr meter_; }; -} // namespace metrics + + +class BoundNoopIntCounter : public BoundIntCounter { + +public: + + BoundNoopIntCounter() = default; + + BoundNoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + + void add(int value){} + + void unbind(); +}; + +class NoopIntCounter : public IntCounter { + +public: + + NoopIntCounter() = default; + + NoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + + + BoundNoopIntCounter bind(const nostd::string_view & /*labels*/){ + return BoundNoopIntCounter(); + } + + void add(int value, const std::string_view & /*labels*/){} + +}; + + +class BoundNoopDoubleCounter : public DoubleCounter { + +public: + + BoundNoopDoubleCounter() = default; + + BoundNoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + + void add(double value){} + + void unbind(); +}; + +class NoopDoubleCounter : public DoubleCounter { + +public: + + NoopDoubleCounter()=default; + + NoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + + + BoundNoopDoubleCounter bind(const nostd::string_view & /*labels*/){ + return BoundNoopDoubleCounter(); + } + + void add(double value, const std::string_view & /*labels*/){} + +}; + +class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter { + +public: + + BoundNoopIntUpDownCounter() = default; + + BoundNoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + + void add(int value){} + + void unbind(); +}; + +class NoopIntUpDownCounter : public IntUpDownCounter { + +public: + + NoopIntUpDownCounter()=default; + + NoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + + + BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ + return BoundNoopIntUpDownCounter(); + } + + void add(int value, const std::string_view & /*labels*/){} +}; + +class BoundNoopDoubleUpDownCounter : public DoubleUpDownCounter { + +public: + + BoundNoopDoubleUpDownCounter() = default; + + BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + + void add(double value){} + + void unbind(); +}; + +class NoopDoubleUpDownCounter : public DoubleUpDownCounter { + +public: + + NoopDoubleUpDownCounter()=default; + + NoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + + + BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ + return BoundNoopIntUpDownCounter(); + } + + void add(double value, const std::string_view & /*labels*/){} +}; + +class BoundNoopIntValueRecorder : public IntValueRecorder { + +public: + + BoundNoopIntValueRecorder() = default; + + BoundNoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + + void record(int value){} + + void unbind(); +}; + +class NoopIntValueRecorder : public IntValueRecorder { + +public: + + NoopIntValueRecorder()=default; + + NoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + + + BoundNoopIntValueRecorder bind(const nostd::string_view & /*labels*/){ + return BoundNoopIntValueRecorder(); + } + + void record(int value, const std::string_view & /*labels*/){} +}; + +class BoundNoopDoubleValueRecorder : public DoubleValueRecorder { + +public: + + BoundNoopDoubleValueRecorder() = default; + + BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + + void record(int value){} + + void unbind(); +}; + +class NoopDoubleValueRecorder : public DoubleValueRecorder { + +public: + + NoopDoubleValueRecorder()=default; + + NoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + + + BoundNoopDoubleValueRecorder bind(const nostd::string_view & /*labels*/){ + return BoundNoopDoubleValueRecorder(); + } + + void record(double value, const std::string_view & /*labels*/){} +}; + +} // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h new file mode 100644 index 0000000000..375be04fcc --- /dev/null +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -0,0 +1,278 @@ +#pragma once + +#include "instrument.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +class BoundIntCounter: public BoundSynchronousInstrument{ //override bind? + +public: + + BoundIntCounter() = default; + + BoundIntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(int value); + + void unbind(); +}; + +class IntCounter: public SynchronousInstrument{ + +public: + + IntCounter() = default; + + IntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + BoundIntCounter bind(nostd::string_view &labels); + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(int value, std::string_view &labels); +}; + +class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? + +public: + + BoundDoubleCounter() = default; + + BoundDoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(int value); +}; + +class DoubleCounter: public SynchronousInstrument{ + +public: + + DoubleCounter() = default; + + DoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + BoundDoubleCounter bind(nostd::string_view &labels); + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(int value, nostd::string_view &labels); +}; + +class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? + +public: + + BoundIntUpDownCounter() = default; + + BoundIntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(int value); +}; + +class IntUpDownCounter: public SynchronousInstrument{ + +public: + + IntUpDownCounter() = default; + + IntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + BoundIntUpDownCounter bind(nostd::string_view &labels); + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. UpDownCounters can + * accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(int value, nostd::string_view &labels); +}; + +class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bind? + +public: + + BoundDoubleUpDownCounter() = default; + + BoundDoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(double value); +}; + +class DoubleUpDownCounter: public SynchronousInstrument{ + +public: + + DoubleUpDownCounter() = default; + + DoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + BoundDoubleUpDownCounter bind(nostd::string_view &labels); + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. UpDownCounters can + * accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void add(double value, nostd::string_view &labels); +}; + +class BoundIntValueRecorder: public BoundSynchronousInstrument{ //override bind? + +public: + + BoundIntValueRecorder() = default; + + BoundIntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + */ + void record(int value); +}; + +class IntValueRecorder: public SynchronousInstrument{ + +public: + + IntValueRecorder() = default; + + IntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + BoundIntValueRecorder bind(nostd::string_view &labels); + + /* + * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels should contain + * the keys and values to be associated with this value. ValueRecorders can + * accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void record(int value, nostd::string_view &labels); +}; + +class BoundDoubleValueRecorder: public BoundSynchronousInstrument{ //override bind? + +public: + + BoundDoubleValueRecorder() = default; + + BoundDoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + */ + void record(double value); +}; + +class DoubleValueRecorder: public SynchronousInstrument{ + +public: + + DoubleValueRecorder() = default; + + DoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + BoundDoubleValueRecorder bind(nostd::string_view &labels); + + /* + * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels should contain + * the keys and values to be associated with this value. ValueRecorders can + * accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + void record(double value, nostd::string_view &labels); +}; + +} // namespace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From bd9f13cf90771f9a95dc1d194bb14a218397e160 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Mon, 29 Jun 2020 15:45:35 -0400 Subject: [PATCH 02/11] test files --- api/test/metrics/BUILD | 13 +++- api/test/metrics/int_counter_test.cc | 92 ++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 api/test/metrics/int_counter_test.cc diff --git a/api/test/metrics/BUILD b/api/test/metrics/BUILD index 54feb0c75b..0ae0c79a60 100644 --- a/api/test/metrics/BUILD +++ b/api/test/metrics/BUILD @@ -1,5 +1,3 @@ -load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") - cc_test( name = "meter_provider_test", srcs = [ @@ -10,3 +8,14 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "int_counter", + srcs = [ + "int_counter_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) \ No newline at end of file diff --git a/api/test/metrics/int_counter_test.cc b/api/test/metrics/int_counter_test.cc new file mode 100644 index 0000000000..c1f596e751 --- /dev/null +++ b/api/test/metrics/int_counter_test.cc @@ -0,0 +1,92 @@ + +#include "opentelemetry/metrics/noop.h" + +#include +#include + +#include + +using namespace opentelemetry::metrics; + +TEST(Counter, DefaultConstruction) +{ + NoopIntCounter alpha("test", "none", "unitless", true); + NoopDoubleCounter beta("other", "none", "unitless", true); + + alpha.bind("key: value"); + + BoundNoopIntCounter gamma= alpha.bind("key: value"); + BoundNoopDoubleCounter delta= beta.bind("key: value"); + +} + +TEST(Counter, Add) +{ + NoopIntCounter alpha("test", "none", "unitless", true); + NoopDoubleCounter beta("other", "none", "unitless", true); + + alpha.add(1, "key:value"); + beta.add(1.0, "key:value"); + + BoundNoopIntCounter gamma= alpha.bind("key: value"); + BoundNoopDoubleCounter delta= beta.bind("key: value"); + + gamma.add(1); + delta.add(1.0); +} + +TEST(UpDownCounter, DefaultConstruction) +{ + NoopIntUpDownCounter alpha("test", "none", "unitless", true); + NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + + alpha.bind("key: value"); + + BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); + BoundNoopIntUpDownCounter delta= beta.bind("key: value"); + +} + +TEST(UpDownCounter, Add) +{ + NoopIntUpDownCounter alpha("test", "none", "unitless", true); + NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + + alpha.add(1, "key:value"); + beta.add(1.0, "key:value"); + + BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); + BoundNoopIntUpDownCounter delta= beta.bind("key: value"); + + gamma.add(1); + delta.add(1.0); + gamma.add(-1); + delta.add(-1.0); +} + +TEST(ValueRecorder, DefaultConstruction) +{ + NoopIntValueRecorder alpha("test", "none", "unitless", true); + NoopDoubleValueRecorder beta("other", "none", "unitless", true); + + alpha.bind("key: value"); + + BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); + BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); + +} + +TEST(ValueRecorder, Record) +{ + NoopIntValueRecorder alpha("test", "none", "unitless", true); + NoopDoubleValueRecorder beta("other", "none", "unitless", true); + + alpha.record(1, "key:value"); + beta.record(1.0, "key:value"); + + BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); + BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); + + gamma.record(1); + delta.record(1.0); +} \ No newline at end of file From c7cf5abf62f3811d7bfed3c3fefa875d1ba55781 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 1 Jul 2020 01:25:21 -0400 Subject: [PATCH 03/11] observer callbacks now conform to spec --- .../opentelemetry/metrics/async_instruments.h | 95 +++++++++++------- .../opentelemetry/metrics/instrument.h | 53 ++++++---- api/include/opentelemetry/metrics/noop.h | 98 ++++++++++++++----- .../opentelemetry/metrics/sync_instruments.h | 56 +++++++---- api/test/metrics/BUILD | 1 + api/test/metrics/int_counter_test.cc | 91 +++++++++++++---- 6 files changed, 274 insertions(+), 120 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index 8451e41f1f..be82aa863e 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -1,37 +1,32 @@ #pragma once #include "instrument.h" -#include "opentelemetry/common/attribute_value.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -class Result {}; - -class IntObserverResult: public Result { +class ObserverResult { public: + ObserverResult() = default; - IntObserverResult() = default; + ObserverResult(AsynchronousInstrument *instrument):instrument_(instrument){} - IntObserverResult(*AsynchronousInstrument instrument, int value); + void observe(nostd::variant value, const nostd::string_view &labels){} -} +private: + AsynchronousInstrument *instrument_; +}; -class DoubleObserverResult: public Result { +class IntValueObserver: public AsynchronousInstrument{ public: - DoubleObserverResult() = default; - - DoubleObserverResult(*AsynchronousInstrument instrument, double value); + IntValueObserver() = default; -} + IntValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} -class IntValueObserver: public AsynchronousInstrument{ - -public: /* * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. @@ -39,17 +34,22 @@ class IntValueObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, std::string_view &labels) override; + void observe(int value, const std::string_view &labels){} private: // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback)(IntObserverResult); + void (*callback_)(ObserverResult); -} +}; class DoubleValueObserver: public AsynchronousInstrument{ public: + + DoubleValueObserver() = default; + + DoubleValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + /* * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. @@ -57,16 +57,22 @@ class DoubleValueObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, std::string_view &labels) override; + void observe(double value, const std::string_view &labels){} private: - void (*callback)(DoubleObserverResult); + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback_)(ObserverResult); -} +}; class IntSumObserver: public AsynchronousInstrument{ public: + + IntSumObserver() = default; + + IntSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + /* * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. @@ -74,16 +80,22 @@ class IntSumObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, std::string_view &labels) override; + void observe(int value, const std::string_view &labels){} private: - void (*callback)(IntObserverResult); + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback_)(ObserverResult); -} +}; class DoubleSumObserver: public AsynchronousInstrument{ public: + + DoubleSumObserver() = default; + + DoubleSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + /* * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. @@ -91,16 +103,22 @@ class DoubleSumObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, std::string_view &labels) override; + void observe(double value, const std::string_view &labels){} private: - void (*callback)(DoubleObserverResult); + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback_)(ObserverResult); -} +}; class IntUpDownSumObserver: public AsynchronousInstrument{ public: + + IntUpDownSumObserver() = default; + + IntUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + /* * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. @@ -108,16 +126,22 @@ class IntUpDownSumObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, std::string_view &labels) override; + void observe(int value, const std::string_view &labels){} private: - void (*callback)(IntObserverResult); + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback_)(ObserverResult); -} +}; class DoubleUpDownSumObserver: public AsynchronousInstrument{ public: + + DoubleUpDownSumObserver() = default; + + DoubleUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + /* * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. @@ -125,12 +149,13 @@ class DoubleUpDownSumObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, std::string_view &labels) override; + void observe(double value, const std::string_view &labels){} private: - void (*callback)(DoubleObserverResult); + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback_)(ObserverResult); -} +}; -} -OPENTELEMETRY_BEGIN_NAMESPACE \ No newline at end of file +} // namespace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index 2dd1f0f7bd..c4ea4f7550 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -9,25 +9,33 @@ namespace metrics enum class InstrumentKind { - Counter, - UpDownCounter, - ValueRecorder, - ValueObserver, - SumObserver, - UpDownSumObserver + IntCounter, + IntUpDownCounter, + IntValueRecorder, + IntValueObserver, + IntSumObserver, + IntUpDownSumObserver, + DoubleCounter, + DoubleUpDownCounter, + DoubleValueRecorder, + DoubleValueObserver, + DoubleSumObserver, + DoubleUpDownSumObserver }; enum class BoundInstrumentKind { - BoundCounter, - BoundUpDownCounter, - BoundValueRecorder, - BoundValueObserver, - BoundSumObserver, - BoundUpDownSumObserver + BoundIntCounter, + BoundIntUpDownCounter, + BoundIntValueRecorder, + BoundDoubleCounter, + BoundDoubleUpDownCounter, + BoundDoubleValueRecorder }; -//Do not need the getters virtual because each base class should have access to them -- will not be overriden. Also, I removed all private variables and will add them in when writing the SDK because I have more flexibility with types and they are not necessary for a minimal noop implementation. +//Do not need the getters virtual because each base class should have access to them -- will not be overriden. +// Also, I removed all private variables and will add them in when writing the SDK because I have more flexibility +// with types and they are not necessary for a minimal noop implementation. class Instrument { public: @@ -64,14 +72,15 @@ class Instrument { }; - +// Should this be a friend class so that it can access name, descripion, etc. +// from the Instrument base class? class BoundSynchronousInstrument: public Instrument { public: BoundSynchronousInstrument() = default; - BoundSynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundSynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /** * Frees the resources associated with this Bound Instrument. @@ -80,16 +89,16 @@ class BoundSynchronousInstrument: public Instrument { * @param none * @return void */ - void unbind(); + void unbind() {} /** * Records a single synchronous metric event. //Call to aggregator - * Since this is a bound synchronous instrument, labels are notrequired in * metric capture calls. + * Since this is a bound synchronous instrument, labels are not required in * metric capture calls. * * @param value the numerical representation of the metric being captured * @return void */ - void update(common::AttributeValue value); //don't want this virtual because the base boundsynchronousinstrument will call the aggregator in the sdk + void update(common::AttributeValue value) {} //don't want this virtual because the base boundsynchronousinstrument will call the aggregator in the sdk }; @@ -101,7 +110,7 @@ class SynchronousInstrument: public Instrument {// need update virtual or not? SynchronousInstrument() = default; - SynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + SynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /** * Returns a Bound Instrument associated with the specified labels. * Multiples requests with the same set of labels may return the same @@ -113,7 +122,9 @@ class SynchronousInstrument: public Instrument {// need update virtual or not? * @param labels the set of labels, as key-value pairs. * @return a Bound Instrument */ - BoundSynchronousInstrument bind(nostd::string_view & labels); + BoundSynchronousInstrument bind(const nostd::string_view & labels) { + return BoundSynchronousInstrument(); + } /** * Records a single synchronous metric event. @@ -124,7 +135,7 @@ class SynchronousInstrument: public Instrument {// need update virtual or not? * @param value the numerical representation of the metric being captured * @return void */ - void update(common::AttributeValue value, nostd::string_view &labels); //add or record + void update(common::AttributeValue value, nostd::string_view &labels){} //add or record }; class AsynchronousInstrument: public Instrument{ diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index b23af52a44..62feda4542 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -9,11 +9,13 @@ #include "opentelemetry/metrics/meter.h" #include "opentelemetry/metrics/meter_provider.h" #include "opentelemetry/metrics/sync_instruments.h" +#include "opentelemetry/metrics/async_instruments.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/unique_ptr.h" #include + OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { @@ -41,6 +43,53 @@ class NoopMeterProvider final : public opentelemetry::metrics::MeterProvider nostd::shared_ptr meter_; }; +class NoopIntValueObserver : public IntValueObserver { + +public: + + NoopIntValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): IntValueObserver(name,description,unit,enabled,callback,kind) {} + +}; + +class NoopDoubleValueObserver : public DoubleValueObserver { + +public: + + NoopDoubleValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): DoubleValueObserver(name,description,unit,enabled,callback,kind) {} + +}; + +class NoopIntSumObserver : public IntSumObserver { + +public: + + NoopIntSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): IntSumObserver(name,description,unit,enabled,callback,kind) {} + +}; + +class NoopDoubleSumObserver : public DoubleSumObserver { + +public: + + NoopDoubleSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): DoubleSumObserver(name,description,unit,enabled,callback,kind) {} + +}; + +class NoopIntUpDownSumObserver : public IntUpDownSumObserver { + +public: + + NoopIntUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): IntUpDownSumObserver(name,description,unit,enabled,callback,kind) {} + +}; + +class NoopDoubleUpDownSumObserver : public DoubleUpDownSumObserver { + +public: + + NoopDoubleUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): DoubleUpDownSumObserver(name,description,unit,enabled,callback,kind) {} + +}; class BoundNoopIntCounter : public BoundIntCounter { @@ -48,11 +97,11 @@ class BoundNoopIntCounter : public BoundIntCounter { BoundNoopIntCounter() = default; - BoundNoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + BoundNoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} void add(int value){} - void unbind(); + }; class NoopIntCounter : public IntCounter { @@ -61,29 +110,28 @@ class NoopIntCounter : public IntCounter { NoopIntCounter() = default; - NoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + NoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} BoundNoopIntCounter bind(const nostd::string_view & /*labels*/){ return BoundNoopIntCounter(); } - void add(int value, const std::string_view & /*labels*/){} + void add(int value, const nostd::string_view & /*labels*/){} }; -class BoundNoopDoubleCounter : public DoubleCounter { +class BoundNoopDoubleCounter : public BoundDoubleCounter { public: BoundNoopDoubleCounter() = default; - BoundNoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + BoundNoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} void add(double value){} - void unbind(); }; class NoopDoubleCounter : public DoubleCounter { @@ -92,14 +140,14 @@ class NoopDoubleCounter : public DoubleCounter { NoopDoubleCounter()=default; - NoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + NoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} BoundNoopDoubleCounter bind(const nostd::string_view & /*labels*/){ return BoundNoopDoubleCounter(); } - void add(double value, const std::string_view & /*labels*/){} + void add(double value, const nostd::string_view & /*labels*/){} }; @@ -109,11 +157,10 @@ class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter { BoundNoopIntUpDownCounter() = default; - BoundNoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + BoundNoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} void add(int value){} - void unbind(); }; class NoopIntUpDownCounter : public IntUpDownCounter { @@ -122,14 +169,14 @@ class NoopIntUpDownCounter : public IntUpDownCounter { NoopIntUpDownCounter()=default; - NoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + NoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ return BoundNoopIntUpDownCounter(); } - void add(int value, const std::string_view & /*labels*/){} + void add(int value, const nostd::string_view & /*labels*/){} }; class BoundNoopDoubleUpDownCounter : public DoubleUpDownCounter { @@ -138,11 +185,10 @@ class BoundNoopDoubleUpDownCounter : public DoubleUpDownCounter { BoundNoopDoubleUpDownCounter() = default; - BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} void add(double value){} - void unbind(); }; class NoopDoubleUpDownCounter : public DoubleUpDownCounter { @@ -151,27 +197,26 @@ class NoopDoubleUpDownCounter : public DoubleUpDownCounter { NoopDoubleUpDownCounter()=default; - NoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + NoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ return BoundNoopIntUpDownCounter(); } - void add(double value, const std::string_view & /*labels*/){} + void add(double value, const nostd::string_view & /*labels*/){} }; -class BoundNoopIntValueRecorder : public IntValueRecorder { +class BoundNoopIntValueRecorder : public BoundIntValueRecorder { public: BoundNoopIntValueRecorder() = default; - BoundNoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + BoundNoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} void record(int value){} - void unbind(); }; class NoopIntValueRecorder : public IntValueRecorder { @@ -180,27 +225,26 @@ class NoopIntValueRecorder : public IntValueRecorder { NoopIntValueRecorder()=default; - NoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + NoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} BoundNoopIntValueRecorder bind(const nostd::string_view & /*labels*/){ return BoundNoopIntValueRecorder(); } - void record(int value, const std::string_view & /*labels*/){} + void record(int value, const nostd::string_view & /*labels*/){} }; -class BoundNoopDoubleValueRecorder : public DoubleValueRecorder { +class BoundNoopDoubleValueRecorder : public BoundDoubleValueRecorder { public: BoundNoopDoubleValueRecorder() = default; - BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/){} + BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} void record(int value){} - void unbind(); }; class NoopDoubleValueRecorder : public DoubleValueRecorder { @@ -209,14 +253,14 @@ class NoopDoubleValueRecorder : public DoubleValueRecorder { NoopDoubleValueRecorder()=default; - NoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/) {} + NoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} BoundNoopDoubleValueRecorder bind(const nostd::string_view & /*labels*/){ return BoundNoopDoubleValueRecorder(); } - void record(double value, const std::string_view & /*labels*/){} + void record(double value, const nostd::string_view & /*labels*/){} }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 375be04fcc..8ea117f16b 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -12,7 +12,7 @@ class BoundIntCounter: public BoundSynchronousInstrument{ //override bind? BoundIntCounter() = default; - BoundIntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundIntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -22,7 +22,6 @@ class BoundIntCounter: public BoundSynchronousInstrument{ //override bind? */ void add(int value); - void unbind(); }; class IntCounter: public SynchronousInstrument{ @@ -31,7 +30,7 @@ class IntCounter: public SynchronousInstrument{ IntCounter() = default; - IntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + IntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /* * Bind creates a bound instrument for this counter. The labels are @@ -40,7 +39,9 @@ class IntCounter: public SynchronousInstrument{ * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - BoundIntCounter bind(nostd::string_view &labels); + BoundIntCounter bind(const nostd::string_view &labels) { + return BoundIntCounter(); + } /* * Add adds the value to the counter's sum. The labels should contain @@ -49,7 +50,7 @@ class IntCounter: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value, std::string_view &labels); + void add(int value, nostd::string_view &labels); }; class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? @@ -58,7 +59,7 @@ class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? BoundDoubleCounter() = default; - BoundDoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundDoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -67,6 +68,7 @@ class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? * @param labels the set of labels, as key-value pairs */ void add(int value); + }; class DoubleCounter: public SynchronousInstrument{ @@ -75,7 +77,7 @@ class DoubleCounter: public SynchronousInstrument{ DoubleCounter() = default; - DoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + DoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /* * Bind creates a bound instrument for this counter. The labels are @@ -84,7 +86,9 @@ class DoubleCounter: public SynchronousInstrument{ * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - BoundDoubleCounter bind(nostd::string_view &labels); + BoundDoubleCounter bind(const nostd::string_view &labels) { + return BoundDoubleCounter(); + } /* * Add adds the value to the counter's sum. The labels should contain @@ -102,7 +106,7 @@ class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? BoundIntUpDownCounter() = default; - BoundIntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundIntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -111,6 +115,7 @@ class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? * @param labels the set of labels, as key-value pairs */ void add(int value); + }; class IntUpDownCounter: public SynchronousInstrument{ @@ -119,7 +124,7 @@ class IntUpDownCounter: public SynchronousInstrument{ IntUpDownCounter() = default; - IntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + IntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /* * Bind creates a bound instrument for this counter. The labels are @@ -128,7 +133,9 @@ class IntUpDownCounter: public SynchronousInstrument{ * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - BoundIntUpDownCounter bind(nostd::string_view &labels); + BoundIntUpDownCounter bind(const nostd::string_view &labels) { + return BoundIntUpDownCounter(); + } /* * Add adds the value to the counter's sum. The labels should contain @@ -147,7 +154,7 @@ class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bi BoundDoubleUpDownCounter() = default; - BoundDoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundDoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -156,6 +163,7 @@ class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bi * @param labels the set of labels, as key-value pairs */ void add(double value); + }; class DoubleUpDownCounter: public SynchronousInstrument{ @@ -164,7 +172,7 @@ class DoubleUpDownCounter: public SynchronousInstrument{ DoubleUpDownCounter() = default; - DoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + DoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /* * Bind creates a bound instrument for this counter. The labels are @@ -173,7 +181,9 @@ class DoubleUpDownCounter: public SynchronousInstrument{ * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - BoundDoubleUpDownCounter bind(nostd::string_view &labels); + BoundDoubleUpDownCounter bind(const nostd::string_view &labels) { + return BoundDoubleUpDownCounter(); + } /* * Add adds the value to the counter's sum. The labels should contain @@ -192,7 +202,7 @@ class BoundIntValueRecorder: public BoundSynchronousInstrument{ //override bind? BoundIntValueRecorder() = default; - BoundIntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundIntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /* * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. @@ -200,6 +210,7 @@ class BoundIntValueRecorder: public BoundSynchronousInstrument{ //override bind? * @param value the numerical representation of the metric being captured */ void record(int value); + }; class IntValueRecorder: public SynchronousInstrument{ @@ -208,7 +219,7 @@ class IntValueRecorder: public SynchronousInstrument{ IntValueRecorder() = default; - IntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + IntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /* * Bind creates a bound instrument for this counter. The labels are @@ -217,7 +228,9 @@ class IntValueRecorder: public SynchronousInstrument{ * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - BoundIntValueRecorder bind(nostd::string_view &labels); + BoundIntValueRecorder bind(const nostd::string_view &labels) { + return BoundIntValueRecorder(); + } /* * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels should contain @@ -236,7 +249,7 @@ class BoundDoubleValueRecorder: public BoundSynchronousInstrument{ //override bi BoundDoubleValueRecorder() = default; - BoundDoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + BoundDoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); /* * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. @@ -244,6 +257,7 @@ class BoundDoubleValueRecorder: public BoundSynchronousInstrument{ //override bi * @param value the numerical representation of the metric being captured */ void record(double value); + }; class DoubleValueRecorder: public SynchronousInstrument{ @@ -252,7 +266,7 @@ class DoubleValueRecorder: public SynchronousInstrument{ DoubleValueRecorder() = default; - DoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + DoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); /* * Bind creates a bound instrument for this counter. The labels are @@ -261,7 +275,9 @@ class DoubleValueRecorder: public SynchronousInstrument{ * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - BoundDoubleValueRecorder bind(nostd::string_view &labels); + BoundDoubleValueRecorder bind(const nostd::string_view &labels) { + return BoundDoubleValueRecorder(); + } /* * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels should contain diff --git a/api/test/metrics/BUILD b/api/test/metrics/BUILD index 0ae0c79a60..d706756e90 100644 --- a/api/test/metrics/BUILD +++ b/api/test/metrics/BUILD @@ -14,6 +14,7 @@ cc_test( srcs = [ "int_counter_test.cc", ], + linkstatic = 1, deps = [ "//api", "@com_google_googletest//:gtest_main", diff --git a/api/test/metrics/int_counter_test.cc b/api/test/metrics/int_counter_test.cc index c1f596e751..ba186c0c96 100644 --- a/api/test/metrics/int_counter_test.cc +++ b/api/test/metrics/int_counter_test.cc @@ -1,29 +1,70 @@ #include "opentelemetry/metrics/noop.h" - #include #include - #include -using namespace opentelemetry::metrics; +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +void noopIntCallback(ObserverResult result){ + result.observe(1,"key: value"); + result.observe(-1,"key: value"); +} + +void noopDoubleCallback(ObserverResult result){ + result.observe(1,"key: value"); + result.observe(-1,"key: value"); +} + +TEST(ValueObserver, Observe){ + NoopIntValueObserver alpha("test", "none", "unitless", true, &noopIntCallback, InstrumentKind::IntValueObserver); + + NoopDoubleValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback, InstrumentKind::DoubleValueObserver); + + alpha.observe(1,"key:value"); + beta.observe(1.0,"key:value"); +} + +TEST(SumObserver, DefaultConstruction){ + NoopIntSumObserver alpha("test", "none", "unitless", true, &noopIntCallback, InstrumentKind::IntSumObserver); + + NoopDoubleSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback, InstrumentKind::DoubleSumObserver); + + alpha.observe(1,"key:value"); + beta.observe(1.0,"key:value"); +} + +TEST(UpDownSumObserver, DefaultConstruction){ + NoopIntUpDownSumObserver alpha("test", "none", "unitless", true, &noopIntCallback, InstrumentKind::IntUpDownSumObserver); + + NoopDoubleUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback, InstrumentKind::DoubleUpDownSumObserver); + + alpha.observe(1,"key:value"); + beta.observe(1.0,"key:value"); + alpha.observe(-1,"key:value"); + beta.observe(-1.0,"key:value"); +} TEST(Counter, DefaultConstruction) { - NoopIntCounter alpha("test", "none", "unitless", true); - NoopDoubleCounter beta("other", "none", "unitless", true); + NoopIntCounter alpha("test", "none", "unitless", true, InstrumentKind::IntCounter); + NoopDoubleCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleCounter); alpha.bind("key: value"); BoundNoopIntCounter gamma= alpha.bind("key: value"); BoundNoopDoubleCounter delta= beta.bind("key: value"); - + + gamma.unbind(); + delta.unbind(); } TEST(Counter, Add) { - NoopIntCounter alpha("test", "none", "unitless", true); - NoopDoubleCounter beta("other", "none", "unitless", true); + NoopIntCounter alpha("test", "none", "unitless", true, InstrumentKind::IntCounter); + NoopDoubleCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleCounter); alpha.add(1, "key:value"); beta.add(1.0, "key:value"); @@ -33,24 +74,29 @@ TEST(Counter, Add) gamma.add(1); delta.add(1.0); + + gamma.unbind(); + delta.unbind(); } TEST(UpDownCounter, DefaultConstruction) { - NoopIntUpDownCounter alpha("test", "none", "unitless", true); - NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + NoopIntUpDownCounter alpha("test", "none", "unitless", true, InstrumentKind::IntUpDownCounter); + NoopDoubleUpDownCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleUpDownCounter); alpha.bind("key: value"); BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); BoundNoopIntUpDownCounter delta= beta.bind("key: value"); + gamma.unbind(); + delta.unbind(); } TEST(UpDownCounter, Add) { - NoopIntUpDownCounter alpha("test", "none", "unitless", true); - NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + NoopIntUpDownCounter alpha("test", "none", "unitless", true, InstrumentKind::IntUpDownCounter); + NoopDoubleUpDownCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleUpDownCounter); alpha.add(1, "key:value"); beta.add(1.0, "key:value"); @@ -62,24 +108,29 @@ TEST(UpDownCounter, Add) delta.add(1.0); gamma.add(-1); delta.add(-1.0); + + gamma.unbind(); + delta.unbind(); } TEST(ValueRecorder, DefaultConstruction) { - NoopIntValueRecorder alpha("test", "none", "unitless", true); - NoopDoubleValueRecorder beta("other", "none", "unitless", true); + NoopIntValueRecorder alpha("test", "none", "unitless", true, InstrumentKind::IntValueRecorder); + NoopDoubleValueRecorder beta("other", "none", "unitless", true, InstrumentKind::DoubleValueRecorder); alpha.bind("key: value"); BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); + gamma.unbind(); + delta.unbind(); } TEST(ValueRecorder, Record) { - NoopIntValueRecorder alpha("test", "none", "unitless", true); - NoopDoubleValueRecorder beta("other", "none", "unitless", true); + NoopIntValueRecorder alpha("test", "none", "unitless", true, InstrumentKind::IntValueRecorder); + NoopDoubleValueRecorder beta("other", "none", "unitless", true, InstrumentKind::DoubleValueRecorder); alpha.record(1, "key:value"); beta.record(1.0, "key:value"); @@ -89,4 +140,10 @@ TEST(ValueRecorder, Record) gamma.record(1); delta.record(1.0); -} \ No newline at end of file + + gamma.unbind(); + delta.unbind(); +} + +} +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From dd64ab6d2d359fb2349a9efef617c9aa1de61b70 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 1 Jul 2020 12:36:32 -0400 Subject: [PATCH 04/11] moving observer result to new file --- .../opentelemetry/metrics/async_instruments.h | 111 ++++++------- .../opentelemetry/metrics/instrument.h | 75 ++++++--- api/include/opentelemetry/metrics/noop.h | 96 ++++++++--- .../opentelemetry/metrics/observer_result.h | 54 +++++++ .../opentelemetry/metrics/sync_instruments.h | 84 +++++++--- api/test/metrics/BUILD | 4 +- api/test/metrics/CMakeLists.txt | 2 +- api/test/metrics/noop_instrument_test.cc | 149 ++++++++++++++++++ 8 files changed, 438 insertions(+), 137 deletions(-) create mode 100644 api/include/opentelemetry/metrics/observer_result.h create mode 100644 api/test/metrics/noop_instrument_test.cc diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index be82aa863e..937f84d181 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -1,23 +1,12 @@ #pragma once #include "instrument.h" +#include "observer_result.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -class ObserverResult { - -public: - ObserverResult() = default; - - ObserverResult(AsynchronousInstrument *instrument):instrument_(instrument){} - - void observe(nostd::variant value, const nostd::string_view &labels){} - -private: - AsynchronousInstrument *instrument_; -}; class IntValueObserver: public AsynchronousInstrument{ @@ -25,20 +14,22 @@ class IntValueObserver: public AsynchronousInstrument{ IntValueObserver() = default; - IntValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + IntValueObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(IntObserverResult)) {} + // Again, I was going to call the asynchronous base + // constructor here, but do not think it is necessary /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * Updates the instruments aggregator with the new value. The labels should + * contain the keys and values to be associated with this value. * * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, const std::string_view &labels){} - -private: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); + virtual void observe(int value, const nostd::string_view &labels) override{} }; @@ -48,20 +39,20 @@ class DoubleValueObserver: public AsynchronousInstrument{ DoubleValueObserver() = default; - DoubleValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + DoubleValueObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(DoubleObserverResult)) {} /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * Updates the instruments aggregator with the new value. The labels should + * contain the keys and values to be associated with this value. * * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(double value, const std::string_view &labels){} - -private: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); + virtual void observe(double value, const nostd::string_view &labels) override {} }; @@ -71,7 +62,11 @@ class IntSumObserver: public AsynchronousInstrument{ IntSumObserver() = default; - IntSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + IntSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(IntObserverResult)) {} /* * Add adds the value to the counter's sum. The labels should contain @@ -80,11 +75,7 @@ class IntSumObserver: public AsynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, const std::string_view &labels){} - -private: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); + virtual void observe(int value, const nostd::string_view &labels) override {} }; @@ -94,20 +85,13 @@ class DoubleSumObserver: public AsynchronousInstrument{ DoubleSumObserver() = default; - DoubleSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - void observe(double value, const std::string_view &labels){} + DoubleSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(DoubleObserverResult)) {} -private: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); + virtual void observe(double value, const nostd::string_view &labels) override {} }; @@ -117,20 +101,21 @@ class IntUpDownSumObserver: public AsynchronousInstrument{ IntUpDownSumObserver() = default; - IntUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + IntUpDownSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(IntObserverResult)) {} /* * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. + * the keys and values to be associated with this value. UpDownCounters can + * accepts both positive and negative updates. * * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void observe(int value, const std::string_view &labels){} - -private: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); + virtual void observe(int value, const nostd::string_view &labels) override {} }; @@ -140,20 +125,14 @@ class DoubleUpDownSumObserver: public AsynchronousInstrument{ DoubleUpDownSumObserver() = default; - DoubleUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): callback_(callback) {} + DoubleUpDownSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(DoubleObserverResult)) {} - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - void observe(double value, const std::string_view &labels){} + virtual void observe(double value, const nostd::string_view &labels) override {} -private: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); }; diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index c4ea4f7550..015b882961 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -7,6 +7,7 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { +// Enum classes to help determine instrument types in other parts of the api enum class InstrumentKind { IntCounter, @@ -23,6 +24,7 @@ enum class InstrumentKind DoubleUpDownSumObserver }; +// Fewer Bound types because Asynchronous instrument cannot bind enum class BoundInstrumentKind { BoundIntCounter, @@ -33,9 +35,8 @@ enum class BoundInstrumentKind BoundDoubleValueRecorder }; -//Do not need the getters virtual because each base class should have access to them -- will not be overriden. -// Also, I removed all private variables and will add them in when writing the SDK because I have more flexibility -// with types and they are not necessary for a minimal noop implementation. + + class Instrument { public: @@ -56,31 +57,43 @@ class Instrument { * @param enabled determins if the metric is currently capturing data * @return Instrument type with the specified attirbutes */ - Instrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled); + Instrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); // Returns true if the instrument is enabled and collecting data - bool IsEnabled(); + virtual bool IsEnabled() { + // False in default implementation + return false; + } // Return the instrument name - nostd::string_view GetName(); + virtual nostd::string_view GetName(){ + return nostd::string_view(""); + } // Return the instrument description - nostd::string_view GetDescription(); + virtual nostd::string_view GetDescription(){ + return nostd::string_view(""); + } // Return the insrument's units of measurement - nostd::string_view GetUnits(); + virtual nostd::string_view GetUnits(){ + return nostd::string_view(""); + } }; -// Should this be a friend class so that it can access name, descripion, etc. -// from the Instrument base class? class BoundSynchronousInstrument: public Instrument { public: BoundSynchronousInstrument() = default; - BoundSynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundSynchronousInstrument(nostd::string_view name, + nostd::string_view description, nostd::string_view unit, + bool enabled, BoundInstrumentKind kind); /** * Frees the resources associated with this Bound Instrument. @@ -89,35 +102,36 @@ class BoundSynchronousInstrument: public Instrument { * @param none * @return void */ - void unbind() {} + virtual void unbind() {} /** - * Records a single synchronous metric event. //Call to aggregator + * Records a single synchronous metric event; a call to the aggregator * Since this is a bound synchronous instrument, labels are not required in * metric capture calls. * * @param value the numerical representation of the metric being captured * @return void */ - void update(common::AttributeValue value) {} //don't want this virtual because the base boundsynchronousinstrument will call the aggregator in the sdk + virtual void update(nostd::variant value) final {} //don't want this overriden because the base boundsynchronousinstrument will call the aggregator in the sdk }; -// Thinking ahead, I think I want to change this to a shared pointer so I can store pointers to instruments and the meter class has access to the ref counts - -class SynchronousInstrument: public Instrument {// need update virtual or not? +class SynchronousInstrument: public Instrument { public: SynchronousInstrument() = default; - SynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + SynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, bool enabled, + InstrumentKind kind); /** * Returns a Bound Instrument associated with the specified labels. * Multiples requests with the same set of labels may return the same * Bound Instrument instance. * - * It is recommended that callers keep a reference to the Bound Instrument instead of always - * calling this method for every operation. + * It is recommended that callers keep a reference to the Bound Instrument + * instead of repeatedly calling this operation. * * @param labels the set of labels, as key-value pairs. * @return a Bound Instrument @@ -130,20 +144,30 @@ class SynchronousInstrument: public Instrument {// need update virtual or not? * Records a single synchronous metric event. * Since this is an unbound synchronous instrument, labels are required in * metric capture calls. * + * update can be used in instruments with both add or record since it simply + * activated the aggregator * * @param labels the set of labels, as key-value pairs. * @param value the numerical representation of the metric being captured * @return void */ - void update(common::AttributeValue value, nostd::string_view &labels){} //add or record + virtual void update(nostd::variant value, nostd::string_view &labels) final {} }; +class ObserverResult; + class AsynchronousInstrument: public Instrument{ public: AsynchronousInstrument() = default; + AsynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) {} + /** * Captures data by activating the callback function associated with the * instrument and storing its return value. Callbacks for asychronous @@ -152,7 +176,14 @@ class AsynchronousInstrument: public Instrument{ * @param none * @return none */ - void observe(); + virtual void observe(int value, const nostd::string_view & labels){} + + virtual void observe(double value, const nostd::string_view & labels){} + + private: + + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. + void (*callback_)(ObserverResult); }; } diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 62feda4542..59b13d5a25 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -47,7 +47,14 @@ class NoopIntValueObserver : public IntValueObserver { public: - NoopIntValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): IntValueObserver(name,description,unit,enabled,callback,kind) {} + NoopIntValueObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(IntObserverResult)) {} + // I was originally calling the base class constructor here as well + // IntValueObserver(name,description, unit,...) + // but do not think that's necessary for the API's noop }; @@ -55,7 +62,10 @@ class NoopDoubleValueObserver : public DoubleValueObserver { public: - NoopDoubleValueObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): DoubleValueObserver(name,description,unit,enabled,callback,kind) {} + NoopDoubleValueObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool enabled, void (*callback)(DoubleObserverResult)) {} }; @@ -63,7 +73,11 @@ class NoopIntSumObserver : public IntSumObserver { public: - NoopIntSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): IntSumObserver(name,description,unit,enabled,callback,kind) {} + NoopIntSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(IntObserverResult)) {} }; @@ -71,7 +85,11 @@ class NoopDoubleSumObserver : public DoubleSumObserver { public: - NoopDoubleSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): DoubleSumObserver(name,description,unit,enabled,callback,kind) {} + NoopDoubleSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(DoubleObserverResult)) {} }; @@ -79,7 +97,11 @@ class NoopIntUpDownSumObserver : public IntUpDownSumObserver { public: - NoopIntUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): IntUpDownSumObserver(name,description,unit,enabled,callback,kind) {} + NoopIntUpDownSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(IntObserverResult)) {} }; @@ -87,7 +109,10 @@ class NoopDoubleUpDownSumObserver : public DoubleUpDownSumObserver { public: - NoopDoubleUpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, void (*callback)(ObserverResult), InstrumentKind kind): DoubleUpDownSumObserver(name,description,unit,enabled,callback,kind) {} + NoopDoubleUpDownSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(DoubleObserverResult)) {} }; @@ -97,11 +122,12 @@ class BoundNoopIntCounter : public BoundIntCounter { BoundNoopIntCounter() = default; - BoundNoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} + BoundNoopIntCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/){} void add(int value){} - - }; class NoopIntCounter : public IntCounter { @@ -110,7 +136,10 @@ class NoopIntCounter : public IntCounter { NoopIntCounter() = default; - NoopIntCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} + NoopIntCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} BoundNoopIntCounter bind(const nostd::string_view & /*labels*/){ @@ -128,7 +157,9 @@ class BoundNoopDoubleCounter : public BoundDoubleCounter { BoundNoopDoubleCounter() = default; - BoundNoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} + BoundNoopDoubleCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/){} void add(double value){} @@ -140,7 +171,10 @@ class NoopDoubleCounter : public DoubleCounter { NoopDoubleCounter()=default; - NoopDoubleCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} + NoopDoubleCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} BoundNoopDoubleCounter bind(const nostd::string_view & /*labels*/){ @@ -157,7 +191,9 @@ class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter { BoundNoopIntUpDownCounter() = default; - BoundNoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} + BoundNoopIntUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/) {} void add(int value){} @@ -169,7 +205,10 @@ class NoopIntUpDownCounter : public IntUpDownCounter { NoopIntUpDownCounter()=default; - NoopIntUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} + NoopIntUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ @@ -179,13 +218,15 @@ class NoopIntUpDownCounter : public IntUpDownCounter { void add(int value, const nostd::string_view & /*labels*/){} }; -class BoundNoopDoubleUpDownCounter : public DoubleUpDownCounter { +class BoundNoopDoubleUpDownCounter : public BoundDoubleUpDownCounter { public: BoundNoopDoubleUpDownCounter() = default; - BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} + BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/){} void add(double value){} @@ -197,7 +238,9 @@ class NoopDoubleUpDownCounter : public DoubleUpDownCounter { NoopDoubleUpDownCounter()=default; - NoopDoubleUpDownCounter(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} + NoopDoubleUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/) {} BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ @@ -213,7 +256,9 @@ class BoundNoopIntValueRecorder : public BoundIntValueRecorder { BoundNoopIntValueRecorder() = default; - BoundNoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} + BoundNoopIntValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/){} void record(int value){} @@ -225,7 +270,10 @@ class NoopIntValueRecorder : public IntValueRecorder { NoopIntValueRecorder()=default; - NoopIntValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} + NoopIntValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} BoundNoopIntValueRecorder bind(const nostd::string_view & /*labels*/){ @@ -241,9 +289,11 @@ class BoundNoopDoubleValueRecorder : public BoundDoubleValueRecorder { BoundNoopDoubleValueRecorder() = default; - BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, BoundInstrumentKind /*kind*/){} + BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/){} - void record(int value){} + void record(double value){} }; @@ -253,7 +303,9 @@ class NoopDoubleValueRecorder : public DoubleValueRecorder { NoopDoubleValueRecorder()=default; - NoopDoubleValueRecorder(nostd::string_view /*name*/, nostd::string_view /*description*/, nostd::string_view /*unit*/, bool /*enabled*/, InstrumentKind /*kind*/) {} + NoopDoubleValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, nostd::string_view /*unit*/, + bool /*enabled*/) {} BoundNoopDoubleValueRecorder bind(const nostd::string_view & /*labels*/){ diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h new file mode 100644 index 0000000000..afb0047aee --- /dev/null +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -0,0 +1,54 @@ +#pragma once + +#include "instrument.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +/** +* ObserverResult class is necessary for batch recording. Callback functions +* to asynchronous instruments are also designed to accept a single +* ObserverResult object and update using its pointer to the instrument itself. +*/ +class ObserverResult { + +public: + ObserverResult() = default; + + ObserverResult(AsynchronousInstrument *instrument):instrument_(instrument){} + + AsynchronousInstrument *instrument_; +}; + +class IntObserverResult: public ObserverResult { + +public: + + IntObserverResult() = default; + + IntObserverResult(AsynchronousInstrument *instrument): + ObserverResult(instrument){} + + void observe(int value, const nostd::string_view &labels){ + instrument_->observe(value, labels); + } +}; + + +class DoubleObserverResult: public ObserverResult { + +public: + + DoubleObserverResult() = default; + + DoubleObserverResult(AsynchronousInstrument *instrument): + ObserverResult(instrument){} + + void observe(double value, const nostd::string_view &labels){ + instrument_->observe(value, labels); + } +}; + +} // namespace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 8ea117f16b..3be7d2f3f8 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -12,7 +12,10 @@ class BoundIntCounter: public BoundSynchronousInstrument{ //override bind? BoundIntCounter() = default; - BoundIntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundIntCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -20,7 +23,7 @@ class BoundIntCounter: public BoundSynchronousInstrument{ //override bind? * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value); + virtual void add(int value) {} }; @@ -30,7 +33,10 @@ class IntCounter: public SynchronousInstrument{ IntCounter() = default; - IntCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + IntCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -50,7 +56,7 @@ class IntCounter: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value, nostd::string_view &labels); + virtual void add(int value, const nostd::string_view &labels) {} }; class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? @@ -59,7 +65,10 @@ class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? BoundDoubleCounter() = default; - BoundDoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundDoubleCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -67,7 +76,7 @@ class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value); + virtual void add(double value){} }; @@ -77,7 +86,10 @@ class DoubleCounter: public SynchronousInstrument{ DoubleCounter() = default; - DoubleCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + DoubleCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -97,7 +109,7 @@ class DoubleCounter: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value, nostd::string_view &labels); + virtual void add(double value, const nostd::string_view &labels){} }; class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? @@ -106,7 +118,10 @@ class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? BoundIntUpDownCounter() = default; - BoundIntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundIntUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -114,7 +129,7 @@ class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value); + virtual void add(int value) {} }; @@ -124,7 +139,10 @@ class IntUpDownCounter: public SynchronousInstrument{ IntUpDownCounter() = default; - IntUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + IntUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -145,7 +163,7 @@ class IntUpDownCounter: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(int value, nostd::string_view &labels); + virtual void add(int value, const nostd::string_view &labels){} }; class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bind? @@ -154,7 +172,10 @@ class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bi BoundDoubleUpDownCounter() = default; - BoundDoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundDoubleUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. @@ -162,7 +183,7 @@ class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bi * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(double value); + virtual void add(double value) {} }; @@ -172,7 +193,10 @@ class DoubleUpDownCounter: public SynchronousInstrument{ DoubleUpDownCounter() = default; - DoubleUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + DoubleUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -193,7 +217,7 @@ class DoubleUpDownCounter: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void add(double value, nostd::string_view &labels); + virtual void add(double value, const nostd::string_view &labels) {} }; class BoundIntValueRecorder: public BoundSynchronousInstrument{ //override bind? @@ -202,14 +226,17 @@ class BoundIntValueRecorder: public BoundSynchronousInstrument{ //override bind? BoundIntValueRecorder() = default; - BoundIntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundIntValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. * * @param value the numerical representation of the metric being captured */ - void record(int value); + virtual void record(int value) {} }; @@ -219,7 +246,10 @@ class IntValueRecorder: public SynchronousInstrument{ IntValueRecorder() = default; - IntValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + IntValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -240,7 +270,7 @@ class IntValueRecorder: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void record(int value, nostd::string_view &labels); + virtual void record(int value, const nostd::string_view &labels) {} }; class BoundDoubleValueRecorder: public BoundSynchronousInstrument{ //override bind? @@ -249,14 +279,17 @@ class BoundDoubleValueRecorder: public BoundSynchronousInstrument{ //override bi BoundDoubleValueRecorder() = default; - BoundDoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, BoundInstrumentKind kind); + BoundDoubleValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. * * @param value the numerical representation of the metric being captured */ - void record(double value); + virtual void record(double value) {} }; @@ -266,7 +299,10 @@ class DoubleValueRecorder: public SynchronousInstrument{ DoubleValueRecorder() = default; - DoubleValueRecorder(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, InstrumentKind kind); + DoubleValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -287,7 +323,7 @@ class DoubleValueRecorder: public SynchronousInstrument{ * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - void record(double value, nostd::string_view &labels); + virtual void record(double value, const nostd::string_view &labels) {} }; } // namespace diff --git a/api/test/metrics/BUILD b/api/test/metrics/BUILD index d706756e90..db8d63bcd1 100644 --- a/api/test/metrics/BUILD +++ b/api/test/metrics/BUILD @@ -10,9 +10,9 @@ cc_test( ) cc_test( - name = "int_counter", + name = "noop_instrument_test", srcs = [ - "int_counter_test.cc", + "noop_instrument_test.cc", ], linkstatic = 1, deps = [ diff --git a/api/test/metrics/CMakeLists.txt b/api/test/metrics/CMakeLists.txt index 1f2aca70a8..204aa992a6 100644 --- a/api/test/metrics/CMakeLists.txt +++ b/api/test/metrics/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(testname meter_provider_test) +foreach(testname noop_instrument_test meter_provider_test) add_executable(${testname} "${testname}.cc") target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc new file mode 100644 index 0000000000..b16bc08f17 --- /dev/null +++ b/api/test/metrics/noop_instrument_test.cc @@ -0,0 +1,149 @@ + +#include "opentelemetry/metrics/noop.h" +#include +#include +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +void noopIntCallback(IntObserverResult result){ + result.observe(1,"key: value"); + result.observe(-1,"key: value"); +} + +void noopDoubleCallback(DoubleObserverResult result){ + result.observe(1.0,"key: value"); + result.observe(-1.0,"key: value"); +} + +TEST(ValueObserver, Observe){ + NoopIntValueObserver alpha("test", "none", "unitless", true, &noopIntCallback); + + NoopDoubleValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback); + + alpha.observe(1,"key:value"); + beta.observe(1.0,"key:value"); +} + +TEST(SumObserver, DefaultConstruction){ + NoopIntSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); + + NoopDoubleSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); + + alpha.observe(1,"key:value"); + beta.observe(1.0,"key:value"); +} + +TEST(UpDownSumObserver, DefaultConstruction){ + NoopIntUpDownSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); + + NoopDoubleUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); + + alpha.observe(1,"key:value"); + beta.observe(1.0,"key:value"); + alpha.observe(-1,"key:value"); + beta.observe(-1.0,"key:value"); +} + +TEST(Counter, DefaultConstruction) +{ + NoopIntCounter alpha("test", "none", "unitless", true); + NoopDoubleCounter beta("other", "none", "unitless", true); + + alpha.bind("key: value"); + + BoundNoopIntCounter gamma= alpha.bind("key: value"); + BoundNoopDoubleCounter delta= beta.bind("key: value"); + + gamma.unbind(); + delta.unbind(); +} + +TEST(Counter, Add) +{ + NoopIntCounter alpha("test", "none", "unitless", true); + NoopDoubleCounter beta("other", "none", "unitless", true); + + alpha.add(1, "key:value"); + beta.add(1.0, "key:value"); + + BoundNoopIntCounter gamma= alpha.bind("key: value"); + BoundNoopDoubleCounter delta= beta.bind("key: value"); + + gamma.add(1); + delta.add(1.0); + + gamma.unbind(); + delta.unbind(); +} + +TEST(UpDownCounter, DefaultConstruction) +{ + NoopIntUpDownCounter alpha("test", "none", "unitless", true); + NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + + alpha.bind("key: value"); + + BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); + BoundNoopIntUpDownCounter delta= beta.bind("key: value"); + + gamma.unbind(); + delta.unbind(); +} + +TEST(UpDownCounter, Add) +{ + NoopIntUpDownCounter alpha("test", "none", "unitless", true); + NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + + alpha.add(1, "key:value"); + beta.add(1.0, "key:value"); + + BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); + BoundNoopIntUpDownCounter delta= beta.bind("key: value"); + + gamma.add(1); + delta.add(1.0); + gamma.add(-1); + delta.add(-1.0); + + gamma.unbind(); + delta.unbind(); +} + +TEST(ValueRecorder, DefaultConstruction) +{ + NoopIntValueRecorder alpha("test", "none", "unitless", true); + NoopDoubleValueRecorder beta("other", "none", "unitless", true); + + alpha.bind("key: value"); + + BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); + BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); + + gamma.unbind(); + delta.unbind(); +} + +TEST(ValueRecorder, Record) +{ + NoopIntValueRecorder alpha("test", "none", "unitless", true); + NoopDoubleValueRecorder beta("other", "none", "unitless", true); + + alpha.record(1, "key:value"); + beta.record(1.0, "key:value"); + + BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); + BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); + + gamma.record(1); + delta.record(1.0); + + gamma.unbind(); + delta.unbind(); +} + +} +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From dafe4fd377caaf94055f021f446e55c38277ed24 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 1 Jul 2020 23:57:13 -0400 Subject: [PATCH 05/11] replacing raw pointers with smart pointers --- .../opentelemetry/metrics/async_instruments.h | 19 ++------------- .../opentelemetry/metrics/instrument.h | 23 +++++++++---------- api/include/opentelemetry/metrics/noop.h | 3 --- .../opentelemetry/metrics/observer_result.h | 16 +++++++------ api/test/metrics/noop_instrument_test.cc | 1 - 5 files changed, 22 insertions(+), 40 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index 937f84d181..c4d7f66092 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -26,7 +26,7 @@ class IntValueObserver: public AsynchronousInstrument{ * Updates the instruments aggregator with the new value. The labels should * contain the keys and values to be associated with this value. * - * @param value the numerical representation of the metric being captured + * @param value is the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ virtual void observe(int value, const nostd::string_view &labels) override{} @@ -45,13 +45,6 @@ class DoubleValueObserver: public AsynchronousInstrument{ bool enabled, void (*callback)(DoubleObserverResult)) {} - /* - * Updates the instruments aggregator with the new value. The labels should - * contain the keys and values to be associated with this value. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ virtual void observe(double value, const nostd::string_view &labels) override {} }; @@ -72,7 +65,7 @@ class IntSumObserver: public AsynchronousInstrument{ * Add adds the value to the counter's sum. The labels should contain * the keys and values to be associated with this value. Counters only * accept positive valued updates. * - * @param value the numerical representation of the metric being captured + * @param value is the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ virtual void observe(int value, const nostd::string_view &labels) override {} @@ -107,14 +100,6 @@ class IntUpDownSumObserver: public AsynchronousInstrument{ bool enabled, void (*callback)(IntObserverResult)) {} - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. UpDownCounters can - * accepts both positive and negative updates. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ virtual void observe(int value, const nostd::string_view &labels) override {} }; diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index 015b882961..316c2a5970 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -24,7 +24,7 @@ enum class InstrumentKind DoubleUpDownSumObserver }; -// Fewer Bound types because Asynchronous instrument cannot bind +// Fewer Bound types because Asynchronous instruments cannot bind enum class BoundInstrumentKind { BoundIntCounter, @@ -53,8 +53,8 @@ class Instrument { * * @param name is the identifier of the instrumenting library * @param description explains what the metric captures - * @param unit specified the data type held in the instrument - * @param enabled determins if the metric is currently capturing data + * @param unit specifies the data type held in the instrument + * @param enabled determines if the metric is currently capturing data * @return Instrument type with the specified attirbutes */ Instrument(nostd::string_view name, @@ -93,7 +93,7 @@ class BoundSynchronousInstrument: public Instrument { BoundSynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, - bool enabled, BoundInstrumentKind kind); + bool enabled){} /** * Frees the resources associated with this Bound Instrument. @@ -108,7 +108,7 @@ class BoundSynchronousInstrument: public Instrument { * Records a single synchronous metric event; a call to the aggregator * Since this is a bound synchronous instrument, labels are not required in * metric capture calls. * - * @param value the numerical representation of the metric being captured + * @param value is the numerical representation of the metric being captured * @return void */ virtual void update(nostd::variant value) final {} //don't want this overriden because the base boundsynchronousinstrument will call the aggregator in the sdk @@ -123,8 +123,7 @@ class SynchronousInstrument: public Instrument { SynchronousInstrument(nostd::string_view name, nostd::string_view description, - nostd::string_view unit, bool enabled, - InstrumentKind kind); + nostd::string_view unit, bool enabled); /** * Returns a Bound Instrument associated with the specified labels. * Multiples requests with the same set of labels may return the same @@ -133,7 +132,7 @@ class SynchronousInstrument: public Instrument { * It is recommended that callers keep a reference to the Bound Instrument * instead of repeatedly calling this operation. * - * @param labels the set of labels, as key-value pairs. + * @param labels the set of labels, as key-value pairs * @return a Bound Instrument */ BoundSynchronousInstrument bind(const nostd::string_view & labels) { @@ -147,8 +146,8 @@ class SynchronousInstrument: public Instrument { * update can be used in instruments with both add or record since it simply * activated the aggregator * - * @param labels the set of labels, as key-value pairs. - * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + * @param value is the numerical representation of the metric being captured * @return void */ virtual void update(nostd::variant value, nostd::string_view &labels) final {} @@ -166,14 +165,14 @@ class AsynchronousInstrument: public Instrument{ nostd::string_view description, nostd::string_view unit, bool enabled, - void (*callback)(ObserverResult)) {} + void (callback)(ObserverResult)) {} /** * Captures data by activating the callback function associated with the * instrument and storing its return value. Callbacks for asychronous * instruments are defined during construction. * - * @param none + * @param value is the numerical representation of the metric being captured * @return none */ virtual void observe(int value, const nostd::string_view & labels){} diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 59b13d5a25..f2d8b6b1a5 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -52,9 +52,6 @@ class NoopIntValueObserver : public IntValueObserver { nostd::string_view /*unit*/, bool /*enabled*/, void (*callback)(IntObserverResult)) {} - // I was originally calling the base class constructor here as well - // IntValueObserver(name,description, unit,...) - // but do not think that's necessary for the API's noop }; diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h index afb0047aee..fe1d97dddb 100644 --- a/api/include/opentelemetry/metrics/observer_result.h +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -1,24 +1,26 @@ #pragma once #include "instrument.h" +#include "opentelemetry/nostd/shared_ptr.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { /** -* ObserverResult class is necessary for batch recording. Callback functions -* to asynchronous instruments are also designed to accept a single -* ObserverResult object and update using its pointer to the instrument itself. +* ObserverResult class is necessary for the callback recording asynchronous +* instrument use. Callback functions asynchronous instruments are designed to +* accept a single ObserverResult object and update using its pointer to the +* instrument itself. */ class ObserverResult { public: ObserverResult() = default; - ObserverResult(AsynchronousInstrument *instrument):instrument_(instrument){} + ObserverResult(nostd::shared_ptr instrument):instrument_(instrument){} - AsynchronousInstrument *instrument_; + nostd::shared_ptr instrument_; }; class IntObserverResult: public ObserverResult { @@ -27,7 +29,7 @@ class IntObserverResult: public ObserverResult { IntObserverResult() = default; - IntObserverResult(AsynchronousInstrument *instrument): + IntObserverResult(nostd::shared_ptr instrument): ObserverResult(instrument){} void observe(int value, const nostd::string_view &labels){ @@ -42,7 +44,7 @@ class DoubleObserverResult: public ObserverResult { DoubleObserverResult() = default; - DoubleObserverResult(AsynchronousInstrument *instrument): + DoubleObserverResult(nostd::shared_ptr instrument): ObserverResult(instrument){} void observe(double value, const nostd::string_view &labels){ diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc index b16bc08f17..4090d0ebf3 100644 --- a/api/test/metrics/noop_instrument_test.cc +++ b/api/test/metrics/noop_instrument_test.cc @@ -1,4 +1,3 @@ - #include "opentelemetry/metrics/noop.h" #include #include From 38bfb1b2310e453d8172fb002654b800cb23a310 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 8 Jul 2020 00:36:12 -0400 Subject: [PATCH 06/11] updated inheritance pattern --- .../opentelemetry/metrics/async_instruments.h | 163 +++--- .../opentelemetry/metrics/instrument.h | 255 +++++----- api/include/opentelemetry/metrics/noop.h | 347 ++++++------- .../opentelemetry/metrics/observer_result.h | 56 +-- .../opentelemetry/metrics/sync_instruments.h | 468 ++++++++---------- api/test/metrics/BUILD | 2 + api/test/metrics/noop_instrument_test.cc | 119 ++--- 7 files changed, 674 insertions(+), 736 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index c4d7f66092..3dad3db03e 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -7,119 +7,116 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { - -class IntValueObserver: public AsynchronousInstrument{ +class IntValueObserver : public AsynchronousInstrument +{ public: - - IntValueObserver() = default; - - IntValueObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(IntObserverResult)) {} - // Again, I was going to call the asynchronous base - // constructor here, but do not think it is necessary - - /* - * Updates the instruments aggregator with the new value. The labels should - * contain the keys and values to be associated with this value. - * - * @param value is the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void observe(int value, const nostd::string_view &labels) override{} - + IntValueObserver() = default; + + IntValueObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + + /* + * Updates the instruments aggregator with the new value. The labels should + * contain the keys and values to be associated with this value. + * + * @param value is the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void observe(int value, const nostd::string_view &labels) {} }; -class DoubleValueObserver: public AsynchronousInstrument{ +class DoubleValueObserver : public AsynchronousInstrument +{ public: + DoubleValueObserver() = default; - DoubleValueObserver() = default; - - DoubleValueObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(DoubleObserverResult)) {} - - virtual void observe(double value, const nostd::string_view &labels) override {} + DoubleValueObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + virtual void observe(double value, const nostd::string_view &labels) {} }; -class IntSumObserver: public AsynchronousInstrument{ +class IntSumObserver : public AsynchronousInstrument +{ public: - - IntSumObserver() = default; - - IntSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(IntObserverResult)) {} - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. - * - * @param value is the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void observe(int value, const nostd::string_view &labels) override {} - + IntSumObserver() = default; + + IntSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive + * valued updates. + * + * @param value is the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void observe(int value, const nostd::string_view &labels) {} }; -class DoubleSumObserver: public AsynchronousInstrument{ +class DoubleSumObserver : public AsynchronousInstrument +{ public: + DoubleSumObserver() = default; - DoubleSumObserver() = default; - - DoubleSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(DoubleObserverResult)) {} - - virtual void observe(double value, const nostd::string_view &labels) override {} + DoubleSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + virtual void observe(double value, const nostd::string_view &labels) {} }; -class IntUpDownSumObserver: public AsynchronousInstrument{ +class IntUpDownSumObserver : public AsynchronousInstrument +{ public: + IntUpDownSumObserver() = default; - IntUpDownSumObserver() = default; - - IntUpDownSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(IntObserverResult)) {} - - virtual void observe(int value, const nostd::string_view &labels) override {} + IntUpDownSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + virtual void observe(int value, const nostd::string_view &labels) {} }; -class DoubleUpDownSumObserver: public AsynchronousInstrument{ +class DoubleUpDownSumObserver : public AsynchronousInstrument +{ public: + DoubleUpDownSumObserver() = default; - DoubleUpDownSumObserver() = default; - - DoubleUpDownSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(DoubleObserverResult)) {} - - virtual void observe(double value, const nostd::string_view &labels) override {} - + DoubleUpDownSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + virtual void observe(double value, const nostd::string_view &labels) {} }; -} // namespace +} // namespace metrics OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index 316c2a5970..a268f4445f 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -1,13 +1,14 @@ #pragma once -#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -// Enum classes to help determine instrument types in other parts of the api +// Enum classes to help determine instrument types in other parts of the API enum class InstrumentKind { IntCounter, @@ -35,155 +36,155 @@ enum class BoundInstrumentKind BoundDoubleValueRecorder }; - - -class Instrument { +class Instrument +{ public: + // Note that Instruments should be created using the Meter class. + // Please refer to meter.h for documentation. + Instrument() = default; + + /** + * Base class constructor for all other instrument types. Whether or not + * an instrument is synchronous or bound, it requires a name, description, + * unit, and enabled flag. + * + * @param name is the identifier of the instrumenting library + * @param description explains what the metric captures + * @param unit specifies the data type held in the instrument + * @param enabled determines if the metric is currently capturing data + * @return Instrument type with the specified attirbutes + */ + Instrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} - // Note that Instruments should be created using the Meter class. - // Please refer to meter.h for documentation. - Instrument() = default; - - - /** - * Base class constructor for all other instrument types. Whether or not - * an instrument is synchronous or bound, it requires a name, description, - * unit, and enabled flag. - * - * @param name is the identifier of the instrumenting library - * @param description explains what the metric captures - * @param unit specifies the data type held in the instrument - * @param enabled determines if the metric is currently capturing data - * @return Instrument type with the specified attirbutes - */ - Instrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - // Returns true if the instrument is enabled and collecting data - virtual bool IsEnabled() { - // False in default implementation - return false; - } - - // Return the instrument name - virtual nostd::string_view GetName(){ - return nostd::string_view(""); - } - - // Return the instrument description - virtual nostd::string_view GetDescription(){ - return nostd::string_view(""); - } - - // Return the insrument's units of measurement - virtual nostd::string_view GetUnits(){ - return nostd::string_view(""); - } + // Returns true if the instrument is enabled and collecting data + virtual bool IsEnabled() + { + // False in default implementation + return false; + } -}; + // Return the instrument name + virtual nostd::string_view GetName() { return nostd::string_view(""); } -class BoundSynchronousInstrument: public Instrument { + // Return the instrument description + virtual nostd::string_view GetDescription() { return nostd::string_view(""); } -public: + // Return the insrument's units of measurement + virtual nostd::string_view GetUnits() { return nostd::string_view(""); } - BoundSynchronousInstrument() = default; + virtual ~Instrument() = default; +}; - BoundSynchronousInstrument(nostd::string_view name, - nostd::string_view description, nostd::string_view unit, - bool enabled){} +class BoundSynchronousInstrument : public Instrument +{ - /** - * Frees the resources associated with this Bound Instrument. - * The Metric from which this instrument was created is not impacted. - * - * @param none - * @return void +public: + BoundSynchronousInstrument() = default; + + BoundSynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + /** + * Frees the resources associated with this Bound Instrument. + * The Metric from which this instrument was created is not impacted. + * + * @param none + * @return void */ - virtual void unbind() {} - - /** - * Records a single synchronous metric event; a call to the aggregator - * Since this is a bound synchronous instrument, labels are not required in * metric capture calls. - * - * @param value is the numerical representation of the metric being captured - * @return void + virtual void unbind() {} + + /** + * Records a single synchronous metric event; a call to the aggregator + * Since this is a bound synchronous instrument, labels are not required in * metric capture + * calls. + * + * @param value is the numerical representation of the metric being captured + * @return void */ - virtual void update(nostd::variant value) final {} //don't want this overriden because the base boundsynchronousinstrument will call the aggregator in the sdk + virtual void update(nostd::variant value) final {} }; - -class SynchronousInstrument: public Instrument { +class SynchronousInstrument : public Instrument +{ public: - - SynchronousInstrument() = default; - - SynchronousInstrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, bool enabled); - - /** - * Returns a Bound Instrument associated with the specified labels. * Multiples requests with the same set of labels may return the same - * Bound Instrument instance. - * - * It is recommended that callers keep a reference to the Bound Instrument - * instead of repeatedly calling this operation. - * - * @param labels the set of labels, as key-value pairs - * @return a Bound Instrument + SynchronousInstrument() = default; + + SynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + /** + * Returns a Bound Instrument associated with the specified labels. * Multiples requests + * with the same set of labels may return the same Bound Instrument instance. + * + * It is recommended that callers keep a reference to the Bound Instrument + * instead of repeatedly calling this operation. + * + * @param labels the set of labels, as key-value pairs + * @return a Bound Instrument */ - BoundSynchronousInstrument bind(const nostd::string_view & labels) { - return BoundSynchronousInstrument(); - } - - /** - * Records a single synchronous metric event. - * Since this is an unbound synchronous instrument, labels are required in * metric capture calls. - * - * update can be used in instruments with both add or record since it simply - * activated the aggregator - * - * @param labels the set of labels, as key-value pairs - * @param value is the numerical representation of the metric being captured - * @return void + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundSynchronousInstrument()); + } + + /** + * Records a single synchronous metric event. + * Since this is an unbound synchronous instrument, labels are required in * metric capture + * calls. + * + * update can be used in instruments with both add or record since it simply + * activated the aggregator + * + * @param labels the set of labels, as key-value pairs + * @param value is the numerical representation of the metric being captured + * @return void */ - virtual void update(nostd::variant value, nostd::string_view &labels) final {} + virtual void update(nostd::variant value, const nostd::string_view &labels) final {} }; class ObserverResult; -class AsynchronousInstrument: public Instrument{ +class AsynchronousInstrument : public Instrument +{ public: + AsynchronousInstrument() = default; + + AsynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void(callback)(ObserverResult)) + {} + + /** + * Captures data by activating the callback function associated with the + * instrument and storing its return value. Callbacks for asychronous + * instruments are defined during construction. + * + * @param value is the numerical representation of the metric being captured + * @return none + */ + virtual void update(nostd::variant value, const nostd::string_view &labels) final {} - AsynchronousInstrument() = default; - - AsynchronousInstrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (callback)(ObserverResult)) {} - - /** - * Captures data by activating the callback function associated with the - * instrument and storing its return value. Callbacks for asychronous - * instruments are defined during construction. - * - * @param value is the numerical representation of the metric being captured - * @return none - */ - virtual void observe(int value, const nostd::string_view & labels){} - - virtual void observe(double value, const nostd::string_view & labels){} - - private: - - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is stored in an observer result type and returns nothing. This function calls the instrument's observe. - void (*callback_)(ObserverResult); +protected: + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is + // stored in an observer result type and returns nothing. This function calls the instrument's + // observe. + void (*callback_)(ObserverResult); }; -} +} // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index f2d8b6b1a5..9388169c11 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -43,273 +43,286 @@ class NoopMeterProvider final : public opentelemetry::metrics::MeterProvider nostd::shared_ptr meter_; }; -class NoopIntValueObserver : public IntValueObserver { +class NoopIntValueObserver : public IntValueObserver +{ public: - - NoopIntValueObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(IntObserverResult)) {} - + NoopIntValueObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} }; -class NoopDoubleValueObserver : public DoubleValueObserver { +class NoopDoubleValueObserver : public DoubleValueObserver +{ public: - - NoopDoubleValueObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool enabled, void (*callback)(DoubleObserverResult)) {} - + NoopDoubleValueObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool enabled, + void (*callback)(ObserverResult)) + {} }; -class NoopIntSumObserver : public IntSumObserver { +class NoopIntSumObserver : public IntSumObserver +{ public: - - NoopIntSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(IntObserverResult)) {} - + NoopIntSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} }; -class NoopDoubleSumObserver : public DoubleSumObserver { +class NoopDoubleSumObserver : public DoubleSumObserver +{ public: - - NoopDoubleSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(DoubleObserverResult)) {} - + NoopDoubleSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} }; -class NoopIntUpDownSumObserver : public IntUpDownSumObserver { +class NoopIntUpDownSumObserver : public IntUpDownSumObserver +{ public: - - NoopIntUpDownSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(IntObserverResult)) {} - + NoopIntUpDownSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} }; -class NoopDoubleUpDownSumObserver : public DoubleUpDownSumObserver { +class NoopDoubleUpDownSumObserver : public DoubleUpDownSumObserver +{ public: - - NoopDoubleUpDownSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(DoubleObserverResult)) {} - + NoopDoubleUpDownSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} }; -class BoundNoopIntCounter : public BoundIntCounter { +class BoundNoopIntCounter : public BoundIntCounter +{ public: + BoundNoopIntCounter() = default; - BoundNoopIntCounter() = default; - - BoundNoopIntCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/){} + BoundNoopIntCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - void add(int value){} + virtual void add(int value) override {} }; -class NoopIntCounter : public IntCounter { +class NoopIntCounter : public IntCounter +{ public: + NoopIntCounter() = default; - NoopIntCounter() = default; - - NoopIntCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) {} - - - BoundNoopIntCounter bind(const nostd::string_view & /*labels*/){ - return BoundNoopIntCounter(); - } + NoopIntCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - void add(int value, const nostd::string_view & /*labels*/){} + nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + { + return nostd::shared_ptr(new BoundNoopIntCounter()); + } + virtual void add(int value, const nostd::string_view & /*labels*/) override {} }; - -class BoundNoopDoubleCounter : public BoundDoubleCounter { +class BoundNoopDoubleCounter : public BoundDoubleCounter +{ public: + BoundNoopDoubleCounter() = default; - BoundNoopDoubleCounter() = default; - - BoundNoopDoubleCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/){} - - void add(double value){} + BoundNoopDoubleCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + virtual void add(double value) override {} }; -class NoopDoubleCounter : public DoubleCounter { +class NoopDoubleCounter : public DoubleCounter +{ public: + NoopDoubleCounter() = default; - NoopDoubleCounter()=default; - - NoopDoubleCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) {} - - - BoundNoopDoubleCounter bind(const nostd::string_view & /*labels*/){ - return BoundNoopDoubleCounter(); - } + NoopDoubleCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - void add(double value, const nostd::string_view & /*labels*/){} + nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + { + return nostd::shared_ptr(new BoundNoopDoubleCounter()); + } + virtual void add(double value, const nostd::string_view & /*labels*/) override {} }; -class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter { +class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter +{ public: + BoundNoopIntUpDownCounter() = default; - BoundNoopIntUpDownCounter() = default; - - BoundNoopIntUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/) {} - - void add(int value){} + BoundNoopIntUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + virtual void add(int value) override {} }; -class NoopIntUpDownCounter : public IntUpDownCounter { +class NoopIntUpDownCounter : public IntUpDownCounter +{ public: + NoopIntUpDownCounter() = default; - NoopIntUpDownCounter()=default; - - NoopIntUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) {} - + NoopIntUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ - return BoundNoopIntUpDownCounter(); - } + nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + { + return nostd::shared_ptr(new BoundNoopIntUpDownCounter()); + } - void add(int value, const nostd::string_view & /*labels*/){} + virtual void add(int value, const nostd::string_view & /*labels*/) override {} }; -class BoundNoopDoubleUpDownCounter : public BoundDoubleUpDownCounter { +class BoundNoopDoubleUpDownCounter : public BoundDoubleUpDownCounter +{ public: + BoundNoopDoubleUpDownCounter() = default; - BoundNoopDoubleUpDownCounter() = default; - - BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/){} - - void add(double value){} + BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + virtual void add(double value) override {} }; -class NoopDoubleUpDownCounter : public DoubleUpDownCounter { +class NoopDoubleUpDownCounter : public DoubleUpDownCounter +{ public: + NoopDoubleUpDownCounter() = default; - NoopDoubleUpDownCounter()=default; - - NoopDoubleUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/) {} - + NoopDoubleUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - BoundNoopIntUpDownCounter bind(const nostd::string_view & /*labels*/){ - return BoundNoopIntUpDownCounter(); - } + nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + { + return nostd::shared_ptr(new BoundNoopDoubleUpDownCounter()); + } - void add(double value, const nostd::string_view & /*labels*/){} + virtual void add(double value, const nostd::string_view & /*labels*/) override {} }; -class BoundNoopIntValueRecorder : public BoundIntValueRecorder { +class BoundNoopIntValueRecorder : public BoundIntValueRecorder +{ public: + BoundNoopIntValueRecorder() = default; - BoundNoopIntValueRecorder() = default; - - BoundNoopIntValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/){} - - void record(int value){} + BoundNoopIntValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + virtual void record(int value) override {} }; -class NoopIntValueRecorder : public IntValueRecorder { +class NoopIntValueRecorder : public IntValueRecorder +{ public: + NoopIntValueRecorder() = default; - NoopIntValueRecorder()=default; - - NoopIntValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) {} - + NoopIntValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - BoundNoopIntValueRecorder bind(const nostd::string_view & /*labels*/){ - return BoundNoopIntValueRecorder(); - } + nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + { + return nostd::shared_ptr(new BoundNoopIntValueRecorder()); + } - void record(int value, const nostd::string_view & /*labels*/){} + virtual void record(int value, const nostd::string_view & /*labels*/) override {} }; -class BoundNoopDoubleValueRecorder : public BoundDoubleValueRecorder { +class BoundNoopDoubleValueRecorder : public BoundDoubleValueRecorder +{ public: + BoundNoopDoubleValueRecorder() = default; - BoundNoopDoubleValueRecorder() = default; - - BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/){} - - void record(double value){} + BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + virtual void record(double value) override {} }; -class NoopDoubleValueRecorder : public DoubleValueRecorder { +class NoopDoubleValueRecorder : public DoubleValueRecorder +{ public: + NoopDoubleValueRecorder() = default; - NoopDoubleValueRecorder()=default; - - NoopDoubleValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, nostd::string_view /*unit*/, - bool /*enabled*/) {} - + NoopDoubleValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} - BoundNoopDoubleValueRecorder bind(const nostd::string_view & /*labels*/){ - return BoundNoopDoubleValueRecorder(); - } + nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + { + return nostd::shared_ptr(new BoundNoopDoubleValueRecorder()); + } - void record(double value, const nostd::string_view & /*labels*/){} + virtual void record(double value, const nostd::string_view & /*labels*/) override {} }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h index fe1d97dddb..1f4b194d42 100644 --- a/api/include/opentelemetry/metrics/observer_result.h +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -7,50 +7,28 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -/** -* ObserverResult class is necessary for the callback recording asynchronous -* instrument use. Callback functions asynchronous instruments are designed to -* accept a single ObserverResult object and update using its pointer to the -* instrument itself. -*/ -class ObserverResult { - -public: - ObserverResult() = default; - - ObserverResult(nostd::shared_ptr instrument):instrument_(instrument){} - - nostd::shared_ptr instrument_; -}; - -class IntObserverResult: public ObserverResult { +/** + * ObserverResult class is necessary for the callback recording asynchronous + * instrument use. Callback functions asynchronous instruments are designed to + * accept a single ObserverResult object and update using its pointer to the + * instrument itself. + */ +class ObserverResult +{ public: + ObserverResult() = default; - IntObserverResult() = default; - - IntObserverResult(nostd::shared_ptr instrument): - ObserverResult(instrument){} - - void observe(int value, const nostd::string_view &labels){ - instrument_->observe(value, labels); - } -}; - - -class DoubleObserverResult: public ObserverResult { - -public: + ObserverResult(nostd::shared_ptr instrument) : instrument_(instrument) {} - DoubleObserverResult() = default; - - DoubleObserverResult(nostd::shared_ptr instrument): - ObserverResult(instrument){} + void observe(nostd::variant value, const nostd::string_view &labels) + { + instrument_->update(value, labels); + } - void observe(double value, const nostd::string_view &labels){ - instrument_->observe(value, labels); - } +private: + nostd::shared_ptr instrument_; }; -} // namespace +} // namespace metrics OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 3be7d2f3f8..99db7abb68 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -6,325 +6,267 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -class BoundIntCounter: public BoundSynchronousInstrument{ //override bind? +class BoundIntCounter : public BoundSynchronousInstrument +{ public: - - BoundIntCounter() = default; - - BoundIntCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(int value) {} - + BoundIntCounter() = default; + + BoundIntCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + /* + * Add adds the value to the counter's sum. The labels are already linked * to the instrument + * and are not specified. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(int value) {} }; -class IntCounter: public SynchronousInstrument{ +class IntCounter : public SynchronousInstrument +{ public: - - IntCounter() = default; - - IntCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - BoundIntCounter bind(const nostd::string_view &labels) { - return BoundIntCounter(); - } - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(int value, const nostd::string_view &labels) {} + IntCounter() = default; + + IntCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundIntCounter()); + } + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive + * valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(int value, const nostd::string_view &labels) {} }; -class BoundDoubleCounter: public BoundSynchronousInstrument{ //override bind? +class BoundDoubleCounter : public BoundSynchronousInstrument +{ public: + BoundDoubleCounter() = default; - BoundDoubleCounter() = default; - - BoundDoubleCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(double value){} + BoundDoubleCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + virtual void add(double value) {} }; -class DoubleCounter: public SynchronousInstrument{ - -public: - - DoubleCounter() = default; - - DoubleCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - BoundDoubleCounter bind(const nostd::string_view &labels) { - return BoundDoubleCounter(); - } - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive valued updates. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(double value, const nostd::string_view &labels){} -}; - -class BoundIntUpDownCounter: public BoundSynchronousInstrument{ //override bind? +class DoubleCounter : public SynchronousInstrument +{ public: + DoubleCounter() = default; - BoundIntUpDownCounter() = default; - - BoundIntUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + DoubleCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} - /* - * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(int value) {} + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundDoubleCounter()); + } + virtual void add(double value, const nostd::string_view &labels) {} }; -class IntUpDownCounter: public SynchronousInstrument{ +class BoundIntUpDownCounter : public BoundSynchronousInstrument +{ public: - - IntUpDownCounter() = default; - - IntUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - BoundIntUpDownCounter bind(const nostd::string_view &labels) { - return BoundIntUpDownCounter(); - } - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. UpDownCounters can - * accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(int value, const nostd::string_view &labels){} + BoundIntUpDownCounter() = default; + + BoundIntUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + /* + * Add adds the value to the counter's sum. The labels are already linked to * the instrument and + * do not need to specified again. UpDownCounters can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(int value) {} }; -class BoundDoubleUpDownCounter: public BoundSynchronousInstrument{ //override bind? +class IntUpDownCounter : public SynchronousInstrument +{ public: - - BoundDoubleUpDownCounter() = default; - - BoundDoubleUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Add adds the value to the counter's sum. The labels are already linked * to the instrument and are not specified. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(double value) {} - + IntUpDownCounter() = default; + + IntUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundIntUpDownCounter()); + } + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. UpDownCounters can + * accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(int value, const nostd::string_view &labels) {} }; -class DoubleUpDownCounter: public SynchronousInstrument{ +class BoundDoubleUpDownCounter : public BoundSynchronousInstrument +{ public: + BoundDoubleUpDownCounter() = default; + + BoundDoubleUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} - DoubleUpDownCounter() = default; - - DoubleUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - BoundDoubleUpDownCounter bind(const nostd::string_view &labels) { - return BoundDoubleUpDownCounter(); - } - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. UpDownCounters can - * accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(double value, const nostd::string_view &labels) {} + virtual void add(double value) {} }; -class BoundIntValueRecorder: public BoundSynchronousInstrument{ //override bind? +class DoubleUpDownCounter : public SynchronousInstrument +{ public: + DoubleUpDownCounter() = default; - BoundIntValueRecorder() = default; - - BoundIntValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + DoubleUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} - /* - * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - */ - virtual void record(int value) {} + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundDoubleUpDownCounter()); + } + virtual void add(double value, const nostd::string_view &labels) {} }; -class IntValueRecorder: public SynchronousInstrument{ +class BoundIntValueRecorder : public BoundSynchronousInstrument +{ public: - - IntValueRecorder() = default; - - IntValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - BoundIntValueRecorder bind(const nostd::string_view &labels) { - return BoundIntValueRecorder(); - } - - /* - * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels should contain - * the keys and values to be associated with this value. ValueRecorders can - * accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void record(int value, const nostd::string_view &labels) {} + BoundIntValueRecorder() = default; + + BoundIntValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + /* + * Records the value by summing it with previous measurements and checking * previously stored + * minimum and maximum values. The labels associated with * new values are already linked to the + * instrument as it is bound. * ValueRecorders can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + */ + virtual void record(int value) {} }; -class BoundDoubleValueRecorder: public BoundSynchronousInstrument{ //override bind? +class IntValueRecorder : public SynchronousInstrument +{ public: + IntValueRecorder() = default; + + IntValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundIntValueRecorder()); + } + + /* + * Records the value by summing it with previous measurements and checking * previously stored + * minimum and maximum values. The labels should contain the keys and values to be associated with + * this value. ValueRecorders can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void record(int value, const nostd::string_view &labels) {} +}; - BoundDoubleValueRecorder() = default; +class BoundDoubleValueRecorder : public BoundSynchronousInstrument +{ - BoundDoubleValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); +public: + BoundDoubleValueRecorder() = default; - /* - * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels associated with * new values are already linked to the instrument as it is bound. * ValueRecorders can accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - */ - virtual void record(double value) {} + BoundDoubleValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + virtual void record(double value) {} }; -class DoubleValueRecorder: public SynchronousInstrument{ +class DoubleValueRecorder : public SynchronousInstrument +{ public: + DoubleValueRecorder() = default; + + DoubleValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + nostd::shared_ptr bind(const nostd::string_view &labels) + { + return nostd::shared_ptr(new BoundDoubleValueRecorder()); + } - DoubleValueRecorder() = default; - - DoubleValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - BoundDoubleValueRecorder bind(const nostd::string_view &labels) { - return BoundDoubleValueRecorder(); - } - - /* - * Records the value by summing it with previous measurements and checking * previously stored minimum and maximum values. The labels should contain - * the keys and values to be associated with this value. ValueRecorders can - * accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void record(double value, const nostd::string_view &labels) {} + virtual void record(double value, const nostd::string_view &labels) {} }; -} // namespace +} // namespace metrics OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/test/metrics/BUILD b/api/test/metrics/BUILD index db8d63bcd1..08cd98cf8e 100644 --- a/api/test/metrics/BUILD +++ b/api/test/metrics/BUILD @@ -1,3 +1,5 @@ +load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") + cc_test( name = "meter_provider_test", srcs = [ diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc index 4090d0ebf3..83157dd877 100644 --- a/api/test/metrics/noop_instrument_test.cc +++ b/api/test/metrics/noop_instrument_test.cc @@ -1,63 +1,68 @@ -#include "opentelemetry/metrics/noop.h" +#include #include #include -#include +#include "opentelemetry/metrics/noop.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -void noopIntCallback(IntObserverResult result){ - result.observe(1,"key: value"); - result.observe(-1,"key: value"); +void noopIntCallback(ObserverResult result) +{ + result.observe(1, "key: value"); + result.observe(-1, "key: value"); } -void noopDoubleCallback(DoubleObserverResult result){ - result.observe(1.0,"key: value"); - result.observe(-1.0,"key: value"); +void noopDoubleCallback(ObserverResult result) +{ + result.observe(1.0, "key: value"); + result.observe(-1.0, "key: value"); } -TEST(ValueObserver, Observe){ +TEST(ValueObserver, Observe) +{ NoopIntValueObserver alpha("test", "none", "unitless", true, &noopIntCallback); NoopDoubleValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback); - alpha.observe(1,"key:value"); - beta.observe(1.0,"key:value"); + alpha.observe(1, "key:value"); + beta.observe(1.0, "key:value"); } -TEST(SumObserver, DefaultConstruction){ +TEST(SumObserver, DefaultConstruction) +{ NoopIntSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); NoopDoubleSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); - alpha.observe(1,"key:value"); - beta.observe(1.0,"key:value"); + alpha.observe(1, "key:value"); + beta.observe(1.0, "key:value"); } -TEST(UpDownSumObserver, DefaultConstruction){ +TEST(UpDownSumObserver, DefaultConstruction) +{ NoopIntUpDownSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); NoopDoubleUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); - alpha.observe(1,"key:value"); - beta.observe(1.0,"key:value"); - alpha.observe(-1,"key:value"); - beta.observe(-1.0,"key:value"); + alpha.observe(1, "key:value"); + beta.observe(1.0, "key:value"); + alpha.observe(-1, "key:value"); + beta.observe(-1.0, "key:value"); } TEST(Counter, DefaultConstruction) { NoopIntCounter alpha("test", "none", "unitless", true); NoopDoubleCounter beta("other", "none", "unitless", true); - + alpha.bind("key: value"); - BoundNoopIntCounter gamma= alpha.bind("key: value"); - BoundNoopDoubleCounter delta= beta.bind("key: value"); + auto gamma = alpha.bind("key: value"); + auto delta = beta.bind("key: value"); - gamma.unbind(); - delta.unbind(); + gamma->unbind(); + delta->unbind(); } TEST(Counter, Add) @@ -68,28 +73,28 @@ TEST(Counter, Add) alpha.add(1, "key:value"); beta.add(1.0, "key:value"); - BoundNoopIntCounter gamma= alpha.bind("key: value"); - BoundNoopDoubleCounter delta= beta.bind("key: value"); + auto gamma = alpha.bind("key: value"); + auto delta = beta.bind("key: value"); - gamma.add(1); - delta.add(1.0); + gamma->add(1); + delta->add(1.0); - gamma.unbind(); - delta.unbind(); + gamma->unbind(); + delta->unbind(); } TEST(UpDownCounter, DefaultConstruction) { NoopIntUpDownCounter alpha("test", "none", "unitless", true); NoopDoubleUpDownCounter beta("other", "none", "unitless", true); - + alpha.bind("key: value"); - BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); - BoundNoopIntUpDownCounter delta= beta.bind("key: value"); - - gamma.unbind(); - delta.unbind(); + auto gamma = alpha.bind("key: value"); + auto delta = beta.bind("key: value"); + + gamma->unbind(); + delta->unbind(); } TEST(UpDownCounter, Add) @@ -100,30 +105,30 @@ TEST(UpDownCounter, Add) alpha.add(1, "key:value"); beta.add(1.0, "key:value"); - BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); - BoundNoopIntUpDownCounter delta= beta.bind("key: value"); + auto gamma = alpha.bind("key: value"); + auto delta = beta.bind("key: value"); - gamma.add(1); - delta.add(1.0); - gamma.add(-1); - delta.add(-1.0); + gamma->add(1); + delta->add(1.0); + gamma->add(-1); + delta->add(-1.0); - gamma.unbind(); - delta.unbind(); + gamma->unbind(); + delta->unbind(); } TEST(ValueRecorder, DefaultConstruction) { NoopIntValueRecorder alpha("test", "none", "unitless", true); NoopDoubleValueRecorder beta("other", "none", "unitless", true); - + alpha.bind("key: value"); - BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); - BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); - - gamma.unbind(); - delta.unbind(); + auto gamma = alpha.bind("key: value"); + auto delta = beta.bind("key: value"); + + gamma->unbind(); + delta->unbind(); } TEST(ValueRecorder, Record) @@ -134,15 +139,15 @@ TEST(ValueRecorder, Record) alpha.record(1, "key:value"); beta.record(1.0, "key:value"); - BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); - BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); + auto gamma = alpha.bind("key: value"); + auto delta = beta.bind("key: value"); - gamma.record(1); - delta.record(1.0); + gamma->record(1); + delta->record(1.0); - gamma.unbind(); - delta.unbind(); + gamma->unbind(); + delta->unbind(); } -} +} // namespace metrics OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From 8acabeca5468db680dffb077a48b38fbf779c02d Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 8 Jul 2020 00:40:46 -0400 Subject: [PATCH 07/11] deleting superfluous files --- api/test/metrics/int_counter_test.cc | 149 --------------------------- 1 file changed, 149 deletions(-) delete mode 100644 api/test/metrics/int_counter_test.cc diff --git a/api/test/metrics/int_counter_test.cc b/api/test/metrics/int_counter_test.cc deleted file mode 100644 index ba186c0c96..0000000000 --- a/api/test/metrics/int_counter_test.cc +++ /dev/null @@ -1,149 +0,0 @@ - -#include "opentelemetry/metrics/noop.h" -#include -#include -#include - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace metrics -{ - -void noopIntCallback(ObserverResult result){ - result.observe(1,"key: value"); - result.observe(-1,"key: value"); -} - -void noopDoubleCallback(ObserverResult result){ - result.observe(1,"key: value"); - result.observe(-1,"key: value"); -} - -TEST(ValueObserver, Observe){ - NoopIntValueObserver alpha("test", "none", "unitless", true, &noopIntCallback, InstrumentKind::IntValueObserver); - - NoopDoubleValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback, InstrumentKind::DoubleValueObserver); - - alpha.observe(1,"key:value"); - beta.observe(1.0,"key:value"); -} - -TEST(SumObserver, DefaultConstruction){ - NoopIntSumObserver alpha("test", "none", "unitless", true, &noopIntCallback, InstrumentKind::IntSumObserver); - - NoopDoubleSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback, InstrumentKind::DoubleSumObserver); - - alpha.observe(1,"key:value"); - beta.observe(1.0,"key:value"); -} - -TEST(UpDownSumObserver, DefaultConstruction){ - NoopIntUpDownSumObserver alpha("test", "none", "unitless", true, &noopIntCallback, InstrumentKind::IntUpDownSumObserver); - - NoopDoubleUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback, InstrumentKind::DoubleUpDownSumObserver); - - alpha.observe(1,"key:value"); - beta.observe(1.0,"key:value"); - alpha.observe(-1,"key:value"); - beta.observe(-1.0,"key:value"); -} - -TEST(Counter, DefaultConstruction) -{ - NoopIntCounter alpha("test", "none", "unitless", true, InstrumentKind::IntCounter); - NoopDoubleCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleCounter); - - alpha.bind("key: value"); - - BoundNoopIntCounter gamma= alpha.bind("key: value"); - BoundNoopDoubleCounter delta= beta.bind("key: value"); - - gamma.unbind(); - delta.unbind(); -} - -TEST(Counter, Add) -{ - NoopIntCounter alpha("test", "none", "unitless", true, InstrumentKind::IntCounter); - NoopDoubleCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleCounter); - - alpha.add(1, "key:value"); - beta.add(1.0, "key:value"); - - BoundNoopIntCounter gamma= alpha.bind("key: value"); - BoundNoopDoubleCounter delta= beta.bind("key: value"); - - gamma.add(1); - delta.add(1.0); - - gamma.unbind(); - delta.unbind(); -} - -TEST(UpDownCounter, DefaultConstruction) -{ - NoopIntUpDownCounter alpha("test", "none", "unitless", true, InstrumentKind::IntUpDownCounter); - NoopDoubleUpDownCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleUpDownCounter); - - alpha.bind("key: value"); - - BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); - BoundNoopIntUpDownCounter delta= beta.bind("key: value"); - - gamma.unbind(); - delta.unbind(); -} - -TEST(UpDownCounter, Add) -{ - NoopIntUpDownCounter alpha("test", "none", "unitless", true, InstrumentKind::IntUpDownCounter); - NoopDoubleUpDownCounter beta("other", "none", "unitless", true, InstrumentKind::DoubleUpDownCounter); - - alpha.add(1, "key:value"); - beta.add(1.0, "key:value"); - - BoundNoopIntUpDownCounter gamma= alpha.bind("key: value"); - BoundNoopIntUpDownCounter delta= beta.bind("key: value"); - - gamma.add(1); - delta.add(1.0); - gamma.add(-1); - delta.add(-1.0); - - gamma.unbind(); - delta.unbind(); -} - -TEST(ValueRecorder, DefaultConstruction) -{ - NoopIntValueRecorder alpha("test", "none", "unitless", true, InstrumentKind::IntValueRecorder); - NoopDoubleValueRecorder beta("other", "none", "unitless", true, InstrumentKind::DoubleValueRecorder); - - alpha.bind("key: value"); - - BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); - BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); - - gamma.unbind(); - delta.unbind(); -} - -TEST(ValueRecorder, Record) -{ - NoopIntValueRecorder alpha("test", "none", "unitless", true, InstrumentKind::IntValueRecorder); - NoopDoubleValueRecorder beta("other", "none", "unitless", true, InstrumentKind::DoubleValueRecorder); - - alpha.record(1, "key:value"); - beta.record(1.0, "key:value"); - - BoundNoopIntValueRecorder gamma= alpha.bind("key: value"); - BoundNoopDoubleValueRecorder delta= beta.bind("key: value"); - - gamma.record(1); - delta.record(1.0); - - gamma.unbind(); - delta.unbind(); -} - -} -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From 5c30d8f112918aa398552671768da4ec1d4ba3a2 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 8 Jul 2020 09:58:23 -0400 Subject: [PATCH 08/11] labels now passed as kv iterable --- .../opentelemetry/metrics/async_instruments.h | 12 +-- .../opentelemetry/metrics/instrument.h | 7 +- api/include/opentelemetry/metrics/noop.h | 24 ++--- .../opentelemetry/metrics/observer_result.h | 2 +- .../opentelemetry/metrics/sync_instruments.h | 22 ++--- api/test/metrics/noop_instrument_test.cc | 97 ++++++++++++------- 6 files changed, 98 insertions(+), 66 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index 3dad3db03e..8bbe24e694 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -27,7 +27,7 @@ class IntValueObserver : public AsynchronousInstrument * @param value is the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void observe(int value, const nostd::string_view &labels) {} + virtual void observe(int value, const trace::KeyValueIterable &labels) {} }; class DoubleValueObserver : public AsynchronousInstrument @@ -43,7 +43,7 @@ class DoubleValueObserver : public AsynchronousInstrument void (*callback)(ObserverResult)) {} - virtual void observe(double value, const nostd::string_view &labels) {} + virtual void observe(double value, const trace::KeyValueIterable &labels) {} }; class IntSumObserver : public AsynchronousInstrument @@ -67,7 +67,7 @@ class IntSumObserver : public AsynchronousInstrument * @param value is the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void observe(int value, const nostd::string_view &labels) {} + virtual void observe(int value, const trace::KeyValueIterable &labels) {} }; class DoubleSumObserver : public AsynchronousInstrument @@ -83,7 +83,7 @@ class DoubleSumObserver : public AsynchronousInstrument void (*callback)(ObserverResult)) {} - virtual void observe(double value, const nostd::string_view &labels) {} + virtual void observe(double value, const trace::KeyValueIterable &labels) {} }; class IntUpDownSumObserver : public AsynchronousInstrument @@ -99,7 +99,7 @@ class IntUpDownSumObserver : public AsynchronousInstrument void (*callback)(ObserverResult)) {} - virtual void observe(int value, const nostd::string_view &labels) {} + virtual void observe(int value, const trace::KeyValueIterable &labels) {} }; class DoubleUpDownSumObserver : public AsynchronousInstrument @@ -115,7 +115,7 @@ class DoubleUpDownSumObserver : public AsynchronousInstrument void (*callback)(ObserverResult)) {} - virtual void observe(double value, const nostd::string_view &labels) {} + virtual void observe(double value, const trace::KeyValueIterable &labels) {} }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index a268f4445f..273ff5a668 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -3,6 +3,7 @@ #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/trace/key_value_iterable_view.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics @@ -134,7 +135,7 @@ class SynchronousInstrument : public Instrument * @param labels the set of labels, as key-value pairs * @return a Bound Instrument */ - nostd::shared_ptr bind(const nostd::string_view &labels) + nostd::shared_ptr bind(const trace::KeyValueIterable &labels) { return nostd::shared_ptr(new BoundSynchronousInstrument()); } @@ -151,7 +152,7 @@ class SynchronousInstrument : public Instrument * @param value is the numerical representation of the metric being captured * @return void */ - virtual void update(nostd::variant value, const nostd::string_view &labels) final {} + virtual void update(nostd::variant value, const trace::KeyValueIterable &labels) final {} }; class ObserverResult; @@ -177,7 +178,7 @@ class AsynchronousInstrument : public Instrument * @param value is the numerical representation of the metric being captured * @return none */ - virtual void update(nostd::variant value, const nostd::string_view &labels) final {} + virtual void update(nostd::variant value, const trace::KeyValueIterable &labels) final {} protected: // Callback function which takes a pointer to an Asynchronous instrument (this) type which is diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 9388169c11..2498e79d55 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -142,12 +142,12 @@ class NoopIntCounter : public IntCounter bool /*enabled*/) {} - nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) { return nostd::shared_ptr(new BoundNoopIntCounter()); } - virtual void add(int value, const nostd::string_view & /*labels*/) override {} + virtual void add(int value, const trace::KeyValueIterable & /*labels*/) override {} }; class BoundNoopDoubleCounter : public BoundDoubleCounter @@ -177,12 +177,12 @@ class NoopDoubleCounter : public DoubleCounter bool /*enabled*/) {} - nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) { return nostd::shared_ptr(new BoundNoopDoubleCounter()); } - virtual void add(double value, const nostd::string_view & /*labels*/) override {} + virtual void add(double value, const trace::KeyValueIterable & /*labels*/) override {} }; class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter @@ -212,12 +212,12 @@ class NoopIntUpDownCounter : public IntUpDownCounter bool /*enabled*/) {} - nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) { return nostd::shared_ptr(new BoundNoopIntUpDownCounter()); } - virtual void add(int value, const nostd::string_view & /*labels*/) override {} + virtual void add(int value, const trace::KeyValueIterable & /*labels*/) override {} }; class BoundNoopDoubleUpDownCounter : public BoundDoubleUpDownCounter @@ -247,12 +247,12 @@ class NoopDoubleUpDownCounter : public DoubleUpDownCounter bool /*enabled*/) {} - nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) { return nostd::shared_ptr(new BoundNoopDoubleUpDownCounter()); } - virtual void add(double value, const nostd::string_view & /*labels*/) override {} + virtual void add(double value, const trace::KeyValueIterable & /*labels*/) override {} }; class BoundNoopIntValueRecorder : public BoundIntValueRecorder @@ -282,12 +282,12 @@ class NoopIntValueRecorder : public IntValueRecorder bool /*enabled*/) {} - nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) { return nostd::shared_ptr(new BoundNoopIntValueRecorder()); } - virtual void record(int value, const nostd::string_view & /*labels*/) override {} + virtual void record(int value, const trace::KeyValueIterable & /*labels*/) override {} }; class BoundNoopDoubleValueRecorder : public BoundDoubleValueRecorder @@ -317,12 +317,12 @@ class NoopDoubleValueRecorder : public DoubleValueRecorder bool /*enabled*/) {} - nostd::shared_ptr bind(const nostd::string_view & /*labels*/) + nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) { return nostd::shared_ptr(new BoundNoopDoubleValueRecorder()); } - virtual void record(double value, const nostd::string_view & /*labels*/) override {} + virtual void record(double value, const trace::KeyValueIterable & /*labels*/) override {} }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h index 1f4b194d42..d4238ec29d 100644 --- a/api/include/opentelemetry/metrics/observer_result.h +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -21,7 +21,7 @@ class ObserverResult ObserverResult(nostd::shared_ptr instrument) : instrument_(instrument) {} - void observe(nostd::variant value, const nostd::string_view &labels) + void observe(nostd::variant value, const trace::KeyValueIterable &labels) { instrument_->update(value, labels); } diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 99db7abb68..268a175410 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -60,7 +60,7 @@ class IntCounter : public SynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void add(int value, const nostd::string_view &labels) {} + virtual void add(int value, const trace::KeyValueIterable &labels) {} }; class BoundDoubleCounter : public BoundSynchronousInstrument @@ -90,12 +90,12 @@ class DoubleCounter : public SynchronousInstrument bool enabled) {} - nostd::shared_ptr bind(const nostd::string_view &labels) + nostd::shared_ptr bind(const trace::KeyValueIterable &labels) { return nostd::shared_ptr(new BoundDoubleCounter()); } - virtual void add(double value, const nostd::string_view &labels) {} + virtual void add(double value, const trace::KeyValueIterable &labels) {} }; class BoundIntUpDownCounter : public BoundSynchronousInstrument @@ -132,7 +132,7 @@ class IntUpDownCounter : public SynchronousInstrument bool enabled) {} - nostd::shared_ptr bind(const nostd::string_view &labels) + nostd::shared_ptr bind(const trace::KeyValueIterable &labels) { return nostd::shared_ptr(new BoundIntUpDownCounter()); } @@ -145,7 +145,7 @@ class IntUpDownCounter : public SynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void add(int value, const nostd::string_view &labels) {} + virtual void add(int value, const trace::KeyValueIterable &labels) {} }; class BoundDoubleUpDownCounter : public BoundSynchronousInstrument @@ -175,12 +175,12 @@ class DoubleUpDownCounter : public SynchronousInstrument bool enabled) {} - nostd::shared_ptr bind(const nostd::string_view &labels) + nostd::shared_ptr bind(const trace::KeyValueIterable &labels) { return nostd::shared_ptr(new BoundDoubleUpDownCounter()); } - virtual void add(double value, const nostd::string_view &labels) {} + virtual void add(double value, const trace::KeyValueIterable &labels) {} }; class BoundIntValueRecorder : public BoundSynchronousInstrument @@ -217,7 +217,7 @@ class IntValueRecorder : public SynchronousInstrument bool enabled) {} - nostd::shared_ptr bind(const nostd::string_view &labels) + nostd::shared_ptr bind(const trace::KeyValueIterable &labels) { return nostd::shared_ptr(new BoundIntValueRecorder()); } @@ -230,7 +230,7 @@ class IntValueRecorder : public SynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void record(int value, const nostd::string_view &labels) {} + virtual void record(int value, const trace::KeyValueIterable &labels) {} }; class BoundDoubleValueRecorder : public BoundSynchronousInstrument @@ -260,12 +260,12 @@ class DoubleValueRecorder : public SynchronousInstrument bool enabled) {} - nostd::shared_ptr bind(const nostd::string_view &labels) + nostd::shared_ptr bind(const trace::KeyValueIterable &labels) { return nostd::shared_ptr(new BoundDoubleValueRecorder()); } - virtual void record(double value, const nostd::string_view &labels) {} + virtual void record(double value, const trace::KeyValueIterable &labels) {} }; } // namespace metrics diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc index 83157dd877..a38d496fa1 100644 --- a/api/test/metrics/noop_instrument_test.cc +++ b/api/test/metrics/noop_instrument_test.cc @@ -2,6 +2,7 @@ #include #include #include "opentelemetry/metrics/noop.h" +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics @@ -9,14 +10,18 @@ namespace metrics void noopIntCallback(ObserverResult result) { - result.observe(1, "key: value"); - result.observe(-1, "key: value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + result.observe(1, labelkv); + result.observe(-1, labelkv); } void noopDoubleCallback(ObserverResult result) { - result.observe(1.0, "key: value"); - result.observe(-1.0, "key: value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + result.observe(1.0, labelkv); + result.observe(-1.0, labelkv); } TEST(ValueObserver, Observe) @@ -25,8 +30,11 @@ TEST(ValueObserver, Observe) NoopDoubleValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback); - alpha.observe(1, "key:value"); - beta.observe(1.0, "key:value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + + alpha.observe(1, labelkv); + beta.observe(1.0, labelkv); } TEST(SumObserver, DefaultConstruction) @@ -35,8 +43,11 @@ TEST(SumObserver, DefaultConstruction) NoopDoubleSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); - alpha.observe(1, "key:value"); - beta.observe(1.0, "key:value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + + alpha.observe(1, labelkv); + beta.observe(1.0, labelkv); } TEST(UpDownSumObserver, DefaultConstruction) @@ -45,10 +56,13 @@ TEST(UpDownSumObserver, DefaultConstruction) NoopDoubleUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); - alpha.observe(1, "key:value"); - beta.observe(1.0, "key:value"); - alpha.observe(-1, "key:value"); - beta.observe(-1.0, "key:value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + + alpha.observe(1, labelkv); + beta.observe(1.0, labelkv); + alpha.observe(-1, labelkv); + beta.observe(-1.0, labelkv); } TEST(Counter, DefaultConstruction) @@ -56,10 +70,12 @@ TEST(Counter, DefaultConstruction) NoopIntCounter alpha("test", "none", "unitless", true); NoopDoubleCounter beta("other", "none", "unitless", true); - alpha.bind("key: value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + alpha.bind(labelkv); - auto gamma = alpha.bind("key: value"); - auto delta = beta.bind("key: value"); + auto gamma = alpha.bind(labelkv); + auto delta = beta.bind(labelkv); gamma->unbind(); delta->unbind(); @@ -70,11 +86,14 @@ TEST(Counter, Add) NoopIntCounter alpha("test", "none", "unitless", true); NoopDoubleCounter beta("other", "none", "unitless", true); - alpha.add(1, "key:value"); - beta.add(1.0, "key:value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + + alpha.add(1, labelkv); + beta.add(1.0, labelkv); - auto gamma = alpha.bind("key: value"); - auto delta = beta.bind("key: value"); + auto gamma = alpha.bind(labelkv); + auto delta = beta.bind(labelkv); gamma->add(1); delta->add(1.0); @@ -88,10 +107,13 @@ TEST(UpDownCounter, DefaultConstruction) NoopIntUpDownCounter alpha("test", "none", "unitless", true); NoopDoubleUpDownCounter beta("other", "none", "unitless", true); - alpha.bind("key: value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; - auto gamma = alpha.bind("key: value"); - auto delta = beta.bind("key: value"); + alpha.bind(labelkv); + + auto gamma = alpha.bind(labelkv); + auto delta = beta.bind(labelkv); gamma->unbind(); delta->unbind(); @@ -102,11 +124,14 @@ TEST(UpDownCounter, Add) NoopIntUpDownCounter alpha("test", "none", "unitless", true); NoopDoubleUpDownCounter beta("other", "none", "unitless", true); - alpha.add(1, "key:value"); - beta.add(1.0, "key:value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + + alpha.add(1, labelkv); + beta.add(1.0, labelkv); - auto gamma = alpha.bind("key: value"); - auto delta = beta.bind("key: value"); + auto gamma = alpha.bind(labelkv); + auto delta = beta.bind(labelkv); gamma->add(1); delta->add(1.0); @@ -122,10 +147,13 @@ TEST(ValueRecorder, DefaultConstruction) NoopIntValueRecorder alpha("test", "none", "unitless", true); NoopDoubleValueRecorder beta("other", "none", "unitless", true); - alpha.bind("key: value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; - auto gamma = alpha.bind("key: value"); - auto delta = beta.bind("key: value"); + alpha.bind(labelkv); + + auto gamma = alpha.bind(labelkv); + auto delta = beta.bind(labelkv); gamma->unbind(); delta->unbind(); @@ -136,11 +164,14 @@ TEST(ValueRecorder, Record) NoopIntValueRecorder alpha("test", "none", "unitless", true); NoopDoubleValueRecorder beta("other", "none", "unitless", true); - alpha.record(1, "key:value"); - beta.record(1.0, "key:value"); + std::map labels = {{"key", "value"}}; + auto labelkv = trace::KeyValueIterableView{labels}; + + alpha.record(1, labelkv); + beta.record(1.0, labelkv); - auto gamma = alpha.bind("key: value"); - auto delta = beta.bind("key: value"); + auto gamma = alpha.bind(labelkv); + auto delta = beta.bind(labelkv); gamma->record(1); delta->record(1.0); From 3df9b0e75acabd2d4558e0177ab44b671c131e18 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Wed, 15 Jul 2020 00:51:43 -0400 Subject: [PATCH 09/11] templating and pure virtuals --- .../opentelemetry/metrics/async_instruments.h | 93 ++---- .../opentelemetry/metrics/instrument.h | 38 +-- api/include/opentelemetry/metrics/noop.h | 295 +++++++----------- .../opentelemetry/metrics/observer_result.h | 14 +- .../opentelemetry/metrics/sync_instruments.h | 220 ++++--------- api/test/metrics/noop_instrument_test.cc | 85 ++--- 6 files changed, 263 insertions(+), 482 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index 8bbe24e694..53e1d2f17e 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -7,17 +7,18 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -class IntValueObserver : public AsynchronousInstrument +template +class ValueObserver : public AsynchronousInstrument { public: - IntValueObserver() = default; + ValueObserver() = default; - IntValueObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) + ValueObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) {} /* @@ -27,36 +28,21 @@ class IntValueObserver : public AsynchronousInstrument * @param value is the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void observe(int value, const trace::KeyValueIterable &labels) {} + virtual void observe(T value, const trace::KeyValueIterable &labels) {} }; -class DoubleValueObserver : public AsynchronousInstrument +template +class SumObserver : public AsynchronousInstrument { public: - DoubleValueObserver() = default; + SumObserver() = default; - DoubleValueObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) - {} - - virtual void observe(double value, const trace::KeyValueIterable &labels) {} -}; - -class IntSumObserver : public AsynchronousInstrument -{ - -public: - IntSumObserver() = default; - - IntSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) + SumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) {} /* @@ -67,56 +53,25 @@ class IntSumObserver : public AsynchronousInstrument * @param value is the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void observe(int value, const trace::KeyValueIterable &labels) {} + virtual void observe(T value, const trace::KeyValueIterable &labels) {} }; -class DoubleSumObserver : public AsynchronousInstrument +template +class UpDownSumObserver : public AsynchronousInstrument { public: - DoubleSumObserver() = default; + UpDownSumObserver() = default; - DoubleSumObserver(nostd::string_view name, + UpDownSumObserver(nostd::string_view name, nostd::string_view description, nostd::string_view unit, bool enabled, - void (*callback)(ObserverResult)) - {} - - virtual void observe(double value, const trace::KeyValueIterable &labels) {} -}; - -class IntUpDownSumObserver : public AsynchronousInstrument -{ - -public: - IntUpDownSumObserver() = default; - - IntUpDownSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) + void (*callback)(ObserverResult)) {} virtual void observe(int value, const trace::KeyValueIterable &labels) {} }; -class DoubleUpDownSumObserver : public AsynchronousInstrument -{ - -public: - DoubleUpDownSumObserver() = default; - - DoubleUpDownSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) - {} - - virtual void observe(double value, const trace::KeyValueIterable &labels) {} -}; - } // namespace metrics -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index 273ff5a668..2d3915feb5 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -63,24 +63,21 @@ class Instrument {} // Returns true if the instrument is enabled and collecting data - virtual bool IsEnabled() - { - // False in default implementation - return false; - } + virtual bool IsEnabled() = 0; // Return the instrument name - virtual nostd::string_view GetName() { return nostd::string_view(""); } + virtual nostd::string_view GetName() = 0; // Return the instrument description - virtual nostd::string_view GetDescription() { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() = 0; // Return the insrument's units of measurement - virtual nostd::string_view GetUnits() { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() = 0; virtual ~Instrument() = default; }; +template class BoundSynchronousInstrument : public Instrument { @@ -90,8 +87,7 @@ class BoundSynchronousInstrument : public Instrument BoundSynchronousInstrument(nostd::string_view name, nostd::string_view description, nostd::string_view unit, - bool enabled) - {} + bool enabled); /** * Frees the resources associated with this Bound Instrument. @@ -100,7 +96,7 @@ class BoundSynchronousInstrument : public Instrument * @param none * @return void */ - virtual void unbind() {} + virtual void unbind() final {} /** * Records a single synchronous metric event; a call to the aggregator @@ -110,9 +106,10 @@ class BoundSynchronousInstrument : public Instrument * @param value is the numerical representation of the metric being captured * @return void */ - virtual void update(nostd::variant value) final {} + virtual void update(T value) final {} }; +template class SynchronousInstrument : public Instrument { @@ -135,10 +132,7 @@ class SynchronousInstrument : public Instrument * @param labels the set of labels, as key-value pairs * @return a Bound Instrument */ - nostd::shared_ptr bind(const trace::KeyValueIterable &labels) - { - return nostd::shared_ptr(new BoundSynchronousInstrument()); - } + nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); /** * Records a single synchronous metric event. @@ -152,11 +146,13 @@ class SynchronousInstrument : public Instrument * @param value is the numerical representation of the metric being captured * @return void */ - virtual void update(nostd::variant value, const trace::KeyValueIterable &labels) final {} + virtual void update(T value, const trace::KeyValueIterable &labels) final {} }; +template class ObserverResult; +template class AsynchronousInstrument : public Instrument { @@ -167,24 +163,24 @@ class AsynchronousInstrument : public Instrument nostd::string_view description, nostd::string_view unit, bool enabled, - void(callback)(ObserverResult)) + void(callback)(ObserverResult)) {} /** * Captures data by activating the callback function associated with the - * instrument and storing its return value. Callbacks for asychronous + * instrument and storing its return value. Callbacks for asynchronous * instruments are defined during construction. * * @param value is the numerical representation of the metric being captured * @return none */ - virtual void update(nostd::variant value, const trace::KeyValueIterable &labels) final {} + virtual void update(T value, const trace::KeyValueIterable &labels) final {} protected: // Callback function which takes a pointer to an Asynchronous instrument (this) type which is // stored in an observer result type and returns nothing. This function calls the instrument's // observe. - void (*callback_)(ObserverResult); + void (*callback_)(ObserverResult); }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 2498e79d55..0f8ca3cf6a 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -43,287 +43,228 @@ class NoopMeterProvider final : public opentelemetry::metrics::MeterProvider nostd::shared_ptr meter_; }; -class NoopIntValueObserver : public IntValueObserver +template +class NoopValueObserver : public ValueObserver { public: - NoopIntValueObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) + NoopValueObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} -}; -class NoopDoubleValueObserver : public DoubleValueObserver -{ + virtual bool IsEnabled() override { return false; } -public: - NoopDoubleValueObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool enabled, - void (*callback)(ObserverResult)) - {} -}; + virtual nostd::string_view GetName() override { return nostd::string_view(""); } -class NoopIntSumObserver : public IntSumObserver -{ + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } -public: - NoopIntSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) - {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopDoubleSumObserver : public DoubleSumObserver +template +class NoopSumObserver : public SumObserver { public: - NoopDoubleSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) + NoopSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} -}; -class NoopIntUpDownSumObserver : public IntUpDownSumObserver -{ + virtual bool IsEnabled() override { return false; } -public: - NoopIntUpDownSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) - {} + virtual nostd::string_view GetName() override { return nostd::string_view(""); } + + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopDoubleUpDownSumObserver : public DoubleUpDownSumObserver +template +class NoopUpDownSumObserver : public UpDownSumObserver { public: - NoopDoubleUpDownSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) + NoopUpDownSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} -}; -class BoundNoopIntCounter : public BoundIntCounter -{ + virtual bool IsEnabled() override { return false; } -public: - BoundNoopIntCounter() = default; + virtual nostd::string_view GetName() override { return nostd::string_view(""); } - BoundNoopIntCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } - virtual void add(int value) override {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopIntCounter : public IntCounter +template +class BoundNoopCounter : public BoundCounter { public: - NoopIntCounter() = default; + BoundNoopCounter() = default; - NoopIntCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + BoundNoopCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} - nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) - { - return nostd::shared_ptr(new BoundNoopIntCounter()); - } + virtual void add(T value) override {} - virtual void add(int value, const trace::KeyValueIterable & /*labels*/) override {} -}; + virtual bool IsEnabled() override { return false; } -class BoundNoopDoubleCounter : public BoundDoubleCounter -{ + virtual nostd::string_view GetName() override { return nostd::string_view(""); } -public: - BoundNoopDoubleCounter() = default; + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } - BoundNoopDoubleCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - virtual void add(double value) override {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopDoubleCounter : public DoubleCounter +template +class NoopCounter : public Counter { public: - NoopDoubleCounter() = default; + NoopCounter() = default; - NoopDoubleCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + NoopCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} - nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) + nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) { - return nostd::shared_ptr(new BoundNoopDoubleCounter()); + return nostd::shared_ptr>(new BoundNoopCounter()); } - virtual void add(double value, const trace::KeyValueIterable & /*labels*/) override {} -}; + virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} -class BoundNoopIntUpDownCounter : public BoundIntUpDownCounter -{ + virtual bool IsEnabled() override { return false; } -public: - BoundNoopIntUpDownCounter() = default; + virtual nostd::string_view GetName() override { return nostd::string_view(""); } - BoundNoopIntUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } - virtual void add(int value) override {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopIntUpDownCounter : public IntUpDownCounter +template +class BoundNoopUpDownCounter : public BoundUpDownCounter { public: - NoopIntUpDownCounter() = default; + BoundNoopUpDownCounter() = default; - NoopIntUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + BoundNoopUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} - nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) - { - return nostd::shared_ptr(new BoundNoopIntUpDownCounter()); - } - - virtual void add(int value, const trace::KeyValueIterable & /*labels*/) override {} -}; + virtual void add(T value) override {} -class BoundNoopDoubleUpDownCounter : public BoundDoubleUpDownCounter -{ + virtual bool IsEnabled() override { return false; } -public: - BoundNoopDoubleUpDownCounter() = default; + virtual nostd::string_view GetName() override { return nostd::string_view(""); } - BoundNoopDoubleUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } - virtual void add(double value) override {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopDoubleUpDownCounter : public DoubleUpDownCounter +template +class NoopUpDownCounter : public UpDownCounter { public: - NoopDoubleUpDownCounter() = default; + NoopUpDownCounter() = default; - NoopDoubleUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + NoopUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} - nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) + nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) { - return nostd::shared_ptr(new BoundNoopDoubleUpDownCounter()); + return nostd::shared_ptr>(new BoundNoopUpDownCounter()); } - virtual void add(double value, const trace::KeyValueIterable & /*labels*/) override {} -}; + virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} -class BoundNoopIntValueRecorder : public BoundIntValueRecorder -{ + virtual bool IsEnabled() override { return false; } -public: - BoundNoopIntValueRecorder() = default; + virtual nostd::string_view GetName() override { return nostd::string_view(""); } - BoundNoopIntValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } - virtual void record(int value) override {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopIntValueRecorder : public IntValueRecorder +template +class BoundNoopValueRecorder : public BoundValueRecorder { public: - NoopIntValueRecorder() = default; + BoundNoopValueRecorder() = default; - NoopIntValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + BoundNoopValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} - nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) - { - return nostd::shared_ptr(new BoundNoopIntValueRecorder()); - } + virtual void record(T value) override {} - virtual void record(int value, const trace::KeyValueIterable & /*labels*/) override {} -}; + virtual bool IsEnabled() override { return false; } -class BoundNoopDoubleValueRecorder : public BoundDoubleValueRecorder -{ + virtual nostd::string_view GetName() override { return nostd::string_view(""); } -public: - BoundNoopDoubleValueRecorder() = default; + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } - BoundNoopDoubleValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - virtual void record(double value) override {} + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; -class NoopDoubleValueRecorder : public DoubleValueRecorder +template +class NoopValueRecorder : public ValueRecorder { public: - NoopDoubleValueRecorder() = default; + NoopValueRecorder() = default; - NoopDoubleValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + NoopValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} - nostd::shared_ptr bind(const trace::KeyValueIterable & /*labels*/) + nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) { - return nostd::shared_ptr(new BoundNoopDoubleValueRecorder()); + return nostd::shared_ptr>(new BoundNoopValueRecorder()); } - virtual void record(double value, const trace::KeyValueIterable & /*labels*/) override {} + virtual void record(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual bool IsEnabled() override { return false; } + + virtual nostd::string_view GetName() override { return nostd::string_view(""); } + + virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + + virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; + } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h index d4238ec29d..3a7bfc9df8 100644 --- a/api/include/opentelemetry/metrics/observer_result.h +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -13,22 +13,18 @@ namespace metrics * accept a single ObserverResult object and update using its pointer to the * instrument itself. */ + +template class ObserverResult { public: ObserverResult() = default; - ObserverResult(nostd::shared_ptr instrument) : instrument_(instrument) {} - - void observe(nostd::variant value, const trace::KeyValueIterable &labels) - { - instrument_->update(value, labels); - } + ObserverResult(nostd::shared_ptr> instrument) {} -private: - nostd::shared_ptr instrument_; + virtual void observe(T value, const trace::KeyValueIterable &labels) {} }; } // namespace metrics -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 268a175410..d8ddc98122 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -6,17 +6,17 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -class BoundIntCounter : public BoundSynchronousInstrument +template +class BoundCounter : public BoundSynchronousInstrument { public: - BoundIntCounter() = default; + BoundCounter() = default; - BoundIntCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + BoundCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument @@ -25,20 +25,20 @@ class BoundIntCounter : public BoundSynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void add(int value) {} + virtual void add(T value) = 0; }; -class IntCounter : public SynchronousInstrument +template +class Counter : public SynchronousInstrument { public: - IntCounter() = default; + Counter() = default; - IntCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + Counter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -47,10 +47,7 @@ class IntCounter : public SynchronousInstrument * @param labels the set of labels, as key-value pairs. * @return a BoundIntCounter tied to the specified labels */ - nostd::shared_ptr bind(const nostd::string_view &labels) - { - return nostd::shared_ptr(new BoundIntCounter()); - } + nostd::shared_ptr> bind(const nostd::string_view &labels); /* * Add adds the value to the counter's sum. The labels should contain @@ -60,55 +57,20 @@ class IntCounter : public SynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void add(int value, const trace::KeyValueIterable &labels) {} + virtual void add(T value, const trace::KeyValueIterable &labels) = 0; }; -class BoundDoubleCounter : public BoundSynchronousInstrument +template +class BoundUpDownCounter : public BoundSynchronousInstrument { public: - BoundDoubleCounter() = default; + BoundUpDownCounter() = default; - BoundDoubleCounter(nostd::string_view name, + BoundUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit, - bool enabled) - {} - - virtual void add(double value) {} -}; - -class DoubleCounter : public SynchronousInstrument -{ - -public: - DoubleCounter() = default; - - DoubleCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} - - nostd::shared_ptr bind(const trace::KeyValueIterable &labels) - { - return nostd::shared_ptr(new BoundDoubleCounter()); - } - - virtual void add(double value, const trace::KeyValueIterable &labels) {} -}; - -class BoundIntUpDownCounter : public BoundSynchronousInstrument -{ - -public: - BoundIntUpDownCounter() = default; - - BoundIntUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked to * the instrument and @@ -117,25 +79,25 @@ class BoundIntUpDownCounter : public BoundSynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void add(int value) {} + virtual void add(T value) = 0; }; -class IntUpDownCounter : public SynchronousInstrument +template +class UpDownCounter : public SynchronousInstrument { public: - IntUpDownCounter() = default; + UpDownCounter() = default; - IntUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + UpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); - nostd::shared_ptr bind(const trace::KeyValueIterable &labels) - { - return nostd::shared_ptr(new BoundIntUpDownCounter()); - } + nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); + //{ + // return nostd::shared_ptr>(new BoundUpDownCounter()); + //} /* * Add adds the value to the counter's sum. The labels should contain @@ -145,55 +107,20 @@ class IntUpDownCounter : public SynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void add(int value, const trace::KeyValueIterable &labels) {} + virtual void add(T value, const trace::KeyValueIterable &labels) = 0; }; -class BoundDoubleUpDownCounter : public BoundSynchronousInstrument +template +class BoundValueRecorder : public BoundSynchronousInstrument { public: - BoundDoubleUpDownCounter() = default; - - BoundDoubleUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} - - virtual void add(double value) {} -}; - -class DoubleUpDownCounter : public SynchronousInstrument -{ - -public: - DoubleUpDownCounter() = default; - - DoubleUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + BoundValueRecorder() = default; - nostd::shared_ptr bind(const trace::KeyValueIterable &labels) - { - return nostd::shared_ptr(new BoundDoubleUpDownCounter()); - } - - virtual void add(double value, const trace::KeyValueIterable &labels) {} -}; - -class BoundIntValueRecorder : public BoundSynchronousInstrument -{ - -public: - BoundIntValueRecorder() = default; - - BoundIntValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + BoundValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Records the value by summing it with previous measurements and checking * previously stored @@ -202,25 +129,25 @@ class BoundIntValueRecorder : public BoundSynchronousInstrument * * @param value the numerical representation of the metric being captured */ - virtual void record(int value) {} + virtual void record(T value) = 0; }; -class IntValueRecorder : public SynchronousInstrument +template +class ValueRecorder : public SynchronousInstrument { public: - IntValueRecorder() = default; + ValueRecorder() = default; - IntValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} + ValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); - nostd::shared_ptr bind(const trace::KeyValueIterable &labels) - { - return nostd::shared_ptr(new BoundIntValueRecorder()); - } + nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); + //{ + // return nostd::shared_ptr>(new BoundValueRecorder()); + //} /* * Records the value by summing it with previous measurements and checking * previously stored @@ -230,43 +157,8 @@ class IntValueRecorder : public SynchronousInstrument * @param value the numerical representation of the metric being captured * @param labels the set of labels, as key-value pairs */ - virtual void record(int value, const trace::KeyValueIterable &labels) {} -}; - -class BoundDoubleValueRecorder : public BoundSynchronousInstrument -{ - -public: - BoundDoubleValueRecorder() = default; - - BoundDoubleValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} - - virtual void record(double value) {} -}; - -class DoubleValueRecorder : public SynchronousInstrument -{ - -public: - DoubleValueRecorder() = default; - - DoubleValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} - - nostd::shared_ptr bind(const trace::KeyValueIterable &labels) - { - return nostd::shared_ptr(new BoundDoubleValueRecorder()); - } - - virtual void record(double value, const trace::KeyValueIterable &labels) {} + virtual void record(T value, const trace::KeyValueIterable &labels) = 0; }; } // namespace metrics -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc index a38d496fa1..7b693046d7 100644 --- a/api/test/metrics/noop_instrument_test.cc +++ b/api/test/metrics/noop_instrument_test.cc @@ -1,63 +1,63 @@ #include #include +#include #include #include "opentelemetry/metrics/noop.h" -#include OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { -void noopIntCallback(ObserverResult result) +void noopIntCallback(ObserverResult result) { std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; result.observe(1, labelkv); result.observe(-1, labelkv); } -void noopDoubleCallback(ObserverResult result) +void noopDoubleCallback(ObserverResult result) { std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; - result.observe(1.0, labelkv); - result.observe(-1.0, labelkv); + auto labelkv = trace::KeyValueIterableView{labels}; + result.observe(1.5, labelkv); + result.observe(-1.5, labelkv); } TEST(ValueObserver, Observe) { - NoopIntValueObserver alpha("test", "none", "unitless", true, &noopIntCallback); + NoopValueObserver alpha("test", "none", "unitless", true, &noopIntCallback); - NoopDoubleValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback); + NoopValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.observe(1, labelkv); - beta.observe(1.0, labelkv); + beta.observe(1.5, labelkv); } TEST(SumObserver, DefaultConstruction) { - NoopIntSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); + NoopSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); - NoopDoubleSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); + NoopSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.observe(1, labelkv); - beta.observe(1.0, labelkv); + beta.observe(1.5, labelkv); } TEST(UpDownSumObserver, DefaultConstruction) { - NoopIntUpDownSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); + NoopUpDownSumObserver alpha("test", "none", "unitless", true, &noopIntCallback); - NoopDoubleUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); + NoopUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.observe(1, labelkv); beta.observe(1.0, labelkv); @@ -67,11 +67,12 @@ TEST(UpDownSumObserver, DefaultConstruction) TEST(Counter, DefaultConstruction) { - NoopIntCounter alpha("test", "none", "unitless", true); - NoopDoubleCounter beta("other", "none", "unitless", true); + NoopCounter alpha("test", "none", "unitless", true); + NoopCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; + alpha.bind(labelkv); auto gamma = alpha.bind(labelkv); @@ -83,20 +84,20 @@ TEST(Counter, DefaultConstruction) TEST(Counter, Add) { - NoopIntCounter alpha("test", "none", "unitless", true); - NoopDoubleCounter beta("other", "none", "unitless", true); + NoopCounter alpha("test", "none", "unitless", true); + NoopCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.add(1, labelkv); - beta.add(1.0, labelkv); + beta.add(1.5, labelkv); auto gamma = alpha.bind(labelkv); auto delta = beta.bind(labelkv); gamma->add(1); - delta->add(1.0); + delta->add(1.5); gamma->unbind(); delta->unbind(); @@ -104,11 +105,11 @@ TEST(Counter, Add) TEST(UpDownCounter, DefaultConstruction) { - NoopIntUpDownCounter alpha("test", "none", "unitless", true); - NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + NoopUpDownCounter alpha("test", "none", "unitless", true); + NoopUpDownCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.bind(labelkv); @@ -121,14 +122,14 @@ TEST(UpDownCounter, DefaultConstruction) TEST(UpDownCounter, Add) { - NoopIntUpDownCounter alpha("test", "none", "unitless", true); - NoopDoubleUpDownCounter beta("other", "none", "unitless", true); + NoopUpDownCounter alpha("test", "none", "unitless", true); + NoopUpDownCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.add(1, labelkv); - beta.add(1.0, labelkv); + beta.add(1.5, labelkv); auto gamma = alpha.bind(labelkv); auto delta = beta.bind(labelkv); @@ -144,11 +145,11 @@ TEST(UpDownCounter, Add) TEST(ValueRecorder, DefaultConstruction) { - NoopIntValueRecorder alpha("test", "none", "unitless", true); - NoopDoubleValueRecorder beta("other", "none", "unitless", true); + NoopValueRecorder alpha("test", "none", "unitless", true); + NoopValueRecorder beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.bind(labelkv); @@ -161,24 +162,24 @@ TEST(ValueRecorder, DefaultConstruction) TEST(ValueRecorder, Record) { - NoopIntValueRecorder alpha("test", "none", "unitless", true); - NoopDoubleValueRecorder beta("other", "none", "unitless", true); + NoopValueRecorder alpha("test", "none", "unitless", true); + NoopValueRecorder beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.record(1, labelkv); - beta.record(1.0, labelkv); + beta.record(1.5, labelkv); auto gamma = alpha.bind(labelkv); auto delta = beta.bind(labelkv); gamma->record(1); - delta->record(1.0); + delta->record(1.5); gamma->unbind(); delta->unbind(); } } // namespace metrics -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE From 51f2c49a3b11416ae82635df7079c24763c77a96 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Mon, 20 Jul 2020 12:20:38 -0400 Subject: [PATCH 10/11] cleaning up for PR --- .../opentelemetry/metrics/async_instruments.h | 24 +- .../opentelemetry/metrics/instrument.h | 29 +-- api/include/opentelemetry/metrics/noop.h | 213 ++++++++++++------ .../opentelemetry/metrics/observer_result.h | 1 + .../opentelemetry/metrics/sync_instruments.h | 37 +-- api/test/metrics/noop_instrument_test.cc | 24 +- 6 files changed, 196 insertions(+), 132 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index 53e1d2f17e..37ce7da900 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -15,10 +15,10 @@ class ValueObserver : public AsynchronousInstrument ValueObserver() = default; ValueObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) {} /* @@ -39,10 +39,10 @@ class SumObserver : public AsynchronousInstrument SumObserver() = default; SumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) {} /* @@ -64,10 +64,10 @@ class UpDownSumObserver : public AsynchronousInstrument UpDownSumObserver() = default; UpDownSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) {} virtual void observe(int value, const trace::KeyValueIterable &labels) {} diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index 2d3915feb5..3e57bd735a 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -12,29 +12,12 @@ namespace metrics // Enum classes to help determine instrument types in other parts of the API enum class InstrumentKind { - IntCounter, - IntUpDownCounter, - IntValueRecorder, - IntValueObserver, - IntSumObserver, - IntUpDownSumObserver, - DoubleCounter, - DoubleUpDownCounter, - DoubleValueRecorder, - DoubleValueObserver, - DoubleSumObserver, - DoubleUpDownSumObserver -}; - -// Fewer Bound types because Asynchronous instruments cannot bind -enum class BoundInstrumentKind -{ - BoundIntCounter, - BoundIntUpDownCounter, - BoundIntValueRecorder, - BoundDoubleCounter, - BoundDoubleUpDownCounter, - BoundDoubleValueRecorder + Counter = 0, + UpDownCounter = 1, + ValueRecorder = 2, + ValueObserver = 3, + SumObserver = 4, + UpDownSumObserver = 5, }; class Instrument diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 0f8ca3cf6a..a0a0504928 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -49,63 +49,90 @@ class NoopValueObserver : public ValueObserver public: NoopValueObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) - {} + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} + + virtual bool IsEnabled() override { + return false; + } - virtual bool IsEnabled() override { return false; } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; + template class NoopSumObserver : public SumObserver { public: NoopSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } }; + template class NoopUpDownSumObserver : public UpDownSumObserver { public: NoopUpDownSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } }; + template class BoundNoopCounter : public BoundCounter { @@ -114,20 +141,29 @@ class BoundNoopCounter : public BoundCounter BoundNoopCounter() = default; BoundNoopCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} virtual void add(T value) override {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; template @@ -138,9 +174,9 @@ class NoopCounter : public Counter NoopCounter() = default; NoopCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) @@ -150,13 +186,22 @@ class NoopCounter : public Counter virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; template @@ -167,20 +212,29 @@ class BoundNoopUpDownCounter : public BoundUpDownCounter BoundNoopUpDownCounter() = default; BoundNoopUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} virtual void add(T value) override {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } }; template @@ -191,9 +245,9 @@ class NoopUpDownCounter : public UpDownCounter NoopUpDownCounter() = default; NoopUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) @@ -203,15 +257,24 @@ class NoopUpDownCounter : public UpDownCounter virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } }; + template class BoundNoopValueRecorder : public BoundValueRecorder { @@ -220,20 +283,28 @@ class BoundNoopValueRecorder : public BoundValueRecorder BoundNoopValueRecorder() = default; BoundNoopValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} virtual void record(T value) override {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } }; template @@ -244,9 +315,9 @@ class NoopValueRecorder : public ValueRecorder NoopValueRecorder() = default; NoopValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) {} nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) @@ -256,13 +327,21 @@ class NoopValueRecorder : public ValueRecorder virtual void record(T value, const trace::KeyValueIterable & /*labels*/) override {} - virtual bool IsEnabled() override { return false; } + virtual bool IsEnabled() override { + return false; + } - virtual nostd::string_view GetName() override { return nostd::string_view(""); } + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetDescription() override { return nostd::string_view(""); } + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } - virtual nostd::string_view GetUnits() override { return nostd::string_view(""); } + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } }; diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h index 3a7bfc9df8..bb61e97bf9 100644 --- a/api/include/opentelemetry/metrics/observer_result.h +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -24,6 +24,7 @@ class ObserverResult ObserverResult(nostd::shared_ptr> instrument) {} virtual void observe(T value, const trace::KeyValueIterable &labels) {} + }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index d8ddc98122..8a36005e9f 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -14,9 +14,9 @@ class BoundCounter : public BoundSynchronousInstrument BoundCounter() = default; BoundCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked * to the instrument @@ -36,9 +36,9 @@ class Counter : public SynchronousInstrument Counter() = default; Counter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Bind creates a bound instrument for this counter. The labels are @@ -68,9 +68,9 @@ class BoundUpDownCounter : public BoundSynchronousInstrument BoundUpDownCounter() = default; BoundUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Add adds the value to the counter's sum. The labels are already linked to * the instrument and @@ -90,9 +90,9 @@ class UpDownCounter : public SynchronousInstrument UpDownCounter() = default; UpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + nostd::string_view description, + nostd::string_view unit, + bool enabled); nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); //{ @@ -118,9 +118,9 @@ class BoundValueRecorder : public BoundSynchronousInstrument BoundValueRecorder() = default; BoundValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + nostd::string_view description, + nostd::string_view unit, + bool enabled); /* * Records the value by summing it with previous measurements and checking * previously stored @@ -132,6 +132,7 @@ class BoundValueRecorder : public BoundSynchronousInstrument virtual void record(T value) = 0; }; + template class ValueRecorder : public SynchronousInstrument { @@ -140,9 +141,9 @@ class ValueRecorder : public SynchronousInstrument ValueRecorder() = default; ValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); + nostd::string_view description, + nostd::string_view unit, + bool enabled); nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); //{ diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc index 7b693046d7..b68206059f 100644 --- a/api/test/metrics/noop_instrument_test.cc +++ b/api/test/metrics/noop_instrument_test.cc @@ -1,8 +1,8 @@ #include #include -#include #include #include "opentelemetry/metrics/noop.h" +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics @@ -11,7 +11,7 @@ namespace metrics void noopIntCallback(ObserverResult result) { std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; result.observe(1, labelkv); result.observe(-1, labelkv); } @@ -19,7 +19,7 @@ void noopIntCallback(ObserverResult result) void noopDoubleCallback(ObserverResult result) { std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; result.observe(1.5, labelkv); result.observe(-1.5, labelkv); } @@ -31,7 +31,7 @@ TEST(ValueObserver, Observe) NoopValueObserver beta("test", "none", "unitless", true, &noopDoubleCallback); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.observe(1, labelkv); beta.observe(1.5, labelkv); @@ -44,7 +44,7 @@ TEST(SumObserver, DefaultConstruction) NoopSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.observe(1, labelkv); beta.observe(1.5, labelkv); @@ -57,7 +57,7 @@ TEST(UpDownSumObserver, DefaultConstruction) NoopUpDownSumObserver beta("test", "none", "unitless", true, &noopDoubleCallback); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.observe(1, labelkv); beta.observe(1.0, labelkv); @@ -71,7 +71,7 @@ TEST(Counter, DefaultConstruction) NoopCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.bind(labelkv); @@ -88,7 +88,7 @@ TEST(Counter, Add) NoopCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.add(1, labelkv); beta.add(1.5, labelkv); @@ -109,7 +109,7 @@ TEST(UpDownCounter, DefaultConstruction) NoopUpDownCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.bind(labelkv); @@ -126,7 +126,7 @@ TEST(UpDownCounter, Add) NoopUpDownCounter beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.add(1, labelkv); beta.add(1.5, labelkv); @@ -149,7 +149,7 @@ TEST(ValueRecorder, DefaultConstruction) NoopValueRecorder beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.bind(labelkv); @@ -166,7 +166,7 @@ TEST(ValueRecorder, Record) NoopValueRecorder beta("other", "none", "unitless", true); std::map labels = {{"key", "value"}}; - auto labelkv = trace::KeyValueIterableView{labels}; + auto labelkv = trace::KeyValueIterableView{labels}; alpha.record(1, labelkv); beta.record(1.5, labelkv); From 0414c0e50be2d36ecbe29229a925bc862c268be1 Mon Sep 17 00:00:00 2001 From: Ankit Bhargava Date: Fri, 24 Jul 2020 13:22:42 -0400 Subject: [PATCH 11/11] modified structure --- .../opentelemetry/metrics/async_instruments.h | 112 ++-- .../opentelemetry/metrics/instrument.h | 290 +++++----- api/include/opentelemetry/metrics/noop.h | 541 ++++++++++-------- .../opentelemetry/metrics/observer_result.h | 11 +- .../opentelemetry/metrics/sync_instruments.h | 255 +++++---- api/test/metrics/noop_instrument_test.cc | 24 +- 6 files changed, 672 insertions(+), 561 deletions(-) diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index 37ce7da900..71504a8480 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -8,69 +8,79 @@ namespace metrics { template -class ValueObserver : public AsynchronousInstrument +class ValueObserver : virtual public AsynchronousInstrument { - + public: - ValueObserver() = default; - - ValueObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) - {} - - /* - * Updates the instruments aggregator with the new value. The labels should - * contain the keys and values to be associated with this value. - * - * @param value is the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void observe(T value, const trace::KeyValueIterable &labels) {} + ValueObserver() = default; + + ValueObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + + /* + * Updates the instruments aggregator with the new value. The labels should + * contain the keys and values to be associated with this value. + * + * @param value is the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void observe(T value, const trace::KeyValueIterable &labels) override = 0; + + /** + * Captures data by activating the callback function associated with the + * instrument and storing its return value. Callbacks for asynchronous + * instruments are defined during construction. + * + * @param none + * @return none + */ + virtual void run () override = 0; + }; template -class SumObserver : public AsynchronousInstrument +class SumObserver : virtual public AsynchronousInstrument { - + public: - SumObserver() = default; - - SumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) - {} - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive - * valued updates. - * - * @param value is the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void observe(T value, const trace::KeyValueIterable &labels) {} + SumObserver() = default; + + SumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + + + virtual void observe(T value, const trace::KeyValueIterable &labels) override = 0; + + virtual void run() override = 0; + }; template -class UpDownSumObserver : public AsynchronousInstrument +class UpDownSumObserver : virtual public AsynchronousInstrument { - + public: - UpDownSumObserver() = default; - - UpDownSumObserver(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void (*callback)(ObserverResult)) - {} - - virtual void observe(int value, const trace::KeyValueIterable &labels) {} + UpDownSumObserver() = default; + + UpDownSumObserver(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void (*callback)(ObserverResult)) + {} + + virtual void observe(T value, const trace::KeyValueIterable &labels) override = 0; + + virtual void run() override = 0; + }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/instrument.h b/api/include/opentelemetry/metrics/instrument.h index 3e57bd735a..9728bab8bb 100644 --- a/api/include/opentelemetry/metrics/instrument.h +++ b/api/include/opentelemetry/metrics/instrument.h @@ -4,6 +4,7 @@ #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/trace/key_value_iterable_view.h" +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics @@ -12,158 +13,191 @@ namespace metrics // Enum classes to help determine instrument types in other parts of the API enum class InstrumentKind { - Counter = 0, - UpDownCounter = 1, - ValueRecorder = 2, - ValueObserver = 3, - SumObserver = 4, - UpDownSumObserver = 5, + Counter = 0, + UpDownCounter = 1, + ValueRecorder = 2, + ValueObserver = 3, + SumObserver = 4, + UpDownSumObserver = 5, }; class Instrument { - + public: - // Note that Instruments should be created using the Meter class. - // Please refer to meter.h for documentation. - Instrument() = default; - - /** - * Base class constructor for all other instrument types. Whether or not - * an instrument is synchronous or bound, it requires a name, description, - * unit, and enabled flag. - * - * @param name is the identifier of the instrumenting library - * @param description explains what the metric captures - * @param unit specifies the data type held in the instrument - * @param enabled determines if the metric is currently capturing data - * @return Instrument type with the specified attirbutes - */ - Instrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} - - // Returns true if the instrument is enabled and collecting data - virtual bool IsEnabled() = 0; - - // Return the instrument name - virtual nostd::string_view GetName() = 0; - - // Return the instrument description - virtual nostd::string_view GetDescription() = 0; - - // Return the insrument's units of measurement - virtual nostd::string_view GetUnits() = 0; - - virtual ~Instrument() = default; + // Note that Instruments should be created using the Meter class. + // Please refer to meter.h for documentation. + Instrument() = default; + + /** + * Base class constructor for all other instrument types. Whether or not + * an instrument is synchronous or bound, it requires a name, description, + * unit, and enabled flag. + * + * @param name is the identifier of the instrumenting library + * @param description explains what the metric captures + * @param unit specifies the data type held in the instrument + * @param enabled determines if the metric is currently capturing data + * @return Instrument type with the specified attirbutes + */ + Instrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + {} + + // Returns true if the instrument is enabled and collecting data + virtual bool IsEnabled() = 0; + + // Return the instrument name + virtual nostd::string_view GetName() = 0; + + // Return the instrument description + virtual nostd::string_view GetDescription() = 0; + + // Return the insrument's units of measurement + virtual nostd::string_view GetUnits() = 0; + + // Return the intrument's kind + virtual InstrumentKind GetKind() = 0; + + virtual ~Instrument() = default; }; template -class BoundSynchronousInstrument : public Instrument +class BoundSynchronousInstrument : virtual public Instrument { - + public: - BoundSynchronousInstrument() = default; - - BoundSynchronousInstrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /** - * Frees the resources associated with this Bound Instrument. - * The Metric from which this instrument was created is not impacted. - * - * @param none - * @return void - */ - virtual void unbind() final {} - - /** - * Records a single synchronous metric event; a call to the aggregator - * Since this is a bound synchronous instrument, labels are not required in * metric capture - * calls. - * - * @param value is the numerical representation of the metric being captured - * @return void - */ - virtual void update(T value) final {} + BoundSynchronousInstrument() = default; + + BoundSynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); + + /** + * Frees the resources associated with this Bound Instrument. + * The Metric from which this instrument was created is not impacted. + * + * @param none + * @return void + */ + virtual void unbind() {} + + /** + * Incremements the reference count of this bound object when a new instance is + * either created or the same instance is returned as a result of binding + * + * @param none + * @return void + */ + virtual void inc_ref () {} + + /** + * Return the object's current reference count. This information is used to remove + * stale objects from instrument registries. + */ + virtual int get_ref() { + return 0; + } + + /** + * Records a single synchronous metric event; a call to the aggregator + * Since this is a bound synchronous instrument, labels are not required in * metric capture + * calls. + * + * @param value is the numerical representation of the metric being captured + * @return void + */ + virtual void update(T value) {} }; template -class SynchronousInstrument : public Instrument +class SynchronousInstrument : virtual public Instrument { - + public: - SynchronousInstrument() = default; - - SynchronousInstrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled) - {} - - /** - * Returns a Bound Instrument associated with the specified labels. * Multiples requests - * with the same set of labels may return the same Bound Instrument instance. - * - * It is recommended that callers keep a reference to the Bound Instrument - * instead of repeatedly calling this operation. - * - * @param labels the set of labels, as key-value pairs - * @return a Bound Instrument - */ - nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); - - /** - * Records a single synchronous metric event. - * Since this is an unbound synchronous instrument, labels are required in * metric capture - * calls. - * - * update can be used in instruments with both add or record since it simply - * activated the aggregator - * - * @param labels the set of labels, as key-value pairs - * @param value is the numerical representation of the metric being captured - * @return void - */ - virtual void update(T value, const trace::KeyValueIterable &labels) final {} + SynchronousInstrument() = default; + + SynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) + { } + + /** + * Returns a Bound Instrument associated with the specified labels. * Multiples requests + * with the same set of labels may return the same Bound Instrument instance. + * + * It is recommended that callers keep a reference to the Bound Instrument + * instead of repeatedly calling this operation. + * + * @param labels the set of labels, as key-value pairs + * @return a Bound Instrument + */ + virtual nostd::shared_ptr> bind(const trace::KeyValueIterable &labels) { + return nostd::shared_ptr>(); + } + + /** + * Records a single synchronous metric event. + * Since this is an unbound synchronous instrument, labels are required in * metric capture + * calls. + * + * update can be used in instruments with both add or record since it simply + * activated the aggregator + * + * @param labels the set of labels, as key-value pairs + * @param value is the numerical representation of the metric being captured + * @return void + */ + virtual void update(T value, const trace::KeyValueIterable &labels) = 0; }; template class ObserverResult; template -class AsynchronousInstrument : public Instrument +class AsynchronousInstrument : virtual public Instrument { - + public: - AsynchronousInstrument() = default; - - AsynchronousInstrument(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled, - void(callback)(ObserverResult)) - {} - - /** - * Captures data by activating the callback function associated with the - * instrument and storing its return value. Callbacks for asynchronous - * instruments are defined during construction. - * - * @param value is the numerical representation of the metric being captured - * @return none - */ - virtual void update(T value, const trace::KeyValueIterable &labels) final {} - + AsynchronousInstrument() = default; + + AsynchronousInstrument(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled, + void(callback)(ObserverResult)) + {} + + /** + * Captures data through a manual call rather than the automatic collection process instituted + * in the run function. Asynchronous instruments are generally expected to obtain data from + * their callbacks rather than direct calls. This function is used by the callback to store data. + * + * @param value is the numerical representation of the metric being captured + * @param labels is the numerical representation of the metric being captured + * @return none + */ + virtual void observe (T value, const trace::KeyValueIterable &labels) = 0; + + /** + * Captures data by activating the callback function associated with the + * instrument and storing its return value. Callbacks for asynchronous + * instruments are defined during construction. + * + * @param none + * @return none + */ + virtual void run() = 0; + protected: - // Callback function which takes a pointer to an Asynchronous instrument (this) type which is - // stored in an observer result type and returns nothing. This function calls the instrument's - // observe. - void (*callback_)(ObserverResult); + // Callback function which takes a pointer to an Asynchronous instrument (this) type which is + // stored in an observer result type and returns nothing. This function calls the instrument's + // observe. + void (*callback_)(ObserverResult); }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index a0a0504928..3695277410 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -46,302 +46,357 @@ class NoopMeterProvider final : public opentelemetry::metrics::MeterProvider template class NoopValueObserver : public ValueObserver { - + public: - NoopValueObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } - + NoopValueObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual void observe(T value, const trace::KeyValueIterable &labels) override {} + + virtual void run() override {} + + virtual InstrumentKind GetKind() override { + return InstrumentKind::Counter; + } + }; template class NoopSumObserver : public SumObserver { - + public: - NoopSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) - {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } + NoopSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual void observe(T value, const trace::KeyValueIterable &labels) override {} + + virtual void run() override {} + + virtual InstrumentKind GetKind() override { + return InstrumentKind::Counter; + } }; template class NoopUpDownSumObserver : public UpDownSumObserver { - + public: - NoopUpDownSumObserver(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/, - void (*callback)(ObserverResult)) - {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } + NoopUpDownSumObserver(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/, + void (*callback)(ObserverResult)) + {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual void observe(T value, const trace::KeyValueIterable &labels) override {} + + virtual void run() override {} + + virtual InstrumentKind GetKind() override { + return InstrumentKind::Counter; + } }; template class BoundNoopCounter : public BoundCounter { - + public: - BoundNoopCounter() = default; - - BoundNoopCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - virtual void add(T value) override {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } - + BoundNoopCounter() = default; + + BoundNoopCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + + virtual void add(T value) override {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual InstrumentKind GetKind() override { + return InstrumentKind::Counter; + } + }; template class NoopCounter : public Counter { - + public: - NoopCounter() = default; - - NoopCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) - { - return nostd::shared_ptr>(new BoundNoopCounter()); - } - - virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } - + NoopCounter() = default; + + NoopCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + + nostd::shared_ptr> bindNoopCounter(const trace::KeyValueIterable & /*labels*/) + { + return nostd::shared_ptr>(new BoundNoopCounter()); + } + + virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual void update(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual InstrumentKind GetKind() override { + return InstrumentKind::Counter; + } + }; template class BoundNoopUpDownCounter : public BoundUpDownCounter { - + public: - BoundNoopUpDownCounter() = default; - - BoundNoopUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - virtual void add(T value) override {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } - + BoundNoopUpDownCounter() = default; + + BoundNoopUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + + virtual void add(T value) override {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual InstrumentKind GetKind() override { + return InstrumentKind::UpDownCounter; + } + }; template class NoopUpDownCounter : public UpDownCounter { - + public: - NoopUpDownCounter() = default; - - NoopUpDownCounter(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) - { - return nostd::shared_ptr>(new BoundNoopUpDownCounter()); - } - - virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } + NoopUpDownCounter() = default; + + NoopUpDownCounter(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + + nostd::shared_ptr> bindNoopUpDownCounter(const trace::KeyValueIterable & /*labels*/) + { + return nostd::shared_ptr>(new BoundNoopUpDownCounter()); + } + + virtual void add(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual void update(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual InstrumentKind GetKind() override { + return InstrumentKind::UpDownCounter; + } }; template class BoundNoopValueRecorder : public BoundValueRecorder { - + public: - BoundNoopValueRecorder() = default; - - BoundNoopValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - virtual void record(T value) override {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } + BoundNoopValueRecorder() = default; + + BoundNoopValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + + virtual void record(T value) override {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual InstrumentKind GetKind() override { + return InstrumentKind::ValueRecorder; + } }; template class NoopValueRecorder : public ValueRecorder { - + public: - NoopValueRecorder() = default; - - NoopValueRecorder(nostd::string_view /*name*/, - nostd::string_view /*description*/, - nostd::string_view /*unit*/, - bool /*enabled*/) - {} - - nostd::shared_ptr> bind(const trace::KeyValueIterable & /*labels*/) - { - return nostd::shared_ptr>(new BoundNoopValueRecorder()); - } - - virtual void record(T value, const trace::KeyValueIterable & /*labels*/) override {} - - virtual bool IsEnabled() override { - return false; - } - - virtual nostd::string_view GetName() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetDescription() override { - return nostd::string_view(""); - } - - virtual nostd::string_view GetUnits() override { - return nostd::string_view(""); - } + NoopValueRecorder() = default; + + NoopValueRecorder(nostd::string_view /*name*/, + nostd::string_view /*description*/, + nostd::string_view /*unit*/, + bool /*enabled*/) + {} + + nostd::shared_ptr> bindNoopValueRecorder(const trace::KeyValueIterable & /*labels*/) + { + return nostd::shared_ptr>(new BoundNoopValueRecorder()); + } + + virtual void record(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual void update(T value, const trace::KeyValueIterable & /*labels*/) override {} + + virtual bool IsEnabled() override { + return false; + } + + virtual nostd::string_view GetName() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetDescription() override { + return nostd::string_view(""); + } + + virtual nostd::string_view GetUnits() override { + return nostd::string_view(""); + } + + virtual InstrumentKind GetKind() override { + return InstrumentKind::ValueRecorder; + } + }; diff --git a/api/include/opentelemetry/metrics/observer_result.h b/api/include/opentelemetry/metrics/observer_result.h index bb61e97bf9..5b675092eb 100644 --- a/api/include/opentelemetry/metrics/observer_result.h +++ b/api/include/opentelemetry/metrics/observer_result.h @@ -21,9 +21,14 @@ class ObserverResult public: ObserverResult() = default; - ObserverResult(nostd::shared_ptr> instrument) {} - - virtual void observe(T value, const trace::KeyValueIterable &labels) {} + ObserverResult(AsynchronousInstrument * instrument): instrument_(instrument) {} + + virtual void observe(T value, const trace::KeyValueIterable &labels) { + instrument_->observe(value, labels); + } + +private: + AsynchronousInstrument * instrument_; }; diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 8a36005e9f..79f3a45097 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -7,158 +7,165 @@ namespace metrics { template -class BoundCounter : public BoundSynchronousInstrument +class BoundCounter : virtual public BoundSynchronousInstrument { - + public: - BoundCounter() = default; - - BoundCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Add adds the value to the counter's sum. The labels are already linked * to the instrument - * and are not specified. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(T value) = 0; + BoundCounter() = default; + + BoundCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); + + /* + * Add adds the value to the counter's sum. The labels are already linked * to the instrument + * and are not specified. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(T value) = 0; }; template -class Counter : public SynchronousInstrument +class Counter : virtual public SynchronousInstrument { - + public: - Counter() = default; - - Counter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Bind creates a bound instrument for this counter. The labels are - * associated with values recorded via subsequent calls to Record. - * - * @param labels the set of labels, as key-value pairs. - * @return a BoundIntCounter tied to the specified labels - */ - nostd::shared_ptr> bind(const nostd::string_view &labels); - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. Counters only * accept positive - * valued updates. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(T value, const trace::KeyValueIterable &labels) = 0; + Counter() = default; + + Counter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled) {} + + /* + * Bind creates a bound instrument for this counter. The labels are + * associated with values recorded via subsequent calls to Record. + * + * @param labels the set of labels, as key-value pairs. + * @return a BoundIntCounter tied to the specified labels + */ + virtual nostd::shared_ptr> bindCounter(const trace::KeyValueIterable &labels) { + return nostd::shared_ptr>(); + } + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. Counters only * accept positive + * valued updates. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(T value, const trace::KeyValueIterable &labels) = 0; + + virtual void update(T value, const trace::KeyValueIterable &labels) override = 0; }; template -class BoundUpDownCounter : public BoundSynchronousInstrument +class BoundUpDownCounter : virtual public BoundSynchronousInstrument { - + public: - BoundUpDownCounter() = default; - - BoundUpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Add adds the value to the counter's sum. The labels are already linked to * the instrument and - * do not need to specified again. UpDownCounters can accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(T value) = 0; + BoundUpDownCounter() = default; + + BoundUpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); + + /* + * Add adds the value to the counter's sum. The labels are already linked to * the instrument and + * do not need to specified again. UpDownCounters can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(T value) = 0; }; template -class UpDownCounter : public SynchronousInstrument +class UpDownCounter : virtual public SynchronousInstrument { - + public: - UpDownCounter() = default; - - UpDownCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); - //{ - // return nostd::shared_ptr>(new BoundUpDownCounter()); - //} - - /* - * Add adds the value to the counter's sum. The labels should contain - * the keys and values to be associated with this value. UpDownCounters can - * accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void add(T value, const trace::KeyValueIterable &labels) = 0; + UpDownCounter() = default; + + UpDownCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); + + virtual nostd::shared_ptr> bindUpDownCounter(const trace::KeyValueIterable &labels) + { + return nostd::shared_ptr>(); + } + + /* + * Add adds the value to the counter's sum. The labels should contain + * the keys and values to be associated with this value. UpDownCounters can + * accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void add(T value, const trace::KeyValueIterable &labels) = 0; + + virtual void update(T value, const trace::KeyValueIterable &labels) override = 0; }; template -class BoundValueRecorder : public BoundSynchronousInstrument +class BoundValueRecorder : virtual public BoundSynchronousInstrument { - + public: - BoundValueRecorder() = default; - - BoundValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - /* - * Records the value by summing it with previous measurements and checking * previously stored - * minimum and maximum values. The labels associated with * new values are already linked to the - * instrument as it is bound. * ValueRecorders can accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - */ - virtual void record(T value) = 0; + BoundValueRecorder() = default; + + BoundValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); + + /* + * Records the value by summing it with previous measurements and checking * previously stored + * minimum and maximum values. The labels associated with * new values are already linked to the + * instrument as it is bound. * ValueRecorders can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + */ + virtual void record(T value) = 0; }; template -class ValueRecorder : public SynchronousInstrument +class ValueRecorder : virtual public SynchronousInstrument { - + public: - ValueRecorder() = default; - - ValueRecorder(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - bool enabled); - - nostd::shared_ptr> bind(const trace::KeyValueIterable &labels); - //{ - // return nostd::shared_ptr>(new BoundValueRecorder()); - //} - - /* - * Records the value by summing it with previous measurements and checking * previously stored - * minimum and maximum values. The labels should contain the keys and values to be associated with - * this value. ValueRecorders can accept positive and negative values. - * - * @param value the numerical representation of the metric being captured - * @param labels the set of labels, as key-value pairs - */ - virtual void record(T value, const trace::KeyValueIterable &labels) = 0; + ValueRecorder() = default; + + ValueRecorder(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + bool enabled); + + virtual nostd::shared_ptr> bindValueRecorder(const trace::KeyValueIterable &labels){ + return nostd::shared_ptr>(); + } + + /* + * Records the value by summing it with previous measurements and checking * previously stored + * minimum and maximum values. The labels should contain the keys and values to be associated with + * this value. ValueRecorders can accept positive and negative values. + * + * @param value the numerical representation of the metric being captured + * @param labels the set of labels, as key-value pairs + */ + virtual void record(T value, const trace::KeyValueIterable &labels) = 0; + + virtual void update(T value, const trace::KeyValueIterable &labels) override = 0; }; } // namespace metrics diff --git a/api/test/metrics/noop_instrument_test.cc b/api/test/metrics/noop_instrument_test.cc index b68206059f..46f4babe42 100644 --- a/api/test/metrics/noop_instrument_test.cc +++ b/api/test/metrics/noop_instrument_test.cc @@ -75,8 +75,8 @@ TEST(Counter, DefaultConstruction) alpha.bind(labelkv); - auto gamma = alpha.bind(labelkv); - auto delta = beta.bind(labelkv); + auto gamma = alpha.bindNoopCounter(labelkv); + auto delta = beta.bindNoopCounter(labelkv); gamma->unbind(); delta->unbind(); @@ -93,8 +93,8 @@ TEST(Counter, Add) alpha.add(1, labelkv); beta.add(1.5, labelkv); - auto gamma = alpha.bind(labelkv); - auto delta = beta.bind(labelkv); + auto gamma = alpha.bindNoopCounter(labelkv); + auto delta = beta.bindNoopCounter(labelkv); gamma->add(1); delta->add(1.5); @@ -113,8 +113,8 @@ TEST(UpDownCounter, DefaultConstruction) alpha.bind(labelkv); - auto gamma = alpha.bind(labelkv); - auto delta = beta.bind(labelkv); + auto gamma = alpha.bindNoopUpDownCounter(labelkv); + auto delta = beta.bindNoopUpDownCounter(labelkv); gamma->unbind(); delta->unbind(); @@ -131,8 +131,8 @@ TEST(UpDownCounter, Add) alpha.add(1, labelkv); beta.add(1.5, labelkv); - auto gamma = alpha.bind(labelkv); - auto delta = beta.bind(labelkv); + auto gamma = alpha.bindNoopUpDownCounter(labelkv); + auto delta = beta.bindNoopUpDownCounter(labelkv); gamma->add(1); delta->add(1.0); @@ -153,8 +153,8 @@ TEST(ValueRecorder, DefaultConstruction) alpha.bind(labelkv); - auto gamma = alpha.bind(labelkv); - auto delta = beta.bind(labelkv); + auto gamma = alpha.bindNoopValueRecorder(labelkv); + auto delta = beta.bindNoopValueRecorder(labelkv); gamma->unbind(); delta->unbind(); @@ -171,8 +171,8 @@ TEST(ValueRecorder, Record) alpha.record(1, labelkv); beta.record(1.5, labelkv); - auto gamma = alpha.bind(labelkv); - auto delta = beta.bind(labelkv); + auto gamma = alpha.bindNoopValueRecorder(labelkv); + auto delta = beta.bindNoopValueRecorder(labelkv); gamma->record(1); delta->record(1.5);