aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/metrics/counter.h
blob: 604334ecd6464dbc186b705a1970292b7e0b0635 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <memory>
#include "metric_id.h"
#include "point.h"

namespace vespalib {
namespace metrics {

class MetricsManager;
struct CounterAggregator;


/**
 * Represents a counter metric that can be incremented.
 **/
class Counter {
    std::shared_ptr<MetricsManager> _manager;
    MetricId _id;
public:
    Counter() : _manager(), _id(0) {}
    Counter(const Counter&) = delete;
    Counter(Counter &&other) = default;
    Counter& operator= (const Counter &) = delete;
    Counter& operator= (Counter &&other) = default;
    Counter(std::shared_ptr<MetricsManager> m, MetricId id)
        : _manager(std::move(m)), _id(id)
    {}

    // convenience methods:
    void add() const { add(1, Point::empty); }
    void add(Point p) { add(1, p); }
    void add(size_t count) const { add(count, Point::empty); }

    /**
     * Increment the counter.
     * @param count the amount to increment by (default 1)
     * @param p the point representing labels for this increment (default empty)
     **/
    void add(size_t count, Point p) const;

    // internal
    struct Increment {
        using Key = std::pair<MetricId, Point>;
        Key idx;
        size_t value;
        Increment() = delete;
        Increment(Key k, size_t v) : idx(k), value(v) {}
    };

    using aggregator_type = CounterAggregator;
    using sample_type = Increment;
};

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