Skip to content
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

chore: add a class to handle aggreggation queries #8446

Merged
merged 7 commits into from
Oct 18, 2024

Conversation

gastonfournier
Copy link
Contributor

About the changes

We have many aggregation queries that run on a schedule:

unleash/src/lib/metrics.ts

Lines 714 to 719 in f63496d

await schedulerService.schedule(
collectStaticCounters.bind(this),
hoursToMilliseconds(2),
'collectStaticCounters',
0, // no jitter
);

These staticCounters are usually doing db query aggregations that traverse tables and we run all of them in parallel:

unleash/src/lib/metrics.ts

Lines 410 to 412 in f63496d

] = await Promise.all([
stores.featureStrategiesReadModel.getMaxFeatureStrategies(),
stores.featureStrategiesReadModel.getMaxFeatureEnvironmentStrategies(),

This can add strain to the db. This PR suggests a way of handling these queries in a more structured way, allowing us to run them sequentially (therefore spreading the load):

for (const task of this.tasks) {
await task();
}

As an additional benefit, we get both the gauge definition and the queries in a single place:

unleash/src/lib/metrics.ts

Lines 131 to 141 in f02fe87

dbMetrics.registerGaugeDbMetric({
name: 'max_feature_strategies',
help: 'Maximum number of strategies in one feature',
labelNames: ['feature'],
query: () =>
stores.featureStrategiesReadModel.getMaxFeatureStrategies(),
map: (result) => ({
count: result.count,
labels: { feature: result.feature },
}),
});

This PR only tackles 1 metric, and it only focuses on gauges to gather initial feedback. The plan is to migrate these metrics and eventually incorporate more types (e.g. counters)

Copy link

vercel bot commented Oct 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2024 8:49am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
unleash-monorepo-frontend ⬜️ Ignored (Inspect) Visit Preview Oct 18, 2024 8:49am

Copy link
Contributor

github-actions bot commented Oct 14, 2024

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

Comment on lines 131 to 140
dbMetrics.registerGaugeDbMetric({
name: 'max_feature_strategies',
help: 'Maximum number of strategies in one feature',
labelNames: ['feature'],
query: () =>
stores.featureStrategiesReadModel.getMaxFeatureStrategies(),
map: (result) => ({
count: result.count,
labels: { feature: result.feature },
}),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gauge and query are now declared together

@@ -408,7 +419,6 @@ export default class MetricsMonitor {
instanceOnboardingMetrics,
projectsOnboardingMetrics,
] = await Promise.all([
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not need to run these in parallel. I would be to get rid of promise all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, we are going to do that gradually. When we register a new metric into this new implementation, the new implementation will run them sequentially

src/lib/metrics-gauge.ts Outdated Show resolved Hide resolved
src/lib/metrics-gauge.ts Outdated Show resolved Hide resolved
src/lib/metrics-gauge.ts Outdated Show resolved Hide resolved
Copy link
Member

@nunogois nunogois left a comment

Choose a reason for hiding this comment

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

Super nice!

Co-authored-by: Nuno Góis <[email protected]>
…ies-sequentially' into proposal/handle-aggregation-queries-sequentially
This is a follow up on #8446
- migrating some metrics (check the commits)
- moving event listener registration outside metric calculation (every 2
hs we are adding new listeners)
- adding jitter to metric refresh
- manually executing some of the tasks immediately to avoid changing the
test logic (with jitter now we'd have to force or wait until the first
execution of the scheduled task that collect the metrics). We might
decide to change this later after the migration
- try catch the execution of the task
- register metrics now "just registers" and returns functions for
refreshing metrics. In the future, we'll return an object with the
ability of independently updating specific metrics so we can tweak the
frequency of updates
@gastonfournier gastonfournier merged commit a9f9be1 into main Oct 18, 2024
8 checks passed
@gastonfournier gastonfournier deleted the proposal/handle-aggregation-queries-sequentially branch October 18, 2024 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants