Skip to content

Commit

Permalink
Connect async storage with async instruments (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored May 16, 2022
1 parent 3fd5ca3 commit 45f0235
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 145 deletions.
56 changes: 25 additions & 31 deletions api/include/opentelemetry/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,15 @@ class Meter
* @param callback the function to be observed by the instrument.
* @return a shared pointer to the created Observable Counter.
*/
virtual nostd::shared_ptr<ObservableCounter<long>> CreateLongObservableCounter(
nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
virtual void CreateLongObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<ObservableCounter<double>> CreateDoubleObservableCounter(
nostd::string_view name,
void (*callback)(ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
virtual void CreateDoubleObservableCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &),
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 Down Expand Up @@ -96,17 +94,15 @@ class Meter
* @param callback the function to be observed by the instrument.
* @return a shared pointer to the created Observable Gauge.
*/
virtual nostd::shared_ptr<ObservableGauge<long>> CreateLongObservableGauge(
nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
virtual void CreateLongObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<ObservableGauge<double>> CreateDoubleObservableGauge(
nostd::string_view name,
void (*callback)(ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
virtual void CreateDoubleObservableGauge(nostd::string_view name,
void (*callback)(ObserverResult<double> &),
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 Down Expand Up @@ -137,17 +133,15 @@ class Meter
* @param callback the function to be observed by the instrument.
* @return a shared pointer to the created Observable UpDownCounter.
*/
virtual nostd::shared_ptr<ObservableUpDownCounter<long>> CreateLongObservableUpDownCounter(
nostd::string_view name,
void (*callback)(ObserverResult<long> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

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

virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name,
void (*callback)(ObserverResult<double> &),
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
Expand Down
84 changes: 30 additions & 54 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,17 @@ class NoopMeter final : public Meter
return nostd::shared_ptr<Counter<double>>{new NoopCounter<double>(name, description, unit)};
}

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

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

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

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

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

nostd::shared_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
nostd::string_view name,
Expand All @@ -211,25 +195,17 @@ class NoopMeter final : public Meter
new NoopUpDownCounter<double>(name, description, unit)};
}

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

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

/**
Expand Down
27 changes: 27 additions & 0 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
# include <chrono>
# include <map>
# include <thread>
# include <vector>
# include "opentelemetry/context/context.h"
# include "opentelemetry/metrics/provider.h"

namespace nostd = opentelemetry::nostd;
namespace metrics_api = opentelemetry::metrics;

namespace
{

std::map<std::string, std::string> get_random_attr()
{
static const std::vector<std::pair<std::string, std::string>> labels = {{"key1", "value1"},
Expand All @@ -23,6 +26,18 @@ std::map<std::string, std::string> get_random_attr()
return std::map<std::string, std::string>{labels[rand() % (labels.size() - 1)],
labels[rand() % (labels.size() - 1)]};
}

class MeasurementFetcher
{
public:
static void Fetcher(opentelemetry::metrics::ObserverResult<double> &observer_result)
{
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*/);
}
};
} // namespace

void foo_library::counter_example(const std::string &name)
Expand All @@ -40,6 +55,18 @@ void foo_library::counter_example(const std::string &name)
}
}

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);
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

void foo_library::histogram_example(const std::string &name)
{
std::string histogram_name = name + "_histogram";
Expand Down
1 change: 1 addition & 0 deletions examples/common/metrics_foo_library/foo_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class foo_library
public:
static void counter_example(const std::string &name);
static void histogram_example(const std::string &name);
static void observable_counter_example(const std::string &name);
};
#endif
19 changes: 19 additions & 0 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ void initMetrics(const std::string &name)
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};
p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// observable counter view
std::string observable_counter_name = name + "_observable_counter";
std::unique_ptr<metric_sdk::InstrumentSelector> observable_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kObservableCounter,
observable_counter_name)};
std::unique_ptr<metric_sdk::MeterSelector> observable_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> observable_sum_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};
p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector),
std::move(observable_sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
std::unique_ptr<metric_sdk::InstrumentSelector> histogram_instrument_selector{
Expand Down Expand Up @@ -83,15 +95,22 @@ int main(int argc, char **argv)
{
foo_library::counter_example(name);
}
else if (example_type == "observable_counter")
{
foo_library::observable_counter_example(name);
}
else if (example_type == "histogram")
{
foo_library::histogram_example(name);
}
else
{
std::thread counter_example{&foo_library::counter_example, name};
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
std::thread histogram_example{&foo_library::histogram_example, name};

counter_example.join();
observable_counter_example.join();
histogram_example.join();
}
}
Expand Down
53 changes: 35 additions & 18 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/meter_context.h"
# include "opentelemetry/sdk/metrics/state/async_metric_storage.h"

# include "opentelemetry/sdk/resource/resource.h"
# include "opentelemetry/version.h"

Expand Down Expand Up @@ -40,18 +42,17 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

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

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

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

nostd::shared_ptr<opentelemetry::metrics::Histogram<long>> CreateLongHistogram(
nostd::string_view name,
nostd::string_view description = "",
Expand All @@ -62,13 +63,12 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

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

nostd::shared_ptr<opentelemetry::metrics::ObservableGauge<double>> CreateDoubleObservableGauge(
void CreateDoubleObservableGauge(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
Expand All @@ -84,15 +84,13 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

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

nostd::shared_ptr<opentelemetry::metrics::ObservableUpDownCounter<double>>
CreateDoubleObservableUpDownCounter(
void CreateDoubleObservableUpDownCounter(
nostd::string_view name,
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
nostd::string_view description = "",
Expand All @@ -116,6 +114,25 @@ class Meter final : public opentelemetry::metrics::Meter

std::unique_ptr<WritableMetricStorage> RegisterMetricStorage(
InstrumentDescriptor &instrument_descriptor);

template <class T>
void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor,
void (*callback)(opentelemetry::metrics::ObserverResult<T> &))
{
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>>(
new AsyncMetricStorage<T>(view_instr_desc, view.GetAggregationType(), callback,
&view.GetAttributesProcessor()));
storage_registry_[instrument_descriptor.name_] = storage;
return true;
});
}
};
} // namespace metrics
} // namespace sdk
Expand Down
Loading

1 comment on commit 45f0235

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 45f0235 Previous: 3fd5ca3 Ratio
BM_CreateBaggageFromTenEntries 8680.020000679038 ns/iter 3776.797465577754 ns/iter 2.30

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.