Send Microsoft Foundry hosted agent telemetry to Elastic
Microsoft Foundry hosted agents can send OpenTelemetry traces, logs, and metrics straight to Elastic. You set the OTLP exporter settings on your hosted agent and point them at the Elastic Cloud Managed OTLP Endpoint. You don't need to change your agent code, add an OpenTelemetry SDK, or run an OpenTelemetry Collector.
The telemetry is stored in Elastic in OTLP format.
Make sure you have the following:
- A Microsoft Foundry project with a hosted agent that you deploy with the Azure Developer CLI (
azd). To set one up, refer to Quickstart: Deploy your first hosted agent in the Microsoft documentation. - An Elastic Observability Serverless project or an Elastic Cloud Hosted deployment. On Elastic Cloud Hosted, the Elastic Cloud Managed OTLP Endpoint requires version 9.0 or later. The Elastic Cloud Managed OTLP Endpoint isn't available for self-managed, Elastic Cloud Enterprise, or Elastic Cloud on Kubernetes deployments.
- Your Elastic Cloud Managed OTLP Endpoint and an API key with the
event:writeprivilege for theapmapplication. To get both, follow the Find your endpoint and Create an API key steps in Quickstart: Send OTLP data to Elastic Serverless or Elastic Cloud Hosted.
For more about how hosted agents export telemetry, refer to Export hosted agent telemetry by using OpenTelemetry in the Microsoft documentation.
-
Store the API key in a Foundry connection
Create the connection before you deploy the agent, so the agent can read the key when it starts.
- In the Foundry portal, go to Manage → Project details → Connections, then select Add connection → Custom keys.
- In Key name, enter
otlp_headers. - In Key value, enter
Authorization=ApiKey <your-api-key>(the wordApiKey, a space, then the encoded key). - Select Is Secret.
- In Connection Name, enter
otel-secrets, then save the connection.
For more about connections, refer to Add a new connection to your project in the Microsoft documentation.
-
Add the exporter settings
Add these three environment variables to your hosted agent. They tell it where to send telemetry and how to authenticate. In
azure.yaml, add them to theenvmap of the service whosehostisazure.ai.agent:services: <your-agent-service>: env: OTEL_EXPORTER_OTLP_ENDPOINT: https://<your-motlp-endpoint> OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf OTEL_EXPORTER_OTLP_HEADERS: $${{connections.otel-secrets.credentials.otlp_headers}}For more about these settings, refer to Configure environment variables for a hosted agent in the Microsoft documentation.
-
Deploy the agent again
Foundry stores environment variables with each agent version, so the settings only apply to a new version. Run
azd deployto create one. For more about deploying, refer to Deploy a hosted agent in the Microsoft documentation.
Hosted agent telemetry can include prompts, tool calls, and model responses. Before you export, check that your organization's policies allow this data to be kept in Elastic.
Send a request to your hosted agent and allow a few seconds for the telemetry to arrive. Then, in Kibana Discover, run the following ES|QL queries. Each one filters on service.name, which Foundry sets to the name of your agent.
Traces:
FROM traces-generic.otel-default | WHERE service.name == "<your-agent-name>"Logs:
FROM logs-generic.otel-default | WHERE service.name == "<your-agent-name>"Metrics:
TS metrics-generic.otel-default | WHERE service.name == "<your-agent-name>"
If the logs show Forbidden errors from Application Insights, assign the agent's identity the Monitoring Metrics Publisher role on your project's Application Insights resource. For the steps, refer to Configure Microsoft Entra authentication for Foundry Agent trace ingestion in the Microsoft documentation.
In Kibana Discover, run this ES|QL query to sum input and output tokens by model. The attributes.gen_ai.operation.name == "chat" filter restricts the query to the spans of model calls:
FROM traces-generic.otel-default
| WHERE service.name == "<your-agent-name>" AND attributes.gen_ai.operation.name == "chat"
| STATS
input_tokens = SUM(attributes.gen_ai.usage.input_tokens),
output_tokens = SUM(attributes.gen_ai.usage.output_tokens)
BY attributes.gen_ai.request.model
- Export hosted agent telemetry by using OpenTelemetry (Microsoft documentation)
- Elastic Cloud Managed OTLP Endpoint reference
- LLM and agentic AI observability
- For model usage metrics and gateway logs from your Foundry resource, use the Microsoft Foundry integration.