diff options
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java b/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java new file mode 100644 index 00000000000..cce082aeb12 --- /dev/null +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/test/MockMetric.java @@ -0,0 +1,53 @@ +// Copyright Verizon Media. 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.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; } + + private static class MapContext implements Context { + + private static final MapContext empty = new MapContext(Map.of()); + + private final Map<String, ?> dimensions; + + private MapContext(Map<String, ?> dimensions) { + this.dimensions = dimensions; + } + + private static MapContext emptyIfNull(Context context) { + return context == null ? empty : (MapContext) context; + } + + } + +} |