Skip to content

Comments

Running telemetry completely in the background#59

Closed
safaricd wants to merge 7 commits intomainfrom
PRI-217
Closed

Running telemetry completely in the background#59
safaricd wants to merge 7 commits intomainfrom
PRI-217

Conversation

@safaricd
Copy link
Collaborator

Change Description

With this PR, we change how telemetry is initialized and ran. Instead of initializing the client and capturing events in the main thread of execution, we process all events in the background. The changes include:

  • Queuing capture events.
  • Shutdown handlers for gracefully flushing remaining events.

@chatgpt-codex-connector
Copy link

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @safaricd, 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 significantly refactors the telemetry service to operate asynchronously, improving application responsiveness by offloading telemetry operations to a background thread. This change ensures that event capturing and client initialization do not introduce delays in the main execution flow, while also providing robust shutdown mechanisms to prevent data loss.

Highlights

  • Background Telemetry Processing: Telemetry initialization and event capturing are now performed in a dedicated background thread, preventing blocking of the main application thread.
  • Event Queuing: A task queue has been implemented to manage telemetry events, allowing them to be processed asynchronously by the background worker.
  • Graceful Shutdown: A shutdown handler is registered using atexit to ensure that all queued telemetry events are flushed and the PostHog client is gracefully shut down when the application exits.
  • Telemetry Initialization Logic Refactor: The logic for checking telemetry enablement (via environment variables or server configuration) has been moved into the background worker's initialization process, making it non-blocking for the main thread.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/tabpfn_common_utils/telemetry/core/service.py
    • Imported atexit, threading, Queue, and Empty for asynchronous processing.
    • Refactored ProductTelemetry to manage a background worker thread for all telemetry operations.
    • Introduced _task_queue, _shutdown_event, and _worker to handle asynchronous tasks.
    • Modified the __init__ method to enqueue the actual PostHog client initialization, making it non-blocking.
    • Removed the telemetry_enabled class method, integrating its logic into the new background initialization.
    • Changed capture and flush methods to enqueue their respective operations for the worker thread.
    • Added private methods _do_capture, _do_flush, _enqueue, _worker_loop, and _shutdown to manage the background processing and lifecycle.
    • Registered _shutdown with atexit for graceful application exit.
    • Updated the default flush_at parameter from 10 to 5 in ProductTelemetry's __init__.
    • Modified capture_event function signature to use keyword-only arguments for max_queue_size and flush_at.
Activity
  • No specific activity recorded for this pull request yet.
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.

@safaricd
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@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 refactors the telemetry service to run in a background thread, significantly improving performance by making event capturing non-blocking. However, a critical vulnerability has been identified: the implementation uses an unbounded queue for telemetry tasks, which poses a risk of memory exhaustion and Denial of Service (DoS) if events are produced faster than they can be processed. To enhance robustness and mitigate this, it is crucial to implement a bounded queue and improve the enqueue logic to handle full queues without blocking, potentially by dropping events. Additionally, consider preventing race conditions during shutdown and reviewing a minor naming choice for clarity.

Copy link
Contributor

@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 introduces background processing for telemetry, which significantly improves performance by making telemetry calls non-blocking through the use of a worker thread and event queue. However, a critical Server-Side Request Forgery (SSRF) vulnerability has been identified. This is due to the new background initialization process retrieving a URL from an environment variable without validation, which could allow an attacker to probe internal networks or access cloud metadata services. Remediation requires validating the URL before use. Furthermore, I've noted a few areas for improvement, including a potential data loss bug in the shutdown logic, a misleading function signature, and a confusing method name.

except Exception as e:
logger.debug(f"Failed to initialize telemetry: {e}")

def capture(
Copy link
Contributor

Choose a reason for hiding this comment

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

I recall that at some point we were using thread-local variables for passing around some telemetry state. Is that still happening?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmmm, yes - that was for setting the extension using contextvars - what are you aiming at?

Copy link
Contributor

Choose a reason for hiding this comment

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

self._do_capture runs on a totally different thread than capture, so those context vars won't be valid, right?

@safaricd safaricd closed this Feb 13, 2026
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