Skip to content

collector: fix transaction handling, guard type assertions - #245

Merged
sravotto merged 2 commits into
mainfrom
sr8_collector
Feb 24, 2026
Merged

sravotto merged 2 commits into
mainfrom
sr8_collector

Conversation

@sravotto

Copy link
Copy Markdown
Contributor

Fix collectLocked to use a single deferred rollback/commit based on error state instead of fragile explicit rollback calls. Guard bare type assertions in counterAdd, clearObsoleteGauges, and cache eviction to prevent panics. Remove unused first field and unused name parameter from gaugeSet. Replace manual inUse flag with TryLock in Collect, and add refreshRunning mutex guard in server Start.

@sravotto
sravotto force-pushed the sr8_collector branch 3 times, most recently from 8e6e57b to a201116 Compare February 23, 2026 20:06

@BramGruneir BramGruneir left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of confused by a good chunk of the changes.

Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/collector.go Outdated
return err
}
defer tx.Commit(ctx)
defer func() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're commiting inside the defer? This seems dangrous.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.

Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/server.go Outdated
}
_, err := s.scheduler.Every(s.config.Refresh).
Do(func() {
if !s.refreshMu.TryLock() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need a new mutex? Can't you just use the original?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked the logic, so I can use just one mutex.

…ad code

Fix collectLocked to use a single deferred rollback/commit based on error
state instead of fragile explicit rollback calls. Guard bare type assertions
in counterAdd, clearObsoleteGauges, and cache eviction to prevent panics.
Remove unused `first` field and unused `name` parameter from gaugeSet.
Replace manual inUse flag with TryLock in Collect, and add refreshRunning
mutex guard in server Start.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@sravotto sravotto left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - please take another look.

Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/collector.go Resolved
Comment thread internal/collector/server.go Outdated
}
_, err := s.scheduler.Every(s.config.Refresh).
Do(func() {
if !s.refreshMu.TryLock() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked the logic, so I can use just one mutex.

Comment thread internal/collector/collector.go Outdated
return err
}
defer tx.Commit(ctx)
defer func() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.

@BramGruneir BramGruneir left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better, just a few minor comments.

s.mu.Unlock()
err := s.Refresh(ctx)
if err != nil {
log.Errorf("Error refreshing metrics %s", err.Error())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is just logged and ignored right?

And might as well combine it with the row above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Comment thread internal/collector/server.go Outdated
return
}
defer s.refreshMu.Unlock()
s.mu.Unlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to unlock before the refresh?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see the refresh relocks.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could make a locked version of it too. To avoid the lock flapping.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.

@BramGruneir BramGruneir left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was supposed to be a LGTM with minor comments.

@sravotto
sravotto merged commit 8658ec9 into main Feb 24, 2026
6 checks passed
@sravotto
sravotto deleted the sr8_collector branch February 24, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants