Skip to content

Commit

Permalink
metrics histogram example (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Apr 18, 2022
1 parent 1d2cd42 commit b33dd09
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
53 changes: 44 additions & 9 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,59 @@
// SPDX-License-Identifier: Apache-2.0

#ifndef ENABLE_METRICS_PREVIEW
# include "foo_library.h"
# include <chrono>
# include <map>
# include <thread>
# include "opentelemetry/metrics/provider.h"

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

void foo_library(const std::string &name)
namespace
{
// Get the Meter from the MeterProvider
std::map<std::string, std::string> get_random_attr()
{
static const std::vector<std::pair<std::string, std::string>> labels = {{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"},
{"key4", "value4"},
{"key5", "value5"}};
return std::map<std::string, std::string>{labels[rand() % (labels.size() - 1)],
labels[rand() % (labels.size() - 1)]};
}
} // namespace

void foo_library::counter_example(const std::string &name)
{
std::string counter_name = name + "_counter";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
auto double_counter = meter->CreateDoubleCounter(name);
double_counter->Add(28.5);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
double_counter->Add(3.14);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
double_counter->Add(23.5);
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
auto double_counter = meter->CreateDoubleCounter(counter_name);

while (true)
{
double val = (rand() % 700) + 1.1;
double_counter->Add(val);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

void foo_library::histogram_example(const std::string &name)
{
std::string histogram_name = name + "_histogram";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name);
auto context = opentelemetry::context::Context{};
while (true)
{
double val = (rand() % 700) + 1.1;
std::map<std::string, std::string> labels = get_random_attr();
auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels};
histogram_counter->Record(val, labelkv, context);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}

#endif
7 changes: 6 additions & 1 deletion examples/common/metrics_foo_library/foo_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
#ifndef ENABLE_METRICS_PREVIEW
# include <string>

void foo_library(const std::string &name);
class foo_library
{
public:
static void counter_example(const std::string &name);
static void histogram_example(const std::string &name);
};
#endif
47 changes: 42 additions & 5 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#ifndef ENABLE_METRICS_PREVIEW
# include <memory>
# include <thread>
# include "opentelemetry/exporters/ostream/metric_exporter.h"
# include "opentelemetry/metrics/provider.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
Expand Down Expand Up @@ -44,21 +45,57 @@ void initMetrics(const std::string &name)
new metric_sdk::MeterProvider(std::move(exporters)));
auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);
p->AddMetricReader(std::move(reader));

// counter view
std::string counter_name = name + "_counter";
std::unique_ptr<metric_sdk::InstrumentSelector> instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, name)};
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, counter_name)};
std::unique_ptr<metric_sdk::MeterSelector> meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> view{
std::unique_ptr<metric_sdk::View> sum_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};
p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
std::unique_ptr<metric_sdk::InstrumentSelector> histogram_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)};
std::unique_ptr<metric_sdk::MeterSelector> histogram_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> histogram_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kHistogram}};
p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
metrics_api::Provider::SetMeterProvider(provider);
}
} // namespace
int main()

int main(int argc, char **argv)
{
std::string example_type;
if (argc >= 2)
{
example_type = argv[1];
}

std::string name{"ostream_metric_example"};
initMetrics(name);
foo_library(name);

if (example_type == "counter")
{
foo_library::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 histogram_example{&foo_library::histogram_example, name};
counter_example.join();
histogram_example.join();
}
}
#else
int main() {}
Expand Down
2 changes: 2 additions & 0 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ sdk::common::ExportResult OStreamMetricExporter::Export(
void OStreamMetricExporter::printInstrumentationInfoMetricData(
const sdk::metrics::InstrumentationInfoMetrics &info_metric)
{
// sout_ is shared
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
sout_ << "{";
sout_ << "\n name\t\t: " << info_metric.instrumentation_library_->GetName()
<< "\n schema url\t: " << info_metric.instrumentation_library_->GetSchemaURL()
Expand Down

2 comments on commit b33dd09

@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 sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: b33dd09 Previous: 1d2cd42 Ratio
BM_BaselineBuffer/1 8240270.614624023 ns/iter 539900.7797241211 ns/iter 15.26
BM_BaselineBuffer/2 5255088.806152344 ns/iter 2074398.0407714844 ns/iter 2.53
BM_LockFreeBuffer/2 2718087.980302714 ns/iter 1174196.481704712 ns/iter 2.31

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

@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: b33dd09 Previous: 1d2cd42 Ratio
BM_ProcYieldSpinLockThrashing/1/process_time/real_time 0.51020224567367 ms/iter 0.10924929273063164 ms/iter 4.67
BM_ProcYieldSpinLockThrashing/2/process_time/real_time 0.9460967520008916 ms/iter 0.21586696489981608 ms/iter 4.38

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

Please sign in to comment.