-
Notifications
You must be signed in to change notification settings - Fork 443
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
prometheus example #1332
prometheus example #1332
Changes from 2 commits
f259e01
ed172a1
d345c63
0b7c793
7807dc0
b82707c
1b27457
e2bd9d1
9dd108c
9e1a744
f819205
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
cc_binary( | ||
name = "prometheus_example", | ||
srcs = [ | ||
"main.cc", | ||
], | ||
linkopts = ["-pthread"], | ||
tags = ["ostream"], | ||
deps = [ | ||
"//api", | ||
"//examples/common/metrics_foo_library:common_metrics_foo_library", | ||
"//exporters/prometheus:prometheus_exporter", | ||
"//sdk/src/metrics", | ||
], | ||
) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include <memory> | ||
# include <thread> | ||
# include "opentelemetry/exporters/prometheus/exporter.h" | ||
# include "opentelemetry/metrics/provider.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" | ||
# include "opentelemetry/sdk/metrics/meter.h" | ||
# include "opentelemetry/sdk/metrics/meter_provider.h" | ||
|
||
# ifdef BAZEL_BUILD | ||
# include "examples/common/metrics_foo_library/foo_library.h" | ||
# else | ||
# include "metrics_foo_library/foo_library.h" | ||
# endif | ||
|
||
namespace metric_sdk = opentelemetry::sdk::metrics; | ||
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. nit - metrics_sdk ? 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, fixed. 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. nit - metrics_sdk ? 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, fixed. |
||
namespace nostd = opentelemetry::nostd; | ||
namespace common = opentelemetry::common; | ||
namespace exportermetrics = opentelemetry::exporter::metrics; | ||
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. nit - metrics_exporter ? 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, fixed. 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. nit - metrics_exporter ? 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, fixed. |
||
namespace metrics_api = opentelemetry::metrics; | ||
|
||
namespace | ||
{ | ||
|
||
void initMetrics(const std::string &name) | ||
{ | ||
std::string addr{"localhost:8080"}; | ||
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. Could this be configurable? 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, fixed. |
||
std::puts("PrometheusExporter example program running ..."); | ||
|
||
std::unique_ptr<metric_sdk::MetricExporter> exporter{ | ||
new exportermetrics::PrometheusExporter(addr)}; | ||
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. nit - need to update to use options. 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, fixed. 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. nit - need to update to use options. 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, fixed. |
||
std::vector<std::unique_ptr<metric_sdk::MetricExporter>> exporters; | ||
|
||
std::string version{"1.2.0"}; | ||
std::string schema{"https://opentelemetry.io/schemas/1.2.0"}; | ||
|
||
// Initialize and set the global MeterProvider | ||
metric_sdk::PeriodicExportingMetricReaderOptions options; | ||
options.export_interval_millis = std::chrono::milliseconds(1000); | ||
options.export_timeout_millis = std::chrono::milliseconds(500); | ||
std::unique_ptr<metric_sdk::MetricReader> reader{ | ||
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; | ||
auto provider = std::shared_ptr<metrics_api::MeterProvider>( | ||
new metric_sdk::MeterProvider(std::move(exporters))); | ||
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. Not related to this PR but need to modify MeterProvider / MeterContext API to not use exporters directly, as they are configured through Reader. Will create a github issue for that. 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. Not related to this PR but need to modify MeterProvider / MeterContext API to not use exporters directly, as they are configured through Reader. |
||
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> sum_view{ | ||
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}}; | ||
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 argc, char **argv) | ||
{ | ||
if (argc != 2) | ||
{ | ||
std::puts("invalid number of command line arguments"); | ||
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. Output a line of example usage? 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. |
||
return 0; | ||
} | ||
|
||
std::string example_type(argv[1]); | ||
std::string name{"prometheus_example"}; | ||
initMetrics(name); | ||
|
||
if (example_type == "counter") | ||
{ | ||
foo_library::counter_example(name); | ||
} | ||
else | ||
{ | ||
foo_library::histogram_example(name); | ||
} | ||
} | ||
#else | ||
int main() {} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
global: | ||
scrape_interval: 5s | ||
scrape_timeout: 2s | ||
evaluation_interval: 5s | ||
alerting: | ||
alertmanagers: | ||
- follow_redirects: true | ||
scheme: http | ||
timeout: 5s | ||
api_version: v2 | ||
static_configs: | ||
- targets: [localhost:8080] | ||
scrape_configs: | ||
- job_name: otel | ||
static_configs: | ||
- targets: ['localhost:8080'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker run -p 9090:9090 -v $(pwd):/etc/prometheus --network="host" prom/prometheus |
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.
Can we add cmake build too, or else github issue to track if plan to add it separately ?
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.
cmake added.