# OtlpConfigurationExtensions Methods

- Package: [Aspire.Hosting](/reference/api/csharp/aspire.hosting.md)
- Type: [OtlpConfigurationExtensions](/reference/api/csharp/aspire.hosting/otlpconfigurationextensions.md)
- Kind: `Methods`
- Members: `4`

Provides extension methods for configuring OpenTelemetry in projects using environment variables.

## AddOtlpEnvironment(IResource, IConfiguration, IHostEnvironment)

- Name: `AddOtlpEnvironment(IResource, IConfiguration, IHostEnvironment)`
- Modifiers: `static`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/OtlpConfigurationExtensions.cs#L25-L33)

Configures OpenTelemetry in projects using environment variables.

```csharp
public static class OtlpConfigurationExtensions
{
    public static void AddOtlpEnvironment(
        IResource resource,
        IConfiguration configuration,
        IHostEnvironment environment)
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource to add annotations to.
- `configuration` (`IConfiguration`)
  The configuration to use for the OTLP exporter endpoint URL.
- `environment` (`IHostEnvironment`)
  The host environment to check if the application is running in development mode.

## AddOtlpEnvironment(IResource, IConfiguration, IHostEnvironment, OtlpProtocol)

- Name: `AddOtlpEnvironment(IResource, IConfiguration, IHostEnvironment, OtlpProtocol)`
- Modifiers: `static`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/OtlpConfigurationExtensions.cs#L44-L52)

Configures OpenTelemetry in projects using environment variables.

```csharp
public static class OtlpConfigurationExtensions
{
    public static void AddOtlpEnvironment(
        IResource resource,
        IConfiguration configuration,
        IHostEnvironment environment,
        OtlpProtocol protocol)
    {
        // ...
    }
}
```

## Parameters

- `resource` ([IResource](/reference/api/csharp/aspire.hosting/iresource.md))
  The resource to add annotations to.
- `configuration` (`IConfiguration`)
  The configuration to use for the OTLP exporter endpoint URL.
- `environment` (`IHostEnvironment`)
  The host environment to check if the application is running in development mode.
- `protocol` ([OtlpProtocol](/reference/api/csharp/aspire.hosting/otlpprotocol.md))
  The protocol to use for the OTLP exporter. If not set, it will try gRPC then Http.

## WithOtlpExporter(IResourceBuilder<T>)

- Name: `WithOtlpExporter(IResourceBuilder<T>)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/OtlpConfigurationExtensions.cs#L138-L142)

Injects the appropriate environment variables to allow the resource to enable sending telemetry to the dashboard.

1. It sets the OTLP endpoint to the value of the `ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL` environment variable.
2. It sets the service name and instance id to the resource name and UID. Values are injected by the orchestrator.
3. It sets a small batch schedule delay in development. This reduces the delay that OTLP exporter waits to sends telemetry and makes the dashboard telemetry pages responsive.

```csharp
public static class OtlpConfigurationExtensions
{
    public static IResourceBuilder<T> WithOtlpExporter<T>(
        this IResourceBuilder<T> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### Ignored by ATS

- Reason: Polyglot app hosts use the internal withOtlpExporter dispatcher export.

## WithOtlpExporter(IResourceBuilder<T>, OtlpProtocol)

- Name: `WithOtlpExporter(IResourceBuilder<T>, OtlpProtocol)`
- Modifiers: `extension`
- Returns: [IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md)
- Source: [GitHub](https://github.com/microsoft/aspire/blob/bd20f904cde09ecb9c9ae5116a6f13614bf2d7c2/src/Aspire.Hosting/OtlpConfigurationExtensions.cs#L172-L176)

Injects the appropriate environment variables to allow the resource to enable sending telemetry to the dashboard.

1. It sets the OTLP endpoint to the value of the `ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL` environment variable.
2. It sets the service name and instance id to the resource name and UID. Values are injected by the orchestrator.
3. It sets a small batch schedule delay in development. This reduces the delay that OTLP exporter waits to sends telemetry and makes the dashboard telemetry pages responsive.

```csharp
public static class OtlpConfigurationExtensions
{
    public static IResourceBuilder<T> WithOtlpExporter<T>(
        this IResourceBuilder<T> builder,
        OtlpProtocol protocol)
    {
        // ...
    }
}
```

## Parameters

- `builder` ([IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md))
  The resource builder.
- `protocol` ([OtlpProtocol](/reference/api/csharp/aspire.hosting/otlpprotocol.md))
  The protocol to use for the OTLP exporter. If not set, it will try gRPC then Http.

## Returns

[IResourceBuilder<T>](/reference/api/csharp/aspire.hosting/iresourcebuilder-1.md) -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### Ignored by ATS

- Reason: Polyglot app hosts use the internal withOtlpExporter dispatcher export.
