본문 바로가기
개발 관련/환경 세팅

Springboot - Sentry 연동

by lazysnack 2022. 7. 15.

스프링 부트 프로젝트에서 에러 로그가 발생했을 시 센트리에 등록되고, 등록된 이슈를 슬렉으로 알람받을 수 있도록 합니다.

1. Sentry 프로젝트 생성하기

Project → Create Project

위와 같이 프로젝트를 생성하면, 의존성 추가 방법이나 사용 방법등이 나온 페이지가 나옵니다.

이제 의존성과 발급된 DSN을 스프링 프로젝트에 등록합니다.

2. Springboot 설정하기

센트리를 추가하고, 관련 설정은 logback을 통해 관리할 것입니다.

dependencies {
	implementation 'io.sentry:sentry-spring-boot-starter:5.7.0'
	implementation 'io.sentry:sentry-logback:5.7.0'
}

build.gradle 에 디펜던시 추가

<configuration>
  <!-- Configure the Console appender -->
  <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
  <appender name="Sentry" class="io.sentry.logback.SentryAppender">
    <options>
      <!-- NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard -->
      <dsn>${sentry.dsn}</dsn>
    </options>

    <!-- Optionally change minimum Event level. Default for Events is ERROR -->
    <minimumEventLevel>WARN</minimumEventLevel>
    <!-- Optionally change minimum Breadcrumbs level. Default for Breadcrumbs is INFO -->
    <minimumBreadcrumbLevel>DEBUG</minimumBreadcrumbLevel>
  </appender>

  <!-- Enable the Console and Sentry appenders, Console is provided as an example
of a non-Sentry logger that is set to a different logging threshold -->
  <!-- 각 환경별 로그 레벨 -->
  <springProfile name="local">
    <root level="INFO">
      <appender-ref ref="Console" />
    </root>
  </springProfile>

  <springProfile name="dev">
    <root level="INFO">
      <appender-ref ref="Console" />
      <appender-ref ref="Sentry" />
    </root>
  </springProfile>

  <springProfile name="stage">
    <root level="debug">
      <appender-ref ref="Console" />
      <appender-ref ref="Sentry" />
    </root>
  </springProfile>

  <springProfile name="prod">
    <root level="error">
      <appender-ref ref="Console" />
      <appender-ref ref="Sentry" />
    </root>
  </springProfile>
  <!-- 각 환경별 로그 레벨 END -->
</configuration>

main.resource 하위에 logback-spring.xml 파일 생성 후 위의 내용을 복사합니다.

sentry:
  dsn: https://~~~~~~~.ingest.sentry.io/111
  # Set traces-sample-rate to 1.0 to capture 100% of transactions for performance monitoring.
  # We recommend adjusting this value in production.
  traces-sample-rate: 1.0

application.yml 파일에 센트리 프로젝트를 추가하면서 발급된 DSN을 등록해줍니다. traces-sample-rate 는 에러 비율을 얼마나 캡쳐할 것인지에 대한 수치인데, 1.0이 100% 입니다, 100%이면 모든 에러를 캡쳐하지만 그만큼 속도나 트래픽등의 이슈가 있을 수 있으므로, 운영하면서 적절히 조절하면 될 것 같습니다.

 

이러면, springboot에서 할 일은 끝났습니다.

이제 센트리에서 발생한 이슈를 슬렉으로 받아보도록 하겠습니다.

3. Slack 으로 알람받기

 

 

Setting → Integrations 에서 슬렉을 선택하고, 워크스페이스를 추가해줍니다. (워크 스페이스를 추가하는 경우는 센트리를 완전 처음 적용하는 경우 이외에는 없기에 생략하겠습니다.)

 

 

Alerts → Create Alert
알럿을 생성할 프로젝트 선택

 

어떤 알럿을 받을지 선택합니다.

 

 

알럿 설정을 합니다.

  • Add alert settings
    1. Environment : 환경을 선택합니다.
    2. Team : 해당 알럿을 편집할 수 있는 팀을 선택합니다.
    3. Alert Name : 알럿 이름 입니다.
  • Set conditions
    1. When : 언제 알럿이 갈지 입니다. 위의 세팅같은 경우는 A new issue is created 입니다.
    2. If : 필터 조건입니다.
    3. Then : 이후에 어떤 행동을 할지 입니다. 슬렉으로 알람을 보낼 것이므로, Send a Slack notification 을 선택합니다.
      1. 워크스페이스를 선택하고, 알람을 받을 채널을 입력해줍니다.
  • Set action interval
    1. 알람 받을 인터벌을 설정합니다.

Save Rule 을 통해 설정한 알럿을 저장하면 슬렉에서 알람을 받아볼 수 있습니다.

(내용에 대해서는 블러 처리를 했습니다.)

4. 이외 설정 관련

  1. 에러가 발생해도, 센트리에 이슈가 등록이 안되는 경우가 있습니다. 이 경우 인프라쪽의 아웃바운드 설정을 확인합니다. (대부분 아웃바운드가 막혀서 센트리에 전송이 안되는 경우가 많습니다.)
  2. 현재 hospital-back은 gloabl 설정이며, 로그를 log.error() 로 찍기 때문에 에러가 아님에도 센트리에 등록이 됩니다. 로그에 대한 정책을 정리할 필요가 있습니다.
  3. TBD?

참고)

'개발 관련 > 환경 세팅' 카테고리의 다른 글

Git Action 으로 배포시 Slack 알람 받기  (0) 2022.07.14
mariaDB 설치 (feat. Docker)  (0) 2022.07.14
nGrinder 부하 테스트  (0) 2022.07.14
아파치 설정  (0) 2022.07.14