-
Notifications
You must be signed in to change notification settings - Fork 293
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
Open
adnanhemani
wants to merge
37
commits into
apache:main
Choose a base branch
from
adnanhemani:ahemani/cloudwatch_event_listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
37 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 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
44 changes: 44 additions & 0 deletions
44
.../src/main/java/org/apache/polaris/service/events/jsonEventListener/JsonEventListener.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,44 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Abstract base class from which all event sinks that output events in JSON format can extend. | ||
* | ||
* <p>This class provides a common framework for transforming Polaris events into JSON format and | ||
* sending them to various destinations. Concrete implementations should override the {@link | ||
* #transformAndSendEvent(HashMap)} method to define how the JSON event data should be transmitted | ||
* or stored. | ||
*/ | ||
public abstract class JsonEventListener 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(); | ||
} |
165 changes: 165 additions & 0 deletions
165
...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,165 @@ | ||
/* | ||
* 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.concurrent.CompletionException; | ||
import org.apache.polaris.core.context.CallContext; | ||
import org.apache.polaris.service.events.jsonEventListener.JsonEventListener; | ||
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.CreateLogGroupResponse; | ||
import software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamRequest; | ||
import software.amazon.awssdk.services.cloudwatchlogs.model.CreateLogStreamResponse; | ||
import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent; | ||
import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest; | ||
import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsResponse; | ||
import software.amazon.awssdk.services.cloudwatchlogs.model.ResourceAlreadyExistsException; | ||
|
||
@ApplicationScoped | ||
@Identifier("aws-cloudwatch") | ||
public class AwsCloudWatchEventListener extends JsonEventListener { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AwsCloudWatchEventListener.class); | ||
private 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) { | ||
this.logStream = config.awsCloudwatchlogStream(); | ||
this.logGroup = config.awsCloudwatchlogGroup(); | ||
this.region = Region.of(config.awsCloudwatchRegion()); | ||
this.synchronousMode = config.synchronousMode(); | ||
this.clock = clock; | ||
} | ||
|
||
@PostConstruct | ||
void start() { | ||
this.client = createCloudWatchAsyncClient(); | ||
ensureLogGroupAndStream(); | ||
} | ||
|
||
protected CloudWatchLogsAsyncClient createCloudWatchAsyncClient() { | ||
return CloudWatchLogsAsyncClient.builder().region(region).build(); | ||
} | ||
|
||
private void ensureLogGroupAndStream() { | ||
try { | ||
CompletableFuture<CreateLogGroupResponse> future = | ||
client.createLogGroup(CreateLogGroupRequest.builder().logGroupName(logGroup).build()); | ||
future.join(); | ||
adnanhemani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (CompletionException e) { | ||
if (e.getCause() instanceof ResourceAlreadyExistsException) { | ||
LOGGER.debug("Log group {} already exists", logGroup); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
try { | ||
CompletableFuture<CreateLogStreamResponse> future = | ||
client.createLogStream( | ||
CreateLogStreamRequest.builder() | ||
.logGroupName(logGroup) | ||
.logStreamName(logStream) | ||
.build()); | ||
future.join(); | ||
} catch (CompletionException e) { | ||
if (e.getCause() instanceof ResourceAlreadyExistsException) { | ||
LOGGER.debug("Log stream {} already exists", logStream); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
@PreDestroy | ||
void shutdown() { | ||
if (client != null) { | ||
client.close(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void transformAndSendEvent(HashMap<String, Object> properties) { | ||
properties.put("realm", callContext.getRealmContext().getRealmIdentifier()); | ||
properties.put("principal", securityContext.getUserPrincipal().getName()); | ||
// 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); | ||
return; | ||
} | ||
InputLogEvent inputLogEvent = createLogEvent(eventAsJson, getCurrentTimestamp()); | ||
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(); | ||
} | ||
} | ||
|
||
private long getCurrentTimestamp() { | ||
return clock.millis(); | ||
} | ||
|
||
private InputLogEvent createLogEvent(String eventAsJson, long timestamp) { | ||
return InputLogEvent.builder().message(eventAsJson).timestamp(timestamp).build(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...ce/quarkus/events/jsonEventListener/aws/cloudwatch/QuarkusAwsCloudWatchConfiguration.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,99 @@ | ||
/* | ||
* 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.quarkus.events.jsonEventListener.aws.cloudwatch; | ||
|
||
import io.quarkus.runtime.annotations.StaticInitSafe; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
import io.smallrye.config.WithName; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.apache.polaris.service.events.jsonEventListener.aws.cloudwatch.AwsCloudWatchConfiguration; | ||
|
||
/** | ||
* Quarkus-specific configuration interface for AWS CloudWatch event listener integration. | ||
* | ||
* <p>This interface extends the base {@link AwsCloudWatchConfiguration} and provides | ||
* Quarkus-specific configuration mappings for AWS CloudWatch logging functionality. | ||
*/ | ||
@StaticInitSafe | ||
@ConfigMapping(prefix = "polaris.event-listener.aws-cloudwatch") | ||
@ApplicationScoped | ||
public interface QuarkusAwsCloudWatchConfiguration extends AwsCloudWatchConfiguration { | ||
eric-maynard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Returns the AWS CloudWatch log group name for event logging. | ||
* | ||
* <p>The log group is a collection of log streams that share the same retention, monitoring, and | ||
* access control settings. If not specified, defaults to "polaris-cloudwatch-default-group". | ||
* | ||
* <p>Configuration property: {@code polaris.event-listener.aws-cloudwatch.log-group} | ||
* | ||
* @return an Optional containing the log group name, or empty if not configured | ||
adnanhemani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
@WithName("log-group") | ||
@WithDefault("polaris-cloudwatch-default-group") | ||
@Override | ||
String awsCloudwatchlogGroup(); | ||
|
||
/** | ||
* Returns the AWS CloudWatch log stream name for event logging. | ||
* | ||
* <p>A log stream is a sequence of log events that share the same source. Each log stream belongs | ||
* to one log group. If not specified, defaults to "polaris-cloudwatch-default-stream". | ||
* | ||
* <p>Configuration property: {@code polaris.event-listener.aws-cloudwatch.log-stream} | ||
* | ||
* @return an Optional containing the log stream name, or empty if not configured | ||
*/ | ||
@WithName("log-stream") | ||
@WithDefault("polaris-cloudwatch-default-stream") | ||
@Override | ||
String awsCloudwatchlogStream(); | ||
|
||
/** | ||
* Returns the AWS region where CloudWatch logs should be sent. | ||
* | ||
* <p>This specifies the AWS region for the CloudWatch service endpoint. The region must be a | ||
* valid AWS region identifier. If not specified, defaults to "us-east-1". | ||
* | ||
* <p>Configuration property: {@code polaris.event-listener.aws-cloudwatch.region} | ||
* | ||
* @return an Optional containing the AWS region, or empty if not configured | ||
*/ | ||
@WithName("region") | ||
@WithDefault("us-east-1") | ||
@Override | ||
String awsCloudwatchRegion(); | ||
|
||
/** | ||
* Returns the synchronous mode setting for CloudWatch logging. | ||
* | ||
* <p>When set to "true", log events are sent to CloudWatch synchronously, which may impact | ||
* application performance but ensures immediate delivery. When set to "false" (default), log | ||
* events are sent asynchronously for better performance. | ||
* | ||
* <p>Configuration property: {@code polaris.event-listener.aws-cloudwatch.synchronous-mode} | ||
* | ||
* @return a boolean value indicating the synchronous mode setting | ||
*/ | ||
@WithName("synchronous-mode") | ||
@WithDefault("false") | ||
@Override | ||
boolean synchronousMode(); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.