简体   繁体   English

Sentry SDK Java 不适用于 aws lambda

[英]Sentry SDK Java is not working on aws lambda

I am trying to use sentry in java based aws lambda.我正在尝试在基于 Java 的 aws lambda 中使用哨兵。 I am trying to used sentry v3.1.1 in a simple lambda which captures exception as below.我试图在一个简单的 lambda 中使用 sentry v3.1.1 ,它捕获如下异常。 The problem is问题是

  • it works fine when run locally using main method , and event appears on sentry issues ui works fine when run locally using main methodworks fine when run locally using main method ,并且事件出现在哨兵问题上
  • but it doesn't work when invoked in as aws lambda and event never appears on sentry ui.但是doesn't work when invoked in as aws lambdadoesn't work when invoked in as aws lambda并且事件永远不会出现在哨兵 ui 上。

SentryLambda.java 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 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 sentry.properties

dsn=...removed intentionally...
environment=development
release=0.0.1-SNAPSHOT
stacktrace.app.packages=playground.demo

samp-template.tml 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 ...

I also tried creating aws lambda with vpc config and internet access but did not succeed Can anyone please guide me why it's not working and how can I make it work?我还尝试使用 vpc 配置和互联网访问创建 aws lambda,但没有成功任何人都可以指导我为什么它不工作,我该如何使它工作?

PS Before all this, I tried working with sentry-log4j2 and sentry-logback , and eventually reached to simple sentry-java . PS在这之前,我尝试使用sentry-log4j2sentry-logback ,最终达到了简单的sentry-java They all work locally but fail on aws lambda它们都在本地工作,但在 aws lambda 上失败


[Update: Nov 02, 2020] [更新:2020 年 11 月 2 日]

As @Manoel suggested to set debug flag, here's the updated code and logs.正如@Manoel 建议设置调试标志,这里是更新的代码和日志。

SentryLambda.java SentryLambda.java

    ...
    Sentry.init(options -> {
      options.setDebug(true);
      options.setDsn("...");
      options.setEnvironment("development");
      options.setRelease("0.0.1-SNAPSHOT");
    });
    ...

Logs日志

/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

could you enable the debug mode and copy-paste the logs?你能启用debug模式并复制粘贴日志吗?

Sentry.init(options -> {
  options.setDebug(true);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM