Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureMonitor] refactor EventSource after LiveMetrics move #48640

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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,6 @@
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
using Azure.Monitor.OpenTelemetry.LiveMetrics.Models;

namespace Azure.Monitor.OpenTelemetry.AspNetCore
{
Expand Down Expand Up @@ -102,18 +101,6 @@ public void FailedToReadEnvironmentVariables(System.Exception ex)
[Event(9, Message = "Failed to read environment variables due to an exception. This may prevent the Exporter from initializing. {0}", Level = EventLevel.Warning)]
public void FailedToReadEnvironmentVariables(string errorMessage) => WriteEvent(9, errorMessage);

[NonEvent]
public void AccessingEnvironmentVariableFailedWarning(string environmentVariable, System.Exception ex)
{
if (IsEnabled(EventLevel.Warning))
{
AccessingEnvironmentVariableFailedWarning(environmentVariable, ex.FlattenException().ToInvariantString());
}
}

[Event(10, Message = "Accessing environment variable - {0} failed with exception: {1}.", Level = EventLevel.Warning)]
public void AccessingEnvironmentVariableFailedWarning(string environmentVariable, string exceptionMessage) => WriteEvent(10, environmentVariable, exceptionMessage);

[NonEvent]
public void SdkVersionCreateFailed(System.Exception ex)
{
Expand All @@ -140,122 +127,5 @@ public void ErrorInitializingPartOfSdkVersion(string typeName, System.Exception

[Event(13, Message = "Failed to get Type version while initialize SDK version due to an exception. Not user actionable. Type: {0}. {1}", Level = EventLevel.Warning)]
public void ErrorInitializingPartOfSdkVersion(string typeName, string exceptionMessage) => WriteEvent(13, typeName, exceptionMessage);

[Event(14, Message = "HttpPipelineBuilder is built with AAD Credentials. TokenCredential: {0} Scope: {1}", Level = EventLevel.Informational)]
public void SetAADCredentialsToPipeline(string credentialTypeName, string scope) => WriteEvent(14, credentialTypeName, scope);

[NonEvent]
public void PingFailed(Response response, string configuredEndpoint, string actualEndpoint)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailed(name: "Ping", response.Status, response.ReasonPhrase, configuredEndpoint, actualEndpoint);
}
}

[NonEvent]
public void PostFailed(Response response, string configuredEndpoint, string actualEndpoint)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailed(name: "Post", response.Status, response.ReasonPhrase, configuredEndpoint, actualEndpoint);
}
}

[NonEvent]
public void PingFailedWithUnknownException(System.Exception ex)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailedWithUnknownException(name: "Ping", ex.ToInvariantString());
}
}

[NonEvent]
public void PostFailedWithUnknownException(System.Exception ex)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailedWithUnknownException(name: "Post", ex.ToInvariantString());
}
}

[NonEvent]
public void PingFailedWithServiceError(int statusCode, ServiceError serviceError)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailedWithServiceError(name: "Ping", statusCode, serviceError.Code, serviceError.Exception, serviceError.Message);
}
}

[NonEvent]
public void PostFailedWithServiceError(int statusCode, ServiceError serviceError)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailedWithServiceError(name: "Post", statusCode, serviceError.Code, serviceError.Exception, serviceError.Message);
}
}

[Event(15, Message = "Service call failed. Name: {0}. Status Code: {1} Reason: {2} Configured Endpoint: {3} Actual Endpoint: {4}", Level = EventLevel.Error)]
public void ServiceCallFailed(string name, int statusCode, string reasonPhrase, string configuredEndpoint, string actualEndpoint) => WriteEvent(15, name, statusCode, reasonPhrase, configuredEndpoint, actualEndpoint);

[Event(16, Message = "Service call failed with exception. Name: {0}. Exception: {1}", Level = EventLevel.Error)]
public void ServiceCallFailedWithUnknownException(string name, string exceptionMessage) => WriteEvent(16, name, exceptionMessage);

[Event(17, Message = "Service call failed. Name: {0}. Status Code: {1}. Code: {2}. Message: {3}. Exception: {4}.", Level = EventLevel.Error)]
public void ServiceCallFailedWithServiceError(string name, int statusCode, string code, string message, string exception) => WriteEvent(17, name, statusCode, code, message, exception);

[NonEvent]
public void StateMachineFailedWithUnknownException(System.Exception ex)
{
if (IsEnabled(EventLevel.Error))
{
StateMachineFailedWithUnknownException(ex.ToInvariantString());
}
}

[Event(18, Message = "LiveMetrics State Machine failed with exception: {0}", Level = EventLevel.Error)]
public void StateMachineFailedWithUnknownException(string exceptionMessage) => WriteEvent(18, exceptionMessage);

[NonEvent]
public void DroppedDocument(DocumentType documentType)
{
if (IsEnabled(EventLevel.Warning))
{
DroppedDocument(documentType.ToString());
}
}

[Event(19, Message = "Document was dropped. DocumentType: {0}. Not user actionable.", Level = EventLevel.Warning)]
public void DroppedDocument(string documentType) => WriteEvent(19, documentType);

[Event(20, Message = "Failure to calculate CPU Counter. Unexpected negative timespan: PreviousCollectedTime: {0}. RecentCollectedTime: {0}. Not user actionable.", Level = EventLevel.Error)]
public void ProcessCountersUnexpectedNegativeTimeSpan(long previousCollectedTime, long recentCollectedTime) => WriteEvent(20, previousCollectedTime, recentCollectedTime);

[Event(21, Message = "Failure to calculate CPU Counter. Unexpected negative value: PreviousCollectedValue: {0}. RecentCollectedValue: {0}. Not user actionable.", Level = EventLevel.Error)]
public void ProcessCountersUnexpectedNegativeValue(long previousCollectedValue, long recentCollectedValue) => WriteEvent(21, previousCollectedValue, recentCollectedValue);

[Event(22, Message = "Calculated Cpu Counter: Period: {0}. DiffValue: {1}. CalculatedValue: {2}. ProcessorCount: {3}. NormalizedValue: {4}", Level = EventLevel.Verbose)]
public void ProcessCountersCpuCounter(long period, long diffValue, double calculatedValue, int processorCount, double normalizedValue) => WriteEvent(22, period, diffValue, calculatedValue, processorCount, normalizedValue);

[NonEvent]
public void FailedToCreateTelemetryDocument(string documentTypeName, System.Exception ex)
{
if (IsEnabled(EventLevel.Error))
{
FailedToCreateTelemetryDocument(documentTypeName, ex.ToInvariantString());
}
}

[Event(23, Message = "Failed to create telemetry document due to an exception. DocumentType: {0}. Exception: {1}", Level = EventLevel.Error)]
public void FailedToCreateTelemetryDocument(string documentTypeName, string exceptionMessage) => WriteEvent(23, documentTypeName, exceptionMessage);

[Event(24, Message = "Redirect received from LiveMetrics service: {0}", Level = EventLevel.Informational)]
public void LiveMetricsRedirectReceived(string redirectUri) => WriteEvent(24, redirectUri);

[Event(25, Message = "Polling Interval received from LiveMetrics service: {0}", Level = EventLevel.Informational)]
public void LiveMetricsPolingIntervalReceived(int pollingInterval) => WriteEvent(25, pollingInterval);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static ConnectionVars GetValues(string connectionString)
AzureMonitorExporterEventSource.Log.FailedToParseConnectionString(ex);
#elif ASP_NET_CORE_DISTRO
AzureMonitorAspNetCoreEventSource.Log.FailedToParseConnectionString(ex);
#else
System.Diagnostics.Debug.Write(ex.ToString());
#elif LIVE_METRICS_PROJECT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend just using else here.

LiveMetrics.AzureMonitorLiveMetricsEventSource.Log.FailedToParseConnectionString(ex);
#endif
throw new InvalidOperationException("Connection String Error: " + ex.Message, ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public DefaultPlatform()
AzureMonitorExporterEventSource.Log.FailedToReadEnvironmentVariables(ex);
#elif ASP_NET_CORE_DISTRO
AzureMonitorAspNetCoreEventSource.Log.FailedToReadEnvironmentVariables(ex);
#else
System.Diagnostics.Debug.Write(ex.ToString());
#elif LIVE_METRICS_PROJECT
LiveMetrics.AzureMonitorLiveMetricsEventSource.Log.FailedToReadEnvironmentVariables(ex);
#endif
_environmentVariables = new Dictionary<string, object>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ internal static bool IsDistro
AzureMonitorExporterEventSource.Log.VersionStringUnexpectedLength(type.Name, versionString);
#elif ASP_NET_CORE_DISTRO
AzureMonitorAspNetCoreEventSource.Log.VersionStringUnexpectedLength(type.Name, versionString);
#elif LIVE_METRICS_PROJECT
LiveMetrics.AzureMonitorLiveMetricsEventSource.Log.VersionStringUnexpectedLength(type.Name, versionString);
#endif
return shortVersion.Substring(0, 20);
}
Expand All @@ -73,8 +75,8 @@ internal static bool IsDistro
AzureMonitorExporterEventSource.Log.ErrorInitializingPartOfSdkVersion(type.Name, ex);
#elif ASP_NET_CORE_DISTRO
AzureMonitorAspNetCoreEventSource.Log.ErrorInitializingPartOfSdkVersion(type.Name, ex);
#else
System.Diagnostics.Debug.Write(ex.ToString());
#elif LIVE_METRICS_PROJECT
LiveMetrics.AzureMonitorLiveMetricsEventSource.Log.ErrorInitializingPartOfSdkVersion(type.Name, ex);
#endif
return null;
}
Expand All @@ -90,9 +92,9 @@ private static string GetSdkVersion()
#if AZURE_MONITOR_EXPORTER
string? extensionVersion = GetVersion(typeof(AzureMonitorTraceExporter));
#elif ASP_NET_CORE_DISTRO
string? extensionVersion = GetVersion(typeof(LiveMetrics.LiveMetricsActivityProcessor));
#else
string extensionVersion = "Undefined";
string? extensionVersion = GetVersion(typeof(AzureMonitorAspNetCoreEventSource));
#elif LIVE_METRICS_PROJECT
string? extensionVersion = GetVersion(typeof(LiveMetrics.AzureMonitorLiveMetricsEventSource));
#endif

if (IsDistro)
Expand All @@ -108,8 +110,8 @@ private static string GetSdkVersion()
AzureMonitorExporterEventSource.Log.SdkVersionCreateFailed(ex);
#elif ASP_NET_CORE_DISTRO
AzureMonitorAspNetCoreEventSource.Log.SdkVersionCreateFailed(ex);
#else
System.Diagnostics.Debug.Write(ex.ToString());
#elif LIVE_METRICS_PROJECT
LiveMetrics.AzureMonitorLiveMetricsEventSource.Log.SdkVersionCreateFailed(ex);
#endif

// Return a default value in case of failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

namespace Azure.Monitor.OpenTelemetry.LiveMetrics
{
#if LIVE_METRICS_PROJECT
/// <summary>
/// EventSource for the AzureMonitor LiveMetrics Client.
/// EventSource Guid at Runtime: TODO.
/// EventSource Guid at Runtime: 72f5588f-fa1c-502e-0627-e13dd2bd67c9.
/// (This guid can be found by debugging this class and inspecting the "Log" singleton and reading the "Guid" property).
/// </summary>
/// <remarks>
Expand All @@ -27,7 +26,7 @@ namespace Azure.Monitor.OpenTelemetry.LiveMetrics
/// </list>
/// Logman Instructions:
/// <list type="number">
/// <item>Create a text file containing providers: <code>echo "{TODO}" > providers.txt</code></item>
/// <item>Create a text file containing providers: <code>echo "{72f5588f-fa1c-502e-0627-e13dd2bd67c9}" > providers.txt</code></item>
/// <item>Start collecting: <code>logman -start exporter -pf providers.txt -ets -bs 1024 -nb 100 256</code></item>
/// <item>Stop collecting: <code>logman -stop exporter -ets</code></item>
/// </list>
Expand All @@ -43,42 +42,6 @@ internal sealed class AzureMonitorLiveMetricsEventSource : EventSource
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsEnabled(EventLevel eventLevel) => IsEnabled(eventLevel, EventKeywords.All);

[NonEvent]
public void MapLogLevelFailed(EventLevel level)
{
if (IsEnabled(EventLevel.Warning))
{
MapLogLevelFailed(level.ToString());
}
}

[NonEvent]
public void ConfigureFailed(System.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);

[Event(2, Message = "Package reference for {0} found. Backing off from default included instrumentation. Action Required: You must manually configure this instrumentation.", Level = EventLevel.Warning)]
public void FoundInstrumentationPackageReference(string packageName) => WriteEvent(2, packageName);

[Event(3, Message = "No instrumentation package found with name: {0}.", Level = EventLevel.Verbose)]
public void NoInstrumentationPackageReference(string packageName) => WriteEvent(3, packageName);

[Event(4, Message = "Vendor instrumentation added for: {0}.", Level = EventLevel.Verbose)]
public void VendorInstrumentationAdded(string packageName) => WriteEvent(4, packageName);

[Event(5, Message = "Failed to map unknown EventSource log level in AzureEventSourceLogForwarder {0}", Level = EventLevel.Warning)]
public void MapLogLevelFailed(string level) => WriteEvent(5, level);

[Event(6, Message = "Found existing Microsoft.Extensions.Azure.AzureEventSourceLogForwarder registration.", Level = EventLevel.Informational)]
public void LogForwarderIsAlreadyRegistered() => WriteEvent(6);

[NonEvent]
public void FailedToParseConnectionString(System.Exception ex)
{
Expand All @@ -103,18 +66,6 @@ public void FailedToReadEnvironmentVariables(System.Exception ex)
[Event(9, Message = "Failed to read environment variables due to an exception. This may prevent the Exporter from initializing. {0}", Level = EventLevel.Warning)]
public void FailedToReadEnvironmentVariables(string errorMessage) => WriteEvent(9, errorMessage);

[NonEvent]
public void AccessingEnvironmentVariableFailedWarning(string environmentVariable, System.Exception ex)
{
if (IsEnabled(EventLevel.Warning))
{
AccessingEnvironmentVariableFailedWarning(environmentVariable, ex.FlattenException().ToInvariantString());
}
}

[Event(10, Message = "Accessing environment variable - {0} failed with exception: {1}.", Level = EventLevel.Warning)]
public void AccessingEnvironmentVariableFailedWarning(string environmentVariable, string exceptionMessage) => WriteEvent(10, environmentVariable, exceptionMessage);

[NonEvent]
public void SdkVersionCreateFailed(System.Exception ex)
{
Expand All @@ -141,7 +92,6 @@ public void ErrorInitializingPartOfSdkVersion(string typeName, System.Exception

[Event(13, Message = "Failed to get Type version while initialize SDK version due to an exception. Not user actionable. Type: {0}. {1}", Level = EventLevel.Warning)]
public void ErrorInitializingPartOfSdkVersion(string typeName, string exceptionMessage) => WriteEvent(13, typeName, exceptionMessage);

[Event(14, Message = "HttpPipelineBuilder is built with AAD Credentials. TokenCredential: {0} Scope: {1}", Level = EventLevel.Informational)]
public void SetAADCredentialsToPipeline(string credentialTypeName, string scope) => WriteEvent(14, credentialTypeName, scope);

Expand Down Expand Up @@ -220,18 +170,6 @@ public void StateMachineFailedWithUnknownException(System.Exception ex)
[Event(18, Message = "LiveMetrics State Machine failed with exception: {0}", Level = EventLevel.Error)]
public void StateMachineFailedWithUnknownException(string exceptionMessage) => WriteEvent(18, exceptionMessage);

[NonEvent]
public void DroppedDocument(DocumentType documentType)
{
if (IsEnabled(EventLevel.Warning))
{
DroppedDocument(documentType.ToString());
}
}

[Event(19, Message = "Document was dropped. DocumentType: {0}. Not user actionable.", Level = EventLevel.Warning)]
public void DroppedDocument(string documentType) => WriteEvent(19, documentType);

[Event(20, Message = "Failure to calculate CPU Counter. Unexpected negative timespan: PreviousCollectedTime: {0}. RecentCollectedTime: {0}. Not user actionable.", Level = EventLevel.Error)]
public void ProcessCountersUnexpectedNegativeTimeSpan(long previousCollectedTime, long recentCollectedTime) => WriteEvent(20, previousCollectedTime, recentCollectedTime);

Expand Down Expand Up @@ -259,5 +197,4 @@ public void FailedToCreateTelemetryDocument(string documentTypeName, System.Exce
[Event(25, Message = "Polling Interval received from LiveMetrics service: {0}", Level = EventLevel.Informational)]
public void LiveMetricsPolingIntervalReceived(int pollingInterval) => WriteEvent(25, pollingInterval);
}
#endif
}
Loading