Skip to content

Commit

Permalink
Merge pull request #6 from fidelity/Fix-Issue-1848
Browse files Browse the repository at this point in the history
1. Record the callstack when the ConsoleExporter is disposed.
2. If ConsoleExporter.Export is ever called after it is disposed, depending on whether it is the 1st time:
2.1 if it is the first time - outputs an error message - including "what happened" and "what's the consequence" and "what should be done in order to fix the issue", plus two callstacks 1) the callstack where the export is being called 2) the callstack where dispose happened.
2.2 if it is not the first time, simply drop the data and don't output anything.
3. Everything here has to be done in a thread-safe manner.
  • Loading branch information
jackyshang12321 authored Aug 29, 2022
2 parents 14e2ff8 + a0e25ba commit f2c5b49
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
Expand All @@ -23,7 +24,10 @@ namespace OpenTelemetry.Exporter
public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
{
private const int RightPaddingLength = 35;
private readonly object disposeLockObj = new();
private bool disposed = false;
private string disposedStackTrace;
private bool isDisposeMessageSent = false;

public ConsoleLogRecordExporter(ConsoleExporterOptions options)
: base(options)
Expand All @@ -32,13 +36,14 @@ public ConsoleLogRecordExporter(ConsoleExporterOptions options)

public override ExportResult Export(in Batch<LogRecord> batch)
{
foreach (var logRecord in batch)
if (this.disposed)
{
if (this.disposed)
{
this.WriteLine("Something is wrong: the ConsoleLogger has been disposed.");
}
this.SendDisposedMessage();
return ExportResult.Failure;
}

foreach (var logRecord in batch)
{
this.WriteLine($"{"LogRecord.Timestamp:",-RightPaddingLength}{logRecord.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ}");

if (logRecord.TraceId != default)
Expand Down Expand Up @@ -138,9 +143,33 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)

protected override void Dispose(bool disposing)
{
if (disposing)
lock (this.disposeLockObj)
{
if (!this.disposed)
{
this.disposed = true;
this.disposedStackTrace = Environment.StackTrace;
}
}

base.Dispose(disposing);
}

private void SendDisposedMessage()
{
lock (this.disposeLockObj)
{
this.disposed = true;
if (this.isDisposeMessageSent)
{
return;
}

this.isDisposeMessageSent = true;

this.WriteLine("The console exporter is still being invoked after it has been disposed. This could be the result of invalid lifecycle management of the OpenTelemetry .NET SDK.");
this.WriteLine(Environment.StackTrace);
this.WriteLine(Environment.NewLine + "The console exporter has been disposed");
this.WriteLine(this.disposedStackTrace);
}
}
}
Expand Down

0 comments on commit f2c5b49

Please sign in to comment.