Skip to content

Commit 331011c

Browse files
authored
Event with handle (opensearch-project#4952)
* Add withEventHandle API to Jackson Event builder Signed-off-by: Kondaka <krishkdk@amazon.com> * Added tests Signed-off-by: Kondaka <krishkdk@amazon.com> * Fixed tests Signed-off-by: Kondaka <krishkdk@amazon.com> * Addressed review comments Signed-off-by: Kondaka <krishkdk@amazon.com> * Fixed failing tests Signed-off-by: Kondaka <krishkdk@amazon.com> * Fixed failing checkstyle tests Signed-off-by: Kondaka <krishkdk@amazon.com> --------- Signed-off-by: Kondaka <krishkdk@amazon.com>
1 parent 13dccc4 commit 331011c

15 files changed

Lines changed: 221 additions & 58 deletions

File tree

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/JacksonEvent.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ protected JacksonEvent(final Builder builder) {
9898
}
9999

100100
this.jsonNode = getInitialJsonNode(builder.data);
101-
this.eventHandle = new DefaultEventHandle(eventMetadata.getTimeReceived());
101+
if (builder.eventHandle != null) {
102+
this.eventHandle = builder.eventHandle;
103+
} else {
104+
this.eventHandle = new DefaultEventHandle(eventMetadata.getTimeReceived());
105+
}
102106
final Instant externalOriginationTime = this.eventMetadata.getExternalOriginationTime();
103107
if (externalOriginationTime != null) {
104108
eventHandle.setExternalOriginationTime(externalOriginationTime);
@@ -532,10 +536,11 @@ public static JacksonEvent fromEvent(final Event event) {
532536
public abstract static class Builder<T extends Builder<T>> {
533537

534538
private EventMetadata eventMetadata;
535-
private Object data;
539+
protected Object data;
536540
private String eventType;
537541
private Instant timeReceived;
538542
private Map<String, Object> eventMetadataAttributes;
543+
protected transient EventHandle eventHandle;
539544

540545
public abstract T getThis();
541546

@@ -575,6 +580,18 @@ public Builder<T> withTimeReceived(final Instant timeReceived) {
575580
return this;
576581
}
577582

583+
/**
584+
* Sets the event handle
585+
*
586+
* @param eventHandle event handle
587+
* @return returns the builder
588+
* @since 2.10
589+
*/
590+
protected Builder<T> withEventHandle(final EventHandle eventHandle) {
591+
this.eventHandle = eventHandle;
592+
return this;
593+
}
594+
578595
/**
579596
* Sets the metadata.
580597
*

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/metric/JacksonExponentialHistogram.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public JacksonExponentialHistogram.Builder getThis() {
125125
* @since 1.4
126126
*/
127127
public JacksonExponentialHistogram.Builder withSum(double sum) {
128-
data.put(SUM_KEY, sum);
128+
put(SUM_KEY, sum);
129129
return this;
130130
}
131131

@@ -137,7 +137,7 @@ public JacksonExponentialHistogram.Builder withSum(double sum) {
137137
* @since 1.4
138138
*/
139139
public JacksonExponentialHistogram.Builder withCount(long count) {
140-
data.put(COUNT_KEY, count);
140+
put(COUNT_KEY, count);
141141
return this;
142142
}
143143

@@ -149,7 +149,7 @@ public JacksonExponentialHistogram.Builder withCount(long count) {
149149
* @since 1.4
150150
*/
151151
public JacksonExponentialHistogram.Builder withScale(int scale) {
152-
data.put(SCALE_KEY, scale);
152+
put(SCALE_KEY, scale);
153153
return this;
154154
}
155155

@@ -162,7 +162,7 @@ public JacksonExponentialHistogram.Builder withScale(int scale) {
162162
* @since 1.4
163163
*/
164164
public JacksonExponentialHistogram.Builder withZeroCount(long zeroCount) {
165-
data.put(ZERO_COUNT_KEY, zeroCount);
165+
put(ZERO_COUNT_KEY, zeroCount);
166166
return this;
167167
}
168168

@@ -174,7 +174,7 @@ public JacksonExponentialHistogram.Builder withZeroCount(long zeroCount) {
174174
* @since 1.4
175175
*/
176176
public JacksonExponentialHistogram.Builder withAggregationTemporality(String aggregationTemporality) {
177-
data.put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
177+
put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
178178
return this;
179179
}
180180

@@ -186,7 +186,7 @@ public JacksonExponentialHistogram.Builder withAggregationTemporality(String agg
186186
* @since 1.4
187187
*/
188188
public JacksonExponentialHistogram.Builder withPositiveBuckets(List<Bucket> exponentialBuckets) {
189-
data.put(POSITIVE_BUCKETS_KEY, exponentialBuckets);
189+
put(POSITIVE_BUCKETS_KEY, exponentialBuckets);
190190
return this;
191191
}
192192

@@ -198,7 +198,7 @@ public JacksonExponentialHistogram.Builder withPositiveBuckets(List<Bucket> exp
198198
* @since 1.4
199199
*/
200200
public JacksonExponentialHistogram.Builder withNegativeBuckets(List<Bucket> exponentialBuckets) {
201-
data.put(NEGATIVE_BUCKETS_KEY, exponentialBuckets);
201+
put(NEGATIVE_BUCKETS_KEY, exponentialBuckets);
202202
return this;
203203
}
204204

@@ -210,7 +210,7 @@ public JacksonExponentialHistogram.Builder withNegativeBuckets(List<Bucket> expo
210210
* @since 1.4
211211
*/
212212
public JacksonExponentialHistogram.Builder withPositive(List<Long> bucketCountsList) {
213-
data.put(POSITIVE_KEY, bucketCountsList);
213+
put(POSITIVE_KEY, bucketCountsList);
214214
return this;
215215
}
216216

@@ -222,7 +222,7 @@ public JacksonExponentialHistogram.Builder withPositive(List<Long> bucketCountsL
222222
* @since 1.4
223223
*/
224224
public JacksonExponentialHistogram.Builder withNegative(List<Long> bucketCountsList) {
225-
data.put(NEGATIVE_KEY, bucketCountsList);
225+
put(NEGATIVE_KEY, bucketCountsList);
226226
return this;
227227
}
228228

@@ -234,7 +234,7 @@ public JacksonExponentialHistogram.Builder withNegative(List<Long> bucketCountsL
234234
* @since 1.4
235235
*/
236236
public JacksonExponentialHistogram.Builder withPositiveOffset(int offset) {
237-
data.put(POSITIVE_OFFSET_KEY, offset);
237+
put(POSITIVE_OFFSET_KEY, offset);
238238
return this;
239239
}
240240

@@ -258,7 +258,7 @@ public JacksonExponentialHistogram.Builder withTimeReceived(final Instant timeRe
258258
* @since 1.4
259259
*/
260260
public JacksonExponentialHistogram.Builder withNegativeOffset(int offset) {
261-
data.put(NEGATIVE_OFFSET_KEY, offset);
261+
put(NEGATIVE_OFFSET_KEY, offset);
262262
return this;
263263
}
264264

@@ -284,13 +284,13 @@ public JacksonExponentialHistogram build(boolean flattenAttributes) {
284284
this.withEventKind(KIND.EXPONENTIAL_HISTOGRAM.toString());
285285
this.withEventType(EventType.METRIC.toString());
286286
checkAndSetDefaultValues();
287-
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, data);
287+
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, (HashMap<String, Object>)data);
288288

289289
return new JacksonExponentialHistogram(this, flattenAttributes);
290290
}
291291

292292
private void checkAndSetDefaultValues() {
293-
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
293+
computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
294294
}
295295
}
296296
}

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/metric/JacksonGauge.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Builder getThis() {
6363
*/
6464
public Builder withValue(final Double value) {
6565
if (value != null) {
66-
data.put(VALUE_KEY, value);
66+
put(VALUE_KEY, value);
6767
}
6868
return this;
6969
}
@@ -100,12 +100,12 @@ public JacksonGauge build(boolean flattenAttributes) {
100100
this.withData(data);
101101
this.withEventType(EventType.METRIC.toString());
102102
checkAndSetDefaultValues();
103-
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, data);
103+
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, (HashMap<String, Object>)data);
104104
return new JacksonGauge(this, flattenAttributes);
105105
}
106106

107107
private void checkAndSetDefaultValues() {
108-
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
108+
computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
109109
}
110110
}
111111
}

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/metric/JacksonHistogram.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public JacksonHistogram.Builder getThis() {
117117
* @since 1.4
118118
*/
119119
public JacksonHistogram.Builder withSum(double sum) {
120-
data.put(SUM_KEY, sum);
120+
put(SUM_KEY, sum);
121121
return this;
122122
}
123123

@@ -129,7 +129,7 @@ public JacksonHistogram.Builder withSum(double sum) {
129129
*/
130130
public JacksonHistogram.Builder withMin(Double min) {
131131
if (min != null) {
132-
data.put(MIN_KEY, min);
132+
put(MIN_KEY, min);
133133
}
134134
return this;
135135
}
@@ -142,7 +142,7 @@ public JacksonHistogram.Builder withMin(Double min) {
142142
*/
143143
public JacksonHistogram.Builder withMax(Double max) {
144144
if (max != null) {
145-
data.put(MAX_KEY, max);
145+
put(MAX_KEY, max);
146146
}
147147
return this;
148148
}
@@ -154,7 +154,7 @@ public JacksonHistogram.Builder withMax(Double max) {
154154
* @since 1.4
155155
*/
156156
public JacksonHistogram.Builder withCount(long count) {
157-
data.put(COUNT_KEY, count);
157+
put(COUNT_KEY, count);
158158
return this;
159159
}
160160

@@ -165,7 +165,7 @@ public JacksonHistogram.Builder withCount(long count) {
165165
* @since 1.4
166166
*/
167167
public JacksonHistogram.Builder withBucketCount(int bucketCount) {
168-
data.put(BUCKET_COUNTS_KEY, bucketCount);
168+
put(BUCKET_COUNTS_KEY, bucketCount);
169169
return this;
170170
}
171171

@@ -176,7 +176,7 @@ public JacksonHistogram.Builder withBucketCount(int bucketCount) {
176176
* @since 1.4
177177
*/
178178
public JacksonHistogram.Builder withExplicitBoundsCount(int explicitBoundsCount) {
179-
data.put(EXPLICIT_BOUNDS_COUNT_KEY, explicitBoundsCount);
179+
put(EXPLICIT_BOUNDS_COUNT_KEY, explicitBoundsCount);
180180
return this;
181181
}
182182

@@ -187,7 +187,7 @@ public JacksonHistogram.Builder withExplicitBoundsCount(int explicitBoundsCount)
187187
* @since 1.4
188188
*/
189189
public JacksonHistogram.Builder withAggregationTemporality(String aggregationTemporality) {
190-
data.put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
190+
put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
191191
return this;
192192
}
193193

@@ -210,7 +210,7 @@ public JacksonHistogram.Builder withTimeReceived(final Instant timeReceived) {
210210
* @since 1.4
211211
*/
212212
public JacksonHistogram.Builder withBuckets(List<Bucket> buckets) {
213-
data.put(BUCKETS_KEY, buckets);
213+
put(BUCKETS_KEY, buckets);
214214
return this;
215215
}
216216

@@ -221,7 +221,7 @@ public JacksonHistogram.Builder withBuckets(List<Bucket> buckets) {
221221
* @since 1.4
222222
*/
223223
public JacksonHistogram.Builder withBucketCountsList(List<Long> bucketCountsList) {
224-
data.put(BUCKET_COUNTS_LIST_KEY, bucketCountsList);
224+
put(BUCKET_COUNTS_LIST_KEY, bucketCountsList);
225225
return this;
226226
}
227227

@@ -232,7 +232,7 @@ public JacksonHistogram.Builder withBucketCountsList(List<Long> bucketCountsList
232232
* @since 1.4
233233
*/
234234
public JacksonHistogram.Builder withExplicitBoundsList(List<Double> explicitBoundsList) {
235-
data.put(EXPLICIT_BOUNDS_KEY, explicitBoundsList);
235+
put(EXPLICIT_BOUNDS_KEY, explicitBoundsList);
236236
return this;
237237
}
238238

@@ -256,13 +256,13 @@ public JacksonHistogram build(boolean flattenAttributes) {
256256
this.withEventKind(KIND.HISTOGRAM.toString());
257257
this.withEventType(EventType.METRIC.toString());
258258
checkAndSetDefaultValues();
259-
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, data);
259+
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, (HashMap<String, Object>)data);
260260

261261
return new JacksonHistogram(this, flattenAttributes);
262262
}
263263

264264
private void checkAndSetDefaultValues() {
265-
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
265+
computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
266266
}
267267

268268
}

0 commit comments

Comments
 (0)