Skip to content
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

Adding Tracing for Agents #32020

Merged
merged 13 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/ai/ai-projects/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "js",
"TagPrefix": "js/ai/ai-projects",
"Tag": "js/ai/ai-projects_34268fa552"
"Tag": "js/ai/ai-projects_57b254f3af"
}
26 changes: 24 additions & 2 deletions sdk/ai/ai-projects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
"//metadata": {
"constantPaths": [
{
"path": "src/projectsClient.ts",
"path": "src/generated/src/projectsClient.ts",
"prefix": "userAgentInfo"
},
{
"path": "src/constants.ts",
"prefix": "SDK_VERSION"
}
]
},
Expand All @@ -64,15 +68,22 @@
"@azure/core-lro": "^2.0.0",
"tslib": "^2.6.2",
"@azure/core-paging": "^1.5.0",
"@azure/core-sse": "^2.1.3"
"@azure/core-sse": "^2.1.3",
"@azure/core-tracing": "^1.2.0"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^4.3.0",
"@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.7",
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.27",
"@azure-tools/test-credential": "^2.0.0",
"@azure-tools/test-recorder": "^4.1.0",
"@azure-tools/test-utils-vitest": "^1.0.0",
"@microsoft/api-extractor": "^7.40.3",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/instrumentation": "0.53.0",
"@opentelemetry/sdk-trace-node": "^1.9.0",
"@vitest/browser": "^2.0.5",
"@vitest/coverage-istanbul": "^2.0.5",
"@types/node": "^18.0.0",
Expand Down Expand Up @@ -114,23 +125,34 @@
"./package.json": "./package.json",
".": {
"browser": {
"source": "./src/index.ts",
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"react-native": {
"source": "./src/index.ts",
"types": "./dist/react-native/index.d.ts",
"default": "./dist/react-native/index.js"
},
"import": {
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"//sampleConfiguration": {
"productName": "Azure AI Projects",
"productSlugs": [
"azure"
],
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure/ai-projects"
},
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js"
Expand Down
9 changes: 9 additions & 0 deletions sdk/ai/ai-projects/review/ai-projects.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class AIProjectsClient {
readonly agents: AgentsOperations;
readonly connections: ConnectionsOperations;
static fromConnectionString(connectionString: string, credential: TokenCredential, options?: AIProjectsClientOptions): AIProjectsClient;
readonly telemetry: TelemetryOperations;
}

// @public (undocumented)
Expand Down Expand Up @@ -1459,6 +1460,14 @@ export interface SystemDataOutput {
readonly lastModifiedAt?: string;
}

// @public
export interface TelemetryOperations {
getConnectionString(): Promise<string>;
ganeshyb marked this conversation as resolved.
Show resolved Hide resolved
getSettings(): TelemetryOptions;
// Warning: (ae-forgotten-export) The symbol "TelemetryOptions" needs to be exported by the entry point index.d.ts
updateSettings(options: TelemetryOptions): void;
}

// @public
export interface ThreadDeletionStatusOutput {
deleted: boolean;
Expand Down
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);
});
Loading
Loading