پرش به مطلب اصلی

Go

نصب​

go get github.com/getsentry/sentry-go

مقداردهی اولیه​

package main

import (
"log"
"time"

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

func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://<public_key>@<your-errora-host>/<project_id>",
Environment: "production",
Release: "myapp@1.4.2",
EnableTracing: true,
TracesSampleRate: 0.2,
ProfilesSampleRate: 1.0,
AttachStacktrace: true,
SendDefaultPII: false,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// پیش از پایان برنامه، رویدادهای بافرشده را تخلیه کنید.
defer sentry.Flush(2 * time.Second)

run()
}

در Go فراخوانی Flush مهم است: انتقال داده ناهم‌زمان است، پس پروسه‌ای که بلافاصله پس از یک panic خارج می‌شود ممکن است رویداد را از دست بدهد.

net/http​

sentryHandler := sentryhttp.New(sentryhttp.Options{Repanic: true})
http.Handle("/", sentryHandler.Handle(mux))

برای Gin از sentrygin.New(...)، برای Echo از sentryecho.New(...) و برای Fiber از sentryfiber.New(...) استفاده کنید.

ثبت دستی​

if err != nil {
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetTag("area", "ledger")
scope.SetContext("order", map[string]any{"id": order.ID})
sentry.CaptureException(err)
})
}

defer func() {
if r := recover(); r != nil {
sentry.CurrentHub().Recover(r)
sentry.Flush(2 * time.Second)
panic(r)
}
}()

اسپن‌های ردیابی​

ctx := context.Background()
span := sentry.StartSpan(ctx, "task", sentry.WithTransactionName("nightly-settlement"))
defer span.Finish()

child := sentry.StartSpan(span.Context(), "db.query")
// … کار …
child.Finish()

چک‌این کران​

checkinID := sentry.CaptureCheckIn(&sentry.CheckIn{
MonitorSlug: "nightly-settlement",
Status: sentry.CheckInStatusInProgress,
}, &sentry.MonitorConfig{
Schedule: sentry.CrontabSchedule("0 3 * * *"),
CheckInMargin: 5,
MaxRuntime: 30,
Timezone: "Asia/Tehran",
})

// … اجرای جاب …

sentry.CaptureCheckIn(&sentry.CheckIn{
ID: *checkinID,
MonitorSlug: "nightly-settlement",
Status: sentry.CheckInStatusOK,
}, nil)

نگاه کنید به مانیتورینگ کران.

بررسی نهایی​

sentry.CaptureException(errors.New("Errora smoke test"))
sentry.Flush(2 * time.Second)