File: examples_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (30 lines) | stat: -rw-r--r-- 933 bytes parent folder | download | duplicates (3)
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
package errortracking_test

import (
	"net/http"

	"gitlab.com/gitlab-org/labkit/errortracking"
)

func ExampleCapture() {
	req, err := http.NewRequest("GET", "http://example.com", nil)
	ctx := req.Context()

	if err != nil {
		// Send the error to the error tracking system
		errortracking.Capture(err,
			errortracking.WithContext(ctx),                          // Extract details such as correlation-id from context
			errortracking.WithRequest(req),                          // Extract additional details from request
			errortracking.WithField("domain", "http://example.com"), // Add additional custom details
			errortracking.WithStackTrace(),                          // Attach the stack trace of up to 10 errors in the chain
		)
	}
}

func ExampleNewHandler() {
	handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
		panic("oh dear")
	})

	http.ListenAndServe(":1234", errortracking.NewHandler(handler))
}