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 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
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);
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
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);
}
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
{
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