Skip to content

DOG-6508: Fix metrics pickling issue in multiprocessing runtime#497

Closed
Hmnt39 wants to merge 3 commits intomasterfrom
DOG-6508-metrics-pickling-issue
Closed

DOG-6508: Fix metrics pickling issue in multiprocessing runtime#497
Hmnt39 wants to merge 3 commits intomasterfrom
DOG-6508-metrics-pickling-issue

Conversation

@Hmnt39
Copy link
Copy Markdown
Contributor

@Hmnt39 Hmnt39 commented Dec 8, 2025

This PR reesolves metrics serialization errors when passing BaseMetrics instances through multiprocessing by changing the runtime to pass the metrics class instead of an instance. The extractor now initializes metrics internally using a singleton pattern to avoid Prometheus registry conflicts.

Added comprehensive tests to verify metrics work correctly across process boundaries.

@Hmnt39 Hmnt39 requested a review from a team as a code owner December 8, 2025 11:36
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @Hmnt39, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue where metrics instances failed to serialize correctly when passed between processes in a multiprocessing runtime, leading to errors and unreliable metrics collection. The solution refactors the metrics handling mechanism to pass the metrics class itself, allowing each process to initialize its own metrics instance using a controlled singleton pattern. This ensures proper metrics collection, avoids conflicts with the Prometheus registry, and enhances the robustness of the metrics system in distributed environments.

Highlights

  • Metrics Serialization Fix: Resolved metrics serialization errors that occurred when passing BaseMetrics instances through multiprocessing by changing the runtime to pass the metrics class instead of an instance.
  • Singleton Metrics Initialization: The extractor now initializes metrics internally using a singleton pattern, which effectively avoids Prometheus registry conflicts across different processes.
  • Improved Test Coverage: Comprehensive tests have been added to thoroughly verify that metrics function correctly and are properly incremented across process boundaries in a multiprocessing environment.
  • API Changes: The Extractor and Runtime classes have been updated to accept a metrics class type instead of a metrics instance, simplifying the API and enabling the new singleton pattern.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses the metrics serialization issue in a multiprocessing environment by shifting from passing metric instances to passing metric classes. The introduction of a singleton pattern for metrics initialization within the extractor is a solid approach to prevent Prometheus registry conflicts. The changes are well-structured and the addition of a comprehensive integration test in test_runtime.py is excellent for verifying the fix. I've found one critical issue in the implementation of the metrics loading logic that could lead to runtime errors and inconsistencies. My feedback focuses on making this implementation more robust and consistent.

Comment on lines +286 to +289
if metrics_class:
metrics_instance = safe_get(metrics_class)
else:
metrics_instance = BaseMetrics(extractor_name=self.EXTERNAL_ID, extractor_version=self.VERSION)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

This logic for creating the metrics instance has two issues:

  1. The call to safe_get(metrics_class) assumes a no-argument constructor, which contradicts the BaseMetrics constructor and the MyMetrics example in the docstring. This will lead to a TypeError.
  2. The else block instantiates BaseMetrics directly, bypassing safe_get. This breaks the singleton pattern and can cause Prometheus registry conflicts if BaseMetrics is instantiated via safe_get elsewhere.

A more robust implementation would be to always use safe_get and pass the required extractor_name and extractor_version arguments. This will require updating any custom metric classes in tests (like TestMetrics) to accept these arguments in their __init__ method.

        cls_to_use = metrics_class or BaseMetrics
        metrics_instance = safe_get(cls_to_use, extractor_name=self.EXTERNAL_ID, extractor_version=self.VERSION)

@Hmnt39 Hmnt39 force-pushed the DOG-6508-metrics-pickling-issue branch from 6fc1fb3 to 44b90c0 Compare December 8, 2025 12:37
@Hmnt39 Hmnt39 closed this Dec 8, 2025
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.

1 participant