aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/vespa/metrics/countmetricvalues.h
blob: 902b5294c28e5b9e4cc7fa71cf11de509a1b9c73 (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
// Copyright Yahoo. 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 <vespa/metrics/metricvalueset.h>
#include <atomic>

namespace metrics {

template <typename T>
struct CountMetricValues : public MetricValueClass {
    T _value;

    struct AtomicImpl {
        AtomicImpl() noexcept : _value(0) {}
        AtomicImpl(const AtomicImpl & rhs) noexcept : _value(rhs._value.load(std::memory_order_relaxed)) {}
        std::atomic<T> _value;
    };

    void relaxedStoreInto(AtomicImpl& target) const noexcept {
        target._value.store(_value, std::memory_order_relaxed);
    }

    void relaxedLoadFrom(const AtomicImpl& source) noexcept {
        _value = source._value.load(std::memory_order_relaxed);
    }

    CountMetricValues() : _value(0) {}

    std::string toString() const;
    double getDoubleValue(stringref) const override;
    uint64_t getLongValue(stringref) const override;
    void output(const std::string&, std::ostream& out) const override;
    void output(const std::string&, vespalib::JsonStream& stream) const override;
    bool inUse() const { return (_value != 0); }
};

} // metrics