Skip to content

Commit

Permalink
adding more details about lambda function into the sentry context
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Oct 21, 2020
1 parent d213b07 commit 941d52d
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 19 deletions.
83 changes: 83 additions & 0 deletions examples/micronaut-log4aws-demo/micronaut-log4aws-demo.gradle
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
}

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);
}

}
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 examples/micronaut-log4aws-demo/src/main/resources/log4j2.xml
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>
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
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)
}

}
17 changes: 11 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
# limitations under the License.
#

# TODO: replace
slug=agorapulse/micronaut-log4aws
group=com.agorapulse
kordampPluginVersion=0.31.2
coverallsPluginVersion=2.9.0
gitPublishPluginVersion=2.1.3
slug = agorapulse/micronaut-log4aws
group = com.agorapulse

kordampPluginVersion = 0.31.2
coverallsPluginVersion = 2.9.0
gitPublishPluginVersion = 2.1.3

micronautVersion = 1.3.7
sentryVersion = 1.7.30
log4jVersion = 2.13.3
awsLog4jVersion = 1.2.0
systemRulesVersion = 1.19.0
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ buildscript {
force = true
}
classpath 'org.ajoberstar.grgit:grgit-core:4.0.1'
classpath 'jp.classmethod.aws:gradle-aws-plugin:0.41'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
}
}

apply plugin: 'org.kordamp.gradle.settings'

projects {
directories = ['subprojects', 'docs']
directories = ['subprojects', 'docs', 'examples']
}

rootProject.name = 'micronaut-log4aws-root'
18 changes: 10 additions & 8 deletions subprojects/micronaut-log4aws/micronaut-log4aws.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ config {
}

dependencies {
annotationProcessor platform('io.micronaut:micronaut-bom:1.2.8')
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 platform('io.micronaut:micronaut-bom:1.2.8')
compile 'io.sentry:sentry-log4j2:1.7.29'
compile group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: '1.2.0'
compile 'org.apache.logging.log4j:log4j-slf4j18-impl:2.13.3'
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"
compileOnly "io.micronaut:micronaut-inject-groovy"

testAnnotationProcessor platform('io.micronaut:micronaut-bom:1.2.8')

testAnnotationProcessor "io.micronaut:micronaut-inject-java"

testCompile platform('io.micronaut:micronaut-bom:1.2.8')
testCompile "io.micronaut:micronaut-inject-groovy"
testCompile 'com.github.stefanbirkner:system-rules:1.19.0'
testCompile "com.github.stefanbirkner:system-rules:$systemRulesVersion"
testCompile("org.spockframework:spock-core") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}
Expand Down
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"));
}

}

0 comments on commit 941d52d

Please sign in to comment.