-
Notifications
You must be signed in to change notification settings - Fork 242
feat: Experimental Trace Metric #1116
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
aldy505
wants to merge
6
commits into
getsentry:master
Choose a base branch
from
aldy505:feat/tracemetrics
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.
+554
−8
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dc99b2
feat: Experimental Trace Metric
aldy505 c341e95
fix: don't allow multiple fields with json struct name
aldy505 1f981fb
fix: apply review from AI comments
aldy505 6ff23fb
chore: avoid golangci-lint errors
aldy505 67d4e1c
Update interfaces.go
aldy505 132f880
Update internal/protocol/envelope.go
aldy505 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,127 @@ | ||
| package sentry | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type BatchMeter struct { | ||
| client *Client | ||
| metricsCh chan Metric | ||
| flushCh chan chan struct{} | ||
| cancel context.CancelFunc | ||
| wg sync.WaitGroup | ||
| startOnce sync.Once | ||
| shutdownOnce sync.Once | ||
| } | ||
|
|
||
| func NewBatchMeter(client *Client) *BatchMeter { | ||
| return &BatchMeter{ | ||
| client: client, | ||
| metricsCh: make(chan Metric, batchSize), | ||
| flushCh: make(chan chan struct{}), | ||
| } | ||
| } | ||
|
|
||
| func (m *BatchMeter) Start() { | ||
| m.startOnce.Do(func() { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| m.cancel = cancel | ||
| m.wg.Add(1) | ||
| go m.run(ctx) | ||
| }) | ||
| } | ||
|
|
||
| func (m *BatchMeter) Flush(timeout <-chan struct{}) { | ||
| done := make(chan struct{}) | ||
| select { | ||
| case m.flushCh <- done: | ||
| select { | ||
| case <-done: | ||
| case <-timeout: | ||
| } | ||
| case <-timeout: | ||
| } | ||
| } | ||
|
|
||
| func (m *BatchMeter) Shutdown() { | ||
| m.shutdownOnce.Do(func() { | ||
| if m.cancel != nil { | ||
| m.cancel() | ||
| m.wg.Wait() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func (m *BatchMeter) run(ctx context.Context) { | ||
| defer m.wg.Done() | ||
| var metrics []Metric | ||
| timer := time.NewTimer(batchTimeout) | ||
| defer timer.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case metric := <-m.metricsCh: | ||
| metrics = append(metrics, metric) | ||
| if len(metrics) >= batchSize { | ||
| m.processEvent(metrics) | ||
| metrics = nil | ||
| if !timer.Stop() { | ||
| <-timer.C | ||
| } | ||
| timer.Reset(batchTimeout) | ||
| } | ||
| case <-timer.C: | ||
| if len(metrics) > 0 { | ||
| m.processEvent(metrics) | ||
| metrics = nil | ||
| } | ||
| timer.Reset(batchTimeout) | ||
| case done := <-m.flushCh: | ||
| flushDrain: | ||
| for { | ||
| select { | ||
| case metric := <-m.metricsCh: | ||
| metrics = append(metrics, metric) | ||
| default: | ||
| break flushDrain | ||
| } | ||
| } | ||
|
|
||
| if len(metrics) > 0 { | ||
| m.processEvent(metrics) | ||
| metrics = nil | ||
| } | ||
| if !timer.Stop() { | ||
| <-timer.C | ||
| } | ||
| timer.Reset(batchTimeout) | ||
| close(done) | ||
| case <-ctx.Done(): | ||
| drain: | ||
| for { | ||
| select { | ||
| case metric := <-m.metricsCh: | ||
| metrics = append(metrics, metric) | ||
| default: | ||
| break drain | ||
| } | ||
| } | ||
|
|
||
| if len(metrics) > 0 { | ||
| m.processEvent(metrics) | ||
| } | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (m *BatchMeter) processEvent(metrics []Metric) { | ||
| event := NewEvent() | ||
| event.Timestamp = time.Now() | ||
| event.EventID = EventID(uuid()) | ||
| event.Type = traceMetricEvent.Type | ||
| event.Metrics = metrics | ||
| m.client.Transport.SendEvent(event) | ||
| } |
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
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.
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.
Bug: Goroutine Leak in Client Close
The Client.Close() method doesn't call Shutdown() on batchMeter (or batchLogger), causing goroutine leaks. The BatchMeter.run() goroutine will continue running indefinitely even after the client is closed, as it only stops when the context is cancelled. The Shutdown() method should be called to properly cancel the context and wait for the goroutine to finish, similar to how Flush() calls both batchLogger.Flush() and batchMeter.Flush().