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
Merge from main.
  • Loading branch information
CodeBlanch committed Jul 18, 2022
commit 12c92addc153625868531cc902787f4a0f7b74c4
8 changes: 1 addition & 7 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Se
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Tracing", "src\OpenTelemetry.Extensions.Tracing\OpenTelemetry.Extensions.Tracing.csproj", "{F1C65913-81EC-4297-A666-A66280E3E1B6}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracing is such a commonly used term. I get it comes from System.Diagnostics.Tracing, but is there a better name?

What about OpenTelemetry.Extensions.EventSource? Or if that's too narrow of a name maybe OpenTelemetry.Extensions.EventTracing? Or... ugh maybe even OpenTelemetry.Extensions.SystemDiagnosticsTracing 🥴?

EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.LogEmitter", "examples\LogEmitter\Examples.LogEmitter.csproj", "{CCBBAD69-4A7D-41F8-8697-6E3F043F7BD5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.LoggingExtensions", "examples\LoggingExtensions\Examples.LoggingExtensions.csproj", "{F5EFF065-7AF5-4D7D-8038-CC419ABD8777}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Serilog", "src\OpenTelemetry.Extensions.Serilog\OpenTelemetry.Extensions.Serilog.csproj", "{0D85558E-15B9-4251-BDBD-9CB7933B57E2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Serilog.Tests", "test\OpenTelemetry.Extensions.Serilog.Tests\OpenTelemetry.Extensions.Serilog.Tests.csproj", "{6A2C122A-C1CD-4B6B-AE09-2ABB7D3C50CE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.LoggingExtensions", "examples\LoggingExtensions\Examples.LoggingExtensions.csproj", "{CCBBAD69-4A7D-41F8-8697-6E3F043F7BD5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#nullable enable
Serilog.OpenTelemetrySerilogExtensions
static Serilog.OpenTelemetrySerilogExtensions.OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration! loggerConfiguration, OpenTelemetry.Logs.OpenTelemetryLoggerProvider! openTelemetryLoggerProvider) -> Serilog.LoggerConfiguration!
static Serilog.OpenTelemetrySerilogExtensions.OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration! loggerConfiguration, OpenTelemetry.Logs.OpenTelemetryLoggerProvider! openTelemetryLoggerProvider, bool disposeProvider = true) -> Serilog.LoggerConfiguration!
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Description>Extensions to enable OpenTelemetry logging when using the Serilog library</Description>
<PackageTags>$(PackageTags);serilog;logging</PackageTags>
<Nullable>enable</Nullable>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
Expand All @@ -17,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog" Version="$(SerilogPkgVer)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ public static class OpenTelemetrySerilogExtensions
/// Adds a sink to Serilog <see cref="LoggerConfiguration"/> which will
/// write to OpenTelemetry.
/// </summary>
/// <param name="loggerConfiguration"><see cref="LoggerSinkConfiguration"/>.</param>
/// <param name="openTelemetryLoggerProvider"><see cref="OpenTelemetryLoggerProvider"/>.</param>
/// <param name="loggerConfiguration"><see
/// cref="LoggerSinkConfiguration"/>.</param>
/// <param name="openTelemetryLoggerProvider"><see
/// cref="OpenTelemetryLoggerProvider"/>.</param>
/// <param name="disposeProvider">Controls whether or not the supplied
/// <paramref name="openTelemetryLoggerProvider"/> will be disposed when
/// the logger is disposed. Default value: <see
/// langword="true"/>.</param>
/// <returns>Supplied <see cref="LoggerConfiguration"/> for chaining calls.</returns>
public static LoggerConfiguration OpenTelemetry(
this LoggerSinkConfiguration loggerConfiguration,
OpenTelemetryLoggerProvider openTelemetryLoggerProvider)
OpenTelemetryLoggerProvider openTelemetryLoggerProvider,
bool disposeProvider = true)
{
Guard.ThrowIfNull(loggerConfiguration);
Guard.ThrowIfNull(openTelemetryLoggerProvider);

return loggerConfiguration.Sink(new OpenTelemetrySerilogSink(openTelemetryLoggerProvider));
#pragma warning disable CA2000 // Dispose objects before losing scope
return loggerConfiguration.Sink(new OpenTelemetrySerilogSink(openTelemetryLoggerProvider, disposeProvider));
#pragma warning restore CA2000 // Dispose objects before losing scope
}
}
}
69 changes: 60 additions & 9 deletions src/OpenTelemetry.Extensions.Serilog/OpenTelemetrySerilogSink.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 System.Diagnostics;
using Microsoft.Extensions.Logging;
Expand All @@ -22,15 +23,32 @@

namespace OpenTelemetry.Logs
{
internal sealed class OpenTelemetrySerilogSink : ILogEventSink
internal sealed class OpenTelemetrySerilogSink : ILogEventSink, IDisposable
{
private readonly OpenTelemetryLoggerProvider openTelemetryLoggerProvider;
private readonly bool includeFormattedMessage;
private readonly LogEmitter logEmitter;
private readonly bool disposeProvider;

public OpenTelemetrySerilogSink(OpenTelemetryLoggerProvider openTelemetryLoggerProvider)
public OpenTelemetrySerilogSink(OpenTelemetryLoggerProvider openTelemetryLoggerProvider, bool disposeProvider)
{
this.includeFormattedMessage = openTelemetryLoggerProvider.IncludeFormattedMessage;
this.logEmitter = openTelemetryLoggerProvider.CreateEmitter();
Debug.Assert(openTelemetryLoggerProvider != null, "openTelemetryLoggerProvider was null");

this.openTelemetryLoggerProvider = openTelemetryLoggerProvider!;
this.disposeProvider = disposeProvider;

var logEmitter = this.openTelemetryLoggerProvider.CreateEmitter();
Debug.Assert(logEmitter != null, "logEmitter was null");

this.logEmitter = logEmitter!;

// TODO: This project can only access IncludeFormattedMessage
// because it can see SDK internals. At some point this is likely
// not to be the case. Need to figure out where to put
// IncludeFormattedMessage so that extensions can see it. Ideas:
// Make it public on OpenTelemetryLoggerProvider or expose it on
// LogEmitter instance.
this.includeFormattedMessage = this.openTelemetryLoggerProvider.IncludeFormattedMessage;
}

public void Emit(LogEvent logEvent)
Expand Down Expand Up @@ -62,23 +80,56 @@ public void Emit(LogEvent logEvent)
else if (property.Value is SequenceValue sequenceValue)
{
IReadOnlyList<LogEventPropertyValue> elements = sequenceValue.Elements;
if (elements.Count > 0 && elements[0] is ScalarValue)
if (elements.Count > 0)
{
object[] values = new object[elements.Count];
// Note: The goal here is to build a typed array (eg
// int[]) if all the element types match otherwise
// fallback to object[]

Type? elementType = null;
Array? values = null;

for (int i = 0; i < elements.Count; i++)
{
if (elements[i] is ScalarValue value)
{
values[i] = value.Value;
Type currentElementType = value.Value?.GetType() ?? typeof(object);

if (values == null)
{
elementType = currentElementType;
values = Array.CreateInstance(elementType, elements.Count);
}
else if (!elementType!.IsAssignableFrom(currentElementType))
{
// Array with mixed types detected
object[] newValues = new object[elements.Count];
values.CopyTo(newValues, 0);
values = newValues;
elementType = typeof(object);
}

values.SetValue(value.Value, i);
}
}

attributes.Add(property.Key, values);
if (values != null)
{
attributes.Add(property.Key, values);
}
}
}
}

this.logEmitter.Log(in data, in attributes);
this.logEmitter.Emit(in data, in attributes);
}

public void Dispose()
{
if (this.disposeProvider)
{
this.openTelemetryLoggerProvider.Dispose();
}
}
}
}
20 changes: 15 additions & 5 deletions src/OpenTelemetry.Extensions.Serilog/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
# OpenTelemetry.Extensions.Serilog

[![NuGet](https://img.shields.io/nuget/v/OpenTelemetry.Extensions.Serilog.svg)](https://www.nuget.org/packages/OpenTelemetry.Extensions.Serilog)
[![NuGet](https://img.shields.io/nuget/dt/OpenTelemetry.Extensions.Serilog.svg)](https://www.nuget.org/packages/OpenTelemetry.Extensions.Serilog)

This project contains a [Serilog](https://github.com/serilog/)
[sink](https://github.com/serilog/serilog/wiki/Configuration-Basics#sinks) for
writing log messages to OpenTelemetry.

## Installation

```shell
dotnet add package OpenTelemetry.Extensions.Serilog
```

## Usage Example

```csharp
// Step 1: Configure OpenTelemetryLoggerProvider...
var resourceBuilder = ResourceBuilder.CreateDefault().AddService("MyService");

using var openTelemetryLoggerProvider = new OpenTelemetryLoggerProvider(options =>
var openTelemetryLoggerProvider = new OpenTelemetryLoggerProvider(options =>
{
options
.SetResourceBuilder(resourceBuilder)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyService"))
.AddConsoleExporter();
});

// Step 2: Register OpenTelemetry sink with Serilog...
Log.Logger = new LoggerConfiguration()
.WriteTo.OpenTelemetry(openTelemetryLoggerProvider)
.WriteTo.OpenTelemetry(openTelemetryLoggerProvider, disposeProvider: true)
.CreateLogger();

// Step 3: When application is shutdown flush all log messages and dispose provider...
Log.CloseAndFlush();
```

## References
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.