Skip to content

Commit 1ed2c30

Browse files
authored
feat: add Sentry error tracking adapter with enterprise features (#63)
Implements comprehensive Sentry integration for production error monitoring and observability. ## Core Features - Automatic error capture with stack traces and breadcrumbs - Transaction tracking for distributed tracing - Message template interpolation preserved in Sentry UI - Thread-safe metrics collection with atomic counters - Environment variable support for DSN configuration ## Performance Optimizations - Stack trace caching with LRU eviction (60.74 ns/op, 95% hit rate) - String builder pooling for zero-allocation message rendering - Retry calculation with exponential backoff (11.07 ns/op, 0 allocs) - Metrics collection using atomics (7.11 ns/op) ## Sampling Strategies - Fixed rate sampling for predictable overhead - Adaptive sampling that adjusts to error volume - Priority-based sampling by log level - Burst detection with rate limiting - Group-based sampling to prevent error flooding - Predefined profiles for common scenarios - Custom sampling functions for full control ## Production Features - Retry logic with exponential backoff and jitter - Real-time metrics and observability - Breadcrumb buffer with automatic age eviction - Context enrichment with user and request data - Performance monitoring with transaction/span tracking - Comprehensive test coverage (500+ lines) - 7 example programs demonstrating all features ## Documentation - Complete integration guide in docs/sinks.md - Quick reference examples in docs/quick-reference - README updates with Sentry examples - Inline documentation for all public APIs Fixes #46
2 parents 1e658f6 + 62ec24a commit 1ed2c30

33 files changed

Lines changed: 7016 additions & 3 deletions

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ jobs:
6464
go test -v ./...
6565
go test -race -v ./...
6666
67+
- name: Test Sentry adapter module
68+
run: |
69+
cd adapters/sentry
70+
go test -v ./...
71+
go test -race -v ./...
72+
6773
- name: Test coverage
6874
run: go test -coverprofile=coverage.out ./...
6975
if: matrix.os == 'ubuntu-latest' && matrix.go == '1.23'

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,12 @@ cmd/mtlog-lsp/mtlog-lsp
8181
zed-extension/mtlog/target/
8282
zed-extension/mtlog/extension.wasm
8383
zed-extension/mtlog/Cargo.lock
84+
85+
# Sentry test infrastructure (generated files)
86+
docker/sentry-config/
87+
docker/relay/
88+
docker/nginx/
89+
docker/postgres/
90+
docker/.env
91+
docker/docker-compose.test.yml
92+
docker/docker-compose.test.yml.new

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mtlog is a high-performance structured logging library for Go, inspired by [Seri
4343
- **Elasticsearch sink** for centralized log storage and search
4444
- **Splunk sink** with HEC (HTTP Event Collector) support
4545
- **OpenTelemetry (OTLP) sink** with gRPC/HTTP transport, batching, and trace correlation
46+
- **Sentry integration** with error tracking, performance monitoring, and intelligent sampling
4647
- **Conditional sink** for predicate-based routing with zero overhead
4748
- **Router sink** for multi-destination routing with FirstMatch/AllMatch modes
4849
- **Async sink wrapper** for high-throughput scenarios
@@ -727,6 +728,37 @@ mtlog.WithSplunkAdvanced("http://localhost:8088",
727728
)
728729
```
729730

731+
### Sentry Integration
732+
733+
```go
734+
import (
735+
"github.com/willibrandon/mtlog"
736+
"github.com/willibrandon/mtlog/adapters/sentry"
737+
)
738+
739+
// Basic Sentry error tracking
740+
sink, _ := sentry.WithSentry("https://key@sentry.io/project")
741+
log := mtlog.New(mtlog.WithSink(sink))
742+
743+
// With sampling for high-volume applications
744+
sink, _ := sentry.WithSentry("https://key@sentry.io/project",
745+
sentry.WithFixedSampling(0.1), // 10% sampling
746+
)
747+
log := mtlog.New(mtlog.WithSink(sink))
748+
749+
// Advanced configuration with performance monitoring
750+
sink, _ := sentry.WithSentry("https://key@sentry.io/project",
751+
sentry.WithEnvironment("production"),
752+
sentry.WithRelease("v1.2.3"),
753+
sentry.WithTracesSampleRate(0.2),
754+
sentry.WithProfilesSampleRate(0.1),
755+
sentry.WithAdaptiveSampling(0.01, 0.5), // 1% to 50% adaptive
756+
sentry.WithRetryPolicy(3, time.Second),
757+
sentry.WithStackTraceCache(1000),
758+
)
759+
log := mtlog.New(mtlog.WithSink(sink))
760+
```
761+
730762
### Async and Durable Sinks
731763

732764
```go
@@ -963,7 +995,7 @@ Benchmark results on AMD Ryzen 9 9950X:
963995

964996
## Examples
965997

966-
See the [examples](./examples) directory and [OTEL examples](./adapters/otel/examples) for complete examples:
998+
See the [examples](./examples) directory and adapter examples ([OTEL](./adapters/otel/examples), [Sentry](./adapters/sentry/examples), [middleware](./adapters/middleware/examples)) for complete examples:
967999

9681000
- [Basic logging](./examples/basic/main.go)
9691001
- [Using enrichers](./examples/enrichers/main.go)
@@ -992,6 +1024,13 @@ See the [examples](./examples) directory and [OTEL examples](./adapters/otel/exa
9921024
- [OTEL with metrics](./adapters/otel/examples/metrics/main.go)
9931025
- [OTEL with sampling](./adapters/otel/examples/sampling/main.go)
9941026
- [OTEL with TLS](./adapters/otel/examples/tls/main.go)
1027+
- [Sentry error tracking](./adapters/sentry/examples/basic/main.go)
1028+
- [Sentry with context](./adapters/sentry/examples/context/main.go)
1029+
- [Sentry breadcrumbs](./adapters/sentry/examples/breadcrumbs/main.go)
1030+
- [Sentry with retry](./adapters/sentry/examples/retry/main.go)
1031+
- [Sentry performance monitoring](./adapters/sentry/examples/performance/main.go)
1032+
- [Sentry metrics dashboard](./adapters/sentry/examples/metrics/main.go)
1033+
- [Sentry sampling strategies](./adapters/sentry/examples/sampling/main.go)
9951034
- [Async logging](./examples/async/main.go)
9961035
- [Durable buffering](./examples/durable/main.go)
9971036
- [Dynamic levels](./examples/dynamic-levels/main.go)

0 commit comments

Comments
 (0)