Skip to content

Commit

Permalink
[Exporter.Geneva] Fix EventSource logging for GenevaExporter (#813)
Browse files Browse the repository at this point in the history
* Fix EventSource logging for GenevaExporter
  • Loading branch information
utpilla authored Dec 9, 2022
1 parent 7cfbe0f commit ff92fa3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
([#797](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/797))
* Fix the overflow bucket value serialization for Histogram.
([#805](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/805))
* Fix EventSource logging
([#813](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/813))

## 1.4.0-beta.5

Expand Down
48 changes: 36 additions & 12 deletions src/OpenTelemetry.Exporter.Geneva/Internal/ExporterEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
namespace OpenTelemetry.Exporter.Geneva;

[EventSource(Name = "OpenTelemetry-Exporter-Geneva")]
internal class ExporterEventSource : EventSource
internal sealed class ExporterEventSource : EventSource
{
public static readonly ExporterEventSource Log = new ExporterEventSource();
private const int EVENT_ID_TRACE = 1; // Failed to send Trace
private const int EVENT_ID_LOG = 2; // Failed to send Log
private const int EVENT_ID_METRIC = 3; // Failed to send Metric
private const int EVENT_ID_ERROR = 4; // Other common exporter exceptions

[Event(EVENT_ID_TRACE, Message = "Exporter failed to send trace data. Exception: {0}", Level = EventLevel.Error)]
[NonEvent]
public void FailedToSendTraceData(Exception ex)
{
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
// https://docs.microsoft.com/en-us/windows/win32/etw/about-event-tracing
// ETW has a size limit: The total event size is greater than 64K. This includes the ETW header plus the data or payload.
Expand All @@ -43,40 +43,64 @@ public void FailedToSendTraceData(Exception ex)
// descrs[0].Size = ((arg1.Length + 1) * 2);
// I'm assuming it calculates the size of string, then it should be:
// (count of chars) * sizeof(char) + sizeof(Length:int) = (str.Length * 2 + 4).
this.WriteEvent(EVENT_ID_TRACE, ToInvariantString(ex));
this.FailedToSendTraceData(ToInvariantString(ex));
}
}

[Event(EVENT_ID_LOG, Message = "Exporter failed to send log data. Exception: {0}", Level = EventLevel.Error)]
[NonEvent]
public void FailedToSendLogData(Exception ex)
{
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
// TODO: Do not hit ETW size limit even for external library exception stack.
this.WriteEvent(EVENT_ID_LOG, ToInvariantString(ex));
this.FailedToSendLogData(ToInvariantString(ex));
}
}

[Event(EVENT_ID_METRIC, Message = "Exporter failed to send metric data. Data will not be sent. MonitoringAccount = {0} MetricNamespace = {1}, MetricName = {2}, Message: {3}", Level = EventLevel.Error)]
[NonEvent]
public void FailedToSendMetricData(string monitoringAccount, string metricNamespace, string metricName, Exception ex)
{
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
// TODO: Do not hit ETW size limit even for external library exception stack.
this.WriteEvent(EVENT_ID_METRIC, monitoringAccount, metricNamespace, metricName, ToInvariantString(ex));
this.FailedToSendMetricData(monitoringAccount, metricNamespace, metricName, ToInvariantString(ex));
}
}

[Event(EVENT_ID_ERROR, Message = "Exporter failed.", Level = EventLevel.Error)]
[NonEvent]
public void ExporterException(string message, Exception ex)
{
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
{
// TODO: Do not hit ETW size limit even for external library exception stack.
this.WriteEvent(EVENT_ID_ERROR, message, ToInvariantString(ex));
this.ExporterException(message, ToInvariantString(ex));
}
}

[Event(EVENT_ID_TRACE, Message = "Exporter failed to send trace data. Exception: {0}", Level = EventLevel.Error)]
public void FailedToSendTraceData(string error)
{
this.WriteEvent(EVENT_ID_TRACE, error);
}

[Event(EVENT_ID_LOG, Message = "Exporter failed to send log data. Exception: {0}", Level = EventLevel.Error)]
public void FailedToSendLogData(string error)
{
this.WriteEvent(EVENT_ID_LOG, error);
}

[Event(EVENT_ID_METRIC, Message = "Exporter failed to send metric data. Data will not be sent. MonitoringAccount = {0} MetricNamespace = {1}, MetricName = {2}, Message: {3}", Level = EventLevel.Error)]
public void FailedToSendMetricData(string monitoringAccount, string metricNamespace, string metricName, string error)
{
this.WriteEvent(EVENT_ID_METRIC, monitoringAccount, metricNamespace, metricName, error);
}

[Event(EVENT_ID_ERROR, Message = "Exporter failed. Message: {0}, Exception: {1}", Level = EventLevel.Error)]
public void ExporterException(string message, string error)
{
this.WriteEvent(EVENT_ID_ERROR, message, error);
}

/// <summary>
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
/// appropriate for diagnostics tracing.
Expand Down

0 comments on commit ff92fa3

Please sign in to comment.