-
Notifications
You must be signed in to change notification settings - Fork 628
prometheus example #1332
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
Merged
Merged
prometheus example #1332
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f259e01
prometheus example
esigo ed172a1
Merge branch 'main' into prometheus-example
esigo d345c63
Merge branch 'main' into prometheus-example
ThomsonTan 0b7c793
Merge branch 'main' into prometheus-example
esigo 7807dc0
comments
esigo b82707c
usage comment
esigo 1b27457
fix CI
esigo e2bd9d1
fix noexcept CI
esigo 9dd108c
Merge branch 'main' into prometheus-example
ThomsonTan 9e1a744
Merge branch 'main' into prometheus-example
lalitb f819205
fix CI
esigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| include_directories(${CMAKE_SOURCE_DIR}/exporters/prometheus/include) | ||
| add_executable(prometheus_example main.cc) | ||
| target_link_libraries( | ||
| prometheus_example ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics | ||
| prometheus_exporter opentelemetry_resources common_metrics_foo_library) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // 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 metrics_sdk = opentelemetry::sdk::metrics; | ||
| namespace nostd = opentelemetry::nostd; | ||
| namespace common = opentelemetry::common; | ||
| namespace metrics_exporter = opentelemetry::exporter::metrics; | ||
| namespace metrics_api = opentelemetry::metrics; | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| void initMetrics(const std::string &name, const std::string &addr) | ||
| { | ||
| metrics_exporter::PrometheusExporterOptions opts; | ||
| if (!addr.empty()) | ||
| { | ||
| opts.url = addr; | ||
| } | ||
| std::puts("PrometheusExporter example program running ..."); | ||
|
|
||
| std::unique_ptr<metrics_sdk::MetricExporter> exporter{ | ||
| new metrics_exporter::PrometheusExporter(opts)}; | ||
|
|
||
| std::string version{"1.2.0"}; | ||
| std::string schema{"https://opentelemetry.io/schemas/1.2.0"}; | ||
|
|
||
| // Initialize and set the global MeterProvider | ||
| metrics_sdk::PeriodicExportingMetricReaderOptions options; | ||
| options.export_interval_millis = std::chrono::milliseconds(1000); | ||
| options.export_timeout_millis = std::chrono::milliseconds(500); | ||
| std::unique_ptr<metrics_sdk::MetricReader> reader{ | ||
| new metrics_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; | ||
| auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metrics_sdk::MeterProvider()); | ||
| auto p = std::static_pointer_cast<metrics_sdk::MeterProvider>(provider); | ||
| p->AddMetricReader(std::move(reader)); | ||
|
|
||
| // counter view | ||
| std::string counter_name = name + "_counter"; | ||
| std::unique_ptr<metrics_sdk::InstrumentSelector> instrument_selector{ | ||
| new metrics_sdk::InstrumentSelector(metrics_sdk::InstrumentType::kCounter, counter_name)}; | ||
| std::unique_ptr<metrics_sdk::MeterSelector> meter_selector{ | ||
| new metrics_sdk::MeterSelector(name, version, schema)}; | ||
| std::unique_ptr<metrics_sdk::View> sum_view{ | ||
| new metrics_sdk::View{name, "description", metrics_sdk::AggregationType::kSum}}; | ||
| 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<metrics_sdk::InstrumentSelector> histogram_instrument_selector{ | ||
| new metrics_sdk::InstrumentSelector(metrics_sdk::InstrumentType::kHistogram, histogram_name)}; | ||
| std::unique_ptr<metrics_sdk::MeterSelector> histogram_meter_selector{ | ||
| new metrics_sdk::MeterSelector(name, version, schema)}; | ||
| std::unique_ptr<metrics_sdk::View> histogram_view{ | ||
| new metrics_sdk::View{name, "description", metrics_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) | ||
| { | ||
| std::string example_type; | ||
| std::string addr{"localhost:8080"}; | ||
| if (argc == 1) | ||
| { | ||
| std::puts("usage: $prometheus_example <example type> <url>"); | ||
| } | ||
|
|
||
| if (argc >= 2) | ||
| { | ||
| example_type = argv[1]; | ||
| } | ||
| if (argc > 2) | ||
| { | ||
| addr = argv[2]; | ||
| } | ||
|
|
||
| std::string name{"prometheus_metric_example"}; | ||
| initMetrics(name, addr); | ||
|
|
||
| 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() {} | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.