This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
/
MetricsAggregator.java
203 lines (175 loc) · 7.08 KB
/
MetricsAggregator.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.datatorrent.common.metric;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
import com.google.common.base.Preconditions;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.datatorrent.api.AutoMetric;
import com.datatorrent.api.annotation.Name;
/**
* An easy to use {@link AutoMetric.Aggregator} that can be configured to perform multiple aggregations on each
* {@link AutoMetric} in an operator.
* <p/>
* The user needs to add an array of {@link SingleMetricAggregator}s for each metric. All the aggregators on each
* metric will be executed during aggregation.
* <p/>
* There are examples of {@link SingleMetricAggregator} provided in the library for common number
* aggregations- sum, min, max, avg.
*
* @since 3.0.0
*/
public class MetricsAggregator implements AutoMetric.Aggregator, Serializable
{
protected static final String DEFAULT_SEPARATOR = "-";
//physical metric -> collection of logical metrics
protected final Map<String, List<LogicalMetricMeta>> metricLogicalAggregates;
protected String aggregatorMetricSeparator;
public MetricsAggregator()
{
metricLogicalAggregates = Maps.newHashMap();
aggregatorMetricSeparator = DEFAULT_SEPARATOR;
}
@Override
public Map<String, Object> aggregate(long windowId, Collection<AutoMetric.PhysicalMetricsContext> physicalMetrics)
{
Multimap<String, Object> metricValues = ArrayListMultimap.create();
for (AutoMetric.PhysicalMetricsContext pmCtx : physicalMetrics) {
for (Map.Entry<String, Object> entry : pmCtx.getMetrics().entrySet()) {
metricValues.put(entry.getKey(), entry.getValue());
}
}
Map<String, Object> aggregates = Maps.newHashMap();
for (String metric : metricValues.keySet()) {
List<LogicalMetricMeta> logicalMetricMetas = metricLogicalAggregates.get(metric);
if (logicalMetricMetas != null) {
for (LogicalMetricMeta logicalMetricMeta : logicalMetricMetas) {
Object aggregatedVal = logicalMetricMeta.aggregator.aggregate(metricValues.get(metric));
aggregates.put(logicalMetricMeta.name, aggregatedVal);
}
}
}
return aggregates;
}
/**
* This can be overridden to change logical metric name.
*
* @param metric metric name
* @param aggregator aggregator
* @return logical metric name which is assigned to the result of the aggregator.
*/
protected String deriveLogicalMetricName(String metric, SingleMetricAggregator aggregator)
{
Name aggregatorName = aggregator.getClass().getAnnotation(Name.class);
String aggregatorDesc;
if (aggregatorName == null) {
aggregatorDesc = aggregator.getClass().getName();
} else {
aggregatorDesc = aggregatorName.value();
}
return aggregatorDesc + aggregatorMetricSeparator + metric;
}
/**
* Adds aggregators for a single physical metric.<br/>
* If there is only one aggregator, then the logical metric name is same as physical metric name.<br/>
* Otherwise logical metric names are derived using {@link #deriveLogicalMetricName}
*
* @param metric physical metric name.
* @param aggregators aggregators for the metric.
*/
public void addAggregators(@NotNull String metric, @NotNull SingleMetricAggregator[] aggregators)
{
Preconditions.checkNotNull(metric, "metric");
Preconditions.checkNotNull(aggregators, "aggregators");
addAggregatorsHelper(metric, aggregators, null);
}
/**
* Adds aggregators and logical metric names for a single physical metric.
*
* @param metric physical metric name.
* @param aggregators aggregators for the metric.
* @param logicalMetricNames logic metric names that will be used for an aggregated value. logicalMetricNames[i] will
* be used for the result of aggregators[i].
*/
public void addAggregators(@NotNull String metric, @NotNull SingleMetricAggregator[] aggregators,
@NotNull String[] logicalMetricNames)
{
Preconditions.checkNotNull(metric, "metric");
Preconditions.checkNotNull(aggregators, "aggregators");
Preconditions.checkNotNull(logicalMetricNames, "logicalMetricNames");
Preconditions.checkArgument(aggregators.length == logicalMetricNames.length,
"different length aggregators and logical names");
addAggregatorsHelper(metric, aggregators, logicalMetricNames);
}
private void addAggregatorsHelper(String metric, SingleMetricAggregator[] aggregators, String[] logicalMetricNames)
{
List<LogicalMetricMeta> laggregators = metricLogicalAggregates.get(metric);
if (laggregators == null) {
laggregators = Lists.newArrayList();
metricLogicalAggregates.put(metric, laggregators);
}
for (int i = 0; i < aggregators.length; i++) {
String resultName = (logicalMetricNames == null || logicalMetricNames[i] == null) ?
(aggregators.length == 1 ? metric : deriveLogicalMetricName(metric, aggregators[i])) : logicalMetricNames[i];
laggregators.add(new LogicalMetricMeta(aggregators[i], resultName));
}
}
public String getAggregatorMetricSeparator()
{
return aggregatorMetricSeparator;
}
public void setAggregatorMetricSeparator(String aggregatorMetricSeparator)
{
this.aggregatorMetricSeparator = aggregatorMetricSeparator;
}
public static class LogicalMetricMeta implements Serializable
{
private SingleMetricAggregator aggregator;
private String name;
protected LogicalMetricMeta(@NotNull SingleMetricAggregator aggregator, @NotNull String name)
{
this.aggregator = Preconditions.checkNotNull(aggregator, "aggregator");
this.name = Preconditions.checkNotNull(name, "logical metric name");
}
public SingleMetricAggregator getAggregator()
{
return aggregator;
}
protected void setAggregator(SingleMetricAggregator aggregator)
{
this.aggregator = aggregator;
}
public String getName()
{
return name;
}
protected void setName(String name)
{
this.name = name;
}
private static final long serialVersionUID = 201604231340L;
}
private static final long serialVersionUID = 201604231337L;
}