blob: 571ffc030de5bbb2b316b59cdced7ae686b7b93e (
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
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
91
92
93
94
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric;
import ai.vespa.metricsproxy.metric.model.MetricId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Once a getter is called, the instance is frozen and no more metrics can be added.
*
* @author Unknown
*/
// TODO: remove timestamp, only used as temporary storage.
// TODO: instances of this class can probably be replaced by a simple freezable map.
public class Metrics {
private final List<Metric> metrics = new ArrayList<>();
private long timestamp;
private boolean isFrozen = false;
public Metrics() {
this(System.currentTimeMillis() / 1000L);
}
public Metrics(long timestamp) {
this.timestamp = timestamp;
}
private void ensureNotFrozen() {
if (isFrozen) throw new IllegalStateException("Frozen Metrics cannot be modified!");
}
public long getTimeStamp() {
return this.timestamp;
}
/**
* Update the timestamp
*
* @param timestamp IN UTC seconds resolution
*/
public void setTimeStamp(long timestamp) {
ensureNotFrozen();
this.timestamp = timestamp;
}
public void add(Metric m) {
ensureNotFrozen();
this.timestamp = m.getTimeStamp();
this.metrics.add(m);
}
/**
* Get the size of the metrics covered. Note that this might also contain expired metrics
*
* @return size of metrics
*/
public int size() {
return this.metrics.size();
}
/**
* TODO: Remove, might be multiple metrics with same name but different dimensions
*
* @param key metric name
* @return the metric, or null
*/
public Metric getMetric(MetricId key) {
isFrozen = true;
for (Metric m: metrics) {
if (m.getName().equals(key)) {
return m;
}
}
return null;
}
public List<Metric> getMetrics() {
isFrozen = true;
return Collections.unmodifiableList(metrics);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (Metric m : metrics) {
sb.append(m.getName()).append(":").append(m.getValue()).append("\n");
}
return sb.toString();
}
}
|