aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/model/ServiceMonitorMetrics.java
blob: d70d38f5d9e97f62408e6974322f81548c4da546 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.service.model;

import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.Timer;

import java.util.function.Consumer;

public class ServiceMonitorMetrics {
    public static String SERVICE_MODEL_METRIC_PREFIX = "serviceModel.";

    private final Metric metric;
    private final Timer timer;

    public ServiceMonitorMetrics(Metric metric, Timer timer) {
        this.metric = metric;
        this.timer = timer;
    }

    public LatencyMeasurement startServiceModelSnapshotLatencyMeasurement() {
        Consumer<Double> atCompletion = elapsedSeconds ->
                setValue(metricKey("snapshot.latency"), elapsedSeconds);
        return new LatencyMeasurement(timer, atCompletion).start();
    }

    private static String metricKey(String suffix) {
        return SERVICE_MODEL_METRIC_PREFIX + suffix;
    }

    private void setValue(String key, Number number) {
        setValue(key, number, null);
    }

    private void setValue(String key, Number number, Metric.Context context) {
        metric.set(key, number, context);
    }
}