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

Fix: Logs could be exported via ConsoleExporter after LoggerFactory is disposed #1848 #3578

Merged
merged 23 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
51bc047
Fix Issue 1848
jackyshang12321 Aug 15, 2022
5b6cc57
Merge pull request #3 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 15, 2022
d44c7a8
add the new function to publicAPI doc
jackyshang12321 Aug 15, 2022
2157ebb
Merge pull request #4 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 15, 2022
699472c
rather than stop exporting data, added a special message telling user…
jackyshang12321 Aug 15, 2022
72a9f5c
Merge pull request #5 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 15, 2022
14e2ff8
Merge branch 'open-telemetry:main' into main
brianwarner Aug 25, 2022
a0e25ba
1. Record the callstack when the ConsoleExporter is disposed.
jackyshang12321 Aug 29, 2022
f2c5b49
Merge pull request #6 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 29, 2022
d455131
Double-checking locking
jackyshang12321 Aug 30, 2022
5b3e2bf
update changelog
jackyshang12321 Aug 30, 2022
3690c47
Merge pull request #7 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 30, 2022
0b7aea7
remove the SendDisposedMessage method
jackyshang12321 Aug 31, 2022
41ce82d
Merge pull request #8 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 31, 2022
407b85a
remove lock and update changelog
jackyshang12321 Aug 31, 2022
505921d
Merge pull request #9 from fidelity/Fix-Issue-1848
jackyshang12321 Aug 31, 2022
eb4c64e
fix code bugs and update changelog
jackyshang12321 Sep 1, 2022
2033c64
Merge pull request #10 from fidelity/Fix-Issue-1848
jackyshang12321 Sep 1, 2022
1d17a21
Update src/OpenTelemetry.Exporter.Console/CHANGELOG.md
jackyshang12321 Sep 1, 2022
96f94e8
Merge branch 'open-telemetry:main' into main
jackyshang12321 Sep 2, 2022
8a1038f
Merge branch 'main' into main
jackyshang12321 Sep 2, 2022
eaadf27
Merge branch 'main' into main
jackyshang12321 Sep 2, 2022
915bce2
make it more clear who is at fault
jackyshang12321 Sep 2, 2022
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
@@ -0,0 +1 @@
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Changed the behavior of `ConsoleExporter`, the exporter will stop outputting
the data if it is disposed.
([#3578](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3578))

## 1.4.0-alpha.2

Released 2022-Aug-18
Expand Down
39 changes: 39 additions & 0 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,6 +24,10 @@ namespace OpenTelemetry.Exporter
public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
{
private const int RightPaddingLength = 35;
private readonly object syncObject = new();
private bool disposed;
private string disposedStackTrace;
private bool isDisposeMessageSent;

public ConsoleLogRecordExporter(ConsoleExporterOptions options)
: base(options)
Expand All @@ -31,6 +36,29 @@ public ConsoleLogRecordExporter(ConsoleExporterOptions options)

public override ExportResult Export(in Batch<LogRecord> batch)
{
if (this.disposed)
{
if (!this.isDisposeMessageSent)
{
lock (this.syncObject)
{
if (this.isDisposeMessageSent)
{
return ExportResult.Failure;
}

this.isDisposeMessageSent = true;
}

this.WriteLine("The console exporter is still being invoked after it has been disposed. This could be due to the application's incorrect lifecycle management of the LoggerFactory/OpenTelemetry .NET SDK.");
this.WriteLine(Environment.StackTrace);
this.WriteLine(Environment.NewLine + "Dispose was called on the following stack trace:");
this.WriteLine(this.disposedStackTrace);
}

return ExportResult.Failure;
}

foreach (var logRecord in batch)
{
this.WriteLine($"{"LogRecord.Timestamp:",-RightPaddingLength}{logRecord.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
Expand Down Expand Up @@ -129,5 +157,16 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)

return ExportResult.Success;
}

protected override void Dispose(bool disposing)
{
if (!this.disposed)
reyang marked this conversation as resolved.
Show resolved Hide resolved
{
this.disposed = true;
this.disposedStackTrace = Environment.StackTrace;
}

base.Dispose(disposing);
}
}
}