Documentation
¶
Overview ¶
Package errorgap is the Go notifier for the Errorgap error-tracking platform. Use Init to configure the package-level default client and Notify / Flush / Close as the simple, package-level entry points.
For libraries or apps that want isolated state (e.g. tests), instantiate a Client directly with NewClient.
Index ¶
- Constants
- Variables
- func Close(ctx context.Context) error
- func FilterParams(params map[string]any, filterKeys []string) map[string]any
- func Flush(ctx context.Context) error
- func Init(cfg Config) error
- func NormalizeSQL(sql string) string
- func RecordDatabase(ctx context.Context, sql string, duration time.Duration)
- func RecordExternal(ctx context.Context, duration time.Duration)
- func Recover()
- func TrackJob(ctx context.Context, jobClass, queue string, ...) error
- type Client
- func (c *Client) Close(ctx context.Context) error
- func (c *Client) Config() Config
- func (c *Client) Flush(ctx context.Context) error
- func (c *Client) Notify(err error, opts ...NoticeOptions) Result
- func (c *Client) NotifyLog(message, level, source string) Result
- func (c *Client) NotifyTransaction(transaction Transaction) Result
- func (c *Client) TrackJob(ctx context.Context, jobClass, queue string, ...) error
- type Config
- type ErrorEntry
- type Frame
- type LogEntry
- type Notice
- type NoticeOptions
- type Result
- type SlogHandler
- type SourceExcerpt
- type Span
- type SpanCollector
- type Transaction
Constants ¶
const Version = "0.2.0"
Version is the SDK version, embedded in every notice's User-Agent header.
Variables ¶
var ( // ErrMissingProjectSlug is returned from validation when ProjectSlug // is empty. ErrMissingProjectSlug = errors.New("errorgap: ProjectSlug is required") // ErrMissingEndpoint is returned from validation when Endpoint is // empty. ErrMissingEndpoint = errors.New("errorgap: Endpoint is required") )
var DefaultFilterKeys = []string{
"password",
"password_confirmation",
"token",
"secret",
"api_key",
"authorization",
"cookie",
}
DefaultFilterKeys are matched (case-insensitive substring) against param keys to mask sensitive values before delivery.
Functions ¶
func FilterParams ¶
FilterParams masks sensitive keys (case-insensitive substring match) in a params map. Nested maps are walked; arrays/slices are not recursed into.
func Init ¶
Init configures the package-level default client. Subsequent calls replace the existing default. The previous client is closed in the background so in-flight deliveries still finish.
func NormalizeSQL ¶ added in v0.2.0
NormalizeSQL replaces string and numeric literals so equivalent queries aggregate into one APM row.
func RecordDatabase ¶ added in v0.2.0
RecordDatabase records a normalized database query against the active request or job transaction.
func RecordExternal ¶ added in v0.2.0
RecordExternal records an outbound request span against the active request or job transaction.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client posts notices to an Errorgap server. Safe for concurrent use.
func NewClient ¶
NewClient validates the config, applies defaults, and starts the async delivery goroutine. The caller should defer Close() to flush in-flight deliveries during shutdown.
func (*Client) Notify ¶
func (c *Client) Notify(err error, opts ...NoticeOptions) Result
Notify queues an error for delivery and returns immediately when Async is true. When Async is false, it blocks until the HTTP call completes.
func (*Client) NotifyLog ¶ added in v0.2.0
NotifyLog sends one structured log event when log forwarding is enabled.
func (*Client) NotifyTransaction ¶ added in v0.2.0
func (c *Client) NotifyTransaction(transaction Transaction) Result
NotifyTransaction sends an APM transaction when APM is enabled and the configured sample rate accepts it.
type Config ¶
type Config struct {
// Endpoint is the base URL of the Errorgap server (no trailing slash).
// Defaults to $ERRORGAP_ENDPOINT or http://127.0.0.1:3030.
Endpoint string
// ProjectSlug is the slug used in the ingestion URL.
// Defaults to $ERRORGAP_PROJECT_SLUG. Required.
ProjectSlug string
// ProjectID is optional and embedded in the notice payload.
// Defaults to $ERRORGAP_PROJECT_ID.
ProjectID string
// APIKey is sent as the x-errorgap-project-key header.
// Defaults to $ERRORGAP_API_KEY.
APIKey string
// Environment labels the deployment ("production", "staging").
// Defaults to $ERRORGAP_ENVIRONMENT or "production".
Environment string
// Release is the application version embedded in the notice context.
Release string
// RootDirectory is used to classify application frames and make their
// filenames relative. Defaults to $ERRORGAP_ROOT_DIRECTORY or the current
// working directory.
RootDirectory string
// Async controls fire-and-forget delivery. Defaults to true.
Async bool
// Logger receives SDK warnings. Defaults to a discard logger.
// Set to a no-op handler to silence.
Logger *slog.Logger
// FilterKeys overrides DefaultFilterKeys.
FilterKeys []string
// HTTPClient lets callers plug in a custom transport.
// Defaults to a copy of http.DefaultClient with a 5s timeout.
HTTPClient *http.Client
// Timeout for the default HTTP client. Ignored if HTTPClient is set.
Timeout time.Duration
// QueueSize bounds the in-flight notice channel when Async is true.
// Drops the new item when full. Defaults to 100.
QueueSize int
// CaptureGlobals installs a recover-and-log hook at the entry point.
// (Go doesn't allow process-wide panic handlers; use the middleware
// adapters instead.) Currently unused; reserved for future use.
CaptureGlobals bool
// APMEnabled controls transaction delivery. Defaults to
// $ERRORGAP_APM_ENABLED or false.
APMEnabled bool
// APMSampleRate is the fraction of transactions to send, from 0 to 1.
// Defaults to $ERRORGAP_APM_SAMPLE_RATE or 1.
APMSampleRate float64
// LogsEnabled controls structured log delivery. Defaults to
// $ERRORGAP_LOGS_ENABLED or false.
LogsEnabled bool
// MinimumLogLevel is used by NewSlogHandler. Defaults to
// $ERRORGAP_MINIMUM_LOG_LEVEL or slog.LevelWarn.
MinimumLogLevel slog.Level
}
Config controls notifier behavior.
type ErrorEntry ¶
type ErrorEntry struct {
Type string `json:"type"`
Message string `json:"message"`
Backtrace []Frame `json:"backtrace"`
}
ErrorEntry is one entry in the notice's errors array.
type Frame ¶
type Frame struct {
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Function string `json:"function,omitempty"`
InApp bool `json:"in_app"`
Index int `json:"index"`
Source *SourceExcerpt `json:"source,omitempty"`
}
Frame is a single backtrace entry in the notice envelope.
type LogEntry ¶ added in v0.2.0
type LogEntry struct {
Message string `json:"message"`
Level string `json:"level"`
Source string `json:"source,omitempty"`
Environment string `json:"environment,omitempty"`
OccurredAt time.Time `json:"occurred_at"`
}
LogEntry is the wire payload accepted by the Errorgap logs endpoint.
type Notice ¶
type Notice struct {
ProjectID string `json:"project_id,omitempty"`
ReceivedAt string `json:"received_at"`
Errors []ErrorEntry `json:"errors"`
Context map[string]any `json:"context"`
Environment map[string]any `json:"environment"`
Session map[string]any `json:"session"`
Params map[string]any `json:"params"`
}
Notice is the wire envelope POSTed to /api/projects/:slug/notices.
type NoticeOptions ¶
type NoticeOptions struct {
Context map[string]any
Environment map[string]any
Session map[string]any
Params map[string]any
// Skip extra runtime.Caller frames when capturing the backtrace.
// Useful when Notify is wrapped by another helper.
BacktraceSkip int
}
NoticeOptions allows callers to add per-notice context.
type Result ¶
Result records the outcome of a single Notify call.
func Notify ¶
func Notify(err error, opts ...NoticeOptions) Result
Notify sends an error via the package-level client. Returns an empty Result if Init has not been called.
func NotifyLog ¶ added in v0.2.0
NotifyLog sends a structured log event via the package-level client.
func NotifyTransaction ¶ added in v0.2.0
func NotifyTransaction(transaction Transaction) Result
NotifyTransaction sends an APM transaction via the package-level client.
type SlogHandler ¶ added in v0.2.0
type SlogHandler struct {
// contains filtered or unexported fields
}
SlogHandler forwards records at or above minimumLevel to Errorgap while preserving delivery to the wrapped handler. Pass nil for client to use the package-level client configured by Init.
func NewSlogHandler ¶ added in v0.2.0
NewSlogHandler returns a standard library slog handler with Errorgap log forwarding. A nil wrapped handler discards local output.
type SourceExcerpt ¶ added in v0.2.0
SourceExcerpt contains source lines surrounding a backtrace frame.
type Span ¶ added in v0.2.0
type Span struct {
Kind string `json:"kind"`
SQL string `json:"sql,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Function string `json:"fn_name,omitempty"`
DurationMS float64 `json:"duration_ms"`
}
Span is one timed operation within an APM transaction.
type SpanCollector ¶ added in v0.2.0
type SpanCollector struct {
// contains filtered or unexported fields
}
SpanCollector safely accumulates spans for a request or job.
func WithSpanCollector ¶ added in v0.2.0
func WithSpanCollector(ctx context.Context) (context.Context, *SpanCollector)
WithSpanCollector attaches a new span collector to ctx.
func (*SpanCollector) Spans ¶ added in v0.2.0
func (c *SpanCollector) Spans() []Span
Spans returns a snapshot of recorded spans.
type Transaction ¶ added in v0.2.0
type Transaction struct {
Kind string `json:"kind"`
Method string `json:"method,omitempty"`
Path string `json:"path,omitempty"`
PathRaw string `json:"path_raw,omitempty"`
StatusCode int `json:"status_code,omitempty"`
DurationMS float64 `json:"duration_ms"`
Environment string `json:"environment,omitempty"`
OccurredAt time.Time `json:"occurred_at"`
Spans []Span `json:"spans"`
JobClass string `json:"job_class,omitempty"`
Queue string `json:"queue,omitempty"`
}
Transaction is a web request or background job sent to the APM endpoint.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
testutil
Package testutil hosts test helpers shared across the package.
|
Package testutil hosts test helpers shared across the package. |
|
Package stdhttp provides net/http error, request-context, and APM instrumentation for Errorgap.
|
Package stdhttp provides net/http error, request-context, and APM instrumentation for Errorgap. |