aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/vespa/metrics/countmetric.h
blob: 2b0db9bd015e7b262c8ed375d14d21ee6fd350c9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class CountMetric
 * \ingroup metrics
 *
 * \brief Metric representing a count.
 *
 * A counter metric have the following properties:
 *   - It can never decrease, only increase.
 *   - Logs its value as a count event.
 *   - When summing counts, the counts are added together.
 */

#pragma once

#include "countmetricvalues.h"
#include <vespa/metrics/metric.h>

namespace metrics {

struct AbstractCountMetric : public Metric {
    bool visit(MetricVisitor& visitor, bool tagAsAutoGenerated) const override
    {
        return visitor.visitCountMetric(*this, tagAsAutoGenerated);
    }
    virtual MetricValueClass::UP getValues() const = 0;
    virtual bool sumOnAdd() const = 0;
    virtual bool inUse(const MetricValueClass& v) const = 0;

protected:
    AbstractCountMetric(const String& name, Tags dimensions,
                        const String& description, MetricSet* owner)
        : Metric(name, std::move(dimensions), description, owner)
    { }

    AbstractCountMetric(const AbstractCountMetric& other, MetricSet* owner)
        : Metric(other, owner)
    { }

    void logWarning(const char* msg, const char * op) const;
};

template<typename T, bool SumOnAdd>
class CountMetric : public AbstractCountMetric
{
    using Values = CountMetricValues<T>;
    MetricValueSet<Values> _values;

public:
    CountMetric(const String& name, Tags dimensions, const String& description)
        : CountMetric(name, std::move(dimensions), description, nullptr)
    {}
    CountMetric(const String& name, Tags dimensions, const String& description, MetricSet* owner);
    CountMetric(const CountMetric<T, SumOnAdd>& other, MetricSet* owner);

    ~CountMetric() override;

    MetricValueClass::UP getValues() const override;

    void set(T value);
    void inc(T value = 1);
    void dec(T value = 1);

    CountMetric & operator+=(const CountMetric &);
    CountMetric & operator-=(const CountMetric &);
    friend CountMetric operator+(const CountMetric & a, const CountMetric & b) {
        CountMetric t(a); t += b; return t;
    }
    friend CountMetric operator-(const CountMetric & a, const CountMetric & b) {
        CountMetric t(a); t -= b; return t;
    }

    CountMetric * clone(std::vector<Metric::UP> &, CopyType, MetricSet* owner, bool) const override {
        return new CountMetric<T, SumOnAdd>(*this, owner);
    }

    T getValue() const { return _values.getValues()._value; }

    void reset() override { _values.reset(); }
    void print(std::ostream&, bool verbose,
               const std::string& indent, uint64_t secondsPassed) const override;

    // Only one metric in valuemetric, so return it on any id.
    int64_t getLongValue(stringref id) const override {
        (void) id;
        return static_cast<int64_t>(getValue());
    }
    double getDoubleValue(stringref id) const override {
        (void) id;
        return static_cast<double>(getValue());
    }

    bool inUse(const MetricValueClass& v) const  override {
        return static_cast<const Values&>(v).inUse();
    }
    bool used() const override { return _values.getValues().inUse(); }
    bool sumOnAdd() const override { return SumOnAdd; }
    void addMemoryUsage(MemoryConsumption&) const override;
    void printDebug(std::ostream&, const std::string& indent) const override;
    void addToPart(Metric&) const override;
    void addToSnapshot(Metric&, std::vector<Metric::UP> &) const override;
};

using LongCountMetric = CountMetric<uint64_t, true>;

} // metrics