summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/metrics/simple/Gauge.java
blob: 684bf4b5db1e6ddc2b819206c3ff34acec5dc845 (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
// Copyright Yahoo. 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 gauge metric, i.e. a bucket of arbitrary sample values. Create a gauge
 * metric by declaring it with {@link MetricReceiver#declareGauge(String)} or
 * {@link MetricReceiver#declareGauge(String, Point)}.
 *
 * @author steinar
 */
@Beta
public class Gauge {

    private final Point defaultPosition;
    private final String name;
    private final MetricReceiver receiver;

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

    /**
     * Record a sample with default or no position.
     *
     * @param x
     *            sample value
     */
    public void sample(double x) {
        sample(x, defaultPosition);
    }

    /**
     * Record a sample at the given position.
     *
     * @param x
     *            sample value
     * @param p
     *            position/dimension values for the sample
     */
    public void sample(double x, Point p) {
        receiver.update(new Sample(new Measurement(Double.valueOf(x)), new Identifier(name, p), AssumedType.GAUGE));
    }

    /**
     * Create a PointBuilder with the default dimension values reflecting those
     * given when this gauge was declared.
     *
     * @return a builder initialized with defaults from this metric instance
     */
    public PointBuilder builder() {
        return new PointBuilder(defaultPosition);
    }
}