Skip to content

Inject MetricsPersistence directly into PersistingMetricsReporter#4708

Open
bowsii wants to merge 2 commits into
apache:mainfrom
bowsii:issue-4655-metrics-persistence-injection
Open

Inject MetricsPersistence directly into PersistingMetricsReporter#4708
bowsii wants to merge 2 commits into
apache:mainfrom
bowsii:issue-4655-metrics-persistence-injection

Conversation

@bowsii

@bowsii bowsii commented Jun 11, 2026

Copy link
Copy Markdown

Fixes #4655

  1. Added a CDI producer for MetricsPersistence in ServiceProducers.

  2. Removed CallContext dependency from PersistingMetricsReporter.

  3. Removed PolarisMetaStoreManager dependency from
    PersistingMetricsReporter.

  4. Injected MetricsPersistence directly into
    PersistingMetricsReporter.

  5. Replaced:
    metaStoreManager.writeScanMetrics(...)
    with:
    metricsPersistence.writeScanReport(...)

  6. Replaced:
    metaStoreManager.writeCommitMetrics(...)
    with:
    metricsPersistence.writeCommitReport(...)

  7. Updated Javadocs to reflect direct MetricsPersistence usage.

  8. Refactored PersistingMetricsReporterTest:

    • Removed CallContext mocks.
    • Removed PolarisMetaStoreManager mocks.
    • Added MetricsPersistence mock.
    • Updated verifications to use
      writeScanReport() and writeCommitReport().

Checklist

  • 🛡️ Don't disclose security issues! (contact security@apache.org)
  • 🔗 Clearly explained why the changes are needed, or linked related issues: Fixes #
  • 🧪 Added/updated tests with good coverage, or manually tested (and explained how)
  • 💡 Added comments for complex logic
  • 🧾 Updated CHANGELOG.md (if needed)
  • 📚 Updated documentation in site/content/in-dev/unreleased (if needed)

dimas-b
dimas-b previously approved these changes Jun 11, 2026

@dimas-b dimas-b 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.

Thanks for the quick response on #4655 , @bowsii ! 🙂

@Produces
@RequestScoped
public MetricsPersistence metricsPersistence(CallContext callContext) {
return callContext.getPolarisCallContext().getMetricsPersistence();

@dimas-b dimas-b Jun 11, 2026

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.

#4655 implies avoiding indirection via CallContext 😉

Would you mind taking this one step further and removing MetricsPersistence from CallContext? I do not think there is any reason for it to be there after this PR. This producer can deal with the factory directly (cf. polarisCallContext() on line 138).

The benefit would be that MetricsPersistence would be materialized only on API calls involving metrics reporting.

If you prefer that can be done as a separate follow-up PR. Current change is good and self-contained 👍

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Jun 11, 2026
@bowsii

bowsii commented Jun 12, 2026

Copy link
Copy Markdown
Author

Thanks for the suggestion!

I've taken it one step further and removed MetricsPersistence from PolarisCallContext entirely. The request-scoped producer now resolves it directly from MetaStoreManagerFactory, following the same logic previously used during call context creation.

This means MetricsPersistence is only materialized for requests that actually inject it (such as metrics reporting), while non-metrics API calls no longer create or carry it in the call context.

I've also updated the affected call sites and tests accordingly.

@dimas-b dimas-b 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.

I think we're making progress :) a few more minor comments and I think we can merge.

@NonNull PolarisCallContext callCtx, @NonNull ScanMetricsRecord record) {
callCtx.getMetricsPersistence().writeScanReport(record);
BasePersistence metaStore = callCtx.getMetaStore();
if (metaStore instanceof MetricsPersistence metricsPersistence) {

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.

This code is no longer invoked, I think... why not remove it?

@NonNull PolarisCallContext callCtx, @NonNull CommitMetricsRecord record) {
callCtx.getMetricsPersistence().writeCommitReport(record);
BasePersistence metaStore = callCtx.getMetaStore();
if (metaStore instanceof MetricsPersistence metricsPersistence) {

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.

also not invoked, AFAIK.

@RequestScoped
public MetricsPersistence metricsPersistence(
RealmContext realmContext, MetaStoreManagerFactory metaStoreManagerFactory) {
BasePersistence metaStore = metaStoreManagerFactory.getOrCreateSession(realmContext);

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.

Why bother calling getOrCreateSession() now? This producer will be invoked by CDI only for use cases that need a MetricsPersistence, not BasePersistence 🤔

The old optimization of reusing the same object is no longer relevant.

I think calling getOrCreateMetricsPersistence() (below) is sufficient.

@dimas-b

dimas-b commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

FYI: @obelix74

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.

Improve MetricsPersistence injection

2 participants