33
33
import com .arpnetworking .tsdcore .statistics .Statistic ;
34
34
import com .google .common .collect .ImmutableList ;
35
35
import com .google .common .collect .ImmutableMap ;
36
+ import com .google .common .collect .ImmutableSet ;
36
37
import com .google .common .collect .Lists ;
37
38
import com .google .common .collect .Sets ;
38
39
import com .google .inject .Inject ;
51
52
import java .util .Set ;
52
53
import java .util .concurrent .TimeUnit ;
53
54
import java .util .function .Predicate ;
55
+ import javax .annotation .Nullable ;
54
56
55
57
/**
56
58
* Actual actor responsible for aggregating.
@@ -66,14 +68,25 @@ public class StreamingAggregator extends AbstractActorWithTimers {
66
68
* @param metricsListener Where to send metrics about aggregation computations.
67
69
* @param emitter Where to send the metrics data.
68
70
* @param clusterHostSuffix The suffix to append to the hostname for cluster aggregations.
71
+ * @param reaggregationDimensions The dimensions to reaggregate over.
72
+ * @param injectClusterAsHost Whether to inject a host dimension based on cluster.
69
73
* @return A new <code>Props</code>.
70
74
*/
71
75
public static Props props (
72
76
final ActorRef lifecycleTracker ,
73
77
final ActorRef metricsListener ,
74
78
final ActorRef emitter ,
75
- final String clusterHostSuffix ) {
76
- return Props .create (StreamingAggregator .class , lifecycleTracker , metricsListener , emitter , clusterHostSuffix );
79
+ final String clusterHostSuffix ,
80
+ final ImmutableSet <String > reaggregationDimensions ,
81
+ final boolean injectClusterAsHost ) {
82
+ return Props .create (
83
+ StreamingAggregator .class ,
84
+ lifecycleTracker ,
85
+ metricsListener ,
86
+ emitter ,
87
+ clusterHostSuffix ,
88
+ reaggregationDimensions ,
89
+ injectClusterAsHost );
77
90
}
78
91
79
92
/**
@@ -83,16 +96,22 @@ public static Props props(
83
96
* @param periodicStatistics Where to send metrics about aggregation computations.
84
97
* @param emitter Where to send the metrics data.
85
98
* @param clusterHostSuffix The suffix to append to the hostname for cluster aggregations.
99
+ * @param reaggregationDimensions The dimensions to reaggregate over.
100
+ * @param injectClusterAsHost Whether to inject a host dimension based on cluster.
86
101
*/
87
102
@ Inject
88
103
public StreamingAggregator (
89
104
@ Named ("bookkeeper-proxy" ) final ActorRef lifecycleTracker ,
90
105
@ Named ("periodic-statistics" ) final ActorRef periodicStatistics ,
91
106
@ Named ("cluster-emitter" ) final ActorRef emitter ,
92
- @ Named ("cluster-host-suffix" ) final String clusterHostSuffix ) {
107
+ @ Named ("cluster-host-suffix" ) final String clusterHostSuffix ,
108
+ @ Named ("reaggregation-dimensions" ) final ImmutableSet <String > reaggregationDimensions ,
109
+ @ Named ("reaggregation-cluster-as-host" ) final boolean injectClusterAsHost ) {
93
110
_lifecycleTracker = lifecycleTracker ;
94
111
_periodicStatistics = periodicStatistics ;
95
112
_clusterHostSuffix = clusterHostSuffix ;
113
+ _reaggregationDimensions = reaggregationDimensions ;
114
+ _injectClusterAsHost = injectClusterAsHost ;
96
115
context ().setReceiveTimeout (FiniteDuration .apply (30 , TimeUnit .MINUTES ));
97
116
98
117
timers ().startPeriodicTimer (BUCKET_CHECK_TIMER_KEY , BucketCheck .getInstance (), FiniteDuration .apply (5 , TimeUnit .SECONDS ));
@@ -284,20 +303,39 @@ private void processAggregationMessage(final Messages.StatisticSetRecord data) {
284
303
}
285
304
286
305
private ImmutableMap <String , String > dimensionsToMap (final Messages .StatisticSetRecord statisticSetRecord ) {
287
- final ImmutableMap .Builder <String , String > builder = ImmutableMap .<String , String >builder ()
288
- .put (CombinedMetricData .CLUSTER_KEY , statisticSetRecord .getCluster ())
289
- .put (CombinedMetricData .SERVICE_KEY , statisticSetRecord .getService ())
290
- .put (CombinedMetricData .HOST_KEY , createHost ());
306
+ // Build a map of dimension key-value pairs dropping any that are to be reaggregated over
307
+ final ImmutableMap .Builder <String , String > builder = ImmutableMap .builder ();
291
308
309
+ // Grab the explicit cluster and service dimensions from the record
310
+ addDimension (CombinedMetricData .CLUSTER_KEY , statisticSetRecord .getCluster (), builder );
311
+ addDimension (CombinedMetricData .SERVICE_KEY , statisticSetRecord .getService (), builder );
312
+
313
+ // Either inject the cluster as the host dimension or grab it from the record dimensions
314
+ if (_injectClusterAsHost ) {
315
+ addDimension (CombinedMetricData .HOST_KEY , createHost (), builder );
316
+ } else {
317
+ final @ Nullable String hostDimension = statisticSetRecord .getDimensionsMap ().get (CombinedMetricData .HOST_KEY );
318
+ if (hostDimension != null ) {
319
+ addDimension (CombinedMetricData .HOST_KEY , hostDimension , builder );
320
+ }
321
+ }
322
+
323
+ // Inject all other dimensions (e.g. not service, cluster or host)
292
324
statisticSetRecord .getDimensionsMap ()
293
325
.entrySet ()
294
326
.stream ()
295
327
.filter (NOT_EXPLICIT_DIMENSION )
296
- .forEach (entry -> builder . put (entry .getKey (), entry .getValue ()));
328
+ .forEach (entry -> addDimension (entry .getKey (), entry .getValue (), builder ));
297
329
298
330
return builder .build ();
299
331
}
300
332
333
+ private void addDimension (final String key , final String value , final ImmutableMap .Builder <String , String > mapBuilder ) {
334
+ if (!_reaggregationDimensions .contains (key )) {
335
+ mapBuilder .put (key , value );
336
+ }
337
+ }
338
+
301
339
private String createHost () {
302
340
return _cluster + "-cluster" + _clusterHostSuffix ;
303
341
}
@@ -307,6 +345,8 @@ private String createHost() {
307
345
private final ActorRef _lifecycleTracker ;
308
346
private final ActorRef _periodicStatistics ;
309
347
private final String _clusterHostSuffix ;
348
+ private final ImmutableSet <String > _reaggregationDimensions ;
349
+ private final boolean _injectClusterAsHost ;
310
350
private final Set <Statistic > _statistics = Sets .newHashSet ();
311
351
private boolean _initialized = false ;
312
352
private Period _period ;
0 commit comments