【发布时间】:2021-02-13 08:14:04
【问题描述】:
我正在尝试在基于 java 的 aws lambda 中使用哨兵。我试图在一个简单的 lambda 中使用哨兵v3.1.1,它捕获如下异常。问题是
-
works fine when run locally using main method,事件出现在哨兵问题 ui 上 - 但它
doesn't work when invoked in as aws lambda和事件永远不会出现在哨兵用户界面上。
SentryLambda.java
package playground.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import io.sentry.Sentry;
public class SentryLambda implements RequestHandler<String, String> {
@Override
public String handleRequest(String input, Context context) {
Sentry.init();
Sentry.captureException(new Exception("Test Sentry"));
return "Returned " + input;
}
public static void main(String[] args) {
SentryLambda sentryLambda = new SentryLambda();
sentryLambda.handleRequest("Requested String", null);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>playground.demo</groupId>
<artifactId>aws-lambda-sentry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<finalName>${project.name}-${project.version}-jar-with-dependencies</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
sentry.properties
dsn=...removed intentionally...
environment=development
release=0.0.1-SNAPSHOT
stacktrace.app.packages=playground.demo
samp-template.tml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sentry Lambda
Resources:
SentryLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: sentryLambda
Handler: playground.demo.SentryLambda
Runtime: java8
CodeUri: target/aws-lambda-sentry-0.0.1-SNAPSHOT-jar-with-dependencies.jar
MemorySize: 512
Timeout: 15
AutoPublishAlias: DEFAULT
Role: ... a role with AWSLambdaFullAccess policy ...
我还尝试使用 vpc 配置和互联网访问创建 aws lambda,但没有成功 谁能指导我为什么它不起作用以及如何使它起作用?
附言 在这之前,我尝试使用sentry-log4j2 和sentry-logback,最终达到了简单的sentry-java。他们都在本地工作,但在 aws lambda 上失败
[更新:2020 年 11 月 2 日]
正如@Manoel 建议设置调试标志,这里是更新的代码和日志。
SentryLambda.java
...
Sentry.init(options -> {
options.setDebug(true);
options.setDsn("...");
options.setEnvironment("development");
options.setRelease("0.0.1-SNAPSHOT");
});
...
日志
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a START RequestId: b3f6c0f5-3a89-4f62-8994-7d4b198351e5 Version: $LATEST
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a INFO: Initializing SDK with DSN: '...'
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a INFO: No outbox dir path is defined in options.
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a INFO: GlobalHubMode: 'false'
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a DEBUG: UncaughtExceptionHandlerIntegration enabled: true
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a DEBUG: UncaughtExceptionHandlerIntegration installed.
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a DEBUG: Capturing event: ff1def349ab34a5eb151d52e7dd72425
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a INFO: Session is null on scope.withSession
/aws/lambda/sentryLambda 2020/11/02/[$LATEST]da849aebc90744409bbc11f98ac49c2a END RequestId: b3f6c0f5-3a89-4f62-8994-7d4b198351e5
【问题讨论】:
-
从 AWS Lambda 发送事件确实有问题。我创建了一个问题:github.com/getsentry/sentry-java/issues/1023
标签: java aws-lambda sentry