Skip to content

Commit b0306ff

Browse files
author
Timothy Mothra
authored
[AzureMonitorExporter] refactor logging (part2) (Azure#36929)
* refactor logs messages * revert change to ExceptionExtensions * fix merge conflict * fix id
1 parent 14fea9c commit b0306ff

File tree

7 files changed

+61
-22
lines changed

7 files changed

+61
-22
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorLogExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override ExportResult Export(in Batch<LogRecord> batch)
4848
}
4949
catch (Exception ex)
5050
{
51-
AzureMonitorExporterEventSource.Log.WriteError("FailedToExport", ex);
51+
AzureMonitorExporterEventSource.Log.FailedToExport(ex);
5252
}
5353

5454
return exportResult;

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorMetricExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public override ExportResult Export(in Batch<Metric> batch)
5757
}
5858
catch (Exception ex)
5959
{
60-
AzureMonitorExporterEventSource.Log.WriteError("FailedToExport", ex);
60+
AzureMonitorExporterEventSource.Log.FailedToExport(ex);
6161
}
6262

6363
return exportResult;

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorTraceExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override ExportResult Export(in Batch<Activity> batch)
4848
}
4949
catch (Exception ex)
5050
{
51-
AzureMonitorExporterEventSource.Log.WriteError("FailedToExport", ex);
51+
AzureMonitorExporterEventSource.Log.FailedToExport(ex);
5252
}
5353

5454
return exportResult;

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/ApplicationInsightsRestClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal async Task<HttpMessage> InternalTrackAsync(IEnumerable<TelemetryItem> b
4141
}
4242
catch (Exception ex)
4343
{
44-
AzureMonitorExporterEventSource.Log.WriteError("FailedToSend", ex);
44+
AzureMonitorExporterEventSource.Log.FailedToTransmit(ex);
4545
if (ex.InnerException?.Source != "System.Net.Http")
4646
{
4747
message?.Dispose();
@@ -69,7 +69,7 @@ internal async Task<HttpMessage> InternalTrackAsync(ReadOnlyMemory<byte> body, C
6969
}
7070
catch (Exception ex)
7171
{
72-
AzureMonitorExporterEventSource.Log.WriteError("FailedToSend", ex);
72+
AzureMonitorExporterEventSource.Log.FailedToTransmit(ex);
7373
if (ex.InnerException?.Source != "System.Net.Http")
7474
{
7575
message?.Dispose();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Diagnostics.Tracing;
6+
using Azure.Monitor.OpenTelemetry.Exporter.Internals.ConnectionString;
7+
8+
namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics
9+
{
10+
internal sealed partial class AzureMonitorExporterEventSource : EventSource
11+
{
12+
[NonEvent]
13+
public void FailedToExport(Exception ex)
14+
{
15+
if (IsEnabled(EventLevel.Error))
16+
{
17+
FailedToExport(ex.FlattenException().ToInvariantString());
18+
}
19+
}
20+
21+
[Event(6, Message = "Failed to export due to an exception: {0}", Level = EventLevel.Error)]
22+
public void FailedToExport(string exceptionMessage) => WriteEvent(6, exceptionMessage);
23+
24+
[NonEvent]
25+
public void FailedToTransmit(Exception ex)
26+
{
27+
if (IsEnabled(EventLevel.Error))
28+
{
29+
FailedToTransmit(ex.FlattenException().ToInvariantString());
30+
}
31+
}
32+
33+
[Event(7, Message = "Failed to transmit due to an exception: {0}", Level = EventLevel.Error)]
34+
public void FailedToTransmit(string exceptionMessage) => WriteEvent(7, exceptionMessage);
35+
36+
[NonEvent]
37+
public void TransmissionFailed(bool fromStorage, int statusCode, ConnectionVars connectionVars, string? requestEndpoint, bool willRetry)
38+
{
39+
// TODO: INCLUDE EXACT ERROR MESSAGE FROM INGESTION
40+
if (IsEnabled(EventLevel.Warning))
41+
{
42+
TransmissionFailed(
43+
message: fromStorage ? "Transmission from storage failed." : "Transmission failed.",
44+
retryDetails: willRetry ? "Telemetry is stored offline for retry." : "Telemetry is dropped.",
45+
metaData: $"Instrumentation Key: {connectionVars.InstrumentationKey}, Configured Endpoint: {connectionVars.IngestionEndpoint}, Actual Endpoint: {requestEndpoint}"
46+
);
47+
}
48+
}
49+
50+
[Event(8, Message = "{0} {1} {2}")]
51+
public void TransmissionFailed(string message, string retryDetails, string metaData) => WriteEvent(8, message, retryDetails, metaData);
52+
}
53+
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Diagnostics/AzureMonitorExporterEventSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics
99
{
1010
[EventSource(Name = EventSourceName)]
11-
internal sealed class AzureMonitorExporterEventSource : EventSource
11+
internal sealed partial class AzureMonitorExporterEventSource : EventSource
1212
{
1313
internal const string EventSourceName = "OpenTelemetry-AzureMonitor-Exporter";
1414

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/HttpPipelineHelper.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,7 @@ internal static ExportResult HandleFailures(HttpMessage httpMessage, PersistentB
207207
}
208208
}
209209

210-
if (result == ExportResult.Success)
211-
{
212-
AzureMonitorExporterEventSource.Log.WriteWarning("FailedToTransmit", $"Error code is {statusCode}: Telemetry is stored offline for retry. Instrumentation Key: {connectionVars.InstrumentationKey}, Configured Endpoint: {connectionVars.IngestionEndpoint}, Actual Endpoint: {httpMessage.Request.Uri.Host}");
213-
}
214-
else
215-
{
216-
AzureMonitorExporterEventSource.Log.WriteWarning("FailedToTransmit", $"Error code is {statusCode}: Telemetry is dropped. Instrumentation Key: {connectionVars.InstrumentationKey}, Configured Endpoint: {connectionVars.IngestionEndpoint}, Actual Endpoint: {httpMessage.Request.Uri.Host}");
217-
}
210+
AzureMonitorExporterEventSource.Log.TransmissionFailed(fromStorage: false, statusCode: statusCode, connectionVars: connectionVars, requestEndpoint: httpMessage.Request.Uri.Host, willRetry: (result == ExportResult.Success));
218211

219212
return result;
220213
}
@@ -259,14 +252,7 @@ internal static void HandleFailures(HttpMessage httpMessage, PersistentBlob blob
259252
}
260253
}
261254

262-
if (shouldRetry)
263-
{
264-
AzureMonitorExporterEventSource.Log.WriteWarning("FailedToTransmitFromStorage", $"Error code is {statusCode}: Telemetry is stored offline for retry. Instrumentation Key: {connectionVars.InstrumentationKey}, Configured Endpoint: {connectionVars.IngestionEndpoint}, Actual Endpoint: {httpMessage.Request.Uri.Host}");
265-
}
266-
else
267-
{
268-
AzureMonitorExporterEventSource.Log.WriteWarning("FailedToTransmitFromStorage", $"Error code is {statusCode}: Telemetry is dropped. Instrumentation Key: {connectionVars.InstrumentationKey}, Configured Endpoint: {connectionVars.IngestionEndpoint}, Actual Endpoint: {httpMessage.Request.Uri.Host}");
269-
}
255+
AzureMonitorExporterEventSource.Log.TransmissionFailed(fromStorage: true, statusCode: statusCode, connectionVars: connectionVars, requestEndpoint: httpMessage.Request.Uri.Host, willRetry: shouldRetry);
270256
}
271257
}
272258
}

0 commit comments

Comments
 (0)