Go error tracking installation
Contents
- 1
Install the Go SDK
RequiredInstall the PostHog Go SDK:
TerminalSource context not yet supportedThe Go SDK captures stack traces with file names, line numbers, and function names, but does not yet support source context (displaying the surrounding lines of code in the error tracking UI). Symbol set uploads for Go are not currently available.
- 2
Initialize the client
RequiredGo - 3
Capture exceptions
RequiredThere are two ways to capture exceptions with the Go SDK:
Option A: Direct capture
Use
NewDefaultExceptionto capture errors directly. This automatically generates a UUID and stack trace for you.GoFor more control, build the
Exceptionstruct manually:GoOption B: Automatic capture with slog
The SDK provides a
SlogCaptureHandlerthat wraps Go's standardlog/sloglogger and automatically captures log records as exceptions.By default, it captures logs at
Warninglevel and above.GoThe handler supports several configuration options:
Option Description Default WithMinCaptureLevel(level)Minimum log level to capture slog.LevelWarnWithDistinctIDFn(fn)Function to extract distinct ID from context/record Returns ""(skips capture)WithFingerprintFn(fn)Custom fingerprint for error grouping nil(PostHog assigns)WithSkip(n)Stack frames to skip 5WithStackTraceExtractor(e)Custom stack trace extractor DefaultStackTraceExtractorWithDescriptionExtractor(e)Custom description extractor ErrorExtractorError extractionThe slog handler automatically extracts error descriptions from log attributes with keys
errorerror(case-insensitive). It also supports wrapped errors via theUnwrap()interface. - 4
Verify error tracking
RecommendedTrigger a test exception to confirm events are being sent to PostHog. You should see them appear in the activity feed.
Go