Skip to content

chore: use less locking in metrics hot paths - #213

Merged
sighphyre merged 3 commits into
fix/use-sync-pool-for-strategy-randomnessfrom
chore/rework-metrics-to-be-less-locky
Nov 21, 2025
Merged

chore: use less locking in metrics hot paths#213
sighphyre merged 3 commits into
fix/use-sync-pool-for-strategy-randomnessfrom
chore/rework-metrics-to-be-less-locky

Conversation

@sighphyre

@sighphyre sighphyre commented Nov 19, 2025

Copy link
Copy Markdown
Member

Yeets the locking in the metrics code in favor of sync primitives. Fixes a bug in the re-insertion code since I was in that space. Variants are still behind a mutex but that mutex is now smaller scoped and locked less often. We can probably remove that too but its a bigger PR and I want to keep this focused on the hot path

@sighphyre
sighphyre changed the base branch from v5 to fix/use-sync-pool-for-strategy-randomness November 19, 2025 13:58
@coveralls

coveralls commented Nov 19, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 19563538142

Details

  • 84 of 115 (73.04%) changed or added relevant lines in 4 files are covered.
  • 67 unchanged lines in 7 files lost coverage.
  • Overall coverage decreased (-4.1%) to 77.151%

Changes Missing Coverage Covered Lines Changed/Added Lines %
api/feature.go 4 6 66.67%
metrics.go 72 101 71.29%
Files with Coverage Reduction New Missed Lines %
nooplistener.go 2 62.5%
internal/api/metrics.go 3 0.0%
debuglistener.go 6 0.0%
unleash_mock.go 6 75.0%
client.go 7 86.93%
feature_state.go 20 59.29%
metrics.go 23 75.37%
Totals Coverage Status
Change from base Build 19538311559: -4.1%
Covered Lines: 1587
Relevant Lines: 2057

💛 - Coveralls

Comment thread metrics.go
return actual.(*toggleCounters)
}

func (m *metrics) reinsertBucket(bucket api.Bucket) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Previously the code just dropped variants on the floor when re-inserting. Needed to get fixed and now seems like a good time I guess so here's a fix

Comment thread metrics.go
atomic.AddInt64(&c.yes, int64(num))
} else {
t.No += num
atomic.AddInt64(&c.no, int64(num))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wheee atomic primitives are awesome. We can do this on the hardware level and not sandwiched between the runtime and OS

Comment thread metrics_test.go
st.Expect(t, gock.IsDone(), true)
}

func TestReinsertingBucketsAlsoRestoresVariants(t *testing.T) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

New test for the re-insert variant logic

@sighphyre sighphyre changed the title Chore/rework metrics to be less locky chore: use less locking in metrics hot paths Nov 19, 2025
@sighphyre
sighphyre marked this pull request as ready for review November 19, 2025 14:07
@sighphyre sighphyre self-assigned this Nov 19, 2025
@chriswk chriswk moved this from New to In Progress in Issues and PRs Nov 20, 2025
@sighphyre
sighphyre force-pushed the fix/use-sync-pool-for-strategy-randomness branch from db94232 to e39f515 Compare November 20, 2025 12:49
@sighphyre
sighphyre force-pushed the chore/rework-metrics-to-be-less-locky branch from 2cf51a1 to bd4c12b Compare November 20, 2025 12:56
@sighphyre
sighphyre force-pushed the fix/use-sync-pool-for-strategy-randomness branch from e39f515 to 1c38a81 Compare November 20, 2025 13:21
@sighphyre
sighphyre force-pushed the chore/rework-metrics-to-be-less-locky branch from bd4c12b to 2b95c7f Compare November 20, 2025 13:27

@FredrikOseberg FredrikOseberg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some nits, otherwise LGTM

Comment thread metrics.go Outdated
Comment thread metrics.go Outdated
Comment thread metrics.go Outdated
}
}

tc := api.ToggleCount{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

toggleCount would be clearer

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, took that suggestion. Uh... is there a pattern for this? The default Go community stance seems to be make variables as short and difficult to read as possible so I've accidentally followed that sometimes but I hate it. Is the a rule where we shouldn't do that?

Comment thread metrics.go Outdated

m.counters.Range(func(key, value any) bool {
name := key.(string)
c := value.(*toggleCounters)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

counters? I find myself scrolling up to figure out what this variable is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair, patched

Comment thread metrics.go Outdated

func (m *metrics) reinsertBucket(bucket api.Bucket) {
for name, tc := range bucket.Toggles {
c := m.getOrCreateCounter(name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

counter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Also fair, also patched

@github-project-automation github-project-automation Bot moved this from In Progress to Approved PRs in Issues and PRs Nov 20, 2025
@sighphyre
sighphyre force-pushed the chore/rework-metrics-to-be-less-locky branch from 4adee3f to 19b67c5 Compare November 20, 2025 15:06
* fix: variant strategies now correctly inherit stickiness from their strategy

* chore: make metric count channels best effort send (#214)
@sighphyre
sighphyre merged commit 09b1a34 into fix/use-sync-pool-for-strategy-randomness Nov 21, 2025
5 checks passed
@sighphyre
sighphyre deleted the chore/rework-metrics-to-be-less-locky branch November 21, 2025 07:41
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Issues and PRs Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants