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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.state;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* A set of metrics.
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
*/
public final class MetricSet implements Iterable<Map.Entry<String, MetricValue>> {
private final static Logger log = Logger.getLogger(MetricSet.class.getName());
private final Map<String, MetricValue> data;
MetricSet() {
data = new HashMap<>();
}
public MetricSet(Map<String, MetricValue> data) {
this.data = data;
}
/** Returns all metrics in this */
@Override
public Iterator<Map.Entry<String, MetricValue>> iterator() {
return data.entrySet().iterator();
}
/** Returns a metric value */
public MetricValue get(String key) {
return data.get(key);
}
void add(String key, Number val) {
add(key, CountMetric.newSingleValue(val));
}
void set(String key, Number val) {
add(key, GaugeMetric.newSingleValue(val));
}
void add(MetricSet metricSet) {
for (Map.Entry<String, MetricValue> entry : metricSet) {
add(entry.getKey(), entry.getValue());
}
}
private void add(String key, MetricValue value) {
MetricValue existingValue = data.get(key);
if (existingValue == null) {
data.put(key, value);
return;
}
if ( ! existingValue.getClass().isAssignableFrom(value.getClass())) {
log.info("Resetting metric '" + key + "' as it changed type. " +
"If you see this outside of deployment changes it means you incorrectly call both set() and add() " +
"on the same metric");
data.put(key, value);
return;
}
existingValue.add(value);
}
boolean isEmpty() { return data.isEmpty(); }
/**
* Create and return a MetricSet which carries over the last values
* set for gauges in the this MetricSet. Aggregate metrics are currently
* not carried over and will not be present in the returned set.
*/
public MetricSet partialClone() {
return new MetricSet(
data.entrySet().stream()
.filter(kv -> kv.getValue() instanceof GaugeMetric)
.collect(Collectors.toMap(
kv -> kv.getKey(),
kv -> ((GaugeMetric)kv.getValue()).newWithPreservedLastValue())));
}
}
|