Initialize the Netra SDK with Gemini instrumentation enabled. The SDK automatically traces all Gemini API calls once initialized.
from netra import Netraimport google.generativeai as genaiimport os# Initialize Netra with Gemini instrumentationNetra.init( app_name="my-ai-app", headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}", trace_content=True)# Use Gemini client as usual - all calls are automatically tracedgenai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))model = genai.GenerativeModel('gemini-pro')response = model.generate_content("What is observability?")print(response.text)
import { Netra } from "netra-sdk";import { GoogleGenerativeAI } from "@google/generative-ai";async function main() { // Initialize Netra with Gemini instrumentation (must await) await Netra.init({ appName: "my-ai-app", headers: `x-api-key=${process.env.NETRA_API_KEY}`, traceContent: true }); // Use Gemini client as usual - all calls are automatically traced const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY); const model = genAI.getGenerativeModel({ model: "gemini-pro" }); const result = await model.generateContent("What is observability?"); const response = await result.response; console.log(response.text());}main();
The SDK automatically handles streaming responses and captures the complete output:
model = genai.GenerativeModel('gemini-pro')response = model.generate_content("Tell me a story", stream=True)for chunk in response: print(chunk.text, end="")
const model = genAI.getGenerativeModel({ model: "gemini-pro" });const result = await model.generateContentStream("Tell me a story");for await (const chunk of result.stream) { const chunkText = chunk.text(); process.stdout.write(chunkText);}
Chat operations are also automatically instrumented:
model = genai.GenerativeModel('gemini-pro')chat = model.start_chat(history=[ {"role": "user", "parts": ["Hello"]}, {"role": "model", "parts": ["Great to meet you. What would you like to know?"]},])response = chat.send_message("I have 2 dogs in my house.")print(response.text)
const model = genAI.getGenerativeModel({ model: "gemini-pro" });const chat = model.startChat({ history: [ { role: "user", parts: [{ text: "Hello" }], }, { role: "model", parts: [{ text: "Great to meet you. What would you like to know?" }], }, ],});const result = await chat.sendMessage("I have 2 dogs in my house.");console.log(result.response.text());
Control which integrations are enabled using the instruments or blockInstruments configuration:
from netra import Netrafrom netra.instrumentation.instruments import InstrumentSet# Only enable Gemini instrumentationNetra.init( headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}", instruments={InstrumentSet.GOOGLE_GENERATIVEAI})# Or block specific instrumentationsNetra.init( headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}", block_instruments={InstrumentSet.HTTPX})
import { Netra, NetraInstruments } from "netra-sdk";// Only enable Gemini instrumentationawait Netra.init({ headers: `x-api-key=${process.env.NETRA_API_KEY}`, instruments: new Set([NetraInstruments.GOOGLE_GENERATIVEAI])});// Or block specific instrumentationsawait Netra.init({ headers: `x-api-key=${process.env.NETRA_API_KEY}`, blockInstruments: new Set([NetraInstruments.HTTP])});