aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/metrics/json_formatter.cpp
blob: dddc57814585d12248c7faacd3c5b3bf87a7e50e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "json_formatter.h"

namespace vespalib {
namespace metrics {

JsonFormatter::JsonFormatter(const Snapshot &snapshot)
    : _data(),
      _top(_data.setObject()),
      _snapLen(snapshot.endTime() - snapshot.startTime())
{
    if (_snapLen < 0.1) {
        _snapLen = 0.1;
    }
    // cosmetics: ordering inside objects
    _data.insert("name");
    _data.insert("dimensions");
    vespalib::slime::Cursor& meta = _top.setObject("snapshot");
    meta.setLong("from", (long)snapshot.startTime());
    meta.setLong("to",   (long)snapshot.endTime());
    handle(snapshot, _top.setArray("values"));
}

void
JsonFormatter::handle(const Snapshot &snapshot, vespalib::slime::Cursor &target)
{
    for (const CounterSnapshot &entry : snapshot.counters()) {
        handle(entry, target.addObject());
    }
    for (const GaugeSnapshot &entry : snapshot.gauges()) {
        handle(entry, target.addObject());
    }
}

void
JsonFormatter::handle(const CounterSnapshot &snapshot, vespalib::slime::Cursor &target)
{
    target.setString("name", snapshot.name());
    // target.setString("description", ?);
    handle(snapshot.point(), target);
    Cursor& inner = target.setObject("values");
    inner.setLong("count", snapshot.count());
    inner.setDouble("rate", snapshot.count() / _snapLen);
}

void
JsonFormatter::handle(const GaugeSnapshot &snapshot, vespalib::slime::Cursor &target)
{
    target.setString("name", snapshot.name());
    // target.setString("description", ?);
    handle(snapshot.point(), target);
    Cursor& inner = target.setObject("values");
    inner.setDouble("average", snapshot.averageValue());
    inner.setDouble("sum", snapshot.sumValue());
    inner.setDouble("min", snapshot.minValue());
    inner.setDouble("max", snapshot.maxValue());
    inner.setDouble("last", snapshot.lastValue());
    inner.setLong("count", snapshot.observedCount());
    inner.setDouble("rate", snapshot.observedCount() / _snapLen);
}

void
JsonFormatter::handle(const PointSnapshot &snapshot, vespalib::slime::Cursor &target)
{
    if (snapshot.dimensions.size() == 0) {
        return;
    }
    Cursor& inner = target.setObject("dimensions");
    for (const DimensionBinding &entry : snapshot.dimensions) {
        inner.setString(entry.dimensionName(), entry.labelValue());
    }
}

} // namespace vespalib::metrics
} // namespace vespalib