-
Notifications
You must be signed in to change notification settings - Fork 559
Add C++ Prometheus Exporter Example Program #310
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
41b6744
added prometheus example
6e80a1e
Merge https://github.com/open-telemetry/opentelemetry-cpp into example
dbda248
changed endl to \n, added anonymous namespace, removed unneeded else …
118d551
made aggregator choice a command line argument, and modified README t…
c89710e
minor README changes
d1e6895
argc check
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2020, OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| cc_binary( | ||
| name = "prometheus_exporter_example", | ||
| srcs = [ | ||
| "main.cc", | ||
| ], | ||
| deps = [ | ||
| "//exporters/prometheus:prometheus_exporter", | ||
| "//exporters/prometheus:prometheus_collector", | ||
| "//exporters/prometheus:prometheus_utils", | ||
| ], | ||
| ) |
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,6 @@ | ||
| include_directories(${CMAKE_SOURCE_DIR}/exporters/prometheus/include) | ||
| find_package(prometheus-cpp CONFIG REQUIRED) | ||
|
|
||
| add_executable(prom_exp_example main.cc) | ||
| target_link_libraries(prom_exp_example ${CMAKE_THREAD_LIBS_INIT} | ||
| prometheus_exporter prometheus-cpp::pull) |
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,66 @@ | ||
| # Prometheus Exporter Example | ||
|
|
||
| In this example, the application in `main.cc` initializes the Prometheus exporter and exports 6 different OTel Aggregator to Prometheus. | ||
| Here are more detailed explanations of it. | ||
|
|
||
| ## Start Prometheus Instance | ||
| Start a Prometheus instance with Docker: | ||
| ```shell script | ||
| docker run prom/prometheus | ||
| ``` | ||
| Add Prometheus Exporter job configuration to `~/docker/prometheus/prometheus.yml` | ||
| ```yaml | ||
| scrape_configs: | ||
| # ... | ||
| # other jobs | ||
| # ... | ||
| - job_name: 'test_prom_exporter' | ||
| static_configs: | ||
| - targets: ['docker.for.mac.localhost:8080'] | ||
| ``` | ||
| Restart Prometheus instance and specify configuration file | ||
| ```shell script | ||
| docker run -d --expose 8080 \ | ||
| -p 9090:9090 \ | ||
| -v ~/docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \ | ||
| prom/prometheus | ||
| ``` | ||
|
|
||
| ## Run Example Program | ||
|
|
||
| To run the program, you must provide a command line argument to specify which Aggregator Kind you want to run the demo. The six choices are: | ||
| ``` | ||
| counter, gauge, mmsc, histogram, exact, sketch | ||
| ``` | ||
|
|
||
| Run with Bazel: | ||
| ```shell | ||
| bazel build //examples/prometheus_exporter:prometheus_exporter_example | ||
| bazel-bin/examples/prometheus_exporter/prometheus_exporter_example histogram | ||
| ``` | ||
| The `histogram` command line argument is the argument `agg` to specify. | ||
|
|
||
| For example, if we choose Histogram, this is what it would look like to build and execute the example: | ||
| ``` | ||
| $ bazel build //examples/prometheus_exporter:prometheus_exporter_example | ||
| $ bazel-bin/examples/prometheus_exporter/prometheus_exporter_example histogram | ||
| Prometheus Exporter example program running on localhost:8080... | ||
| Running histogram example program... | ||
| ``` | ||
| The example program generates 4 metrics every second, and exports every 15 seconds. | ||
|
|
||
| Wait for a while, and visit `localhost:9090`. Choose the name of metrics to view the graphs. | ||
|
|
||
| Histogram Count: Each batch of metric data contains 60 metrics | ||
|
|
||
|  | ||
|
|
||
| Histogram Sum: | ||
|
|
||
|  | ||
|
|
||
| Histogram Buckets: | ||
|
|
||
|  | ||
|
|
||
| The process is similar for other OTel Aggregator. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,173 @@ | ||
| #include "opentelemetry/exporters/prometheus/prometheus_collector.h" | ||
| #include "opentelemetry/exporters/prometheus/prometheus_exporter.h" | ||
| #include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" | ||
| #include "opentelemetry/sdk/metrics/aggregator/exact_aggregator.h" | ||
| #include "opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h" | ||
| #include "opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h" | ||
| #include "opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h" | ||
| #include "opentelemetry/sdk/metrics/aggregator/sketch_aggregator.h" | ||
|
|
||
| #include <chrono> | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
| namespace prom_exp = opentelemetry::exporter::prometheus; | ||
| namespace metric_sdk = opentelemetry::sdk::metrics; | ||
| namespace metric_api = opentelemetry::metrics; | ||
|
|
||
| namespace | ||
| { | ||
| template <typename T> | ||
| metric_sdk::Record get_record(const std::string &type, | ||
| int version, | ||
| const std::string &label, | ||
| std::shared_ptr<metric_sdk::Aggregator<T>> aggregator) | ||
| { | ||
| std::string name = "test-" + type + "-metric-record-v_" + std::to_string(version) + ".0"; | ||
| std::string desc = "this is a test " + type + " metric record"; | ||
| metric_sdk::Record record(name, desc, label, aggregator); | ||
| return record; | ||
| } | ||
|
|
||
| template <typename T> | ||
| void do_example(const std::shared_ptr<metric_sdk::Aggregator<T>> &aggregator, | ||
| std::vector<metric_sdk::Record> collection, | ||
| const metric_sdk::Record &record, | ||
| prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| int counter = 0; | ||
| while (true) | ||
| { | ||
| int val = (rand() % 100) + 1; | ||
| aggregator->update(val); | ||
|
|
||
| counter++; | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(250)); | ||
|
|
||
| if (counter % 60 == 0) | ||
| { | ||
| // add record to collection and export the collection | ||
| aggregator->checkpoint(); | ||
| collection.emplace_back(record); | ||
| exporter.Export(collection); | ||
| counter = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void counter_example(prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| auto aggregator = std::shared_ptr<metric_sdk::Aggregator<int>>( | ||
| new metric_sdk::CounterAggregator<int>(metric_api::InstrumentKind::Counter)); | ||
|
|
||
| // collection of records to export | ||
| std::vector<metric_sdk::Record> collection; | ||
| auto record = get_record("counter", 1, "{label-1:v1,label2:v2,}", aggregator); | ||
| do_example(aggregator, collection, record, exporter); | ||
| } | ||
|
|
||
| void gauge_example(prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| auto aggregator = std::shared_ptr<metric_sdk::Aggregator<int>>( | ||
| new metric_sdk::GaugeAggregator<int>(metric_api::InstrumentKind::Counter)); | ||
|
|
||
| // collection of records to export | ||
| std::vector<metric_sdk::Record> collection; | ||
| auto record = get_record("gauge", 1, "{label-1:v1,label2:v2,}", aggregator); | ||
| do_example(aggregator, collection, record, exporter); | ||
| } | ||
|
|
||
| void mmsc_example(prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| auto aggregator = std::shared_ptr<metric_sdk::Aggregator<double>>( | ||
| new metric_sdk::MinMaxSumCountAggregator<double>(metric_api::InstrumentKind::Counter)); | ||
|
|
||
| // collection of records to export | ||
| std::vector<metric_sdk::Record> collection; | ||
| auto record = get_record("mmsc", 1, "{label-1:v1,label2:v2,}", aggregator); | ||
| do_example(aggregator, collection, record, exporter); | ||
| } | ||
|
|
||
| void histogram_example(prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| std::vector<double> boundaries{10, 20, 30, 40, 50}; | ||
| auto aggregator = std::shared_ptr<metric_sdk::Aggregator<int>>( | ||
| new metric_sdk::HistogramAggregator<int>(metric_api::InstrumentKind::Counter, boundaries)); | ||
|
|
||
| // collection of records to export | ||
| std::vector<metric_sdk::Record> collection; | ||
| auto record = get_record("histogram", 1, "{label-1:v1,label2:v2,}", aggregator); | ||
| do_example(aggregator, collection, record, exporter); | ||
| } | ||
|
|
||
| void exact_example(prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| auto aggregator = std::shared_ptr<metric_sdk::Aggregator<int>>( | ||
| new metric_sdk::ExactAggregator<int>(metric_api::InstrumentKind::Counter, true)); | ||
|
|
||
| // collection of records to export | ||
| std::vector<metric_sdk::Record> collection; | ||
| auto record = get_record("exact", 1, "{label-1:v1,label2:v2,}", aggregator); | ||
| do_example(aggregator, collection, record, exporter); | ||
| } | ||
|
|
||
| void sketch_example(prom_exp::PrometheusExporter &exporter) | ||
| { | ||
| auto aggregator = std::shared_ptr<metric_sdk::Aggregator<int>>( | ||
| new metric_sdk::SketchAggregator<int>(metric_api::InstrumentKind::Counter, true)); | ||
|
|
||
| // collection of records to export | ||
| std::vector<metric_sdk::Record> collection; | ||
| auto record = get_record("sketch", 1, "{label-1:v1,label2:v2,}", aggregator); | ||
| do_example(aggregator, collection, record, exporter); | ||
| } | ||
| } | ||
| int main(int argc, char** argv) | ||
| { | ||
| if(argc != 2) { | ||
| std::cout << "invalid number of command line arguments\n"; | ||
| return 0; | ||
| } | ||
|
|
||
| std::string agg(argv[1]); | ||
|
Contributor
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. Please handle argc < 2 before doing this.
Contributor
Author
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. Missed this earlier, but fixed now. |
||
|
|
||
| std::string address = "localhost:8080"; | ||
| prom_exp::PrometheusExporter exporter{address}; | ||
|
|
||
| std::cout << "Prometheus Exporter example program running on " << address << "...\n"; | ||
| std::cout << "Running " << agg << " example program...\n"; | ||
|
|
||
| if (agg == "counter") | ||
| { | ||
| counter_example(exporter); | ||
| } | ||
| else if (agg == "gauge") | ||
| { | ||
| gauge_example(exporter); | ||
| } | ||
| else if (agg == "mmsc") | ||
| { | ||
| mmsc_example(exporter); | ||
| } | ||
| else if (agg == "histogram") | ||
| { | ||
| histogram_example(exporter); | ||
| } | ||
| else if (agg == "exact") | ||
| { | ||
| exact_example(exporter); | ||
| } | ||
| else if (agg == "sketch") | ||
| { | ||
| sketch_example(exporter); | ||
| } | ||
| else | ||
| { | ||
| std::cout << "invalid aggregator kind given\n"; | ||
| } | ||
|
|
||
|
|
||
| return 0; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.