Skip to content

Add getEventType() expression function#5686

Merged
dinujoh merged 5 commits intoopensearch-project:mainfrom
ps48:event-type-routing
May 14, 2025
Merged

Add getEventType() expression function#5686
dinujoh merged 5 commits intoopensearch-project:mainfrom
ps48:event-type-routing

Conversation

@ps48
Copy link
Copy Markdown
Member

@ps48 ps48 commented May 7, 2025

Description

Today DataPrepper have a great event metadata structure that users can use to route between sub-pipelines. We already have functions supported in the expression language to help users route these events:

  1. getMetaData- Function to fetch the value of attribute keys from the event
  2. hasTags - Function to validate existence of tags in an event

This PR adds a third metadata related function to help users route based on event Type. getEventType: New function to fetch the event type.

  • Updated Antlr grammar
  • Updated Expressions documentation
  • Updated GenericExpressionIT tests

Example Usage of the new function, the example will be more helpful in the upcoming OTLP source:

otel-logs-pipelines:
  source:
    otel_logs_source:
      ssl: false
      proto_reflection_service: true
  route:
    - logs: "getEventType() == \"LOG\""
  sink:
    - pipeline:
        name: "logs-pipeline"
        routes:
          - "logs"

logs-pipeline:
  source:
    pipeline:
      name: "otel-logs-pipelines"
  sink:
    - stdout:

Issues Resolved

Related issue: #5596
Dependent PR: #5677

Check List

  • New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

ps48 added 3 commits May 6, 2025 15:59
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Copy link
Copy Markdown
Member

@dlvenable dlvenable left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this improvement @ps48 !

}

@Override
public Object evaluate(final List<Object> args, Event event, Function<Object, Object> convertLiteralType) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add final modifier to all the parameters.

Copy link
Copy Markdown
Member Author

@ps48 ps48 May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dlvenable Sure updated here: 01c342c

public ParseTreeCoercionService(final Map<Class<? extends Serializable>, Function<Object, Object>> literalTypeConversions, ExpressionFunctionProvider expressionFunctionProvider) {
public ParseTreeCoercionService(
final Map<Class<? extends Serializable>, Function<Object, Object>> literalTypeConversions,
ExpressionFunctionProvider expressionFunctionProvider) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a final modifier here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated: 01c342c

@Test
void testGetEventTypeReturnsCorrectType() {
GetEventTypeExpressionFunction function = createObjectUnderTest();
Event testEvent = createTestEvent("LOG");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an @ParameterizedTest with multiple event types. e.g. LOG, METRIC, TRACE.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated: 01c342c

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Comment on lines +53 to +58
void testGetEventTypeFunctionRegistered() {
Event testEvent = createTestEvent("event");
final GenericExpressionEvaluator evaluator = mock(GenericExpressionEvaluator.class);
when(evaluator.evaluateConditional("getEventType() == \"event\"", testEvent)).thenReturn(true);
assertDoesNotThrow(() -> evaluator.evaluateConditional("getEventType() == \"event\"", testEvent));
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this test relevant here ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dinujoh This function is to check if the function name registration for getEventType is working as expected within expressions. I have a similar one in generic to check actual execution over mock (as done here):

arguments("getEventType() == \"event\"", longEvent, true),
arguments("getEventType() == \"LOG\"", longEvent, false)

I feel it is good to have both, but not necessary. Please let me know if want it removed, can do it right away.

@ps48
Copy link
Copy Markdown
Member Author

ps48 commented May 13, 2025

@dinujoh @dlvenable can you please help merge this PR. I don't have permissions to merge this.

@dinujoh dinujoh merged commit a08cd7b into opensearch-project:main May 14, 2025
45 of 47 checks passed
@dlvenable dlvenable added this to the v2.12 milestone May 14, 2025
alparish pushed a commit to alparish/data-prepper that referenced this pull request May 22, 2025
* add geteventType expresion

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

* update antlr grammar, parser and add expression tests

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

* update expression syntax documentation

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

* move expression parse exception check to evaluation exceptions

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

* resolve comments, update getEventTypet test to become parameterized

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>

---------

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants