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 histogram example #1330

Merged
merged 5 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 27 additions & 8 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,43 @@
// 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)
void foo_library::counter_example(const std::string &name)
{
// Get the Meter from the MeterProvider
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));

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)
{
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
auto histogram_counter = meter->CreateDoubleHistogram(name);
auto context = opentelemetry::context::Context{};
std::map<std::string, std::string> labels = {{"key", "value"}};
auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels};
while (true)
{
double val = (rand() % 700) + 1.1;
histogram_counter->Record(val, labelkv, context);
Copy link
Member

@lalitb lalitb Apr 18, 2022

Choose a reason for hiding this comment

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

May be as an enhancement later, just like val, we can also have random attributes passed. Define 4-5 kv-attributes, select one of them randomly, and pass in Record method.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, added random attribute.

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
36 changes: 32 additions & 4 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,49 @@ 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::unique_ptr<metric_sdk::InstrumentSelector> instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, 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::unique_ptr<metric_sdk::InstrumentSelector> histogram_instrument_selector{
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, 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)
{
if (argc != 2)
{
std::puts("invalid number of command line arguments");
return 0;
}

std::string 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
{
foo_library::histogram_example(name);
}
Copy link
Member

Choose a reason for hiding this comment

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

Would also be a good use-case to create both counter and histogram if no argument is specified - to demonstrate getting metrics from multiple instruments simultaneously.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, added multiple instruments simultaneously.

}
#else
int main() {}
Expand Down