diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj index 0b43f3c8680c8..3a613943a4d6f 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj @@ -19,4 +19,9 @@ + + + + + diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/AzureMonitorAspNetCoreEventSource.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/AzureMonitorAspNetCoreEventSource.cs new file mode 100644 index 0000000000000..0b45da414cf2b --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/AzureMonitorAspNetCoreEventSource.cs @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Diagnostics.Tracing; +using System.Runtime.CompilerServices; +using Azure.Monitor.OpenTelemetry.Exporter.Internals; + +namespace Azure.Monitor.OpenTelemetry.AspNetCore +{ + /// + /// EventSource for the AzureMonitor AspNetCore Distro. + /// EventSource Guid at Runtime: ????. + /// + /// + /// PerfView Instructions: + /// + /// To collect all events: PerfView.exe collect -MaxCollectSec:300 -NoGui /onlyProviders=*OpenTelemetry-AzureMonitor-AspNetCore + /// To collect events based on LogLevel: PerfView.exe collect -MaxCollectSec:300 -NoGui /onlyProviders:OpenTelemetry-AzureMonitor-AspNetCore::Verbose + /// + /// Dotnet-Trace Instructions: + /// + /// To collect all events: dotnet-trace collect --process-id PID --providers OpenTelemetry-AzureMonitor-AspNetCore + /// To collect events based on LogLevel: dotnet-trace collect --process-id PID --providers OpenTelemetry-AzureMonitor-AspNetCore::Verbose + /// + /// Logman Instructions: + /// + /// Create a text file containing providers: echo "{????}" > providers.txt + /// Start collecting: logman -start exporter -pf providers.txt -ets -bs 1024 -nb 100 256 + /// Stop collecting: logman -stop exporter -ets + /// + /// + [EventSource(Name = EventSourceName)] + internal sealed class AzureMonitorAspNetCoreEventSource : EventSource + { + internal const string EventSourceName = "OpenTelemetry-AzureMonitor-AspNetCore"; + + internal static readonly AzureMonitorAspNetCoreEventSource Log = new AzureMonitorAspNetCoreEventSource(); + + [NonEvent] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private bool IsEnabled(EventLevel eventLevel) => IsEnabled(eventLevel, EventKeywords.All); + + [NonEvent] + public void ConfigureFailed(Exception ex) + { + if (IsEnabled(EventLevel.Error)) + { + ConfigureFailed(ex.FlattenException().ToInvariantString()); + } + } + + [Event(1, Message = "Failed to configure AzureMonitorOptions using the connection string from environment variables due to an exception: {0}", Level = EventLevel.Error)] + public void ConfigureFailed(string exceptionMessage) => WriteEvent(1, exceptionMessage); + } +} diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/DefaultAzureMonitorOptions.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/DefaultAzureMonitorOptions.cs index fd3afcb3403b2..d37562eb873aa 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/DefaultAzureMonitorOptions.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/DefaultAzureMonitorOptions.cs @@ -38,9 +38,9 @@ public void Configure(AzureMonitorOptions options) options.ConnectionString = connectionString; } } - catch + catch (Exception ex) { - // TODO: Log Error. + AzureMonitorAspNetCoreEventSource.Log.ConfigureFailed(ex); } } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj index 93c6de29aa42c..816cc45d6f430 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests.csproj @@ -16,6 +16,11 @@ + + + + + diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureMonitorAspNetCoreEventSourceTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureMonitorAspNetCoreEventSourceTests.cs new file mode 100644 index 0000000000000..a910acb3a19ac --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureMonitorAspNetCoreEventSourceTests.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Xunit; +using Azure.Monitor.OpenTelemetry.Exporter.Tests.CommonTestFramework; + +namespace Azure.Monitor.OpenTelemetry.AspNetCore.Tests +{ + public class AzureMonitorAspNetCoreEventSourceTests + { + /// + /// This test uses reflection to invoke every Event method in our EventSource class. + /// This validates that parameters are logged and helps to confirm that EventIds are correct. + /// + [Fact] + public void EventSourceTest_AzureMonitorExporterEventSource() + { + EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(AzureMonitorAspNetCoreEventSource.Log); + } + } +} diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs index 2c250ba190872..5a8947d463947 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs @@ -7,9 +7,6 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Tests { - /// - /// These tests depend on the to subscribe to the and write events to the . - /// public class AzureMonitorExporterEventSourceTests { /// diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Integration.Tests/Azure.Monitor.OpenTelemetry.Exporter.Integration.Tests.csproj b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Integration.Tests/Azure.Monitor.OpenTelemetry.Exporter.Integration.Tests.csproj index d8e6277b8bc2b..ecd1b3cd46055 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Integration.Tests/Azure.Monitor.OpenTelemetry.Exporter.Integration.Tests.csproj +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Integration.Tests/Azure.Monitor.OpenTelemetry.Exporter.Integration.Tests.csproj @@ -21,7 +21,7 @@ - +