Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features Added

### Breaking Changes
- Extended multi-tenant export to logs, off by default and enabled with the same `Azure.Monitor.OpenTelemetry.EnableMultiTenantExport` AppContext switch used for traces. When enabled, a `LogRecord` carrying the `microsoft.instrumentation_key` and `microsoft.ingestion_endpoint` attributes is sent to that endpoint instead of the exporter's own; log records without both attributes are dropped. The two routing attributes are consumed for routing and are not emitted as custom properties. Routing reads only `LogRecord.Attributes`, not logging scopes. Live Metrics disablement and the Microsoft Entra ID restriction that apply to multi-tenant traces apply to logs as well, since both are enforced on the shared transmitter.
Comment thread
harsimar marked this conversation as resolved.
Resolved

### Bugs Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Azure.Core.Pipeline;
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.MultiTenant;
using OpenTelemetry;
using OpenTelemetry.Logs;

Expand All @@ -17,8 +18,11 @@ namespace Azure.Monitor.OpenTelemetry.Exporter
public sealed class AzureMonitorLogExporter : BaseExporter<LogRecord>
{
private readonly ITransmitter _transmitter;
private readonly IMultiTenantTransmitter? _multiTenantTransmitter;
private readonly string _instrumentationKey;
private readonly bool _multiTenantEnabled;
private AzureMonitorResource? _resource;
private EndpointRouteBatch? _routeBatch;
private bool _disposed;

/// <summary>
Expand All @@ -30,9 +34,35 @@ public AzureMonitorLogExporter(AzureMonitorExporterOptions options) : this(Trans
}

internal AzureMonitorLogExporter(ITransmitter transmitter)
: this(transmitter, MultiTenantConfig.Enabled)
{
}

/// <remarks>
/// The gate is a constructor parameter so a test can exercise either path without mutating
/// process-wide state that other tests observe.
/// </remarks>
internal AzureMonitorLogExporter(ITransmitter transmitter, bool multiTenantEnabled)
{
_transmitter = transmitter;
_instrumentationKey = transmitter.InstrumentationKey;
_multiTenantEnabled = multiTenantEnabled;

if (_multiTenantEnabled)
{
if (transmitter is not IMultiTenantTransmitter multiTenantTransmitter)
{
// The caller already took a reference on the shared transmitter, which owns
// storage timers and statsbeat, so it has to be released before unwinding.
transmitter.Dispose();

throw new NotSupportedException($"Multi-tenant export requires a transmitter implementing {nameof(IMultiTenantTransmitter)}.");
}

_multiTenantTransmitter = multiTenantTransmitter;

AzureMonitorExporterEventSource.Log.MultiTenantExportEnabled();
}
}

internal AzureMonitorResource? LogResource => _resource ??= ParentProvider?.GetResource().CreateAzureMonitorResource(_instrumentationKey);
Expand All @@ -45,6 +75,11 @@ public override ExportResult Export(in Batch<LogRecord> batch)
// Prevent Azure Monitor's HTTP operations from being instrumented.
using var scope = SuppressInstrumentationScope.Begin();

if (_multiTenantEnabled)
{
return ExportMultiTenant(batch);
}

ExportResult exportResult = ExportResult.Failure;

try
Expand All @@ -63,6 +98,39 @@ public override ExportResult Export(in Batch<LogRecord> batch)
return exportResult;
}

private ExportResult ExportMultiTenant(in Batch<LogRecord> batch)
{
// A concurrent Export takes a fresh batch rather than sharing the cached one.
var routeBatch = Interlocked.Exchange(ref _routeBatch, null) ?? new EndpointRouteBatch();

try
{
LogsHelper.OtelToAzureMonitorLogsMultiTenant(batch, LogResource, routeBatch);

if (routeBatch.Count == 0)
{
// Routing attributes are stamped upstream only on records meant to be routed;
// a batch where nothing carried them is not addressed to any tenant, so report
// success rather than treating an empty routed batch as a failed export.
return ExportResult.Success;
}

// Blocks until every group has been sent, so Reset cannot run under a consumer that
// still holds a group's item list.
return _multiTenantTransmitter!.Track(routeBatch, TelemetryItemOrigin.AzureMonitorLogExporter, CancellationToken.None);
}
catch (Exception ex)
{
AzureMonitorExporterEventSource.Log.FailedToExport(nameof(AzureMonitorLogExporter), _instrumentationKey, ex);
return ExportResult.Failure;
}
finally
{
routeBatch.Reset();
Interlocked.Exchange(ref _routeBatch, routeBatch);
}
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public void FailedToPersistOnShutdown(string instrumentationKey, Exception ex)
[Event(60, Message = "Ingestion rejected a batch of {0} stored payloads with status code {1}. Retrying them individually to isolate the rejected payload.", Level = EventLevel.Warning)]
public void CoalescedBatchRejected(int batchSize, int statusCode) => WriteEvent(60, batchSize, statusCode);

[Event(61, Message = "Multi-tenant export is enabled. Activities are routed by their microsoft.instrumentation_key and microsoft.ingestion_endpoint tags.", Level = EventLevel.Informational)]
[Event(61, Message = "Multi-tenant export is enabled. Telemetry is routed by its microsoft.instrumentation_key and microsoft.ingestion_endpoint tags.", Level = EventLevel.Informational)]
Comment thread
harsimar marked this conversation as resolved.
Resolved
public void MultiTenantExportEnabled() => WriteEvent(61);

[Event(62, Message = "Live Metrics was disabled because multi-tenant export is enabled. Live Metrics streams to the endpoint from the exporter's own connection string and cannot serve routed tenants.", Level = EventLevel.Warning)]
Expand Down
Loading
Loading