LoginSignup
2
0

More than 1 year has passed since last update.

【Golang】Sentry (APMツール) の導入

Last updated at Posted at 2023-01-31

概要

GoのAPIプロジェクト上に、Sentry(APMツール)を導入します。

環境構築

前提

Goプロジェクト上で動作するため、環境を用意します。

shell
go mod init

アカウント作成

ホームページよりSentryのアカウントを作成します。
今回はTRIALで進めます。

Welcome to Sentry

今回は、SDKを使用するため「Install Sentry」を選択します。
スクリーンショット 2023-01-30 17.36.09.png

Select the platforms you want to monitor

Goを選択します。
スクリーンショット 2023-01-30 17.37.14.png

Prepare the Go SDK

SDKをインストールします。

shell
go get github.com/getsentry/sentry-go

動作確認を行うため、下部のコードをmain.goを作成し記述します。
Dsnは各自のURLを記述してください。

main.go
package main

import (
	"log"

	"github.com/getsentry/sentry-go"
)

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Dsn: "https://",
		// Set TracesSampleRate to 1.0 to capture 100%
		// of transactions for performance monitoring.
		// We recommend adjusting this value in production,
		TracesSampleRate: 1.0,
	})
	if err != nil {
		log.Fatalf("sentry.Init: %s", err)
	}
	// Flush buffered events before the program terminates.
	defer sentry.Flush(2 * time.Second)

	sentry.CaptureMessage("It works!")
}

スクリーンショット 2023-01-30 18.30.45.png

接続確認

先程のmain.goを実行します。

shell
go run main

IssuesにてCaptureMessageが確認できます。
スクリーンショット 2023-01-31 15.42.39.png

APM環境構築

APIのトランザクションを監視するため、先程のmain.goにWebサーバー部分を追記します。
今回はGoの標準ライブラリである net/http を使用します。

ドキュメント通りに進めます。

net/http用のSDKをインストールします。

shell
go get github.com/getsentry/sentry-go/http

ドキュメントのUsageを参考にコードに追記し、実行します。

main.go
package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/getsentry/sentry-go"
	sentryhttp "github.com/getsentry/sentry-go/http"
)

type handler struct{}

func (h *handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	if hub := sentry.GetHubFromContext(r.Context()); hub != nil {
		hub.WithScope(func(scope *sentry.Scope) {
			scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
			hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
		})
	}
	rw.WriteHeader(http.StatusOK)
}

func enhanceSentryEvent(handler http.HandlerFunc) http.HandlerFunc {
	return func(rw http.ResponseWriter, r *http.Request) {
		if hub := sentry.GetHubFromContext(r.Context()); hub != nil {
			hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
		}
		handler(rw, r)
	}
}

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Dsn: "https://",
        EnableTracing: true,
		// Set TracesSampleRate to 1.0 to capture 100%
		// of transactions for performance monitoring.
		// We recommend adjusting this value in production,
		TracesSampleRate: 1.0,
	})
	if err != nil {
		log.Fatalf("sentry.Init: %s", err)
	}
	// Flush buffered events before the program terminates.
	defer sentry.Flush(2 * time.Second)

	sentry.CaptureMessage("It works!")

	sentryHandler := sentryhttp.New(sentryhttp.Options{
		Repanic: true,
	})

	http.Handle("/", sentryHandler.Handle(&handler{}))
	http.HandleFunc("/foo", sentryHandler.HandleFunc(
		enhanceSentryEvent(func(rw http.ResponseWriter, r *http.Request) {
			panic("y tho")
		}),
	))

	fmt.Println("Listening and serving HTTP on :3000")

	if err := http.ListenAndServe(":3000", nil); err != nil {
		panic(err)
	}

実行後、トランザクションを確認したため、それぞれにアクセスします。

監視

リクエストの詳細,パフォーマンスはPerformanceにて確認できます。

Performanceでは、自分が監視できるすべてのリクエストを一覧として見ることができます。
各エンドポイントの詳細を見たい場合は、画面下部から選択します。
スクリーンショット 2023-01-31 19.11.06.png

Transaction Summary

各エンドポイントのごとの詳細を確認できます。
リクエスト単位で詳細を見たい場合は、EVENTを選択します。

スクリーンショット 2023-01-31 19.13.09.png

Event Details

リクエストの詳細を見ることができます。
ボトルネックとなっている箇所に関して、炎のマークにて確認できます。
今回は、main.goのみのAPIでしたが、外部APIやDBを利用するプロジェクトだと、そちらも確認することができます。

スクリーンショット 2023-01-31 19.15.50.png

さいごに

Go+Sentryの導入記事が少なかったため、作成しました。
日々の開発のお役に立てればと思います。

これ以外にもSentryは様々な監視を行えます。ぜひ試してみてください!!

(今回は標準ライブラリを使用しましたが、ドキュメントを見る限りginechoと言ったフレームワークにも対応しています。
しかし、接続は可能でしたが、APIのパフォーマンスを監視することができませんでした。
そのため、できた方がいらっしゃいましたら、コメントに教えていただけると幸いです。)

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0