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-openai instrumentor. It also supports the legacy @azure/openai OpenAIClient when that package is present.
  • Python has no separate Azure package. The AzureOpenAI client ships inside the openai package, 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.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Azure OpenAI gateway setup to route calls through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-openai openai

Python has no dedicated Azure instrumentation package. The respan-instrumentation-openai OpenAIInstrumentor traces AzureOpenAI client calls because that client is part of the openai package.

2

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.

3

Initialize and run

1import os
2from openai import AzureOpenAI
3from respan import Respan
4from respan_instrumentation_openai import OpenAIInstrumentor
5
6respan = Respan(instrumentations=[OpenAIInstrumentor()])
7
8client = AzureOpenAI(
9 api_key=os.environ["AZURE_OPENAI_API_KEY"],
10 azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
11 api_version=os.environ["OPENAI_API_VERSION"],
12 azure_deployment="gpt-4o-mini",
13)
14
15response = client.chat.completions.create(
16 model="gpt-4o-mini",
17 messages=[{"role": "user", "content": "Say hello in one sentence."}],
18)
19print(response.choices[0].message.content)
4

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.

ParameterTypeDefaultDescription
apiKeystring | undefinedRESPAN_API_KEYRespan API key used to export traces.
baseURLstring | undefinedRESPAN_BASE_URLOptional Respan trace export API URL.
instrumentationsRespanInstrumentation[][]Include new AzureOpenAIInstrumentor() to activate Azure OpenAI tracing.
openAIModuleobject | undefineddynamic importPass the imported openai module when your app imports AzureOpenAI before initializing Respan.
azureOpenAIModuleobject | undefineddynamic importOptional legacy @azure/openai module for OpenAIClient applications.
traceContentbooleantrueCapture prompt and completion content when spans are exported.

Attributes

With propagateAttributes

Override per-request attributes using a context scope.

1import { AzureOpenAI } from "openai";
2import * as OpenAI from "openai";
3import { Respan } from "@respan/respan";
4import { AzureOpenAIInstrumentor } from "@respan/instrumentation-azure-openai";
5
6const respan = new Respan({
7 instrumentations: [
8 new AzureOpenAIInstrumentor({ openAIModule: OpenAI }),
9 ],
10});
11await respan.initialize();
12
13const client = new AzureOpenAI({
14 apiKey: process.env.AZURE_OPENAI_API_KEY,
15 endpoint: process.env.AZURE_OPENAI_ENDPOINT,
16 apiVersion: process.env.OPENAI_API_VERSION,
17 deployment: "gpt-4o-mini",
18});
19
20async function handleRequest(userId: string, question: string) {
21 await respan.propagateAttributes(
22 {
23 customer_identifier: userId,
24 thread_identifier: "conv_abc_123",
25 metadata: { plan: "pro" },
26 },
27 async () => {
28 const response = await client.chat.completions.create({
29 model: "gpt-4o-mini",
30 messages: [{ role: "user", content: question }],
31 });
32 console.log(response.choices[0]?.message?.content);
33 }
34 );
35}
AttributeTypeDescription
customer_identifierstringIdentifies the end user in Respan analytics.
thread_identifierstringGroups related messages into a conversation.
metadataRecord<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/openai OpenAIClient chat, completion, embedding, and streaming methods