aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/metrics/simple/Counter.java
blob: 017a919c5db6be569aded41f4f74590514f778c8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics.simple;

import com.yahoo.api.annotations.Beta;
import com.yahoo.metrics.simple.UntypedMetric.AssumedType;

/**
 * A counter metric. Create a counter by declaring it with
 * {@link MetricReceiver#declareCounter(String)} or
 * {@link MetricReceiver#declareCounter(String, Point)}.
 *
 * @author steinar
 */
@Beta
public class Counter {
    private final Point defaultPosition;
    private final String name;
    private final MetricReceiver metricReceiver;

    Counter(String name, Point defaultPosition, MetricReceiver receiver) {
        this.name = name;
        this.defaultPosition = defaultPosition;
        this.metricReceiver = receiver;
    }

    /**
     * Increase the dimension-less/zero-point value of this counter by 1.
     */
    public void add() {
        add(1L, defaultPosition);
    }

    /**
     * Add to the dimension-less/zero-point value of this counter.
     *
     * @param n the amount by which to increase this counter
     */
    public void add(long n) {
        add(n, defaultPosition);
    }

    /**
     * Increase this metric at the given point by 1.
     *
     * @param p the point in the metric space at which to increase this metric by 1
     */
    public void add(Point p) {
        add(1L, p);
    }

    /**
     * Add to this metric at the given point.
     *
     * @param n
     *            the amount by which to increase this counter
     * @param p
     *            the point in the metric space at which to add to the metric
     */
    public void add(long n, Point p) {
        metricReceiver.update(new Sample(new Measurement(n), new Identifier(name, p), AssumedType.COUNTER));
    }

    /**
     * Create a PointBuilder with default dimension values as given when this
     * counter was declared.
     *
     * @return a PointBuilder reflecting the default dimension values of this
     *         counter
     */
    public PointBuilder builder() {
        return new PointBuilder(defaultPosition);
    }
}