Skip to content
Closed
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
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(plugin)
add_subdirectory(simple)
add_subdirectory(batch)
add_subdirectory(metrics_simple)
add_subdirectory(prometheus_exporter)
25 changes: 25 additions & 0 deletions examples/prometheus_exporter/BUILD
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",
],
)
6 changes: 6 additions & 0 deletions examples/prometheus_exporter/CMakeLists.txt
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)
66 changes: 66 additions & 0 deletions examples/prometheus_exporter/README.md
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

![IMG](./images/count.png)

Histogram Sum:

![IMG](./images/sum.png)

Histogram Buckets:

![IMG](./images/buckets.png)

The process is similar for other OTel Aggregator.
Binary file added examples/prometheus_exporter/images/buckets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/prometheus_exporter/images/count.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/prometheus_exporter/images/sum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
173 changes: 173 additions & 0 deletions examples/prometheus_exporter/main.cc
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;

Comment thread
g-easy marked this conversation as resolved.
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]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please handle argc < 2 before doing this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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;
}