Azure OpenAI (tracing)
Trace calls made through the openai package’s AzureOpenAI client. The instrumentation differs by language:
- TypeScript uses the dedicated
@respan/instrumentation-azure-openaiinstrumentor. It also supports the legacy@azure/openaiOpenAIClientwhen that package is present. - Python has no separate Azure package. The
AzureOpenAIclient ships inside theopenaipackage, so the OpenAI instrumentation (respan-instrumentation-openai) captures Azure OpenAI calls.
This tracing setup uses your Azure OpenAI credentials directly. To route requests through Respan instead, use the Azure OpenAI gateway setup.
Set up Respan
Create an account at platform.respan.ai and grab an API key .
Paste this into your coding agent:
Help me set up Respan tracing with the Azure OpenAI integration.
Follow https://www.respan.ai/docs/integrations/azure-openai-tracing.mdSetup
Install packages
pip install respan-ai respan-instrumentation-openai openaiPython has no dedicated Azure instrumentation package. The respan-instrumentation-openai OpenAIInstrumentor traces AzureOpenAI client calls because that client is part of the openai package.
Set environment variables
export AZURE_OPENAI_API_KEY="YOUR_AZURE_OPENAI_API_KEY"
export AZURE_OPENAI_ENDPOINT="https://YOUR_RESOURCE.openai.azure.com"
export OPENAI_API_VERSION="2024-10-21"
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and OPENAI_API_VERSION are used for Azure OpenAI requests. RESPAN_API_KEY exports traces to Respan.
Initialize and run
import os
from openai import AzureOpenAI
from respan import Respan
from respan_instrumentation_openai import OpenAIInstrumentor
respan = Respan(instrumentations=[OpenAIInstrumentor()])
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version=os.environ["OPENAI_API_VERSION"],
azure_deployment="gpt-4o-mini",
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
print(response.choices[0].message.content)View your trace
Open the Traces page to see Azure OpenAI chat, completion, embedding, and streaming spans.
Configuration
The configuration, attributes, and captured-operations below describe the TypeScript AzureOpenAIInstrumentor. In Python, configuration lives on the OpenAI instrumentation — see the OpenAI SDK tracing page.
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey | string | undefined | RESPAN_API_KEY | Respan API key used to export traces. |
baseURL | string | undefined | RESPAN_BASE_URL | Optional Respan trace export API URL. |
instrumentations | RespanInstrumentation[] | [] | Include new AzureOpenAIInstrumentor() to activate Azure OpenAI tracing. |
openAIModule | object | undefined | dynamic import | Pass the imported openai module when your app imports AzureOpenAI before initializing Respan. |
azureOpenAIModule | object | undefined | dynamic import | Optional legacy @azure/openai module for OpenAIClient applications. |
traceContent | boolean | true | Capture prompt and completion content when spans are exported. |
Attributes
With propagateAttributes
Override per-request attributes using a context scope.
import { AzureOpenAI } from "openai";
import * as OpenAI from "openai";
import { Respan } from "@respan/respan";
import { AzureOpenAIInstrumentor } from "@respan/instrumentation-azure-openai";
const respan = new Respan({
instrumentations: [
new AzureOpenAIInstrumentor({ openAIModule: OpenAI }),
],
});
await respan.initialize();
const client = new AzureOpenAI({
apiKey: process.env.AZURE_OPENAI_API_KEY,
endpoint: process.env.AZURE_OPENAI_ENDPOINT,
apiVersion: process.env.OPENAI_API_VERSION,
deployment: "gpt-4o-mini",
});
async function handleRequest(userId: string, question: string) {
await respan.propagateAttributes(
{
customer_identifier: userId,
thread_identifier: "conv_abc_123",
metadata: { plan: "pro" },
},
async () => {
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: question }],
});
console.log(response.choices[0]?.message?.content);
}
);
}| Attribute | Type | Description |
|---|---|---|
customer_identifier | string | Identifies the end user in Respan analytics. |
thread_identifier | string | Groups related messages into a conversation. |
metadata | Record<string, unknown> | Custom key-value pairs. |
Captured operations
AzureOpenAI.chat.completions.create()AzureOpenAI.completions.create()AzureOpenAI.embeddings.create()- Streaming chat and text completion calls
- Legacy
@azure/openaiOpenAIClientchat, completion, embedding, and streaming methods
Gateway
See Azure OpenAI gateway setup to route calls through the Respan gateway.