summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/DummyMetricManager.java
blob: 100cc19477d4542ef3f630c966f5d3337e52e10a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http;

import com.google.inject.AbstractModule;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.application.MetricConsumer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="mailto:ssameer@yahoo-inc.com">ssameer</a>
 *         Date: 2/15/13
 *         Time: 11:49 AM
 */
public class DummyMetricManager extends AbstractModule implements MetricConsumer {

    private final Map<String, Integer> metrics = new HashMap<>();
    private Map<String, ?> lastContextDimensions;

    @Override
    protected void configure() {
        bind(MetricConsumer.class).toInstance(this);
    }

    @Override
    public void add(String key, Number val, Metric.Context ctx) {
        synchronized (metrics) {
            metrics.put(key, get(key) + val.intValue());
        }
    }

    @Override
    public void set(String key, Number val, Metric.Context ctx) {
        synchronized (metrics) {
            metrics.put(key, val.intValue());
        }
    }

    @Override
    public Metric.Context createContext(Map<String, ?> dimensions) {
        lastContextDimensions = dimensions;
        return new Metric.Context() { };
    }

    public Map<String, ?> getLastContextDimensions() {
        return lastContextDimensions;
    }

    public int get(String key) {
        Integer val;
        synchronized (metrics) {
            val = metrics.get(key);
        }
        return val != null ? val : 0;
    }

}