54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
)
|
|
|
|
func main() {
|
|
// Check for the right number of command line arguments
|
|
if len(os.Args) != 2 {
|
|
fmt.Println("Usage: ./go-glitch \"Your log message here\"")
|
|
os.Exit(1)
|
|
}
|
|
|
|
logMessage := os.Args[1]
|
|
|
|
dsn := os.Getenv("SENTRY_DSN")
|
|
if dsn == "" {
|
|
fmt.Println("Error: SENTRY_DSN environment variable is not set.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Initialize Sentry with the DSN
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
Dsn: dsn,
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Error initializing Sentry: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Defer a function to flush Sentry's buffer
|
|
defer func() {
|
|
success := sentry.Flush(5 * time.Second)
|
|
if !success {
|
|
fmt.Println("Failed to flush Sentry buffer within the expected time.")
|
|
} else {
|
|
fmt.Println("Sentry buffer flushed successfully.")
|
|
}
|
|
}()
|
|
|
|
// Capture the message and output the event ID
|
|
eventID := sentry.CaptureMessage(logMessage)
|
|
if eventID != nil {
|
|
fmt.Printf("Sent message to Sentry with event ID: %s\n", *eventID)
|
|
} else {
|
|
fmt.Println("Failed to send message to Sentry.")
|
|
}
|
|
}
|
|
|