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

LogEmitter System.Diagnostics.Tracing extension project #3305

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6d2f153
LogEmitter API.
CodeBlanch May 24, 2022
101ce19
Code review.
CodeBlanch May 25, 2022
7eb0006
Added LogRecordPool.
CodeBlanch May 26, 2022
20479f5
Nullable annotation updates.
CodeBlanch May 26, 2022
c64ca11
Merge branch 'main' into log-emitter
CodeBlanch May 26, 2022
53a6854
Cleanup.
CodeBlanch May 26, 2022
fd5a87e
Cleanup.
CodeBlanch May 26, 2022
035823b
Added reference counting into the log record pool.
CodeBlanch May 26, 2022
80cbdbb
Tweaks.
CodeBlanch May 28, 2022
744c399
Tweak.
CodeBlanch May 29, 2022
df93cf5
Test fix.
CodeBlanch May 29, 2022
1ba8afe
Merge branch 'main' into log-emitter
CodeBlanch May 29, 2022
9a0ee32
Test fix.
CodeBlanch May 29, 2022
888eb54
Rename.
CodeBlanch May 30, 2022
558e8d2
Trigger state buffering by processor inspection.
CodeBlanch May 31, 2022
4d76b87
Implement copy for in-memory log exporter.
CodeBlanch May 31, 2022
617d88d
Added GetDataRef.
CodeBlanch May 31, 2022
ca7e319
Tweaks.
CodeBlanch May 31, 2022
6fae76d
Merge branch 'main' into log-emitter
CodeBlanch May 31, 2022
a92e78e
Revert CompositeProcessor change.
CodeBlanch May 31, 2022
5e33474
Add log stress tests to solution.
CodeBlanch May 31, 2022
6b15b3b
Tweaks.
CodeBlanch May 31, 2022
2114dca
Code review.
CodeBlanch Jun 1, 2022
0d34533
Code review, example app, serilog + eventsource extensions.
CodeBlanch Jun 2, 2022
bb4293c
Rename.
CodeBlanch Jun 2, 2022
380f139
Typo.
CodeBlanch Jun 2, 2022
87597f8
New pool design + tests.
CodeBlanch Jun 6, 2022
345e1a2
Pool selection based on processor.
CodeBlanch Jun 6, 2022
ea63af4
Merge from main.
CodeBlanch Jun 7, 2022
a12d432
Update public api files.
CodeBlanch Jun 7, 2022
3b35268
Public api fix.
CodeBlanch Jun 7, 2022
57775a5
Lint and race comment.
CodeBlanch Jun 7, 2022
9ee18df
Comments in log emitter example app.
CodeBlanch Jun 7, 2022
f347e51
Switch to Volatile.Read.
CodeBlanch Jun 7, 2022
5537c73
Bump Microsoft.DotNet.ApiCompat.
CodeBlanch Jun 7, 2022
e0c2347
Typo fix.
CodeBlanch Jun 7, 2022
dd80cc9
Bump Microsoft.DotNet.ApiCompat.
CodeBlanch Jun 7, 2022
4f6b9e0
Attempting to fix ApiCompat failure.
CodeBlanch Jun 7, 2022
1dcd193
Tweak ApiCompatExcludeAttributeList path.
CodeBlanch Jun 7, 2022
226facf
Exclude NullableContextAttribute from ApiCompat.
CodeBlanch Jun 7, 2022
d3c5443
Merge from main.
CodeBlanch Jun 15, 2022
31f5a97
Merge from main.
CodeBlanch Jun 16, 2022
cb69e49
Fix merge.
CodeBlanch Jun 16, 2022
8693d93
Merge from main.
CodeBlanch Jun 17, 2022
b71006f
Updates.
CodeBlanch Jun 17, 2022
6269015
Merge from main.
CodeBlanch Jun 18, 2022
7647145
Revert OtlpLogExporter use of logRecord.GetDataRef because it is now …
CodeBlanch Jun 18, 2022
4f498af
Merge branch 'main' into log-emitter
cijothomas Jun 27, 2022
55537b5
Merge from main.
CodeBlanch Jun 30, 2022
d1a4b3b
Merge fix.
CodeBlanch Jun 30, 2022
ffda1ce
Merge from main.
CodeBlanch Jul 11, 2022
12c92ad
Merge from main.
CodeBlanch Jul 18, 2022
cf69b8c
Updates.
CodeBlanch Jul 18, 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
Prev Previous commit
Next Next commit
Implement copy for in-memory log exporter.
  • Loading branch information
CodeBlanch committed May 31, 2022
commit 4d76b87dd520389910138e7fcd8833c72b301a55
17 changes: 5 additions & 12 deletions src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,29 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using OpenTelemetry.Logs;

namespace OpenTelemetry.Exporter
{
public class InMemoryExporter<T> : BaseExporter<T>
where T : class
{
private readonly ICollection<T> exportedItems;
private readonly Func<Batch<T>, ExportResult> onExport;
private readonly bool isLogExporter;
private readonly ExportFunc onExport;

public InMemoryExporter(ICollection<T> exportedItems)
{
this.exportedItems = exportedItems;
this.onExport = (Batch<T> batch) => this.DefaultExport(batch);
this.isLogExporter = typeof(T) == typeof(LogRecord);
this.onExport = (in Batch<T> batch) => this.DefaultExport(in batch);
}

internal InMemoryExporter(Func<Batch<T>, ExportResult> exportFunc)
internal InMemoryExporter(ExportFunc exportFunc)
{
this.onExport = exportFunc;
}

internal delegate ExportResult ExportFunc(in Batch<T> batch);

public override ExportResult Export(in Batch<T> batch) => this.onExport(batch);

private ExportResult DefaultExport(in Batch<T> batch)
Expand All @@ -50,11 +48,6 @@ private ExportResult DefaultExport(in Batch<T> batch)

foreach (var data in batch)
{
if (this.isLogExporter)
{
LogRecordPool.TrackReference((LogRecord)(object)data);
}

this.exportedItems.Add(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,35 @@ public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryL
Guard.ThrowIfNull(loggerOptions);
Guard.ThrowIfNull(exportedItems);

return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(new InMemoryExporter<LogRecord>(exportedItems)));
var logExporter = new InMemoryExporter<LogRecord>(
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));

return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(logExporter));
}

private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
{
if (exportedItems == null)
{
return ExportResult.Failure;
}

foreach (var log in batch)
{
log.BufferLogScopes();

LogRecord copy = new()
{
Data = log.Data,
State = log.State,
StateValues = log.StateValues,
BufferedScopes = log.BufferedScopes,
};

exportedItems.Add(copy);
}

return ExportResult.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static MeterProviderBuilder AddInMemoryExporter(
configureMetricReader?.Invoke(metricReaderOptions);

var metricExporter = new InMemoryExporter<Metric>(
exportFunc: metricBatch => ExportMetricSnapshot(metricBatch, exportedItems));
exportFunc: (in Batch<Metric> metricBatch) => ExportMetricSnapshot(in metricBatch, exportedItems));

var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
metricExporter,
Expand Down
11 changes: 5 additions & 6 deletions src/OpenTelemetry/Logs/LogRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ public sealed class LogRecord
internal int PoolReferences = int.MaxValue;
internal LogRecordData Data;
internal List<KeyValuePair<string, object?>>? AttributeStorage;
internal List<object?>? BufferedScopes;

private static readonly Action<object?, List<object?>> AddScopeToBufferedList = (object? scope, List<object?> state) =>
{
state.Add(scope);
};

private List<object?>? bufferedScopes;

internal LogRecord()
{
}
Expand Down Expand Up @@ -164,9 +163,9 @@ public void ForEachScope<TState>(Action<LogRecordScope, TState> callback, TState

var forEachScopeState = new ScopeForEachState<TState>(callback, state);

if (this.bufferedScopes != null)
if (this.BufferedScopes != null)
{
foreach (object? scope in this.bufferedScopes)
foreach (object? scope in this.BufferedScopes)
{
ScopeForEachState<TState>.ForEachScope(scope, forEachScopeState);
}
Expand All @@ -183,7 +182,7 @@ public void ForEachScope<TState>(Action<LogRecordScope, TState> callback, TState
/// </summary>
internal void BufferLogScopes()
{
if (this.ScopeProvider == null || this.bufferedScopes != null)
if (this.ScopeProvider == null || this.BufferedScopes != null)
{
return;
}
Expand All @@ -192,7 +191,7 @@ internal void BufferLogScopes()

this.ScopeProvider?.ForEachScope(AddScopeToBufferedList, scopes);

this.bufferedScopes = scopes;
this.BufferedScopes = scopes;
}

private readonly struct ScopeForEachState<TState>
Expand Down