Skip to content

Commit

Permalink
[AzureMonitorDistro] misc cleanup post LiveMetrics migration (#43517)
Browse files Browse the repository at this point in the history
* cleanup usings. merge EventSources. move LiveMetrics Processors to subdirectory

* merge conflict

* fix EventSource Ids

* remove region
  • Loading branch information
TimothyMothra committed Apr 19, 2024
1 parent 8dc1163 commit 88d92fb
Show file tree
Hide file tree
Showing 27 changed files with 237 additions and 336 deletions.
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using Azure.Monitor.OpenTelemetry.AspNetCore.Models;
using Azure.Monitor.OpenTelemetry.Exporter.Internals;

namespace Azure.Monitor.OpenTelemetry.AspNetCore
Expand Down Expand Up @@ -42,29 +43,20 @@ internal sealed class AzureMonitorAspNetCoreEventSource : EventSource
private bool IsEnabled(EventLevel eventLevel) => IsEnabled(eventLevel, EventKeywords.All);

[NonEvent]
public void ConfigureFailed(Exception ex)
public void MapLogLevelFailed(EventLevel level)
{
if (IsEnabled(EventLevel.Error))
if (IsEnabled(EventLevel.Warning))
{
ConfigureFailed(ex.FlattenException().ToInvariantString());
MapLogLevelFailed(level.ToString());
}
}

[NonEvent]
public void GetEnvironmentVariableFailed(string envVarName, Exception ex)
public void ConfigureFailed(System.Exception ex)
{
if (IsEnabled(EventLevel.Error))
{
GetEnvironmentVariableFailed(envVarName, ex.FlattenException().ToInvariantString());
}
}

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

Expand All @@ -80,6 +72,15 @@ public void MapLogLevelFailed(EventLevel level)
[Event(4, Message = "Vendor instrumentation added for: {0}.", Level = EventLevel.Verbose)]
public void VendorInstrumentationAdded(string packageName) => WriteEvent(4, packageName);

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

[Event(5, Message = "Failed to Read environment variable {0}, exception: {1}", Level = EventLevel.Error)]
public void GetEnvironmentVariableFailed(string envVarName, string exceptionMessage) => WriteEvent(5, envVarName, exceptionMessage);

Expand All @@ -88,5 +89,179 @@ public void MapLogLevelFailed(EventLevel level)

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

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

[Event(8, Message = "Failed to parse ConnectionString due to an exception: {0}", Level = EventLevel.Error)]
public void FailedToParseConnectionString(string exceptionMessage) => WriteEvent(8, exceptionMessage);

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

[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)
{
if (IsEnabled(EventLevel.Warning))
{
SdkVersionCreateFailed(ex.FlattenException().ToInvariantString());
}
}

[Event(11, Message = "Failed to create an SDK version due to an exception. Not user actionable. {0}", Level = EventLevel.Warning)]
public void SdkVersionCreateFailed(string exceptionMessage) => WriteEvent(11, exceptionMessage);

[Event(12, Message = "Version string exceeds expected length. This is only for internal telemetry and can safely be ignored. Type Name: {0}. Version: {1}", Level = EventLevel.Verbose)]
public void VersionStringUnexpectedLength(string typeName, string value) => WriteEvent(12, typeName, value);

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

[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)
{
if (IsEnabled(EventLevel.Error))
{
ServiceCallFailed(name: "Ping", response.Status, response.ReasonPhrase);
}
}

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

[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}.", Level = EventLevel.Error)]
public void ServiceCallFailed(string name, int statusCode, string reasonPhrase) => WriteEvent(15, name, statusCode, reasonPhrase);

[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);
}
}
Expand Up @@ -3,16 +3,11 @@

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using System.Threading;
using Azure.Core.Pipeline;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.ConnectionString;
using Azure.Monitor.OpenTelemetry.AspNetCore.Models;
using Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics;
using Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Diagnostics;
using Azure.Monitor.OpenTelemetry.AspNetCore.Models;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.ConnectionString;

namespace Azure.Monitor.OpenTelemetry.AspNetCore
{
Expand Down Expand Up @@ -83,11 +78,11 @@ public QuickPulseResponse CustomPing(string ikey, string apikey = null, int? xMs
{
using var document = JsonDocument.Parse(message.Response.ContentStream);
value = ServiceError.DeserializeServiceError(document.RootElement);
LiveMetricsExporterEventSource.Log.PingFailedWithServiceError(message.Response.Status, value);
AzureMonitorAspNetCoreEventSource.Log.PingFailedWithServiceError(message.Response.Status, value);
}

Debug.WriteLine($"{DateTime.Now}: Ping FAILED: {message.Response.Status} {message.Response.ReasonPhrase}.");
LiveMetricsExporterEventSource.Log.PingFailed(message.Response);
AzureMonitorAspNetCoreEventSource.Log.PingFailed(message.Response);
return new QuickPulseResponse(success: false);
}
default:
Expand Down Expand Up @@ -142,11 +137,11 @@ public QuickPulseResponse CustomPost(string ikey, string apikey = null, string x
{
using var document = JsonDocument.Parse(message.Response.ContentStream);
value = ServiceError.DeserializeServiceError(document.RootElement);
LiveMetricsExporterEventSource.Log.PostFailedWithServiceError(message.Response.Status, value);
AzureMonitorAspNetCoreEventSource.Log.PostFailedWithServiceError(message.Response.Status, value);
}

Debug.WriteLine($"{DateTime.Now}: Post FAILED: {message.Response.Status} {message.Response.ReasonPhrase}.");
LiveMetricsExporterEventSource.Log.PostFailed(message.Response);
AzureMonitorAspNetCoreEventSource.Log.PostFailed(message.Response);
return new QuickPulseResponse(success: false);
}
default:
Expand Down
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.Monitor.OpenTelemetry.AspNetCore.Models
{
internal partial class CollectionConfigurationError
Expand Down
@@ -1,6 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;

namespace Azure.Monitor.OpenTelemetry.AspNetCore.Models
{
Expand Down
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics;

namespace Azure.Monitor.OpenTelemetry.AspNetCore.Models
{
/// <summary> Additional properties used to calculate metrics. </summary>
Expand Down
Expand Up @@ -2,9 +2,6 @@
// Licensed under the MIT License.

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.Diagnostics;
using Azure.Monitor.OpenTelemetry.AspNetCore.Models;

namespace Azure.Monitor.OpenTelemetry.AspNetCore.LiveMetrics.DataCollection
Expand All @@ -30,7 +27,7 @@ public void Add(DocumentIngress document)
}
else
{
LiveMetricsExporterEventSource.Log.DroppedDocument(documentType: document.DocumentType);
AzureMonitorAspNetCoreEventSource.Log.DroppedDocument(documentType: document.DocumentType);
}
}

Expand Down

0 comments on commit 88d92fb

Please sign in to comment.