-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Adding Tracing for Agents #32020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Adding Tracing for Agents #32020
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1dc85b4
First version of agent tracking
ganeshyb f3cea7c
Tracing for agents, run, thread and message
ganeshyb 53c09bb
Merge branch 'feature/azure-ai-sdk' into users/ganeshbh/agents-tracing
ganeshyb 92c00a5
Tracing for submit tools and list messages
ganeshyb 6c7f020
Merge branch 'feature/azure-ai-sdk' into users/ganeshbh/agents-tracing
ganeshyb 8fdf4ef
address comments
ganeshyb fdd2d84
added test and recording for telemetry
ganeshyb 50e29b4
fixed an error wrt to playback
ganeshyb 80b3186
Address PR comments
ganeshyb 4050662
Merge branch 'feature/azure-ai-sdk' into users/ganeshbh/agents-tracing
ganeshyb 5e3ded2
Add tracing for more functions
ganeshyb 3262c60
Address test failures in playback mode
ganeshyb 7495468
Merge branch 'feature/azure-ai-sdk' into users/ganeshbh/agents-tracing
ganeshyb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
sdk/ai/ai-projects/samples-dev/agents/agentCreateWithTracingConsole.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* Demonstrates How to instrument and get tracing using open telemetry. | ||
* | ||
* @summary Create Agent and instrument using open telemetry. | ||
*/ | ||
|
||
import { trace, context } from "@opentelemetry/api"; | ||
import { registerInstrumentations } from "@opentelemetry/instrumentation"; | ||
import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk"; | ||
import { AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry-exporter" | ||
import { | ||
ConsoleSpanExporter, | ||
NodeTracerProvider, | ||
SimpleSpanProcessor, | ||
} from "@opentelemetry/sdk-trace-node"; | ||
|
||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
const provider = new NodeTracerProvider(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.register(); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [createAzureSdkInstrumentation()], | ||
}); | ||
|
||
import { AIProjectsClient } from "@azure/ai-projects" | ||
import { delay } from "@azure/core-util"; | ||
import { DefaultAzureCredential } from "@azure/identity"; | ||
|
||
const connectionString = process.env["AZURE_AI_PROJECTS_CONNECTION_STRING"] || "<endpoint>>;<subscription>;<resource group>;<project>"; | ||
let appInsightsConnectionString = process.env["APPLICATIONINSIGHTS_CONNECTION_STRING"] | ||
|
||
export async function main(): Promise<void> { | ||
|
||
const tracer = trace.getTracer("Agents Sample", "1.0.0"); | ||
|
||
const client = AIProjectsClient.fromConnectionString(connectionString || "", new DefaultAzureCredential()); | ||
|
||
if (!appInsightsConnectionString) { | ||
appInsightsConnectionString = await client.telemetry.getConnectionString(); | ||
} | ||
|
||
if (appInsightsConnectionString) { | ||
const exporter = new AzureMonitorTraceExporter({ connectionString: appInsightsConnectionString }); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
} | ||
|
||
await tracer.startActiveSpan("main", async (span) => { | ||
|
||
client.telemetry.updateSettings({enableContentRecording: true}) | ||
|
||
const agent = await client.agents.createAgent("gpt-4o", { name: "my-agent", instructions: "You are helpful agent" }, { tracingOptions: { tracingContext: context.active() } }); | ||
|
||
|
||
console.log(`Created agent, agent ID : ${agent.id}`); | ||
|
||
const thread = await client.agents.createThread(); | ||
console.log(`Created Thread, thread ID: ${thread.id}`); | ||
|
||
// Create message | ||
const message = await client.agents.createMessage(thread.id, { role: "user", content: "Hello, tell me a joke" }) | ||
console.log(`Created message, message ID ${message.id}`); | ||
|
||
// Create run | ||
let run = await client.agents.createRun(thread.id, agent.id); | ||
console.log(`Created Run, Run ID: ${run.id}`); | ||
|
||
while (["queued", "in_progress", "requires_action"].includes(run.status)) { | ||
await delay(1000); | ||
run = await client.agents.getRun(thread.id, run.id); | ||
console.log(`Current Run status - ${run.status}, run ID: ${run.id}`); | ||
} | ||
|
||
await client.agents.deleteAgent(agent.id); | ||
|
||
console.log(`Deleted agent`); | ||
|
||
await client.agents.listMessages(thread.id) | ||
|
||
span.end(); | ||
}); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.