-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (55 loc) · 1.63 KB
/
main.go
File metadata and controls
63 lines (55 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"log"
"math/rand/v2"
"net/http"
"strconv"
"strings"
"time"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
)
func main() {
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://963b4d3c413a1d71dfb5bb25f2712659@o4509204366491648.ingest.us.sentry.io/4509204371668992",
TracesSampleRate: 1.0,
}); err != nil {
fmt.Printf("Sentry initialization failed: %v\n", err)
}
defer sentry.Flush(2 * time.Second)
// Create an instance of sentryhttp
sentryHandler := sentryhttp.New(sentryhttp.Options{})
// Once it's done, you can set up routes and attach the handler as one of your middleware
http.HandleFunc("/guess/", sentryHandler.HandleFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
hub := sentry.GetHubFromContext(ctx)
guessStr := strings.TrimPrefix(r.URL.Path, "/guess/")
var guess int
hub.WithScope(func(scope *sentry.Scope) {
scope.SetTag("phase", "parse")
time.Sleep(1 * time.Second)
guessInt, err := strconv.ParseInt(guessStr, 10, 32)
if err != nil {
panic(err)
}
guess = int(guessInt)
})
target := rand.N(10)
hub.AddBreadcrumb(&sentry.Breadcrumb{
Message: fmt.Sprintf("Target: %d", target),
}, nil)
log.Println(guess, target)
if guess == target {
fmt.Fprintf(rw, "You guessed the number! %d", target)
} else {
hub.CaptureException(fmt.Errorf("wrong guess"))
http.Error(rw, "You guessed so wrong!", http.StatusBadRequest)
}
}))
fmt.Println("Listening and serving HTTP on :3000")
// And run it
if err := http.ListenAndServe(":3000", nil); err != nil {
panic(err)
}
}