Skip to content

Commit

Permalink
Event with handle (opensearch-project#4952)
Browse files Browse the repository at this point in the history
* Add withEventHandle API to Jackson Event builder

Signed-off-by: Kondaka <[email protected]>

* Added tests

Signed-off-by: Kondaka <[email protected]>

* Fixed tests

Signed-off-by: Kondaka <[email protected]>

* Addressed review comments

Signed-off-by: Kondaka <[email protected]>

* Fixed failing tests

Signed-off-by: Kondaka <[email protected]>

* Fixed failing checkstyle tests

Signed-off-by: Kondaka <[email protected]>

---------

Signed-off-by: Kondaka <[email protected]>
  • Loading branch information
kkondaka authored Oct 25, 2024
1 parent 13dccc4 commit 331011c
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ protected JacksonEvent(final Builder builder) {
}

this.jsonNode = getInitialJsonNode(builder.data);
this.eventHandle = new DefaultEventHandle(eventMetadata.getTimeReceived());
if (builder.eventHandle != null) {
this.eventHandle = builder.eventHandle;
} else {
this.eventHandle = new DefaultEventHandle(eventMetadata.getTimeReceived());
}
final Instant externalOriginationTime = this.eventMetadata.getExternalOriginationTime();
if (externalOriginationTime != null) {
eventHandle.setExternalOriginationTime(externalOriginationTime);
Expand Down Expand Up @@ -532,10 +536,11 @@ public static JacksonEvent fromEvent(final Event event) {
public abstract static class Builder<T extends Builder<T>> {

private EventMetadata eventMetadata;
private Object data;
protected Object data;
private String eventType;
private Instant timeReceived;
private Map<String, Object> eventMetadataAttributes;
protected transient EventHandle eventHandle;

public abstract T getThis();

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

/**
* Sets the event handle
*
* @param eventHandle event handle
* @return returns the builder
* @since 2.10
*/
protected Builder<T> withEventHandle(final EventHandle eventHandle) {
this.eventHandle = eventHandle;
return this;
}

/**
* Sets the metadata.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public JacksonExponentialHistogram.Builder getThis() {
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withSum(double sum) {
data.put(SUM_KEY, sum);
put(SUM_KEY, sum);
return this;
}

Expand All @@ -137,7 +137,7 @@ public JacksonExponentialHistogram.Builder withSum(double sum) {
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withCount(long count) {
data.put(COUNT_KEY, count);
put(COUNT_KEY, count);
return this;
}

Expand All @@ -149,7 +149,7 @@ public JacksonExponentialHistogram.Builder withCount(long count) {
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withScale(int scale) {
data.put(SCALE_KEY, scale);
put(SCALE_KEY, scale);
return this;
}

Expand All @@ -162,7 +162,7 @@ public JacksonExponentialHistogram.Builder withScale(int scale) {
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withZeroCount(long zeroCount) {
data.put(ZERO_COUNT_KEY, zeroCount);
put(ZERO_COUNT_KEY, zeroCount);
return this;
}

Expand All @@ -174,7 +174,7 @@ public JacksonExponentialHistogram.Builder withZeroCount(long zeroCount) {
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withAggregationTemporality(String aggregationTemporality) {
data.put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
return this;
}

Expand All @@ -186,7 +186,7 @@ public JacksonExponentialHistogram.Builder withAggregationTemporality(String agg
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withPositiveBuckets(List<Bucket> exponentialBuckets) {
data.put(POSITIVE_BUCKETS_KEY, exponentialBuckets);
put(POSITIVE_BUCKETS_KEY, exponentialBuckets);
return this;
}

Expand All @@ -198,7 +198,7 @@ public JacksonExponentialHistogram.Builder withPositiveBuckets(List<Bucket> exp
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withNegativeBuckets(List<Bucket> exponentialBuckets) {
data.put(NEGATIVE_BUCKETS_KEY, exponentialBuckets);
put(NEGATIVE_BUCKETS_KEY, exponentialBuckets);
return this;
}

Expand All @@ -210,7 +210,7 @@ public JacksonExponentialHistogram.Builder withNegativeBuckets(List<Bucket> expo
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withPositive(List<Long> bucketCountsList) {
data.put(POSITIVE_KEY, bucketCountsList);
put(POSITIVE_KEY, bucketCountsList);
return this;
}

Expand All @@ -222,7 +222,7 @@ public JacksonExponentialHistogram.Builder withPositive(List<Long> bucketCountsL
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withNegative(List<Long> bucketCountsList) {
data.put(NEGATIVE_KEY, bucketCountsList);
put(NEGATIVE_KEY, bucketCountsList);
return this;
}

Expand All @@ -234,7 +234,7 @@ public JacksonExponentialHistogram.Builder withNegative(List<Long> bucketCountsL
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withPositiveOffset(int offset) {
data.put(POSITIVE_OFFSET_KEY, offset);
put(POSITIVE_OFFSET_KEY, offset);
return this;
}

Expand All @@ -258,7 +258,7 @@ public JacksonExponentialHistogram.Builder withTimeReceived(final Instant timeRe
* @since 1.4
*/
public JacksonExponentialHistogram.Builder withNegativeOffset(int offset) {
data.put(NEGATIVE_OFFSET_KEY, offset);
put(NEGATIVE_OFFSET_KEY, offset);
return this;
}

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

return new JacksonExponentialHistogram(this, flattenAttributes);
}

private void checkAndSetDefaultValues() {
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Builder getThis() {
*/
public Builder withValue(final Double value) {
if (value != null) {
data.put(VALUE_KEY, value);
put(VALUE_KEY, value);
}
return this;
}
Expand Down Expand Up @@ -100,12 +100,12 @@ public JacksonGauge build(boolean flattenAttributes) {
this.withData(data);
this.withEventType(EventType.METRIC.toString());
checkAndSetDefaultValues();
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, data);
new ParameterValidator().validate(REQUIRED_KEYS, REQUIRED_NON_EMPTY_KEYS, REQUIRED_NON_NULL_KEYS, (HashMap<String, Object>)data);
return new JacksonGauge(this, flattenAttributes);
}

private void checkAndSetDefaultValues() {
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public JacksonHistogram.Builder getThis() {
* @since 1.4
*/
public JacksonHistogram.Builder withSum(double sum) {
data.put(SUM_KEY, sum);
put(SUM_KEY, sum);
return this;
}

Expand All @@ -129,7 +129,7 @@ public JacksonHistogram.Builder withSum(double sum) {
*/
public JacksonHistogram.Builder withMin(Double min) {
if (min != null) {
data.put(MIN_KEY, min);
put(MIN_KEY, min);
}
return this;
}
Expand All @@ -142,7 +142,7 @@ public JacksonHistogram.Builder withMin(Double min) {
*/
public JacksonHistogram.Builder withMax(Double max) {
if (max != null) {
data.put(MAX_KEY, max);
put(MAX_KEY, max);
}
return this;
}
Expand All @@ -154,7 +154,7 @@ public JacksonHistogram.Builder withMax(Double max) {
* @since 1.4
*/
public JacksonHistogram.Builder withCount(long count) {
data.put(COUNT_KEY, count);
put(COUNT_KEY, count);
return this;
}

Expand All @@ -165,7 +165,7 @@ public JacksonHistogram.Builder withCount(long count) {
* @since 1.4
*/
public JacksonHistogram.Builder withBucketCount(int bucketCount) {
data.put(BUCKET_COUNTS_KEY, bucketCount);
put(BUCKET_COUNTS_KEY, bucketCount);
return this;
}

Expand All @@ -176,7 +176,7 @@ public JacksonHistogram.Builder withBucketCount(int bucketCount) {
* @since 1.4
*/
public JacksonHistogram.Builder withExplicitBoundsCount(int explicitBoundsCount) {
data.put(EXPLICIT_BOUNDS_COUNT_KEY, explicitBoundsCount);
put(EXPLICIT_BOUNDS_COUNT_KEY, explicitBoundsCount);
return this;
}

Expand All @@ -187,7 +187,7 @@ public JacksonHistogram.Builder withExplicitBoundsCount(int explicitBoundsCount)
* @since 1.4
*/
public JacksonHistogram.Builder withAggregationTemporality(String aggregationTemporality) {
data.put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
put(AGGREGATION_TEMPORALITY_KEY, aggregationTemporality);
return this;
}

Expand All @@ -210,7 +210,7 @@ public JacksonHistogram.Builder withTimeReceived(final Instant timeReceived) {
* @since 1.4
*/
public JacksonHistogram.Builder withBuckets(List<Bucket> buckets) {
data.put(BUCKETS_KEY, buckets);
put(BUCKETS_KEY, buckets);
return this;
}

Expand All @@ -221,7 +221,7 @@ public JacksonHistogram.Builder withBuckets(List<Bucket> buckets) {
* @since 1.4
*/
public JacksonHistogram.Builder withBucketCountsList(List<Long> bucketCountsList) {
data.put(BUCKET_COUNTS_LIST_KEY, bucketCountsList);
put(BUCKET_COUNTS_LIST_KEY, bucketCountsList);
return this;
}

Expand All @@ -232,7 +232,7 @@ public JacksonHistogram.Builder withBucketCountsList(List<Long> bucketCountsList
* @since 1.4
*/
public JacksonHistogram.Builder withExplicitBoundsList(List<Double> explicitBoundsList) {
data.put(EXPLICIT_BOUNDS_KEY, explicitBoundsList);
put(EXPLICIT_BOUNDS_KEY, explicitBoundsList);
return this;
}

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

return new JacksonHistogram(this, flattenAttributes);
}

private void checkAndSetDefaultValues() {
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
}

}
Expand Down
Loading

0 comments on commit 331011c

Please sign in to comment.