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

#include "textwriter.h"
#include "countmetric.h"
#include "metricset.h"
#include "metricsnapshot.h"
#include "valuemetric.h"
#include <sstream>

using vespalib::to_string;

namespace metrics {

TextWriter::TextWriter(std::ostream& out, vespalib::duration period,
                       const std::string& regex, bool verbose)
    : _period(period), _out(out), _regex(), _verbose(verbose)
{
    try {
        _regex = std::regex(regex);
    } catch (std::regex_error &) {
    }
}

TextWriter::~TextWriter() = default;

bool
TextWriter::visitSnapshot(const MetricSnapshot& snapshot)
{
    _out << "snapshot \"" << snapshot.getName() << "\" from "
         << to_string(snapshot.getFromTime()) << " to " << to_string(snapshot.getToTime())
         << " period " << vespalib::count_s(snapshot.getPeriod());
    return true;
}

void
TextWriter::doneVisitingSnapshot(const MetricSnapshot&)
{
}

bool
TextWriter::visitMetricSet(const MetricSet& set, bool)
{
    _path.push_back(set.getMangledName());
    return true;
}
void
TextWriter::doneVisitingMetricSet(const MetricSet&) {
    _path.pop_back();
}

bool
TextWriter::writeCommon(const Metric& metric)
{
    std::ostringstream path;
    for (uint32_t i=0; i<_path.size(); ++i) {
        path << _path[i] << ".";
    }
    std::string mypath(path.str());
    path << metric.getMangledName();
    if (_regex && std::regex_search(path.str(), *_regex)) {
        if (metric.used() || _verbose) {
            _out << "\n" << mypath;
            return true;
        }
    }
    return false;
}

bool
TextWriter::visitCountMetric(const AbstractCountMetric& m, bool)
{
    if (writeCommon(m)) {
        if (_verbose || m.used()) {
            MetricValueClass::UP values(m.getValues());
            _out << m.getMangledName()
                 << (m.sumOnAdd() ? " count=" : " value=");
            values->output("count", _out);
        }
    }
    return true;
}

bool
TextWriter::visitValueMetric(const AbstractValueMetric& m, bool)
{
    if (writeCommon(m)) {
        m.print(_out, _verbose, "  ", vespalib::count_s(_period));
    }
    return true;
}

} // metrics