-
Notifications
You must be signed in to change notification settings - Fork 8
Use Agent365ExporterOptions for exporter and create AgenticTokenCache… #46
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
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
996b5e7
use Agent365ExporterOptions for exporter and create AgenticTokenCache…
jsl517 9c50c72
tests
jsl517 51b4d86
copilot comment
jsl517 a0de073
configure batch span processor
jsl517 88605eb
copilot comment
jsl517 ca17b96
checkpoint to use cached context and authorization to exchange token.
jsl517 1b89510
only do refresh token in message handler, unit tests
jsl517 fa95349
sample update
jsl517 1c3940e
comments
jsl517 36c5966
comments
jsl517 3d174b3
observability always use prod
jsl517 9ca9f6a
Merge branch 'main' into users/pefan/exportoption
jsl517 7271de5
should be lowercase
jsl517 ea0cfc6
Revert "observability always use prod"
jsl517 5221582
Merge branch 'main' into users/pefan/exportoption
jsl517 64728ca
default to prod
jsl517 f1d9e61
expose Agent365ExporterOptions
jsl517 e07318b
expose Agent365ExporterOptions
jsl517 057aae7
move azure token cache to its own package
jsl517 20133be
readme update
jsl517 d322096
Merge branch 'main' into users/pefan/exportoption
jsl517 6058c84
fix package.json error
jsl517 1149e09
fix logging
jsl517 5e3d93b
lint,jest config
jsl517 55b5536
cleanup
jsl517 cf31254
comment
jsl517 142ec28
rename
jsl517 54ddd3e
lint
jsl517 5303929
comment
jsl517 0b11ffa
Merge branch 'main' into users/pefan/exportoption
fpfp100 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
100 changes: 100 additions & 0 deletions
100
packages/agents-a365-observability-tokencache/README.md
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,100 @@ | ||
| # @microsoft/agents-a365-observability-tokencache | ||
|
|
||
| Observability token cache utilities for the Agent365 SDK. This package provides: | ||
|
|
||
| - In‑memory storage for observability (telemetry/export) bearer tokens | ||
| - Early refresh using an expiration skew (default 60s before real expiry) | ||
| - Automatic fallback TTL if the token lacks an `exp` claim | ||
| - Linear retry on transient failures (timeouts, 5xx, 408, 429) during token exchange | ||
| - Per key (agent + tenant) serialization to avoid thundering herds | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pnpm add @microsoft/agents-a365-observability-tokencache | ||
| ``` | ||
|
|
||
| ## Core API | ||
|
|
||
| ```ts | ||
| import { AgenticTokenCacheInstance } from '@microsoft/agents-a365-observability-tokencache'; | ||
| ``` | ||
|
|
||
| ## Using With Observability Builder (Telemetry Exporter) | ||
|
|
||
| When configuring the observability manager, supply a token resolver. Do **not** pass the method reference directly (it would lose `this`); wrap it to preserve context or use `bind`: | ||
|
|
||
| ```ts | ||
| import { Builder, ObservabilityManager, Agent365ExporterOptions } from '@microsoft/agents-a365-observability'; | ||
| import { AgenticTokenCacheInstance } from '@microsoft/agents-a365-observability-tokencache'; | ||
|
|
||
| export const a365Observability = ObservabilityManager.configure((builder: Builder) => { | ||
| const exporterOptions = new Agent365ExporterOptions(); | ||
| exporterOptions.maxQueueSize = 10; | ||
|
|
||
| builder | ||
| .withService('TypeScript Sample Agent', '1.0.0') | ||
| .withClusterCategory('prod') | ||
| .withExporterOptions(exporterOptions) | ||
| // Wrap to ensure `this` binding (so internal map & methods work). | ||
| .withTokenResolver((agentId, tenantId) => AgenticTokenCacheInstance.getObservabilityToken(agentId, tenantId)); | ||
| }); | ||
| ``` | ||
|
|
||
| Alternatively: | ||
|
|
||
| ```ts | ||
| builder.withTokenResolver(AgenticTokenCacheInstance.getObservabilityToken.bind(AgenticTokenCacheInstance)); | ||
| ``` | ||
|
|
||
| ## Example: Preloading in an Agent Turn | ||
|
|
||
| ```ts | ||
| import { AgenticTokenCacheInstance } from '@microsoft/agents-a365-observability-tokencache'; | ||
| import { getObservabilityAuthenticationScope } from '@microsoft/agents-a365-runtime'; | ||
|
|
||
| // Inside activity handler: | ||
| await AgenticTokenCacheInstance.RefreshObservabilityToken( | ||
| agentInfo.agentId, | ||
| tenantInfo.tenantId, | ||
| context, | ||
| agentApplication.authorization, | ||
| getObservabilityAuthenticationScope() | ||
| ); | ||
| // Token is now cached (non-blocking if acquisition fails; subsequent resolver will return null until success). | ||
| ``` | ||
|
|
||
| ## Custom Token Resolver Example (Using Application-Level Cache) | ||
|
|
||
| If you prefer to manage the token yourself and only use this cache for retrieval: | ||
|
|
||
| ```ts | ||
| const tokenResolver = (agentId: string, tenantId: string): string | null => { | ||
| const t = AgenticTokenCacheInstance.getObservabilityToken(agentId, tenantId); | ||
| return t ?? null; | ||
| }; | ||
|
|
||
| builder.withTokenResolver(tokenResolver); | ||
| ``` | ||
|
|
||
| ## When to Refresh vs. When to Read | ||
|
|
||
| - Use `RefreshObservabilityToken` when you have access to `TurnContext` and `Authorization` and want to ensure a fresh token is available. | ||
| - Use `getObservabilityToken` inside exporters / resolvers where only agent & tenant IDs are available, and you can tolerate `null` (meaning skip authenticated export or wait until later). | ||
|
|
||
| ## Handling Expiration | ||
|
|
||
| The cache considers a token expired if: | ||
| 1. It has an `exp` and current time >= `exp * 1000 - skewMs` (default skew 60s) | ||
| 2. Or it has no `exp` and current time >= `acquiredOn + maxTokenAgeMs` (default 1h) | ||
|
|
||
| Expired tokens are not returned; they force a refresh on next `RefreshObservabilityToken` call. | ||
|
|
||
| ## Error & Retry Behavior | ||
|
|
||
| - Transient errors (timeouts, network issues, 408, 429, 5xx) trigger up to 2 linear backoff retries (200ms, then 400ms). | ||
| - Non-retriable errors clear the entry’s token & expiry; subsequent reads return `null` until a successful refresh. | ||
| - All events are logged via lightweight console wrappers (info/warn/error). | ||
|
|
||
| ## License | ||
| MIT | ||
6 changes: 6 additions & 0 deletions
6
packages/agents-a365-observability-tokencache/jest.config.cjs
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,6 @@ | ||
| module.exports = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| testPathIgnorePatterns: ['/integration/'], | ||
| clearMocks: true, | ||
| }; |
63 changes: 63 additions & 0 deletions
63
packages/agents-a365-observability-tokencache/package.json
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,63 @@ | ||
| { | ||
| "name": "@microsoft/agents-a365-observability-tokencache", | ||
| "version": "0.0.0-placeholder", | ||
| "description": "Microsoft Agent 365 SDK observability token cache utilities", | ||
| "keywords": [ | ||
| "agent365", | ||
| "observability", | ||
| "telemetry", | ||
| "token", | ||
| "cache" | ||
| ], | ||
| "homepage": "https://github.com/microsoft/Agent365-nodejs", | ||
| "bugs": { | ||
| "url": "https://github.com/microsoft/Agent365-nodejs/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/microsoft/Agent365-nodejs.git", | ||
| "directory": "packages/agents-a365-observability-tokencache" | ||
| }, | ||
| "license": "MIT", | ||
| "author": "Microsoft", | ||
| "main": "./dist/cjs/index.js", | ||
| "module": "./dist/esm/index.js", | ||
| "types": "./dist/cjs/index.d.ts", | ||
| "files": [ | ||
| "dist", | ||
| "README.md", | ||
| "CHANGELOG.md" | ||
| ], | ||
| "scripts": { | ||
| "build": "npm run build:cjs && npm run build:esm", | ||
| "build:cjs": "npx tsc --project tsconfig.cjs.json", | ||
| "build:esm": "npx tsc --project tsconfig.esm.json", | ||
| "build:watch": "npx tsc --watch", | ||
| "clean": "npx rimraf dist", | ||
| "lint": "eslint src/**/*.ts", | ||
| "lint:fix": "eslint src/**/*.ts --fix", | ||
| "test": "jest --config ./jest.config.cjs --passWithNoTests", | ||
| "test:watch": "jest --watch", | ||
| "test:coverage": "jest --coverage", | ||
| "pack": "npm pack --pack-destination=../" | ||
| }, | ||
| "dependencies": { | ||
| "@microsoft/agents-hosting": "^1.1.0-alpha.85", | ||
| "@microsoft/agents-a365-runtime": "workspace:*", | ||
| "@microsoft/agents-a365-observability": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/jest": "^29.5.12", | ||
| "@types/node": "^20.0.0", | ||
| "@typescript-eslint/eslint-plugin": "^6.0.0", | ||
| "@typescript-eslint/parser": "^6.0.0", | ||
| "eslint": "^8.0.0", | ||
| "jest": "^29.7.0", | ||
| "rimraf": "^6.0.0", | ||
| "ts-jest": "^29.2.0", | ||
| "typescript": "^5.0.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18.0.0" | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
packages/agents-a365-observability-tokencache/src/.eslintrc.json
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,32 @@ | ||
| { | ||
| "root": true, | ||
| "parser": "@typescript-eslint/parser", | ||
| "parserOptions": { | ||
| "ecmaVersion": 2020, | ||
| "sourceType": "module" | ||
| }, | ||
| "plugins": ["@typescript-eslint"], | ||
| "extends": ["eslint:recommended"], | ||
| "rules": { | ||
| "no-unused-vars": "off", | ||
| "@typescript-eslint/no-unused-vars": ["error", { | ||
| "argsIgnorePattern": "^_", | ||
| "varsIgnorePattern": "^_", | ||
| "caughtErrorsIgnorePattern": "^_" | ||
| }], | ||
| "@typescript-eslint/no-explicit-any": "error", | ||
| "prefer-const": "error", | ||
| "no-var": "error", | ||
| "no-console": "error", | ||
| "semi": ["error", "always"], | ||
| "quotes": ["error", "single"], | ||
| "indent": ["error", 4], | ||
| "no-trailing-spaces": "error" | ||
| }, | ||
| "env": { | ||
| "node": true, | ||
| "es6": true, | ||
| "jest": true | ||
| }, | ||
| "ignorePatterns": ["dist/**/*", "node_modules/**/*", "*.js"] | ||
| } |
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.