summaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/metrics_producer.cpp
blob: 09af3eca19429e930aead67634f4c368f7e1b247 (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
// Copyright 2016 Yahoo Inc. 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>

namespace slobrok {

namespace {

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& _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()),
      _values(_metrics.setArray("values")),
      _snapLen(currTime - prevTime)
{
    vespalib::slime::Cursor& snapshot = _metrics.setObject("snapshot");
    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.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);
    return snapshot.asString();
}

} // namespace <unnamed>


MetricsProducer::MetricsProducer(const RPCHooks &hooks,
                                               FNET_Transport &transport)
    : _rpcHooks(hooks),
      _lastMetrics{ 0, 0, 0, 0, 0, 0, 0},
      _producer(),
      _startTime(time(NULL)),
      _lastSnapshotStart(_startTime),
      _snapshotter(new MetricsSnapshotter(transport, *this))
{
}

MetricsProducer::~MetricsProducer() {
}

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

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


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


} // namespace slobrok