Azure OpenAI (tracing)

The Azure OpenAI TypeScript instrumentation traces calls made through the current openai package’s AzureOpenAI client. It also supports the legacy @azure/openai OpenAIClient when that package is present.

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

$npm install @respan/respan @respan/instrumentation-azure-openai openai
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 { 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 apiKey: process.env.RESPAN_API_KEY,
8 baseURL: process.env.RESPAN_BASE_URL,
9 instrumentations: [
10 new AzureOpenAIInstrumentor({ openAIModule: OpenAI }),
11 ],
12});
13await respan.initialize();
14
15const client = new AzureOpenAI({
16 apiKey: process.env.AZURE_OPENAI_API_KEY,
17 endpoint: process.env.AZURE_OPENAI_ENDPOINT,
18 apiVersion: process.env.OPENAI_API_VERSION,
19 deployment: "gpt-4o-mini",
20});
21
22const response = await client.chat.completions.create({
23 model: "gpt-4o-mini",
24 messages: [{ role: "user", content: "Say hello in one sentence." }],
25});
26console.log(response.choices[0]?.message?.content);
27
28await respan.flush();
29await respan.shutdown();
4

View your trace

Open the Traces page to see Azure OpenAI chat, completion, embedding, and streaming spans.

Configuration

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