Spring Boot整合Sentry

Sentry 是一个日志平台,分为客户端和服务端,客户端(目前客户端有Python, PHP,C#, Ruby等多种语言)就嵌入在你的应用程序中间,程序出现异常就向服务端发送消息,服务端将消息记录到数据库中并提供一个web节目方便查看。

Sentry搭建

使用Docker方式运行,步骤:
git clone https://github.com/getsentry/self-hosted.git
cd self-hosted
sudo ./install.sh
docker compose up -d

Sentry中新建项目

登录Sentry,创建项目,项目类型选择java。
在这里插入图片描述
在项目的配置里可以,获取项目DSN,之后需要使用到。
在这里插入图片描述

集成SpringBoot

1. 添加依赖

pom.xml文件中添加Sentry的Spring Boot Starter依赖:

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-spring-boot-starter</artifactId>
    <version>7.14.0</version>
</dependency>

2. 配置Sentry

application.yml文件中添加Sentry的配置。你需要替换your-dsn为你的实际DSN(Data Source Name),可以从Sentry的项目设置中获取。

sentry:
  dsn: http://81c1d156c742567a99e115b8fa9b6395@192.168.1.77:9000/4

4. 日志集成(可选)

如果你想要将日志事件也发送到Sentry,你可以配置Spring Boot的日志框架。
添加依赖

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-logback</artifactId>
    <version>7.14.0</version> 
</dependency>

logback.xml中添加Sentry Appender:

<configuration>
  <appender name="SENTRY" class="ch.qos.logback.classic.sentry.SentryAppender">
    <minimumEventLevel>warn</minimumEventLevel>
    <minimumBreadcrumbLevel>info</minimumBreadcrumbLevel>
  </appender>

  <root level="INFO">
    <appender-ref ref="SENTRY" />
  </root>
</configuration>
  • minimumEventLevel用来指定warn级别会产生一次「Event」
  • minimumBreadcrumbLevel用来配置面包屑级别。

使用官方文档的日志级别配置,不起作用:

  • sentry.logging.minimum-event-level=info
  • sentry.logging.minimum-breadcrumb-level=debug

5. 测试Sentry集成

你可以通过手动触发一个异常来测试Sentry是否正确配置:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/test-error")
    public void test() {
    log.info("info, test throw exception");
    log.error("error, test throw exception");
    throw new IllegalArgumentException("hello world");
  }
}

在sentry查看异常是否被正确捕获:
在这里插入图片描述

6. 配置实时告警

国内使用钉钉,企业微信告警,Sentry不支持。但是支持WebHooks对接,可以自己写一个服务接受WebHooks的消息,然后转发到钉钉,或者企业微信。

配置Alert Settings

在项目的Alert Settings里配置 WEBHOOKS:
在这里插入图片描述

配置警报规则

在项目的Alert Settings,点击"View Alert Rules",编辑你的警报规则,把通知发给”WebHooks"
在这里插入图片描述

发送消息服务代码

在钉钉群里配置webhooks机器人,活动weebhooks 链接。
写一个springboot服务接受Sentry通过WebHooks发来的消息,然后转发给钉钉。

  1. SentryEvent Sentry消息
package com.alert.domain;

import com.fasterxml.jackson.annotation.JsonProperty;

public class SentryEvent {

  private String message;
  private String url;
  private String project;
  private String group;
  private String id;
  private String eventID;
  private String level;
  private String timestamp;
  private String logger;
  private String culprit;
  private String platform;
  private String modules;
  private String user;
  private String tags;
  private String fingerprint;
  private String logEntry;
  private String stacktrace;
  private String extra;
  private String context;
  private String threads;
  private String request;
  private String exceptions;
  private String debugMeta;

  @JsonProperty("message")
  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public String getLevel() {
    return level;
  }

  public void setLevel(String level) {
    this.level = level;
  }

  public String getTimestamp() {
    return timestamp;
  }

  public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
  }

  // 其他getter和setter方法
}
  1. restful服务
package com.alert.controller;

import com.wits.alert.domain.SentryEvent;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/alert" )
public class SentryWebhookController {

  // 设置钉钉机器人的Webhook URL
  String dingTalkUrl = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxx";


  @PostMapping(value = "/sentry", consumes = MediaType.APPLICATION_JSON_VALUE)
  public void handleSentryCrm(@RequestBody SentryEvent event, String name) {
    // 处理事件
    sendToDingTalk(event, name);
  }

  private void sendToDingTalk(SentryEvent event, String appName) {
    // 构建消息
    Map<String, Object> message = new HashMap<>();
    message.put("msgtype", "text");
    Map<String, String> textContent = new HashMap<>();
    textContent.put("content", appName + "告警: [" + event.getLevel() + "]" + event.getMessage() + "\n" + event.getUrl());
    message.put("text", textContent);

    // 发送请求
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Map<String, Object>> request = new HttpEntity<>(message, headers);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate.postForEntity(dingTalkUrl, request, String.class);

    // 检查响应
    if (response.getStatusCodeValue() == 200) {
      System.out.println("Message sent successfully to DingTalk.");
    } else {
      System.err.println("Failed to send message to DingTalk: " + response.getBody());
    }
  }
}

参照文档

https://docs.sentry.io//

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值