-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(go): add zap integration docs #16207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
giortzisg
wants to merge
1
commit into
master
Choose a base branch
from
docs/go/zap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| --- | ||
| title: Zap | ||
| description: "Zap is a blazing-fast, structured logging library for Go. This guide demonstrates how to integrate zap with Sentry to capture and send logs to Sentry." | ||
| sidebar_order: 100 | ||
| --- | ||
|
|
||
| For a quick reference, there is a [complete example](https://github.com/getsentry/sentry-go/tree/master/_examples/zap) at the Go SDK source code repository. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This link 404s; will this example be added later? |
||
|
|
||
| Go API documentation for the [`sentryzap` package](https://pkg.go.dev/github.com/getsentry/sentry-go/zap) is also available. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly a 404 |
||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| go get github.com/getsentry/sentry-go | ||
| go get github.com/getsentry/sentry-go/zap | ||
| ``` | ||
|
|
||
| ## Configure | ||
|
|
||
| ### Initialize the Sentry SDK | ||
|
|
||
| <PlatformContent includePath="getting-started-include-logs-config" /> | ||
|
|
||
| ### Options | ||
|
|
||
| `sentryzap` provides a `Core` implementation that integrates with zap's logging pipeline. It accepts a struct of `sentryzap.Option` that allows you to configure how logs are captured and sent to Sentry. The options are: | ||
|
|
||
| | Field | Type | Description | Default | | ||
| |-------|------|-------------|---------| | ||
| | `Level` | `[]zapcore.Level` | Zap levels to capture and send to Sentry as log entries | All levels (Debug through Fatal) | | ||
| | `AddCaller` | `bool` | Include caller info (file, line, function) in logs | `false` | | ||
| | `FlushTimeout` | `time.Duration` | How long to wait when syncing/flushing logs | 5 seconds | | ||
|
|
||
| ## Verify | ||
|
|
||
| This example shows how to create a zap logger that sends logs to Sentry. | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/getsentry/sentry-go" | ||
| sentryzap "github.com/getsentry/sentry-go/zap" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Initialize Sentry with logs enabled | ||
| err := sentry.Init(sentry.ClientOptions{ | ||
| Dsn: "___PUBLIC_DSN___", | ||
| EnableLogs: true, | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer sentry.Flush(2 * time.Second) | ||
|
|
||
| // Create the Sentry core | ||
| ctx := context.Background() | ||
| sentryCore := sentryzap.NewSentryCore(ctx, sentryzap.Option{ | ||
| Level: []zapcore.Level{ | ||
| zapcore.InfoLevel, | ||
| zapcore.WarnLevel, | ||
| zapcore.ErrorLevel, | ||
| }, | ||
| AddCaller: true, | ||
| }) | ||
|
|
||
| // Create a zap logger with the Sentry core | ||
| logger := zap.New(sentryCore) | ||
|
|
||
| // Log messages will be sent to Sentry | ||
| logger.Info("Application started", | ||
| zap.String("version", "1.0.0"), | ||
| zap.String("environment", "production"), | ||
| ) | ||
|
|
||
| logger.Warn("High memory usage", | ||
| zap.Float64("usage_percent", 85.5), | ||
| ) | ||
|
|
||
| logger.Error("Database connection failed", | ||
| zap.Error(errors.New("connection timeout")), | ||
| zap.String("host", "db.example.com"), | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Context and Tracing | ||
|
|
||
| The Sentry core respects the context passed during initialization. If you have Sentry tracing enabled, logs will be associated with the current span. | ||
|
|
||
| ### Option 1: Pass context when creating the core | ||
|
|
||
| This example shows how to pass a context with an active span when creating the core. | ||
|
|
||
| ```go | ||
| ctx := context.Background() | ||
|
|
||
| // Start a transaction | ||
| span := sentry.StartSpan(ctx, "operation.name") | ||
| defer span.Finish() | ||
|
|
||
| // Create logger with the span's context | ||
| ctx = span.Context() | ||
| sentryCore := sentryzap.NewSentryCore(ctx, sentryzap.Option{}) | ||
| logger := zap.New(sentryCore) | ||
|
|
||
| // This log will be associated with the transaction | ||
| logger.Info("Processing started") | ||
| ``` | ||
|
|
||
| ### Option 2: Use the Context() helper for dynamic trace propagation | ||
|
|
||
| This example shows how to use the `Context()` helper to propagate different contexts for different log calls. | ||
|
|
||
| ```go | ||
| // Create logger with base context | ||
| sentryCore := sentryzap.NewSentryCore(context.Background(), sentryzap.Option{}) | ||
| logger := zap.New(sentryCore) | ||
|
|
||
| // Start a transaction | ||
| span := sentry.StartTransaction(ctx, "operation.name") | ||
| defer span.Finish() | ||
|
|
||
| // Create a logger scoped to this transaction | ||
| scopedLogger := logger.With(sentryzap.Context(span.Context())) | ||
|
|
||
| // These logs will be associated with the transaction | ||
| scopedLogger.Info("Processing started") | ||
| scopedLogger.Info("Processing completed") | ||
| ``` | ||
|
|
||
| <Include name="logs/go-ctx-usage-alert.mdx"/> | ||
|
|
||
| ## Logs | ||
|
|
||
| For comprehensive logging setup with zap, including advanced configuration options and best practices, see the [Go Logs documentation](/platforms/go/logs/). The zap integration shown above provides seamless integration with Sentry's structured logging features. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other Go SDKs are listed alphabetically, did you intend for this one to be shown at the top? If not, you can remove the sidebar order.