Skip to content

Commit

Permalink
Update OTEL collector to forward non-project resources (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Dec 7, 2024
1 parent 0c27e4e commit 32583d1
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace MetricsApp.AppHost.OpenTelemetryCollector;

internal sealed class OpenTelemetryCollectorLifecycleHook : IDistributedApplicationLifecycleHook
{
private const string OtelExporterOtlpEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT";

private readonly ILogger<OpenTelemetryCollectorLifecycleHook> _logger;

public OpenTelemetryCollectorLifecycleHook(ILogger<OpenTelemetryCollectorLifecycleHook> logger)
Expand All @@ -28,13 +30,16 @@ public Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, C
return Task.CompletedTask;
}

foreach (var resource in appModel.GetProjectResources())
foreach (var resource in appModel.Resources)
{
_logger.LogDebug("Forwarding telemetry for {ResourceName} to the collector.", resource.Name);

resource.Annotations.Add(new EnvironmentCallbackAnnotation((EnvironmentCallbackContext context) =>
{
context.EnvironmentVariables["OTEL_EXPORTER_OTLP_ENDPOINT"] = endpoint;
if (context.EnvironmentVariables.ContainsKey(OtelExporterOtlpEndpoint))
{
_logger.LogDebug("Forwarding telemetry for {ResourceName} to the collector.", resource.Name);

context.EnvironmentVariables[OtelExporterOtlpEndpoint] = endpoint;
}
}));
}

Expand Down

0 comments on commit 32583d1

Please sign in to comment.