-
Notifications
You must be signed in to change notification settings - Fork 245
Add initial logger implementation #1010
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
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4262307
initial logger impl
giortzisg 67128ea
add attributes
giortzisg 3e25a81
remove tests
giortzisg 3e17902
add BeforeSendLog option
giortzisg bd907d9
fix metadata attributes
giortzisg 67975c0
improve test coverage
giortzisg 2b8e407
add sentry origin
giortzisg 7b181b7
change flush timeout
giortzisg 74ce60d
fix trace & span propagation
giortzisg bcac19d
add ctx on log functions & handle trace propagation
giortzisg 14827a0
lint issues
giortzisg 6325542
Merge branch 'master' into feat/logger
giortzisg bfa595d
Merge branch 'master' into feat/logger
giortzisg 95f8ed9
review changes
giortzisg d2c5b2a
change severity and level json
giortzisg 98cf9fe
add Printf like methods & docs
giortzisg 5b2aa71
setAttributes doc comment
giortzisg e0e251d
add examples and fix setAttributes
giortzisg bd47723
fix setAttributes
giortzisg 2c8665c
fix warn level name
giortzisg 710be2f
write to stdout if debug true
giortzisg b4360c9
Merge remote-tracking branch 'origin/master' into feat/logger
giortzisg 10d9ae0
ignore coverage
giortzisg d40ff0a
change server addr & beforeSendLog
giortzisg 120a48f
update doc comments
giortzisg 9b7b141
add attributes tests
giortzisg 74a43a0
add comment for check-in type
giortzisg 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,36 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "github.com/getsentry/sentry-go" | ||
| "github.com/getsentry/sentry-go/attribute" | ||
| "time" | ||
| ) | ||
|
|
||
| func main() { | ||
| err := sentry.Init(sentry.ClientOptions{ | ||
| Dsn: "", | ||
| EnableLogs: true, | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer sentry.Flush(2 * time.Second) | ||
|
|
||
| ctx := context.Background() | ||
| logger := sentry.NewLogger(ctx) | ||
|
|
||
| // you can use the logger like [fmt.Print] | ||
| logger.Info(ctx, "Expecting ", 2, " params") | ||
| // or like [fmt.Printf] | ||
| logger.Infof(ctx, "format: %v", "value") | ||
|
|
||
| // and you can also set attributes on the log like this | ||
cleptric marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logger.SetAttributes( | ||
| attribute.Int("key.int", 42), | ||
| attribute.Bool("key.boolean", true), | ||
| attribute.Float64("key.float", 42.4), | ||
| attribute.String("key.string", "string"), | ||
| ) | ||
| logger.Warnf(ctx, "I have params %v and attributes", "example param") | ||
| } | ||
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,94 @@ | ||
| package sentry | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| batchSize = 100 | ||
| batchTimeout = 5 * time.Second | ||
| ) | ||
|
|
||
| type BatchLogger struct { | ||
| client *Client | ||
| logCh chan Log | ||
| cancel context.CancelFunc | ||
| wg sync.WaitGroup | ||
| startOnce sync.Once | ||
| } | ||
|
|
||
| func NewBatchLogger(client *Client) *BatchLogger { | ||
| return &BatchLogger{ | ||
| client: client, | ||
| logCh: make(chan Log, batchSize), | ||
| } | ||
| } | ||
|
|
||
| func (l *BatchLogger) Start() { | ||
| l.startOnce.Do(func() { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| l.cancel = cancel | ||
| l.wg.Add(1) | ||
| go l.run(ctx) | ||
| }) | ||
| } | ||
|
|
||
| func (l *BatchLogger) Flush() { | ||
| if l.cancel != nil { | ||
| l.cancel() | ||
cleptric marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| l.wg.Wait() | ||
| } | ||
| } | ||
|
|
||
| func (l *BatchLogger) run(ctx context.Context) { | ||
| defer l.wg.Done() | ||
| var logs []Log | ||
| timer := time.NewTimer(batchTimeout) | ||
|
|
||
| for { | ||
| select { | ||
| case log := <-l.logCh: | ||
| logs = append(logs, log) | ||
| if len(logs) >= batchSize { | ||
| l.processEvent(logs) | ||
| logs = nil | ||
| if !timer.Stop() { | ||
| <-timer.C | ||
| } | ||
| timer.Reset(batchTimeout) | ||
| } | ||
| case <-timer.C: | ||
| if len(logs) > 0 { | ||
| l.processEvent(logs) | ||
| logs = nil | ||
| } | ||
| timer.Reset(batchTimeout) | ||
| case <-ctx.Done(): | ||
| // Drain remaining logs from channel | ||
| drain: | ||
| for { | ||
| select { | ||
| case log := <-l.logCh: | ||
| logs = append(logs, log) | ||
| default: | ||
| break drain | ||
| } | ||
| } | ||
|
|
||
| if len(logs) > 0 { | ||
| l.processEvent(logs) | ||
| } | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (l *BatchLogger) processEvent(logs []Log) { | ||
| event := NewEvent() | ||
| event.Timestamp = time.Now() | ||
| event.Type = logType | ||
| event.Logs = logs | ||
| l.client.CaptureEvent(event, nil, nil) | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.