Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple async callbacks #1495

Merged
merged 32 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions api/include/opentelemetry/metrics/async_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{
class AsynchronousInstrument
{};

template <class T>
class ObservableCounter : public AsynchronousInstrument
{};
// typedef void (*ObservableCallbackPtr)(ObserverResult &, void *);
esigo marked this conversation as resolved.
Show resolved Hide resolved
using ObservableCallbackPtr = void (*)(ObserverResult, void *);

template <class T>
class ObservableGauge : public AsynchronousInstrument
{};
class ObservableInstrument
{
public:
/**
* Sets up a function that will be called whenever a metric collection is initiated.
*/
virtual void AddCallback(ObservableCallbackPtr, void *state) noexcept = 0;

template <class T>
class ObservableUpDownCounter : public AsynchronousInstrument
{};
/**
* Remove a function that was configured to be called whenever a metric collection is initiated.
*/
virtual void RemoveCallback(ObservableCallbackPtr, void *state) noexcept = 0;
};

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
Expand Down
67 changes: 27 additions & 40 deletions api/include/opentelemetry/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,18 @@ class Meter
* shared_ptr to that Observable Counter
*
* @param name the name of the new Observable Counter.
* @param callback the function to be observed by the instrument.
* @param description a brief description of what the Observable Counter is used for.
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
* @param state to be passed back to callback
*/
virtual void CreateLongObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;

virtual void CreateDoubleObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;
virtual nostd::shared_ptr<ObservableInstrument> CreateLongObservableCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

/**
* Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram.
Expand All @@ -91,22 +87,18 @@ class Meter
* shared_ptr to that Observable Counter
*
* @param name the name of the new Observable Gauge.
* @param callback the function to be observed by the instrument.
* @param description a brief description of what the Observable Gauge is used for.
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
* @param state to be passed back to callback
*/
virtual void CreateLongObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;

virtual void CreateDoubleObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;
virtual nostd::shared_ptr<ObservableInstrument> CreateLongObservableGauge(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableGauge(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

/**
* Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that
Expand All @@ -132,23 +124,18 @@ class Meter
* a shared_ptr to that Observable UpDownCounter
*
* @param name the name of the new Observable UpDownCounter.
* @param callback the function to be observed by the instrument.
* @param description a brief description of what the Observable UpDownCounter is used for.
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
* @param state to be passed back to callback
*/
virtual void CreateLongObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;

virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &,
void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;
virtual nostd::shared_ptr<ObservableInstrument> CreateLongObservableUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
Expand Down
136 changes: 54 additions & 82 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,56 +64,16 @@ class NoopUpDownCounter : public UpDownCounter<T>
{}
};

template <class T>
class NoopObservableCounter : public ObservableCounter<T>
{
public:
NoopObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<T> &),
nostd::string_view description,
nostd::string_view unit) noexcept
{}

NoopObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<T> &, const common::KeyValueIterable &),
nostd::string_view description,
nostd::string_view unit) noexcept
{}
};

template <class T>
class NoopObservableGauge : public ObservableGauge<T>
class NoopObservableInstrument : public ObservableInstrument
{
public:
NoopObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<T> &),
nostd::string_view description,
nostd::string_view unit) noexcept
NoopObservableInstrument(nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
{}

NoopObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<T> &, const common::KeyValueIterable &),
nostd::string_view description,
nostd::string_view unit) noexcept
{}
};

template <class T>
class NoopObservableUpDownCounter : public ObservableUpDownCounter<T>
{
public:
NoopObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<T> &),
nostd::string_view description,
nostd::string_view unit) noexcept
{}

NoopObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<T> &,
const common::KeyValueIterable &),
nostd::string_view description,
nostd::string_view unit) noexcept
{}
void AddCallback(ObservableCallbackPtr, void *state) noexcept override {}
void RemoveCallback(ObservableCallbackPtr, void *state) noexcept override {}
};

/**
Expand All @@ -137,19 +97,23 @@ class NoopMeter final : public Meter
return nostd::shared_ptr<Counter<double>>{new NoopCounter<double>(name, description, unit)};
}

void CreateLongObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept override
{}
nostd::shared_ptr<ObservableInstrument> CreateLongObservableCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<ObservableInstrument>(
new NoopObservableInstrument(name, description, unit));
}

void CreateDoubleObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept override
{}
nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<ObservableInstrument>(
new NoopObservableInstrument(name, description, unit));
}

nostd::shared_ptr<Histogram<long>> CreateLongHistogram(
nostd::string_view name,
Expand All @@ -167,19 +131,23 @@ class NoopMeter final : public Meter
return nostd::shared_ptr<Histogram<double>>{new NoopHistogram<double>(name, description, unit)};
}

void CreateLongObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept override
{}
nostd::shared_ptr<ObservableInstrument> CreateLongObservableGauge(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<ObservableInstrument>(
new NoopObservableInstrument(name, description, unit));
}

void CreateDoubleObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept override
{}
nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableGauge(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<ObservableInstrument>(
new NoopObservableInstrument(name, description, unit));
}

nostd::shared_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
nostd::string_view name,
Expand All @@ -199,19 +167,23 @@ class NoopMeter final : public Meter
new NoopUpDownCounter<double>(name, description, unit)};
}

void CreateLongObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept override
{}
nostd::shared_ptr<ObservableInstrument> CreateLongObservableUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<ObservableInstrument>(
new NoopObservableInstrument(name, description, unit));
}

void CreateDoubleObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "",
void *state = nullptr) noexcept override
{}
nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<ObservableInstrument>(
new NoopObservableInstrument(name, description, unit));
}
};

/**
Expand Down
9 changes: 6 additions & 3 deletions api/include/opentelemetry/metrics/observer_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# include "opentelemetry/common/attribute_value.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/nostd/type_traits.h"
Expand All @@ -15,12 +16,11 @@ namespace metrics
{

/**
* ObserverResult class is necessary for the callback recording asynchronous
* ObserverResultT class is necessary for the callback recording asynchronous
* instrument use.
*/

template <class T>
class ObserverResult
class ObserverResultT
{

public:
Expand All @@ -44,6 +44,9 @@ class ObserverResult
}
};

using ObserverResult = nostd::variant<nostd::shared_ptr<ObserverResultT<long>>,
nostd::shared_ptr<ObserverResultT<double>>>;

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
#endif
14 changes: 10 additions & 4 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ std::map<std::string, std::string> get_random_attr()
class MeasurementFetcher
{
public:
static void Fetcher(opentelemetry::metrics::ObserverResult<double> &observer_result, void *state)
static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result, void *state)
{
double val = (rand() % 700) + 1.1;
std::map<std::string, std::string> labels = get_random_attr();
auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels};
observer_result.Observe(val /*, labelkv*/);
if (nostd::holds_alternative<
nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<double>>>(observer_result))
{
double val = (rand() % 700) + 1.1;
nostd::get<nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<double>>>(
observer_result)
->Observe(val /*, labelkv */);
}
}
};
} // namespace
Expand All @@ -60,7 +66,7 @@ void foo_library::observable_counter_example(const std::string &name)
std::string counter_name = name + "_observable_counter";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
meter->CreateDoubleObservableCounter(counter_name, MeasurementFetcher::Fetcher);
auto counter = meter->CreateDoubleObservableCounter(counter_name);
esigo marked this conversation as resolved.
Show resolved Hide resolved
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
Expand Down
Loading