-
Notifications
You must be signed in to change notification settings - Fork 446
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() {} | ||
|
There was a problem hiding this comment.
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 randomattributes
passed. Define 4-5 kv-attributes, select one of them randomly, and pass inRecord
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, added random attribute.