﻿---
title: Send Microsoft Foundry hosted agent telemetry to Elastic
description: Send OpenTelemetry traces, logs, and metrics from Microsoft Foundry hosted agents to Elastic Observability through the Elastic Cloud Managed OTLP Endpoint.
url: https://www.elastic.co/elastic/docs-builder/docs/5113/solutions/observability/get-started/opentelemetry/use-cases/llms/send-microsoft-foundry-hosted-agent-telemetry
products:
  - Elastic Cloud Hosted
  - Elastic Cloud Serverless
  - Elastic Documentation
  - Elastic Observability
applies_to:
  - Serverless Observability projects: Generally available
  - Elastic Cloud Hosted: Generally available
---

# Send Microsoft Foundry hosted agent telemetry to Elastic
[Microsoft Foundry](https://learn.microsoft.com/en-us/azure/foundry/what-is-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.

## Before you begin

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](https://learn.microsoft.com/en-us/azure/foundry/agents/quickstarts/quickstart-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:write` privilege for the `apm` application. 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](/elastic/docs-builder/docs/5113/solutions/observability/get-started/quickstart-elastic-cloud-otel-endpoint#send-data-to-elastic).


## Add Elastic as an export destination

For more about how hosted agents export telemetry, refer to [Export hosted agent telemetry by using OpenTelemetry](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/configure-hosted-agent-telemetry?tabs=otlp-export) in the Microsoft documentation.
<stepper>
  <step title="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.
    1. In the Foundry portal, go to **Manage** → **Project details** → **Connections**, then select **Add connection** → **Custom keys**.
    2. In **Key name**, enter `otlp_headers`.
    3. In **Key value**, enter `Authorization=ApiKey <your-api-key>` (the word `ApiKey`, a space, then the encoded key).
    4. Select **Is Secret**.
    5. In **Connection Name**, enter `otel-secrets`, then save the connection.
    For more about connections, refer to [Add a new connection to your project](https://learn.microsoft.com/en-us/azure/foundry/how-to/connections-add) in the Microsoft documentation.
  </step>

  <step title="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 the `env` map of the service whose `host` is `azure.ai.agent`:
    ```yaml
    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](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/configure-hosted-agent-env-variables) in the Microsoft documentation.
  </step>

  <step title="Deploy the agent again">
    Foundry stores environment variables with each agent version, so the settings only apply to a new version. Run `azd deploy` to create one. For more about deploying, refer to [Deploy a hosted agent](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/deploy-hosted-agent) in the Microsoft documentation.
  </step>
</stepper>

<important>
  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.
</important>


## Check that telemetry arrives

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:
  ```esql
  FROM traces-generic.otel-default
  | WHERE service.name == "<your-agent-name>"
  ```
- Logs:
  ```esql
  FROM logs-generic.otel-default
  | WHERE service.name == "<your-agent-name>"
  ```
- Metrics:
  ```esql
  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](https://learn.microsoft.com/en-us/azure/foundry/observability/how-to/trace-ingestion-entra-authentication) in the Microsoft documentation.

## Example: Token usage by model

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:
```esql
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
```


## Related pages

- [Export hosted agent telemetry by using OpenTelemetry](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/configure-hosted-agent-telemetry?tabs=otlp-export) (Microsoft documentation)
- [Elastic Cloud Managed OTLP Endpoint reference](https://www.elastic.co/elastic/docs-builder/docs/5113/reference/managed-inputs/managed-otlp-endpoint)
- [LLM and agentic AI observability](https://www.elastic.co/elastic/docs-builder/docs/5113/solutions/observability/applications/llm-observability)
- For model usage metrics and gateway logs from your Foundry resource, use the [Microsoft Foundry integration](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/5113/reference/azure_ai_foundry).