aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/metrics_producer.cpp
blob: f25a0681397a71827db13df53d957c2283e54e05 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "metrics_producer.h"
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/fnet/task.h>
#include <vespa/fnet/transport.h>

namespace slobrok {

using namespace std::chrono;

namespace {

time_t
secondsSinceEpoch() {
    return duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
}

class MetricsSnapshotter : public FNET_Task
{
    MetricsProducer &_owner;

    void PerformTask() override {
        _owner.snapshot();
        Schedule(60.0);
    }
public:
    MetricsSnapshotter(FNET_Transport &transport, MetricsProducer &owner)
        :  FNET_Task(transport.GetScheduler()),
           _owner(owner)
    {
        Schedule(60.0);
    }

    ~MetricsSnapshotter() { Kill(); }
};

class MetricSnapshot
{
private:
    vespalib::Slime _data;
    vespalib::slime::Cursor& _metrics;
    vespalib::slime::Cursor& _snapshot;
    vespalib::slime::Cursor& _values;
    double _snapLen;

public:
    MetricSnapshot(uint32_t prevTime, uint32_t currTime);
    void addCount(const char *name, const char *desc, uint32_t count);

    vespalib::string asString() const {
        return _data.toString();
    }
};

MetricSnapshot::MetricSnapshot(uint32_t prevTime, uint32_t currTime)
    : _data(),
      _metrics(_data.setObject()),
      _snapshot(_metrics.setObject("snapshot")),
      _values(_metrics.setArray("values")),
      _snapLen(currTime - prevTime)
{
    _snapshot.setLong("from", prevTime);
    _snapshot.setLong("to",   currTime);
    if (_snapLen < 1.0) {
        _snapLen = 1.0;
    }
}


void
MetricSnapshot::addCount(const char *name, const char *desc, uint32_t count)
{
    using namespace vespalib::slime::convenience;
    Cursor& value = _values.addObject();
    value.setString("name", name);
    value.setString("description", desc);
    Cursor& inner = value.setObject("values");
    inner.setLong("count", count);
    inner.setDouble("rate", count / _snapLen);
}

vespalib::string
makeSnapshot(const RPCHooks::Metrics &prev, const RPCHooks::Metrics &curr,
             uint32_t prevTime, uint32_t currTime)
{
    MetricSnapshot snapshot(prevTime, currTime);
    snapshot.addCount("slobrok.heartbeats.failed",
             "count of failed heartbeat requests",
             curr.heartBeatFails - prev.heartBeatFails);
    snapshot.addCount("slobrok.requests.register",
             "count of register requests received",
             curr.registerReqs - prev.registerReqs);
    snapshot.addCount("slobrok.requests.mirror",
             "count of mirroring requests received",
             curr.mirrorReqs - prev.mirrorReqs);
    snapshot.addCount("slobrok.requests.admin",
             "count of administrative requests received",
             curr.adminReqs - prev.adminReqs);
    snapshot.addCount("slobrok.missing.consensus",
             "number of seconds without full consensus with all other brokers",
             curr.missingConsensusTime);
    return snapshot.asString();
}

} // namespace <unnamed>


MetricsProducer::MetricsProducer(const RPCHooks &hooks,
                                               FNET_Transport &transport)
    : _rpcHooks(hooks),
      _lastMetrics(RPCHooks::Metrics::zero()),
      _producer(),
      _startTime(secondsSinceEpoch()),
      _lastSnapshotStart(_startTime),
      _snapshotter(new MetricsSnapshotter(transport, *this))
{
}

MetricsProducer::~MetricsProducer() = default;

vespalib::string
MetricsProducer::getMetrics(const vespalib::string &consumer)
{
    return _producer.getMetrics(consumer);
}

vespalib::string
MetricsProducer::getTotalMetrics(const vespalib::string &)
{
    uint32_t now = secondsSinceEpoch();
    RPCHooks::Metrics current = _rpcHooks.getMetrics();
    RPCHooks::Metrics start = RPCHooks::Metrics::zero();
    return makeSnapshot(start, current, _startTime, now);
}


void
MetricsProducer::snapshot()
{
    uint32_t now = secondsSinceEpoch();
    RPCHooks::Metrics current = _rpcHooks.getMetrics();
    _producer.setMetrics(makeSnapshot(_lastMetrics, current, _lastSnapshotStart, now));
    _lastMetrics = current;
    _lastSnapshotStart = now;
}


} // namespace slobrok