aboutsummaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/metrics.cpp
blob: d94e1b94465858dc1eb69af804d9ebbcdcc9c5a8 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "metrics.h"
#include <vespa/vespalib/metrics/simple_metrics.h>

namespace config::sentinel {

using vespalib::metrics::SimpleMetricsManager;
using vespalib::metrics::SimpleManagerConfig;

StartMetrics::StartMetrics()
    : metrics(SimpleMetricsManager::create(SimpleManagerConfig())),
      producer(metrics),
      currentlyRunningServices(0),
      totalRestartsCounter(0),
      startedTime(vespalib::steady_clock::now()),
      sentinel_restarts(metrics->counter("sentinel.restarts",
              "how many times sentinel restarted a service")),
      sentinel_totalRestarts(metrics->gauge("sentinel.totalRestarts",
              "how many times sentinel restarted a service since sentinel start")),
      sentinel_running(metrics->gauge("sentinel.running",
              "how many services the sentinel has running currently")),
      sentinel_uptime(metrics->gauge("sentinel.uptime",
              "how many seconds has the sentinel been running"))
{
    // account for the sentinel itself restarting
    sentinel_restarts.add();
    lastRestartTime = vespalib::steady_clock::now();
}

StartMetrics::~StartMetrics() = default;

void
StartMetrics::maybeLog()
{
    using namespace std::chrono_literals;
    vespalib::steady_time curTime = vespalib::steady_clock::now();
    if (curTime - lastRestartTime > 2h) {
        totalRestartsCounter = 0;
        lastRestartTime = vespalib::steady_clock::now();
    }
    sentinel_totalRestarts.sample(totalRestartsCounter);
    sentinel_running.sample(currentlyRunningServices);
    sentinel_uptime.sample(vespalib::to_s(curTime - startedTime));
}

void
StartMetrics::incRestartsCounter()
{
    ++totalRestartsCounter;
    lastRestartTime = vespalib::steady_clock::now();
}

}