aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java
blob: bca6e7082c98ec859c0f83027dace82a530e1081 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.test;

import com.yahoo.jdisc.Metric;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Metric implementation for tests.
 *
 * @author jonmv
 */
public class MockMetric implements Metric {

    private final Map<String, Map<Map<String, ?>, Double>> metrics = new ConcurrentHashMap<>();

    @Override
    public void set(String key, Number val, Context ctx) {
        metrics.computeIfAbsent(key, k -> new ConcurrentHashMap<>())
               .put(MapContext.emptyIfNull(ctx).dimensions, val.doubleValue());
    }

    @Override
    public void add(String key, Number val, Context ctx) {
        metrics.computeIfAbsent(key, k -> new ConcurrentHashMap<>())
               .merge(MapContext.emptyIfNull(ctx).dimensions, val.doubleValue(), Double::sum);
    }

    @Override
    public Context createContext(Map<String, ?> properties) {
        return properties == null ? MapContext.empty : new MapContext(properties);
    }

    public Map<String, Map<Map<String, ?>, Double>> metrics() { return metrics; }

    @Override
    public String toString() {
        return "MockMetric{" +
                "metrics=" + metrics +
                '}';
    }

    private static class MapContext implements Context {

        private static final MapContext empty = new MapContext(Map.of());

        private final Map<String, Object> dimensions;

        private MapContext(Map<String, ?> dimensions) {
            this.dimensions = new HashMap<>(dimensions.size());
            this.dimensions.putAll(dimensions);
        }

        private static MapContext emptyIfNull(Context context) {
            return context == null ? empty : (MapContext) context;
        }

    }

}