aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/vespa/http/server/CollectingMetric.java
blob: 5f88231b565a8983f627d38617ae85e1acf12de0 (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.http.server;

import com.yahoo.jdisc.Metric;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author ollivir
 */
public final class CollectingMetric implements Metric {
    private final Context DUMMY_CONTEXT = new Context() {};
    private final Map<String, AtomicLong> values = new ConcurrentHashMap<>();

    public CollectingMetric() {}

    @Override
    public void set(String key, Number val, Context ctx) {
        values.computeIfAbsent(key, ignored -> new AtomicLong(0)).set(val.longValue());
    }

    @Override
    public void add(String key, Number val, Context ctx) {
        values.computeIfAbsent(key, ignored -> new AtomicLong(0)).addAndGet(val.longValue());
    }

    public long get(String key) {
        return Optional.ofNullable(values.get(key)).map(AtomicLong::get).orElse(0L);
    }

    @Override
    public Context createContext(Map<String, ?> properties) {
        return DUMMY_CONTEXT;
    }
}