-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding more details about lambda function into the sentry context
- Loading branch information
Showing
11 changed files
with
333 additions
and
19 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
examples/micronaut-log4aws-demo/micronaut-log4aws-demo.gradle
This file contains 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,83 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020 Agorapulse. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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. | ||
*/ | ||
plugins { | ||
id 'groovy' | ||
id "jp.classmethod.aws.lambda" | ||
id "com.github.johnrengelman.shadow" | ||
} | ||
|
||
config { | ||
bintray { | ||
enabled = true | ||
} | ||
} | ||
|
||
dependencies { | ||
compile project(':micronaut-log4aws') | ||
|
||
annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion") | ||
compile platform("io.micronaut:micronaut-bom:$micronautVersion") | ||
testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion") | ||
testCompile platform("io.micronaut:micronaut-bom:$micronautVersion") | ||
|
||
annotationProcessor "io.micronaut:micronaut-inject-java" | ||
|
||
compile "io.sentry:sentry-log4j2:$sentryVersion" | ||
compile group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: awsLog4jVersion | ||
compile "org.apache.logging.log4j:log4j-slf4j18-impl:$log4jVersion" | ||
|
||
compile "io.micronaut:micronaut-core" | ||
compile "io.micronaut:micronaut-function" | ||
|
||
compileOnly "io.micronaut:micronaut-inject-groovy" | ||
|
||
|
||
|
||
testAnnotationProcessor "io.micronaut:micronaut-inject-java" | ||
|
||
testCompile "io.micronaut:micronaut-inject-groovy" | ||
testCompile "com.github.stefanbirkner:system-rules:$systemRulesVersion" | ||
testCompile("org.spockframework:spock-core") { | ||
exclude group: "org.codehaus.groovy", module: "groovy-all" | ||
} | ||
|
||
} | ||
|
||
|
||
shadowJar { | ||
mergeServiceFiles() | ||
mergeGroovyExtensionModules() | ||
transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer) | ||
} | ||
|
||
aws { | ||
profileName = 'beta' | ||
region = 'eu-west-1' | ||
} | ||
|
||
task deploy(type: jp.classmethod.aws.gradle.lambda.AWSLambdaMigrateFunctionTask, dependsOn: shadowJar) { | ||
functionName = "MicronautLog4AwsDemo" | ||
handler = "com.agorapulse.micronaut.log4aws.demo.LoggingDemo::apply" | ||
role = "arn:aws:iam::${aws.accountId}:role/lambda-basic-role" | ||
runtime = com.amazonaws.services.lambda.model.Runtime.Java8 | ||
zipFile = shadowJar.archivePath | ||
memorySize = 1024 | ||
timeout = 300 | ||
publish = true | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
...cronaut-log4aws-demo/src/main/java/com/agorapulse/micronaut/log4aws/demo/LoggingDemo.java
This file contains 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,23 @@ | ||
package com.agorapulse.micronaut.log4aws.demo; | ||
|
||
import io.micronaut.function.FunctionBean; | ||
import io.micronaut.function.executor.FunctionInitializer; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
@FunctionBean( | ||
method = "apply", | ||
name = "logging-demo" | ||
) | ||
public class LoggingDemo extends FunctionInitializer implements Function<Map<String, String>, String> { | ||
|
||
@Inject private LoggingService loggingService; | ||
|
||
@Override | ||
public String apply(Map<String, String> payload) { | ||
return loggingService.apply(payload); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...naut-log4aws-demo/src/main/java/com/agorapulse/micronaut/log4aws/demo/LoggingService.java
This file contains 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 @@ | ||
package com.agorapulse.micronaut.log4aws.demo; | ||
|
||
import com.agorapulse.micronaut.log4aws.LogError; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Singleton; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Singleton | ||
public class LoggingService { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingService.class); | ||
|
||
@LogError | ||
String apply(Map<String, String> payload) { | ||
if (payload.containsKey("trace")) { | ||
LOGGER.trace(payload.get("trace")); | ||
} | ||
|
||
if (payload.containsKey("debug")) { | ||
LOGGER.debug(payload.get("debug")); | ||
} | ||
|
||
if (payload.containsKey("warn")) { | ||
LOGGER.warn(payload.get("warn")); | ||
} | ||
|
||
if (payload.containsKey("error")) { | ||
LOGGER.error(payload.get("error"), new RuntimeException(payload.get("error"))); | ||
} | ||
|
||
if (payload.containsKey("env")) { | ||
LOGGER.debug(System.getenv().entrySet().stream().map(e -> e.getKey() + ": " + e.getValue()).collect(Collectors.joining("\n"))); | ||
} | ||
|
||
if (payload.containsKey("throw")) { | ||
throw new RuntimeException(payload.get("throw")); | ||
} | ||
return "nothing to do"; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
examples/micronaut-log4aws-demo/src/main/resources/log4j2.xml
This file contains 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,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright 2020 Agorapulse. | ||
Licensed 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 | ||
https://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. | ||
--> | ||
<Configuration packages="org.apache.logging.log4j.core,io.sentry.log4j2,com.amazonaws.services.lambda.runtime.log4j2"> | ||
<Appenders> | ||
<Lambda name="Lambda"> | ||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %m%n"/> | ||
</Lambda> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> | ||
</Console> | ||
<Sentry name="Sentry"/> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="Lambda"/> | ||
<!-- Note that the Sentry logging threshold is overridden to the WARN level --> | ||
<AppenderRef ref="Sentry" level="warn"/> | ||
</Root> | ||
<Logger name="io.sentry" level="trace" additivity="false"> | ||
<AppenderRef ref="Console" /> | ||
</Logger> | ||
<Logger name="com.agorapulse.micronaut.log4aws" level="trace" additivity="false"> | ||
<AppenderRef ref="Console" /> | ||
</Logger> | ||
<Logger name="software.amazon" level="warn" additivity="false"> | ||
<AppenderRef ref="Console" /> | ||
</Logger> | ||
</Loggers> | ||
</Configuration> |
21 changes: 21 additions & 0 deletions
21
examples/micronaut-log4aws-demo/src/main/resources/sentry.properties
This file contains 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,21 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Copyright 2020 Agorapulse. | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
# | ||
|
||
async=false | ||
|
||
stacktrace.app.packages=com.agorapulse |
26 changes: 26 additions & 0 deletions
26
...4aws-demo/src/test/groovy/com/agorapulse/micronaut/log4aws/demo/LoggingServiceSpec.groovy
This file contains 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,26 @@ | ||
package com.agorapulse.micronaut.log4aws.demo | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Specification | ||
|
||
class LoggingServiceSpec extends Specification { | ||
|
||
@AutoCleanup ApplicationContext context = ApplicationContext.build().build().start() | ||
|
||
LoggingService service = context.getBean(LoggingService) | ||
|
||
void 'exception thrown'() { | ||
when: | ||
service.apply( | ||
trace: 'Testing Trace', | ||
debug: 'Testing Debug', | ||
warn: 'Testing Warning', | ||
error: 'Testing Error', | ||
throw: 'Testing Exception' | ||
) | ||
then: | ||
thrown(RuntimeException) | ||
} | ||
|
||
} |
This file contains 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 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 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
21 changes: 21 additions & 0 deletions
21
...t-log4aws/src/main/groovy/com/agorapulse/micronaut/log4aws/AwsLambdaEventBuildHelper.java
This file contains 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,21 @@ | ||
package com.agorapulse.micronaut.log4aws; | ||
|
||
import io.sentry.event.EventBuilder; | ||
import io.sentry.event.helper.EventBuilderHelper; | ||
|
||
public class AwsLambdaEventBuildHelper implements EventBuilderHelper { | ||
|
||
@Override | ||
public void helpBuildingEvent(EventBuilder eventBuilder) { | ||
eventBuilder.withTag("aws_region", System.getenv("AWS_REGION")); | ||
eventBuilder.withTag("aws_default_region", System.getenv("AWS_DEFAULT_REGION")); | ||
eventBuilder.withTag("lambda_function_name", System.getenv("AWS_LAMBDA_FUNCTION_NAME")); | ||
eventBuilder.withTag("lambda_function_version", System.getenv("AWS_LAMBDA_FUNCTION_VERSION")); | ||
eventBuilder.withTag("lambda_handler", System.getenv("_HANDLER")); | ||
eventBuilder.withTag("lambda_execution_environment", System.getenv("AWS_EXECUTION_ENV")); | ||
eventBuilder.withTag("lambda_log_group_name", System.getenv("AWS_LAMBDA_LOG_GROUP_NAME")); | ||
eventBuilder.withTag("lambda_log_stream_name", System.getenv("AWS_LAMBDA_LOG_STREAM_NAME")); | ||
eventBuilder.withTag("lambda_memory_size", System.getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")); | ||
} | ||
|
||
} |
Oops, something went wrong.