File: sample_server_multi.cc

package info (click to toggle)
prometheus-cpp 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: cpp: 3,596; sh: 37; makefile: 12
file content (54 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <chrono>
#include <memory>
#include <thread>

#include "prometheus/client_metric.h"
#include "prometheus/counter.h"
#include "prometheus/exposer.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"

int main() {
  using namespace prometheus;

  // create an http server running on port 8080
  Exposer exposer{"127.0.0.1:8080", 1};

  auto registryA = std::make_shared<Registry>();

  // add a new counter family to the registry (families combine values with the
  // same name, but distinct label dimensions)
  auto& counter_familyA = BuildCounter()
                              .Name("time_running_seconds_total")
                              .Help("How many seconds is this server running?")
                              .Register(*registryA);

  // add a counter to the metric family
  auto& seconds_counterA = counter_familyA.Add(
      {{"another_label", "bar"}, {"yet_another_label", "baz"}});

  // ask the exposer to scrape registryA on incoming scrapes for "/metricsA"
  exposer.RegisterCollectable(registryA, "/metricsA");

  auto registryB = std::make_shared<Registry>();

  auto& counter_familyB =
      BuildCounter()
          .Name("other_time_running_seconds_total")
          .Help("How many seconds has something else been running?")
          .Register(*registryB);

  auto& seconds_counterB = counter_familyB.Add(
      {{"another_label", "not_bar"}, {"yet_another_label", "not_baz"}});

  // This endpoint exposes registryB.
  exposer.RegisterCollectable(registryB, "/metricsB");

  for (;;) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    // increment the counters by one (second)
    seconds_counterA.Increment(1.0);
    seconds_counterB.Increment(1.5);
  }
  return 0;
}