summaryrefslogtreecommitdiffstats
path: root/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/metrics/GaugeWrapper.java
blob: db0670c2f87e332e7199476e6911e0c48fca7f31 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.dockerapi.metrics;

import com.yahoo.metrics.simple.Gauge;

/**
 * Forwards sample to {@link com.yahoo.metrics.simple.Gauge} to be displayed in /state/v1/metrics,
 * while also saving the value so it can be accessed programatically later.
 *
 * @author valerijf
 */
public class GaugeWrapper implements MetricValue {
    private final Object lock = new Object();

    private final Gauge gauge;
    private double value;

    GaugeWrapper(Gauge gauge) {
        this.gauge = gauge;
    }

    public void sample(double x) {
        synchronized (lock) {
            gauge.sample(x);
            this.value = x;
        }
    }

    @Override
    public Number getValue() {
        synchronized (lock) {
            return value;
        }
    }
}