Skip to content

Commit 157d964

Browse files
vjkoskelaBrandonArp
authored andcommitted
Dimension injecting sink. (#111)
* Dimension injecting sink. * Separated the configuration into defaultDimensions and overrideDimensions and inject this into the union map between the data dimensions. Specifically, data overrides defaults and overrides overwrite both.
1 parent f53c915 commit 157d964

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2016 Groupon.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.arpnetworking.tsdcore.sinks;
17+
18+
import com.arpnetworking.tsdcore.model.DefaultKey;
19+
import com.arpnetworking.tsdcore.model.PeriodicData;
20+
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.collect.Maps;
22+
import net.sf.oval.constraint.NotNull;
23+
24+
import java.util.Map;
25+
26+
/**
27+
* Sink adds any specified dimensions.
28+
*
29+
* @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
30+
*/
31+
public final class DimensionInjectingSink extends BaseSink {
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
@Override
37+
public void recordAggregateData(final PeriodicData data) {
38+
final Map<String, String> mergedDimensions = Maps.newHashMap(_defaultDimensions);
39+
mergedDimensions.putAll(data.getDimensions().getParameters());
40+
mergedDimensions.putAll(_overrideDimensions);
41+
final PeriodicData.Builder dataBuilder = PeriodicData.Builder.clone(data);
42+
dataBuilder.setDimensions(new DefaultKey(ImmutableMap.copyOf(mergedDimensions)));
43+
_sink.recordAggregateData(dataBuilder.build());
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
@Override
50+
public void close() {
51+
// Nothing to do
52+
}
53+
54+
private DimensionInjectingSink(final Builder builder) {
55+
super(builder);
56+
_sink = builder._sink;
57+
_defaultDimensions = builder._defaultDimensions;
58+
_overrideDimensions = builder._overrideDimensions;
59+
}
60+
61+
private final Sink _sink;
62+
private final ImmutableMap<String, String> _defaultDimensions;
63+
private final ImmutableMap<String, String> _overrideDimensions;
64+
65+
/**
66+
* Implementation of builder pattern for {@link DimensionInjectingSink}.
67+
*
68+
* @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
69+
*/
70+
public static final class Builder extends BaseSink.Builder<Builder, DimensionInjectingSink> {
71+
72+
/**
73+
* Public constructor.
74+
*/
75+
public Builder() {
76+
super(DimensionInjectingSink::new);
77+
}
78+
79+
/**
80+
* Sets the default dimensions to inject. Matching dimensions in the data
81+
* overwrite any default dimensions.
82+
*
83+
* @param value The default dimensions to inject.
84+
* @return This instance of {@link Builder}.
85+
*/
86+
public Builder setDefaultDimensions(final ImmutableMap<String, String> value) {
87+
_defaultDimensions = value;
88+
return self();
89+
}
90+
91+
/**
92+
* Sets the override dimensions to inject. Matching dimensions in the data
93+
* are overwritten by override dimensions.
94+
*
95+
* @param value The overrride dimensions to inject.
96+
* @return This instance of {@link Builder}.
97+
*/
98+
public Builder setOverrideDimensions(final ImmutableMap<String, String> value) {
99+
_overrideDimensions = value;
100+
return self();
101+
}
102+
103+
/**
104+
* The sink to wrap. Cannot be null.
105+
*
106+
* @param value The sink to wrap.
107+
* @return This instance of {@link Builder}.
108+
*/
109+
public Builder setSink(final Sink value) {
110+
_sink = value;
111+
return this;
112+
}
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
@Override
118+
protected Builder self() {
119+
return this;
120+
}
121+
122+
@NotNull
123+
private ImmutableMap<String, String> _defaultDimensions = ImmutableMap.of();
124+
@NotNull
125+
private ImmutableMap<String, String> _overrideDimensions = ImmutableMap.of();
126+
@NotNull
127+
private Sink _sink;
128+
}
129+
}

0 commit comments

Comments
 (0)