opik icon indicating copy to clipboard operation
opik copied to clipboard

[FR]: Conditional tracing at runtime

Open agustinparrotta opened this issue 7 months ago • 5 comments

Feature Request: Enable/Disable Opik Tracing Dynamically at Runtime

Currently, Opik tracing, once configured and applied (e.g., via the @opik_track decorator or integrations like track_bedrock), appears to be always active for the instrumented functions or integrations. There isn't a straightforward, built-in way to dynamically enable or disable tracing for the entire application or specific parts of it at runtime without changing the instrumentation code or restarting the application.

This limitation can be problematic in several scenarios:

  • We want to trace only a subset of requests/operations (e.g., based on user type, request parameters or a dynamic sampling rate).
  • We want to avoid the overhead of tracing for all invocations, especially in high-throughput systems where constant tracing might be unnecessary or too costly.

Proposal

A global runtime toggle within the Opik SDK that controls whether tracing is active. The @opik_track decorator and other integrated tracing functions (like track_bedrock()) should consult this global state before initiating any tracing activities.

This could be implemented by:

  1. Introducing a global configuration function and state checker within the Opik SDK:

    from opik import set_tracing_active, is_tracing_active
    
    # To enable tracing globally
    set_tracing_active(True)
    
    # To disable tracing globally
    set_tracing_active(False)
    
    # To check current state
    if is_tracing_active():
        print("Opik tracing is currently active.")
    
  2. Modifying Opik's tracing mechanisms to respect this global state: The @opik_track decorator and other automatic instrumentation points (e.g., for Bedrock, other SDKs) would internally check opik.is_tracing_active() before creating spans or sending data. If is_tracing_active() returns False, the original function would be called directly without any tracing overhead.

    Simplified internal logic for @opik_track would look like this:

    # This is a conceptual representation of the internal change
    def opik_track_decorator_factory(*decorator_args, **decorator_kwargs):
        def actual_decorator(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                if opik.is_tracing_active(): # The new runtime check
                    # Proceed with actual opik_track logic
                    return get_original_opik_track_decorator(*decorator_args, **decorator_kwargs)(func)(*args, **kwargs)
                else:
                    # Execute the function without tracing
                    return func(*args, **kwargs)
            return wrapper
        return actual_decorator
    

agustinparrotta avatar May 09 '25 08:05 agustinparrotta

/bounty 100

vincentkoc avatar May 23 '25 09:05 vincentkoc

💎 $100 bounty • Comet

Steps to solve:

  1. Read Contributing Docs: See contributing guide and read on how to setup Opik and contribute to various parts of the code base.
  2. Start working: Comment /attempt #2090 with your implementation plan
  3. Submit work: Create a pull request including /claim #2090 in the PR body to claim the bounty
  4. Review: Team will review PR and any clarifying questions and if successful changes will be merged
  5. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

❗ Important guidelines:

  • Do NOT start multiple bounties
  • To claim a bounty, you need to provide a short demo video of your changes in your pull request
  • If anything is unclear, ask for clarification before starting as this will help avoid potential rework
  • Low quality AI PRs will not receive review and will be closed
  • Please ask to be assigned before attempting to work on the bounty

Thank you for contributing to Comet!

Attempt Started (UTC) Solution Actions
🟢 @vladimirrotariu Jul 21, 2025, 05:54:39 AM #2789 Reward
🟢 @Gmin2 May 27, 2025, 07:31:28 PM #2289 Reward

algora-pbc[bot] avatar May 23 '25 09:05 algora-pbc[bot]

can i take a shot at this @vincentkoc ?

Gmin2 avatar May 27 '25 06:05 Gmin2

@Gmin2 you can proceed 🙏

vincentkoc avatar May 27 '25 07:05 vincentkoc

/attempt #2090

Gmin2 avatar May 27 '25 19:05 Gmin2

Is this issue describing a situation I have encountered where, since adding @opik.track decorator explicitly (instead of just relying on track_openai as we did prior), in environments where we have Opik disabled (i.e. opik.configure is NOT called), I'm still seeing attempts to reach the Opik API?

test-1     | !!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
test-1     | ============================== 1 failed in 1.58s ===============================
test-1     | OPIK: Failed to process CreateSpansBatchMessage. Error: headers: {'date': 'Tue, 17 Jun 2025 18:38:47 GMT', 'content-type': 'application/json', 'content-length': '51', 'connection': 'keep-alive', 'set-cookie': 'AWSALB=ksqCJbUNuI5KNP2CiejwxuuSLpgyoOuQGT/UuxdtcS5GrGnf3OGq/j7dkB22vzzd4ckTuIRjuTVwICD2mqlBJqNuKm3lqJzEPYv9CGfYfe2Txze80Egks/yXDAwQ; Expires=Tue, 24 Jun 2025 18:38:47 GMT; Path=/, AWSALBCORS=ksqCJbUNuI5KNP2CiejwxuuSLpgyoOuQGT/UuxdtcS5GrGnf3OGq/j7dkB22vzzd4ckTuIRjuTVwICD2mqlBJqNuKm3lqJzEPYv9CGfYfe2Txze80Egks/yXDAwQ; Expires=Tue, 24 Jun 2025 18:38:47 GMT; Path=/; SameSite=None; Secure', 'server': 'nginx'}, status_code: 401, body: {'code': 401, 'message': 'API key should be provided'}

Is there any workaround where the Opik SDK will only attempt to connect to the remote API if it's been explicitly configured, like an "is active" concept?

mecampbellsoup avatar Jun 17 '25 18:06 mecampbellsoup

/attempt #2090

vladimirrotariu avatar Jul 21 '25 05:07 vladimirrotariu

Hi @vincentkoc , could you please assign to me this FR? 😁

vladimirrotariu avatar Jul 21 '25 13:07 vladimirrotariu

🎉🎈 @vladimirrotariu has been awarded $100 by Comet! 🎈🎊

algora-pbc[bot] avatar Jul 30 '25 12:07 algora-pbc[bot]