-
Notifications
You must be signed in to change notification settings - Fork 332
AWS CloudWatch Event Sink Implementation #1965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
singhpk234
merged 47 commits into
apache:main
from
adnanhemani:ahemani/cloudwatch_event_listener
Sep 3, 2025
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
2e07dde
Add AWS CloudWatch integration through Event Listener
adnanhemani 06eaca4
cleanup
adnanhemani 1515fbb
spotlessapply
adnanhemani 854501b
Added unit test with LocalStack
adnanhemani c1c94b2
typo
adnanhemani ab3c5f9
spotlessapply
adnanhemani ab9ccbe
Merge remote-tracking branch 'origin/main' into ahemani/cloudwatch_ev…
adnanhemani a641136
recompile from main
adnanhemani 5a355d1
first revision change, based on review from @eric-maynard
adnanhemani bab4439
merge from origin/main
adnanhemani 04c310a
spotlessapply
adnanhemani d4b44ff
Merge branch 'main' into ahemani/cloudwatch_event_listener
adnanhemani 4d0554a
injected securitycontext and callcontext
adnanhemani cc715ad
todo
adnanhemani 518aaaa
modify test
adnanhemani 8758255
first draft of revision
adnanhemani f3f62a0
resolve comments from @eric-maynard and @snazy
adnanhemani d21dabc
refactor into separate package
adnanhemani 9054511
typo
adnanhemani 828760a
revising comments from @eric-maynard
adnanhemani ae79600
Merge branch 'main' into ahemani/cloudwatch_event_listener
adnanhemani 9d47684
spotlessapply
adnanhemani 025de74
revision on review from @singhpk234
adnanhemani e4ec3f8
resolve conflicts
adnanhemani 491ea3a
resolve conflicts, pt. 2
adnanhemani d453660
spotlessapply
adnanhemani f89b0ae
spotlessapply again
adnanhemani e5c02b7
address comments from @RussellSpitzer
adnanhemani 4f8a15b
merge from main
adnanhemani 1305321
prior to manual test
adnanhemani 27f28f4
addressing comments from @snazy
adnanhemani 6b42071
Merge remote-tracking branch 'origin/main' into ahemani/cloudwatch_ev…
adnanhemani ec2bee8
merge from main
adnanhemani 69b7feb
spotlesscheck
adnanhemani e8b5e93
documentation updates
adnanhemani 3030d6a
review comments from @RussellSpitzer
adnanhemani b9abab6
removed mocked tests, as per review from @RussellSpitzer
adnanhemani 4c91c57
Address comments from @RussellSpitzer and merge from main
adnanhemani b0c6160
typo
adnanhemani 44fad9a
spotlessapply
adnanhemani e8447a9
fix docstrings
adnanhemani 688ec97
refactor
adnanhemani 360cb91
use awaitility
adnanhemani 7c09d9b
Add negative case testing
adnanhemani 5e34884
spotlessapply
adnanhemani e2ed743
Revision based on comments from @eric-maynard and @singhpk234
adnanhemani a870aea
Addressing comments from @singhpk234
adnanhemani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...in/java/org/apache/polaris/service/events/jsonEventListener/PropertyMapEventListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.polaris.service.events.jsonEventListener; | ||
|
|
||
| import java.util.HashMap; | ||
| import org.apache.polaris.service.events.AfterTableRefreshedEvent; | ||
| import org.apache.polaris.service.events.PolarisEventListener; | ||
|
|
||
| /** | ||
| * This class provides a common framework for transforming Polaris events into a HashMap, which can | ||
| * be used to transform the event further, such as transforming into a JSON string, and send them to | ||
| * various destinations. Concrete implementations should override the | ||
| * {{@code @link#transformAndSendEvent(HashMap)}} method to define how the event data should be | ||
| * transformed into a JSON string, transmitted, and/or stored. | ||
| */ | ||
| public abstract class PropertyMapEventListener extends PolarisEventListener { | ||
| protected abstract void transformAndSendEvent(HashMap<String, Object> properties); | ||
|
|
||
| @Override | ||
| public void onAfterTableRefreshed(AfterTableRefreshedEvent event) { | ||
| HashMap<String, Object> properties = new HashMap<>(); | ||
| properties.put("event_type", event.getClass().getSimpleName()); | ||
| properties.put("table_identifier", event.tableIdentifier().toString()); | ||
| transformAndSendEvent(properties); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
...e/polaris/service/events/jsonEventListener/aws/cloudwatch/AwsCloudWatchConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.polaris.service.events.jsonEventListener.aws.cloudwatch; | ||
|
|
||
| /** Configuration interface for AWS CloudWatch event listener settings. */ | ||
| public interface AwsCloudWatchConfiguration { | ||
| String awsCloudWatchLogGroup(); | ||
|
|
||
| String awsCloudWatchLogStream(); | ||
|
|
||
| String awsCloudWatchRegion(); | ||
|
|
||
| boolean synchronousMode(); | ||
| } |
190 changes: 190 additions & 0 deletions
190
...e/polaris/service/events/jsonEventListener/aws/cloudwatch/AwsCloudWatchEventListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,190 @@ | ||||||
| /* | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||
| * or more contributor license agreements. See the NOTICE file | ||||||
| * distributed with this work for additional information | ||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||
| * to you under the Apache License, Version 2.0 (the | ||||||
| * "License"); you may not use this file except in compliance | ||||||
| * with the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, | ||||||
| * software distributed under the License is distributed on an | ||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| * KIND, either express or implied. See the License for the | ||||||
| * specific language governing permissions and limitations | ||||||
| * under the License. | ||||||
| */ | ||||||
|
|
||||||
| package org.apache.polaris.service.events.jsonEventListener.aws.cloudwatch; | ||||||
|
|
||||||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
| import io.smallrye.common.annotation.Identifier; | ||||||
| import jakarta.annotation.PostConstruct; | ||||||
| import jakarta.annotation.PreDestroy; | ||||||
| import jakarta.enterprise.context.ApplicationScoped; | ||||||
| import jakarta.inject.Inject; | ||||||
| import jakarta.ws.rs.core.Context; | ||||||
| import jakarta.ws.rs.core.SecurityContext; | ||||||
| import java.time.Clock; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.List; | ||||||
| import java.util.concurrent.CompletableFuture; | ||||||
| import java.util.function.Supplier; | ||||||
| import org.apache.polaris.core.auth.PolarisPrincipal; | ||||||
| import org.apache.polaris.core.context.CallContext; | ||||||
| import org.apache.polaris.service.config.PolarisIcebergObjectMapperCustomizer; | ||||||
| import org.apache.polaris.service.events.jsonEventListener.PropertyMapEventListener; | ||||||
| import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | ||||||
| import software.amazon.awssdk.regions.Region; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsAsyncClient; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogGroupRequest; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogGroupsRequest; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest; | ||||||
| import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse; | ||||||
|
|
||||||
| @ApplicationScoped | ||||||
| @Identifier("aws-cloudwatch") | ||||||
| public class AwsCloudWatchEventListener extends PropertyMapEventListener { | ||||||
| private static final Logger LOGGER = LoggerFactory.getLogger(AwsCloudWatchEventListener.class); | ||||||
| final ObjectMapper objectMapper = new ObjectMapper(); | ||||||
|
|
||||||
| private CloudWatchLogsAsyncClient client; | ||||||
|
|
||||||
| private final String logGroup; | ||||||
| private final String logStream; | ||||||
| private final Region region; | ||||||
| private final boolean synchronousMode; | ||||||
| private final Clock clock; | ||||||
|
|
||||||
| @Inject CallContext callContext; | ||||||
|
|
||||||
| @Context SecurityContext securityContext; | ||||||
|
|
||||||
| @Inject | ||||||
| public AwsCloudWatchEventListener( | ||||||
| AwsCloudWatchConfiguration config, | ||||||
| Clock clock, | ||||||
| PolarisIcebergObjectMapperCustomizer customizer) { | ||||||
| this.logStream = config.awsCloudWatchLogStream(); | ||||||
| this.logGroup = config.awsCloudWatchLogGroup(); | ||||||
| this.region = Region.of(config.awsCloudWatchRegion()); | ||||||
| this.synchronousMode = config.synchronousMode(); | ||||||
| this.clock = clock; | ||||||
| customizer.customize(this.objectMapper); | ||||||
| } | ||||||
|
|
||||||
| @PostConstruct | ||||||
| void start() { | ||||||
| this.client = createCloudWatchAsyncClient(); | ||||||
| ensureLogGroupAndStream(); | ||||||
| } | ||||||
|
|
||||||
| protected CloudWatchLogsAsyncClient createCloudWatchAsyncClient() { | ||||||
| return CloudWatchLogsAsyncClient.builder().region(region).build(); | ||||||
| } | ||||||
|
|
||||||
| private void ensureLogGroupAndStream() { | ||||||
| ensureResourceExists( | ||||||
| () -> | ||||||
| client | ||||||
| .describeLogGroups( | ||||||
| DescribeLogGroupsRequest.builder().logGroupNamePrefix(logGroup).build()) | ||||||
| .join() | ||||||
| .logGroups() | ||||||
| .stream() | ||||||
| .anyMatch(g -> g.logGroupName().equals(logGroup)), | ||||||
| () -> | ||||||
| client | ||||||
| .createLogGroup(CreateLogGroupRequest.builder().logGroupName(logGroup).build()) | ||||||
| .join(), | ||||||
| "group", | ||||||
| logGroup); | ||||||
| ensureResourceExists( | ||||||
| () -> | ||||||
| client | ||||||
| .describeLogStreams( | ||||||
| DescribeLogStreamsRequest.builder() | ||||||
| .logGroupName(logGroup) | ||||||
| .logStreamNamePrefix(logStream) | ||||||
| .build()) | ||||||
| .join() | ||||||
| .logStreams() | ||||||
| .stream() | ||||||
| .anyMatch(s -> s.logStreamName().equals(logStream)), | ||||||
| () -> | ||||||
| client | ||||||
| .createLogStream( | ||||||
| CreateLogStreamRequest.builder() | ||||||
| .logGroupName(logGroup) | ||||||
| .logStreamName(logStream) | ||||||
| .build()) | ||||||
| .join(), | ||||||
| "stream", | ||||||
| logStream); | ||||||
| } | ||||||
|
|
||||||
| private static void ensureResourceExists( | ||||||
| Supplier<Boolean> existsCheck, | ||||||
| Runnable createAction, | ||||||
| String resourceType, | ||||||
| String resourceName) { | ||||||
| if (existsCheck.get()) { | ||||||
| LOGGER.debug("Log {} [{}] already exists", resourceType, resourceName); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No - the |
||||||
| } else { | ||||||
| LOGGER.debug("Attempting to create log {}: {}", resourceType, resourceName); | ||||||
| createAction.run(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @PreDestroy | ||||||
| void shutdown() { | ||||||
| if (client != null) { | ||||||
| client.close(); | ||||||
singhpk234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| client = null; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| protected void transformAndSendEvent(HashMap<String, Object> properties) { | ||||||
| properties.put("realm_id", callContext.getRealmContext().getRealmIdentifier()); | ||||||
| properties.put("principal", securityContext.getUserPrincipal().getName()); | ||||||
singhpk234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| properties.put( | ||||||
| "activated_roles", ((PolarisPrincipal) securityContext.getUserPrincipal()).getRoles()); | ||||||
| // TODO: Add request ID when it is available | ||||||
| String eventAsJson; | ||||||
| try { | ||||||
| eventAsJson = objectMapper.writeValueAsString(properties); | ||||||
| } catch (JsonProcessingException e) { | ||||||
| LOGGER.error("Error processing event into JSON string: ", e); | ||||||
singhpk234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| LOGGER.debug("Failed to convert the following object into JSON string: {}", properties); | ||||||
| return; | ||||||
| } | ||||||
| InputLogEvent inputLogEvent = | ||||||
| InputLogEvent.builder().message(eventAsJson).timestamp(clock.millis()).build(); | ||||||
| PutLogEventsRequest.Builder requestBuilder = | ||||||
| PutLogEventsRequest.builder() | ||||||
| .logGroupName(logGroup) | ||||||
| .logStreamName(logStream) | ||||||
| .logEvents(List.of(inputLogEvent)); | ||||||
| CompletableFuture<PutLogEventsResponse> future = | ||||||
| client | ||||||
| .putLogEvents(requestBuilder.build()) | ||||||
| .whenComplete( | ||||||
| (resp, err) -> { | ||||||
| if (err != null) { | ||||||
| LOGGER.error( | ||||||
| "Error writing log to CloudWatch. Event: {}, Error: ", inputLogEvent, err); | ||||||
| } | ||||||
| }); | ||||||
| if (synchronousMode) { | ||||||
| future.join(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional] wondering if we need catalog name too in this ? lets say i have a same namespace and table in the different catalogs in the realm ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this but we should put this as part of the instrumentation of the event itself. I will look into this for #2480.