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

[Metrics API/SDK] - Pass state to async callback function. #1408

Merged
merged 8 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 26 additions & 19 deletions api/include/opentelemetry/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,26 @@ class Meter
nostd::string_view unit = "") noexcept = 0;

/**
* Creates a Asynchronouse (Observable) counter with the passed characteristics and returns a
* Creates a Asynchronous (Observable) counter with the passed characteristics and returns a
* shared_ptr to that Observable Counter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:
Creates a Asynchronouse --> Creates an Asynchronous

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*
* @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 callback the function to be observed by the instrument.
* @return a shared pointer to the created Observable Counter.
* @param state to be passed back to callback
*/
virtual void CreateLongObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &),
void (*callback)(ObserverResult<long> &, void *),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change for the alpha users?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes , it would be breaking change.

nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;

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

/**
* Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram.
Expand All @@ -89,20 +91,22 @@ 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 callback the function to be observed by the instrument.
* @return a shared pointer to the created Observable Gauge.
* @param state to be passed back to callback
*/
virtual void CreateLongObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<long> &),
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;

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

/**
* Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that
Expand All @@ -128,20 +132,23 @@ 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 callback the function to be observed by the instrument.
* @return a shared pointer to the created Observable UpDownCounter.
* @param state to be passed back to callback
*/
virtual void CreateLongObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &),
void (*callback)(ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;

virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &),
void (*callback)(ObserverResult<double> &,
void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
nostd::string_view unit = "",
void *state = nullptr) noexcept = 0;
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
Expand Down
30 changes: 18 additions & 12 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,17 @@ class NoopMeter final : public Meter
}

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

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

nostd::shared_ptr<Histogram<long>> CreateLongHistogram(
Expand All @@ -166,15 +168,17 @@ class NoopMeter final : public Meter
}

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

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

nostd::shared_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
Expand All @@ -196,15 +200,17 @@ class NoopMeter final : public Meter
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::map<std::string, std::string> get_random_attr()
class MeasurementFetcher
{
public:
static void Fetcher(opentelemetry::metrics::ObserverResult<double> &observer_result)
static void Fetcher(opentelemetry::metrics::ObserverResult<double> &observer_result, void *state)
{
double val = (rand() % 700) + 1.1;
std::map<std::string, std::string> labels = get_random_attr();
Expand Down
54 changes: 35 additions & 19 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view unit = "") noexcept override;

void CreateLongObservableCounter(nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
void (*callback)(opentelemetry::metrics::ObserverResult<long> &,
void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
nostd::string_view unit = "",
void *state = nullptr) noexcept override;

void CreateDoubleObservableCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
void (*callback)(opentelemetry::metrics::ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
nostd::string_view unit = "",
void *state = nullptr) noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Histogram<long>> CreateLongHistogram(
nostd::string_view name,
Expand All @@ -64,15 +67,18 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view unit = "") noexcept override;

void CreateLongObservableGauge(nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
void (*callback)(opentelemetry::metrics::ObserverResult<long> &,
void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
nostd::string_view unit = "",
void *state = nullptr) noexcept override;

void CreateDoubleObservableGauge(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
void (*callback)(opentelemetry::metrics::ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
nostd::string_view unit = "",
void *state = nullptr) noexcept override;

nostd::shared_ptr<opentelemetry::metrics::UpDownCounter<long>> CreateLongUpDownCounter(
nostd::string_view name,
Expand All @@ -86,15 +92,17 @@ class Meter final : public opentelemetry::metrics::Meter

void CreateLongObservableUpDownCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
void (*callback)(opentelemetry::metrics::ObserverResult<long> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
nostd::string_view unit = "",
void *state = nullptr) noexcept override;

void CreateDoubleObservableUpDownCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
void (*callback)(opentelemetry::metrics::ObserverResult<double> &, void *),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
nostd::string_view unit = "",
void *state = nullptr) noexcept override;

/** Returns the associated instruementation library */
const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary()
Expand All @@ -117,18 +125,26 @@ class Meter final : public opentelemetry::metrics::Meter

template <class T>
void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor,
void (*callback)(opentelemetry::metrics::ObserverResult<T> &))
void (*callback)(opentelemetry::metrics::ObserverResult<T> &,
void *),
void *state = nullptr)
{
auto view_registry = meter_context_->GetViewRegistry();
auto success = view_registry->FindViews(
instrument_descriptor, *instrumentation_library_,
[this, &instrument_descriptor, callback](const View &view) {
auto view_instr_desc = instrument_descriptor;
view_instr_desc.name_ = view.GetName();
view_instr_desc.description_ = view.GetDescription();
auto storage = std::shared_ptr<AsyncMetricStorage<T>>(
[this, &instrument_descriptor, callback, state](const View &view) {
auto view_instr_desc = instrument_descriptor;
if (!view.GetName().empty())
{
view_instr_desc.name_ = view.GetName();
}
if (!view.GetDescription().empty())
{
view_instr_desc.description_ = view.GetDescription();
}
auto storage = std::shared_ptr<AsyncMetricStorage<T>>(
new AsyncMetricStorage<T>(view_instr_desc, view.GetAggregationType(), callback,
&view.GetAttributesProcessor()));
&view.GetAttributesProcessor(), state));
storage_registry_[instrument_descriptor.name_] = storage;
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ class AsyncMetricStorage : public MetricStorage
public:
AsyncMetricStorage(InstrumentDescriptor instrument_descriptor,
const AggregationType aggregation_type,
void (*measurement_callback)(opentelemetry::metrics::ObserverResult<T> &),
const AttributesProcessor *attributes_processor)
void (*measurement_callback)(opentelemetry::metrics::ObserverResult<T> &,
void *),
const AttributesProcessor *attributes_processor,
void *state = nullptr)
: instrument_descriptor_(instrument_descriptor),
aggregation_type_{aggregation_type},
measurement_collection_callback_{measurement_callback},
attributes_processor_{attributes_processor},
state_{state},
cumulative_hash_map_(new AttributesHashMap()),
temporal_metric_storage_(instrument_descriptor)
{}
Expand All @@ -46,7 +49,7 @@ class AsyncMetricStorage : public MetricStorage
opentelemetry::sdk::metrics::ObserverResult<T> ob_res(attributes_processor_);

// read the measurement using configured callback
measurement_collection_callback_(ob_res);
measurement_collection_callback_(ob_res, state_);
std::shared_ptr<AttributesHashMap> delta_hash_map(new AttributesHashMap());
// process the read measurements - aggregate and store in hashmap
for (auto &measurement : ob_res.GetMeasurements())
Expand Down Expand Up @@ -79,8 +82,9 @@ class AsyncMetricStorage : public MetricStorage
private:
InstrumentDescriptor instrument_descriptor_;
AggregationType aggregation_type_;
void (*measurement_collection_callback_)(opentelemetry::metrics::ObserverResult<T> &);
void (*measurement_collection_callback_)(opentelemetry::metrics::ObserverResult<T> &, void *);
const AttributesProcessor *attributes_processor_;
void *state_;
std::unique_ptr<AttributesHashMap> cumulative_hash_map_;
TemporalMetricStorage temporal_metric_storage_;
};
Expand Down
Loading