Fix: OpenAI sample agent - propagate tenantId and fix service name in observability config#278
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Node.js OpenAI sample agent to (1) correct its observability service name and (2) propagate tenantId into observability scope metadata to support multi-tenant trace correlation.
Changes:
- Fixes the observability service name to “TypeScript OpenAI Sample Agent”.
- Extracts
tenantIdfromturnContext.activity.recipient.tenantIdwith an env var fallback and threads it intoOpenAIClient. - Adds
tenantIdontoAgentDetailspassed toInferenceScope.start().
| export async function getClient(authorization: Authorization, authHandlerName: string, turnContext: TurnContext, displayName = 'unknown'): Promise<Client> { | ||
| const modelName = getModelName(); | ||
| const tenantId = turnContext?.activity?.recipient?.tenantId ?? process.env.connections__service_connection__settings__tenantId ?? ''; |
There was a problem hiding this comment.
tenantId resolution here can become inconsistent with the tenant ID used during observability token preloading in src/agent.ts (which currently derives tenantId only from turnContext.activity.recipient.tenantId). In local/Playground scenarios where recipient.tenantId is missing, this code will fall back to the connections__...__tenantId env var, but the preloaded token may have been cached under an empty-tenant key; the exporter/tokenResolver will then look up a different key and fail to resolve a token. Consider centralizing tenantId resolution (and treating empty string as missing) so both token preloading and AgentDetails.tenantId use the exact same value.
Summary
Updates the
nodejs/openai/sample-agentto propagate tenant context through the observability pipeline and fixes an incorrect service name that was a copy-paste artifact from the Claude sample.Changes
src/client.ts
• L42: Changed observability service name from
'TypeScript Claude Sample Agent'to'TypeScript OpenAI Sample Agent'Reason: Copy-paste artifact from the Claude sample caused this agent's telemetry to be misattributed.
• L73: Added
tenantIdextraction fromturnContext.activity.recipient.tenantIdwith fallback toconnections__service_connection__settings__tenantIdenv varReason: Tenant context is needed downstream for observability span correlation in multi-tenant deployments.
• L107: Updated
new OpenAIClient(agent)tonew OpenAIClient(agent, tenantId)Reason: Threads the resolved tenant ID into the client instance so it can be included in
AgentDetails.• L114–L118: Added
tenantId: stringfield and updatedOpenAIClientconstructor to accept itReason: The client needs to hold the tenant ID for use when creating observability scopes.
• L157: Added
tenantId: this.tenantIdtoAgentDetailsininvokeAgentWithScope()Reason: Without
tenantIdonAgentDetails,InferenceScopetraces lack tenant context, making it impossible to filter and correlate traces per tenant.