traceloop / openllmetry

Open-source observability for your LLM application, based on OpenTelemetry
https://www.traceloop.com/openllmetry
Apache License 2.0
2.77k stars 441 forks source link

πŸ› Bug Report: Google Instrumentation does not work #2140

Open coffecup25 opened 1 week ago

coffecup25 commented 1 week ago

Which component is this bug for?

Traceloop SDK

πŸ“œ Description

The python sdk does not instrument the calls when using the google_generativeai sdk. However when manually calling the instrumentation like this:

from opentelemetry.instrumentation.google_generativeai import GoogleGenerativeAiInstrumentor

GoogleGenerativeAiInstrumentor().instrument()

The calls are correctly added to the trace.

πŸ‘Ÿ Reproduction steps

The example code doesn't work from the traceloop website doesn't work.

import google.generativeai as genai
from traceloop.sdk import Traceloop
from traceloop.sdk.decorators import workflow
from traceloop.sdk.instruments import Instruments

import os 
import dotenv

dotenv.load_dotenv(dotenv.find_dotenv())
Traceloop.init(app_name="joke_generation_service", instruments={Instruments.GOOGLE_GENERATIVEAI})

genai.configure(api_key=os.environ.get("GENAI_API_KEY"))

@workflow(name="joke_creation")
def create_joke():
    model = genai.GenerativeModel("gemini-1.0-pro-latest")
    response = model.generate_content(
        "Give me ten interview questions for the role of program manager.",
    )

    return response.text

print(create_joke())

πŸ‘ Expected behavior

The instrumentation should automatically work instead of having to manually initialise it.

πŸ‘Ž Actual Behavior with Screenshots

The calls using the google_generativeai calls didn't show up in my Grafana Tempo until I started the instrumentor from opentelementry manually

πŸ€– Python Version

3.12

πŸ“ƒ Provide any additional context for the Bug.

No response

πŸ‘€ Have you spent some time to check if this bug has been raised before?

Are you willing to submit PR?

None

dosubot[bot] commented 1 week ago

Hey there, @coffecup25! I'm here to help you out with any bugs, questions, or contributions you have in mind. Let's squash those bugs together! 🐞

The issue you're experiencing with the Traceloop SDK not automatically instrumenting the google_generativeai SDK can occur due to several reasons. Here are some steps and considerations to help address this:

  1. Manual Instrumentation: As you've discovered, manually calling the instrumentation using GoogleGenerativeAiInstrumentor().instrument() works. This suggests that the automatic instrumentation might not be initializing correctly in your setup.

  2. Initialization Order: Ensure that the Traceloop SDK is initialized before any calls to the google_generativeai SDK. This is crucial for the automatic instrumentation to hook into the library correctly.

  3. Check for Errors: The Traceloop SDK provides logging features that can help identify issues with automatic instrumentation. Ensure that any exceptions during the initialization are logged. You can modify the init_google_generativeai_instrumentor function to log exceptions if it's not already doing so.

  4. Environment Variables: Verify that all necessary environment variables, such as TRACELOOP_API_KEY, are correctly set and accessible in your environment.

  5. Disable Batch Sending: If you're working locally, try disabling batch sending to see if traces are sent immediately:

    Traceloop.init(disable_batch=True)
  6. Console Exporter: Use the ConsoleExporter to verify if traces are being generated and output to the console:

    from opentelemetry.sdk.trace.export import ConsoleSpanExporter
    
    Traceloop.init(exporter=ConsoleSpanExporter())

If these steps do not resolve the issue, it might be necessary to continue using manual instrumentation as a workaround until the automatic instrumentation is fixed or updated in the SDK.

To continue talking to Dosu, mention @dosu.