Skip to content

Commit

Permalink
Disable self-logging and self-tracing in Azure Monitor distro, export…
Browse files Browse the repository at this point in the history
…er, and livemetrics (#43359)

* Disable seld-logging and self-tracing in Azure Monitor distro, exporter, and livemetrics
  • Loading branch information
lmolkova committed Apr 17, 2024
1 parent 018a6e1 commit f40ee39
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

### Bugs Fixed

* Turned off internal spans and logs in distro HTTP pipelines
([#43359](https://github.com/Azure/azure-sdk-for-net/pull/43359))

### Other Changes

* Update OpenTelemetry dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public class AzureMonitorOptions : ClientOptions
/// </summary>
public string StorageDirectory { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="AzureMonitorOptions"/>.
/// </summary>
public AzureMonitorOptions()
{
// users can explicitly change it, but by default we don't want internal logs to be reported to Azure Monitor.
this.Diagnostics.IsDistributedTracingEnabled = false;
this.Diagnostics.IsLoggingEnabled = false;
}

internal void SetValueToExporterOptions(AzureMonitorExporterOptions exporterOptions)
{
exporterOptions.ConnectionString = ConnectionString;
Expand All @@ -70,6 +80,8 @@ internal void SetValueToExporterOptions(AzureMonitorExporterOptions exporterOpti
{
exporterOptions.Transport = Transport;
}
exporterOptions.Diagnostics.IsDistributedTracingEnabled = Diagnostics.IsDistributedTracingEnabled;
exporterOptions.Diagnostics.IsLoggingEnabled = Diagnostics.IsLoggingEnabled;
}

internal void SetValueToLiveMetricsExporterOptions(LiveMetricsExporterOptions liveMetricsExporterOptions)
Expand All @@ -81,6 +93,8 @@ internal void SetValueToLiveMetricsExporterOptions(LiveMetricsExporterOptions li
{
liveMetricsExporterOptions.Transport = Transport;
}
liveMetricsExporterOptions.Diagnostics.IsDistributedTracingEnabled = Diagnostics.IsDistributedTracingEnabled;
liveMetricsExporterOptions.Diagnostics.IsLoggingEnabled = Diagnostics.IsLoggingEnabled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AzureSdkLoggingTests
public async Task DistroLogForwarderIsAdded(LogLevel eventLevel, string expectedMessage)
{
var builder = WebApplication.CreateBuilder();
var transport = new MockTransport(new MockResponse(200).SetContent("ok"));
var transport = new MockTransport(_ => new MockResponse(200).SetContent("ok"));
SetUpOTelAndLogging(builder, transport, LogLevel.Information);

using var app = builder.Build();
Expand Down Expand Up @@ -56,7 +56,7 @@ public async Task DistroLogForwarderIsAdded(LogLevel eventLevel, string expected
public async Task PublicLogForwarderIsAdded(LogLevel eventLevel, string expectedMessage)
{
var builder = WebApplication.CreateBuilder();
var transport = new MockTransport(new MockResponse(200).SetContent("ok"));
var transport = new MockTransport(_ => new MockResponse(200).SetContent("ok"));
SetUpOTelAndLogging(builder, transport, LogLevel.Information);

builder.Services.TryAddSingleton<Microsoft.Extensions.Azure.AzureEventSourceLogForwarder>();
Expand Down Expand Up @@ -86,18 +86,62 @@ public async Task PublicLogForwarderIsAdded(LogLevel eventLevel, string expected
}
}

private void WaitForRequest(MockTransport transport)
[Fact]
public async Task SelfDiagnosticsIsDisabled()
{
var enableLevel = LogLevel.Debug;

var builder = WebApplication.CreateBuilder();
var transport = new MockTransport(_ => new MockResponse(200).SetContent("ok"));
builder.Logging.ClearProviders();
bool logAzureFilterCalled = false;
builder.Logging.AddFilter((name, level) =>
{
if (name != null && name.StartsWith("Azure"))
{
logAzureFilterCalled = true;
return level >= enableLevel;
}
return false;
});
builder.Services.AddOpenTelemetry().UseAzureMonitor(config =>
{
config.Transport = transport;
config.ConnectionString = $"InstrumentationKey={Guid.NewGuid()}";
config.EnableLiveMetrics = true;
Assert.False(config.Diagnostics.IsLoggingEnabled);
Assert.False(config.Diagnostics.IsDistributedTracingEnabled);
});

using var app = builder.Build();
await app.StartAsync();

// let's get some live metric requests first to check that no logs were recorded for them
WaitForRequest(transport, r => r.Uri.Host == "rt.services.visualstudio.com");

// now let's wait for track requests
var trackRequests = WaitForRequest(transport, r => r.Uri.Host == "dc.services.visualstudio.com");
Assert.Empty(trackRequests);

// since LiveMetrics logging is disabled, we shouldn't even have logging policy trying to log anything.
Assert.False(logAzureFilterCalled);
}

private IEnumerable<MockRequest> WaitForRequest(MockTransport transport, Func<MockRequest, bool>? filter = null)
{
filter = filter ?? (_ => true);
SpinWait.SpinUntil(
condition: () =>
{
Thread.Sleep(10);
return transport.Requests.Count > 0;
return transport.Requests.Where(filter).Any();
},
timeout: TimeSpan.FromSeconds(10));

return transport.Requests.Where(filter);
}

private static async Task AssertContentContains(MockRequest request, string expectedMessage, LogLevel expectedLevel)
private static async Task<string> AssertContentContains(MockRequest request, string expectedMessage, LogLevel expectedLevel)
{
using var contentStream = new MemoryStream();
await request.Content.WriteToAsync(contentStream, default);
Expand All @@ -110,6 +154,7 @@ private static async Task AssertContentContains(MockRequest request, string expe

// also check that message appears just once
Assert.Equal(content.IndexOf(jsonMessage), content.LastIndexOf(jsonMessage));
return content;
}

private static async Task AssertContentDoesNotContain(List<MockRequest> requests, string expectedMessage)
Expand Down Expand Up @@ -143,8 +188,6 @@ private static void SetUpOTelAndLogging(WebApplicationBuilder builder, MockTrans
config.Transport = transport;
config.ConnectionString = $"InstrumentationKey={Guid.NewGuid()}";
config.EnableLiveMetrics = false;
config.Diagnostics.IsDistributedTracingEnabled = false;
config.Diagnostics.IsLoggingEnabled = false;
});
}

Expand Down
2 changes: 2 additions & 0 deletions sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* All three signals (Traces, Metrics, and Logs) now support OpenTelemetry's ["service.version"](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/resource#service) in Resource attributes.
This is mapped as [Application Version](https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete#application-version) in Application Insights.
([#42174](https://github.com/Azure/azure-sdk-for-net/pull/42174))
* Turned off internal spans and logs in exporter HTTP pipeline
([#43359](https://github.com/Azure/azure-sdk-for-net/pull/43359))

### Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public AzureMonitorExporterOptions() : this(LatestVersion)
public AzureMonitorExporterOptions(ServiceVersion version = LatestVersion)
{
this.Version = version;
// users can explicitly change it, but by default we don't want exporter internal logs to be reported to Azure Monitor.
this.Diagnostics.IsDistributedTracingEnabled = false;
this.Diagnostics.IsLoggingEnabled = false;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

* Fix for "Comitted Memory" not updating.
([#42760](https://github.com/Azure/azure-sdk-for-net/pull/42760))
* Turned off internal spans and logs in LiveMetrics HTTP pipeline
([#43359](https://github.com/Azure/azure-sdk-for-net/pull/43359))

### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ public class LiveMetricsExporterOptions : ClientOptions
/// Get or sets the value of <see cref="TokenCredential" />.
/// </summary>
public TokenCredential Credential { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="LiveMetricsExporterOptions"/>.
/// </summary>
public LiveMetricsExporterOptions()
{
// users can explicitly change it, but by default we don't want live metrics internal logs to be reported to Azure Monitor.
this.Diagnostics.IsDistributedTracingEnabled = false;
this.Diagnostics.IsLoggingEnabled = false;
}
}

0 comments on commit f40ee39

Please sign in to comment.