Skip to content

Commit 54d7c81

Browse files
committed
Fork from Serilog.Sinks.OpenTelemetry, version 4.2.0-dev
1 parent 40f00c0 commit 54d7c81

38 files changed

+1713
-4
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Auto detect text files and perform LF normalization
2+
3+
* text=auto
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
name: Bug report
3+
about: Report a bug and help us to improve Serilog.Sinks.OpenTelemetry
4+
title: ''
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
10+
The Serilog maintainers want you to have a great experience using Serilog.Sinks.OpenTelemetry, and will happily track down and resolve bugs. We all have limited time, though, so please think through all of the factors that might be involved and include as much useful information as possible 😊.
11+
12+
ℹ If the problem is caused by a sink or other extension package, please track down the correct repository for that package and create the report there: this tracker is for the **Serilog.Sinks.OpenTelemetry** package only.
13+
14+
**Description**
15+
What's going wrong?
16+
17+
**Reproduction**
18+
Please provide code samples showing how you're configuring and calling Serilog to produce the behavior.
19+
20+
**Expected behavior**
21+
A concise description of what you expected to happen.
22+
23+
**Relevant package, tooling and runtime versions**
24+
Which package versions are you using, on what platform?
25+
26+
**Additional context**
27+
Add any other context about the problem here.

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
contact_links:
2+
- name: Ask for help
3+
url: https://github.com/serilog/serilog/wiki/Usage-help
4+
about: Ask the community for help on how to use Serilog
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an improvement to Serilog.Sinks.OpenTelemetry
4+
title: ''
5+
labels: enhancement
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. `When [...], it would be great if [...]`.
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# If this file is renamed, the incrementing run attempt number will be reset.
2+
3+
name: CI
4+
5+
on:
6+
push:
7+
branches: [ "dev", "main" ]
8+
pull_request:
9+
branches: [ "dev", "main" ]
10+
11+
env:
12+
CI_BUILD_NUMBER_BASE: ${{ github.run_number }}
13+
CI_TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
14+
15+
jobs:
16+
build:
17+
18+
# The build must run on Windows so that .NET Framework targets can be built and tested.
19+
runs-on: windows-latest
20+
21+
permissions:
22+
contents: write
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Setup
27+
uses: actions/setup-dotnet@v4
28+
with:
29+
dotnet-version: 9.0.x
30+
- name: Compute build number
31+
shell: bash
32+
run: |
33+
echo "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER_BASE+2300))" >> $GITHUB_ENV
34+
- name: Build and Publish
35+
env:
36+
DOTNET_CLI_TELEMETRY_OPTOUT: true
37+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
shell: pwsh
40+
run: |
41+
./Build.ps1

.gitignore

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ bld/
3737
# Uncomment if you have tasks that create the project's static files in wwwroot
3838
#wwwroot/
3939

40+
# JetBrains Rider cache
41+
.idea/
42+
43+
# VSCode cache
44+
.vscode/
45+
4046
# Visual Studio 2017 auto generated files
4147
Generated\ Files/
4248

@@ -82,8 +88,6 @@ StyleCopReport.xml
8288
*.pgc
8389
*.pgd
8490
*.rsp
85-
# but not Directory.Build.rsp, as it configures directory-level build defaults
86-
!Directory.Build.rsp
8791
*.sbr
8892
*.tlb
8993
*.tli
@@ -398,3 +402,6 @@ FodyWeavers.xsd
398402

399403
# JetBrains Rider
400404
*.sln.iml
405+
/test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.received.txt
406+
407+
.DS_Store

Build.ps1

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Write-Output "build: Tool versions follow"
2+
3+
dotnet --version
4+
dotnet --list-sdks
5+
6+
Write-Output "build: Build started"
7+
8+
Push-Location $PSScriptRoot
9+
try {
10+
if(Test-Path .\artifacts) {
11+
Write-Output "build: Cleaning ./artifacts"
12+
Remove-Item ./artifacts -Force -Recurse
13+
}
14+
15+
& dotnet restore --no-cache
16+
17+
$dbp = [Xml] (Get-Content .\Directory.Version.props)
18+
$versionPrefix = $dbp.Project.PropertyGroup.VersionPrefix
19+
20+
Write-Output "build: Package version prefix is $versionPrefix"
21+
22+
$branch = @{ $true = $env:CI_TARGET_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$NULL -ne $env:CI_TARGET_BRANCH];
23+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:CI_BUILD_NUMBER, 10); $false = "local" }[$NULL -ne $env:CI_BUILD_NUMBER];
24+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)) -replace '([^a-zA-Z0-9\-]*)', '')-$revision"}[$branch -eq "main" -and $revision -ne "local"]
25+
$commitHash = $(git rev-parse --short HEAD)
26+
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]
27+
28+
Write-Output "build: Package version suffix is $suffix"
29+
Write-Output "build: Build version suffix is $buildSuffix"
30+
31+
& dotnet build -c Release --version-suffix=$buildSuffix /p:ContinuousIntegrationBuild=true
32+
if($LASTEXITCODE -ne 0) { throw "Build failed" }
33+
34+
foreach ($src in Get-ChildItem src/*) {
35+
Push-Location $src
36+
37+
Write-Output "build: Packaging project in $src"
38+
39+
if ($suffix) {
40+
& dotnet pack -c Release --no-build --no-restore -o ../../artifacts --version-suffix=$suffix
41+
} else {
42+
& dotnet pack -c Release --no-build --no-restore -o ../../artifacts
43+
}
44+
if($LASTEXITCODE -ne 0) { throw "Packaging failed" }
45+
46+
Pop-Location
47+
}
48+
49+
foreach ($test in Get-ChildItem test/*.Tests) {
50+
Push-Location $test
51+
52+
Write-Output "build: Testing project in $test"
53+
54+
& dotnet test -c Release --no-build --no-restore
55+
if($LASTEXITCODE -ne 0) { throw "Testing failed" }
56+
57+
Pop-Location
58+
}
59+
60+
if ($env:NUGET_API_KEY) {
61+
# GitHub Actions will only supply this to branch builds and not PRs. We publish
62+
# builds from any branch this action targets (i.e. main and dev).
63+
64+
Write-Output "build: Publishing NuGet packages"
65+
66+
foreach ($nupkg in Get-ChildItem artifacts/*.nupkg) {
67+
& dotnet nuget push -k $env:NUGET_API_KEY -s https://api.nuget.org/v3/index.json "$nupkg"
68+
if($LASTEXITCODE -ne 0) { throw "Publishing failed" }
69+
}
70+
71+
if (!($suffix)) {
72+
Write-Output "build: Creating release for version $versionPrefix"
73+
74+
iex "gh release create v$versionPrefix --title v$versionPrefix --generate-notes $(get-item ./artifacts/*.nupkg) $(get-item ./artifacts/*.snupkg)"
75+
}
76+
}
77+
} finally {
78+
Pop-Location
79+
}

Directory.Build.props

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project>
2+
<!-- Properties in this file are expected to be identical for all Serilog organization projects. If
3+
a property value is project-specific, please record it in the CSPROJ file instead. -->
4+
<Import Project="$(MSBuildThisFileDirectory)Directory.Version.props" />
5+
<PropertyGroup>
6+
<LangVersion>latest</LangVersion>
7+
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
8+
<!-- The condition is required to support BenchmarkDotNet -->
9+
<SignAssembly Condition="Exists('$(MSBuildThisFileDirectory)assets/Serilog.snk')">true</SignAssembly>
10+
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)assets/Serilog.snk</AssemblyOriginatorKeyFile>
11+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
12+
<Nullable>enable</Nullable>
13+
<ImplicitUsings>enable</ImplicitUsings>
14+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
15+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
16+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
17+
<IncludeSymbols>true</IncludeSymbols>
18+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
19+
</PropertyGroup>
20+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
21+
<Reference Include="System" />
22+
<Reference Include="System.Core" />
23+
<Reference Include="Microsoft.CSharp" />
24+
</ItemGroup>
25+
</Project>

Directory.Version.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<VersionPrefix>1.0.0</VersionPrefix>
4+
</PropertyGroup>
5+
</Project>

README.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,72 @@
1-
# serilog-sinks-opentelemetrysdk
2-
An experimental Serilog sink that writes log events directly to the OpenTelemetry SDK.
1+
# Serilog.Sinks.OpenTelemetrySdk&nbsp;[![Build status](https://github.com/serilog/serilog-sinks-opentelemetrysdk/actions/workflows/ci.yml/badge.svg?branch=dev)](https://github.com/serilog/serilog-sinks-opentelemetrysdk/actions)&nbsp;[![NuGet Version](https://img.shields.io/nuget/vpre/Serilog.Sinks.OpenTelemetrySdk.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.OpenTelemetrySdk/)
2+
3+
This Serilog sink passes Serilog `LogEvents` to the OpenTelemetry SDK.
4+
5+
> [!IMPORTANT] This package relies on experimental OpenTelemetry features.
6+
> It will only work with pre-release builds
7+
> of the .NET OpenTelemetry SDK. Because there's no guarantee that these
8+
> experimental features will ever ship as stable APIs, there's also no guarantee
9+
> that a stable version of this sink will ever be available.
10+
11+
## Getting started
12+
13+
To use this sink, you should already have a **pre-release** (`-beta` or `-rc`) version of the OpenTeleme~~~~try SDK installed
14+
and configured.
15+
16+
Install the [NuGet package](https://nuget.org/packages/serilog.sinks.opentelemetrysdk):
17+
18+
```shell
19+
dotnet add package Serilog.Sinks.OpenTelemetrySdk
20+
```
21+
22+
Then enable the sink using `WriteTo.OpenTelemetrySdk()`, passing your application's `IServiceProvider`:
23+
24+
```csharp
25+
// var app = ...
26+
27+
Log.Logger = new LoggerConfiguration()
28+
.WriteTo.OpenTelemetrySdk(app.Services)
29+
.CreateLogger();
30+
```
31+
32+
Logs written with `Log.Information(...)` and similar methods will be output by the configured OpenTelemetry exporters.
33+
34+
## Serilog `LogEvent` to OpenTelemetry log record mapping
35+
36+
The following table provides the mapping between the Serilog log
37+
events and the OpenTelemetry log records.
38+
39+
Serilog `LogEvent` | OpenTelemetry `LogRecord` | Comments |
40+
---------------------------------|--------------------------------------------|-----------------------------------------------------------------------------------------------|
41+
`Exception.GetType().ToString()` | `Attributes["exception.type"]` | |
42+
`Exception.Message` | `Attributes["exception.message"]` | Ignored if empty |
43+
`Exception.StackTrace` | `Attributes[ "exception.stacktrace"]` | Value of `ex.ToString()` |
44+
`Level` | `LogRecordSeverity` | Serilog levels are mapped to corresponding OpenTelemetry severities |
45+
`Level.ToString()` | `SeverityText` | |
46+
`Message` | `Body` | Culture-specific formatting can be provided via sink configuration |
47+
`MessageTemplate` | `Attributes[ "message_template.text"]` | Requires `IncludedData. MessageTemplateText` (enabled by default) |
48+
`MessageTemplate` (MD5) | `Attributes[ "message_template.hash.md5"]` | Requires `IncludedData. MessageTemplateMD5 HashAttribute` |
49+
`Properties` | `Attributes` | Each property is mapped to an attribute keeping the name; the value's structure is maintained |
50+
`SpanId` (`Activity.Current`) | `SpanId` | Requires `IncludedData.SpanIdField` (enabled by default) |
51+
`Timestamp` | `TimeUnixNano` | .NET provides 100-nanosecond precision |
52+
`TraceId` (`Activity.Current`) | `TraceId` | Requires `IncludedData.TraceIdField` (enabled by default) |
53+
54+
Serialized data is passed using the `Dictionary<string, object?>` type. Logged arrays are passed as `List<object?>`.
55+
56+
### Configuring included data
57+
58+
This sink supports configuration of how common OpenTelemetry fields are populated from
59+
the Serilog `LogEvent` by passing `includedData`:
60+
61+
```csharp
62+
Log.Logger = new LoggerConfiguration()
63+
.WriteTo.OpenTelemetrySdk(app.Services, includedData: IncludedData.MessageTemplateTextAttribute)
64+
.CreateLogger();
65+
```
66+
67+
## Acknowledgements
68+
69+
This sink is a fork of [`Serilog.Sinks.OpenTelemetry`](https://github.com/serilog/serilog-sinks-opentelemetry), which
70+
sends log events using the OpenTelemetry wire protocol, and does not have a dependency on the OpenTelemetry SDK.
71+
72+
_Copyright &copy; Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._

0 commit comments

Comments
 (0)