summaryrefslogtreecommitdiffstats
path: root/metrics/src/vespa/metrics/xmlwriter.cpp
blob: 197ffdd1e3d28d87f6fc80e548420656aee34c3a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "xmlwriter.h"
#include "countmetric.h"
#include "metricset.h"
#include "metricsnapshot.h"
#include "valuemetric.h"
#include <vespa/vespalib/util/xmlstream.h>
#include <sstream>

namespace metrics {

XmlWriter::XmlWriter(vespalib::xml::XmlOutputStream& xos,
          uint32_t period, int verbosity)
    : _period(period), _xos(xos), _verbosity(verbosity) {}

bool
XmlWriter::visitSnapshot(const MetricSnapshot& snapshot)
{
    using namespace vespalib::xml;
    _xos << XmlTag("snapshot") << XmlAttribute("name", snapshot.getName())
         << XmlAttribute("from", snapshot.getFromTime())
         << XmlAttribute("to", snapshot.getToTime())
         << XmlAttribute("period", snapshot.getPeriod());
    return true;
}

void
XmlWriter::doneVisitingSnapshot(const MetricSnapshot&)
{
    using namespace vespalib::xml;
    _xos << XmlEndTag();
}

bool
XmlWriter::visitMetricSet(const MetricSet& set, bool)
{
    using namespace vespalib::xml;
    if (set.used() || _verbosity >= 2) {
        _xos << XmlTag(set.getName(), CONVERT_ILLEGAL_CHARACTERS);
        printCommonXmlParts(set);
        return true;
    }
    return false;
}
void
XmlWriter::doneVisitingMetricSet(const MetricSet&) {
    using namespace vespalib::xml;
    _xos << XmlEndTag();
}

bool
XmlWriter::visitCountMetric(const AbstractCountMetric& metric, bool)
{
    MetricValueClass::UP values(metric.getValues());
    if (!metric.inUse(*values) && _verbosity < 2) return true;
    using namespace vespalib::xml;
    std::ostringstream ost;
    _xos << XmlTag(metric.getName(), CONVERT_ILLEGAL_CHARACTERS)
         << XmlAttribute(metric.sumOnAdd()
                        ? "count" : "value", values->toString("count"));
    printCommonXmlParts(metric);
    _xos << XmlEndTag();
    return true;
}

bool
XmlWriter::visitValueMetric(const AbstractValueMetric& metric, bool)
{
    MetricValueClass::UP values(metric.getValues());
    if (!metric.inUse(*values) && _verbosity < 2) return true;
    using namespace vespalib::xml;
    _xos << XmlTag(metric.getName(), CONVERT_ILLEGAL_CHARACTERS)
         << XmlAttribute("average", values->getLongValue("count") == 0
                    ? 0 : values->getDoubleValue("total")
                          / values->getDoubleValue("count"))
         << XmlAttribute("last", values->toString("last"));
    if (!metric.summedAverage()) {
        if (values->getLongValue("count") > 0) {
            _xos << XmlAttribute("min", values->toString("min"))
                 << XmlAttribute("max", values->toString("max"));
        }
        _xos << XmlAttribute("count", values->getLongValue("count"));
        if (_verbosity >= 2) {
            _xos << XmlAttribute("total", values->toString("total"));
        }
    }
    printCommonXmlParts(metric);
    _xos << XmlEndTag();
    return true;
}

void
XmlWriter::printCommonXmlParts(const Metric& metric) const
{
    using namespace vespalib::xml;
    const Metric::Tags& tags(metric.getTags());
    if (_verbosity >= 3 && tags.size() > 0) {
        std::ostringstream ost;
        // XXX print tag values as well
        ost << tags[0].key;
        for (uint32_t i=1; i<tags.size(); ++i) {
            ost << "," << tags[i].key;
        }
        _xos << XmlAttribute("tags", ost.str());
    }
    if (_verbosity >= 1 && !metric.getDescription().empty()) {
        _xos << XmlAttribute("description", metric.getDescription());
    }
}

} // metrics