Skip to content

Commit

Permalink
Merge pull request #102 from nblumhardt/sp-tr
Browse files Browse the repository at this point in the history
Add support for Serilog 3.1 trace `@tr` and span `@sp` ids
  • Loading branch information
nblumhardt authored Oct 3, 2023
2 parents 938b02d + 2f35b97 commit 77ac5aa
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Serilog.Expressions/Expressions/BuiltInProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ static class BuiltInProperty
public const string Properties = "p";
public const string Renderings = "r";
public const string EventId = "i";
public const string TraceId = "tr";
public const string SpanId = "sp";
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ protected override ExpressionBody Transform(AmbientNameExpression px)
BuiltInProperty.Message => Splice(context => new ScalarValue(Intrinsics.RenderMessage(formatter, context))),
BuiltInProperty.Exception => Splice(context =>
context.LogEvent.Exception == null ? null : new ScalarValue(context.LogEvent.Exception)),
BuiltInProperty.TraceId => Splice(context =>
context.LogEvent.TraceId == null ? null : new ScalarValue(context.LogEvent.TraceId.Value)),
BuiltInProperty.SpanId => Splice(context =>
context.LogEvent.SpanId == null ? null : new ScalarValue(context.LogEvent.SpanId.Value)),
BuiltInProperty.Timestamp => Splice(context => new ScalarValue(context.LogEvent.Timestamp)),
BuiltInProperty.MessageTemplate => Splice(context => new ScalarValue(context.LogEvent.MessageTemplate.Text)),
BuiltInProperty.Properties => Splice(context =>
Expand Down
13 changes: 13 additions & 0 deletions src/Serilog.Expressions/Expressions/Runtime/Coerce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Serilog.Events;

Expand Down Expand Up @@ -77,6 +78,18 @@ public static bool String(LogEventPropertyValue? value, [MaybeNullWhen(false)] o
str = sv.Value.ToString()!;
return true;
}

if (sv.Value is ActivityTraceId traceId)
{
str = traceId.ToHexString();
return true;
}

if (sv.Value is ActivitySpanId spanId)
{
str = spanId.ToHexString();
return true;
}
}

str = default;
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog.Expressions/Serilog.Expressions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/serilog/serilog-expressions</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog" Version="3.1.0-*" />
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="" />
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,12 @@ undefined() = undefined() ci ⇶ undefined()
'test' like '%' ⇶ true
'test' like '' ⇶ false
'' like '' ⇶ true

// Built-ins

@m ⇶ 'Hello, World!'
@mt ⇶ 'Hello, {Name}!'
tostring(@x) like 'System.DivideByZeroException%' ⇶ true
@l ⇶ 'Warning'
@sp ⇶ 'bb1111820570b80e'
@tr ⇶ '1befc31e94b01d1a473f63a7905f6c9b'
24 changes: 17 additions & 7 deletions test/Serilog.Expressions.Tests/ExpressionEvaluationTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics;
using System.Globalization;
using Serilog.Events;
using Serilog.Expressions.Runtime;
using Serilog.Expressions.Tests.Support;
using Serilog.Parsing;
using Xunit;

namespace Serilog.Expressions.Tests;
Expand All @@ -15,14 +17,22 @@ public class ExpressionEvaluationTests
[MemberData(nameof(ExpressionEvaluationCases))]
public void ExpressionsAreCorrectlyEvaluated(string expr, string result)
{
var evt = Some.InformationEvent();

evt.AddPropertyIfAbsent(
new("User", new StructureValue(new[]
var evt = new LogEvent(
new DateTimeOffset(2025, 5, 15, 13, 12, 11, 789, TimeSpan.FromHours(10)),
LogEventLevel.Warning,
new DivideByZeroException(),
new MessageTemplateParser().Parse("Hello, {Name}!"),
new []
{
new LogEventProperty("Id", new ScalarValue(42)),
new LogEventProperty("Name", new ScalarValue("nblumhardt")),
})));
new LogEventProperty("Name", new ScalarValue("World")),
new LogEventProperty("User", new StructureValue(new[]
{
new LogEventProperty("Id", new ScalarValue(42)),
new LogEventProperty("Name", new ScalarValue("nblumhardt")),
}))
},
ActivityTraceId.CreateFromString("1befc31e94b01d1a473f63a7905f6c9b"),
ActivitySpanId.CreateFromString("bb1111820570b80e"));

var frFr = CultureInfo.GetCultureInfoByIetfLanguageTag("fr-FR");
var testHelpers = new TestHelperNameResolver();
Expand Down

0 comments on commit 77ac5aa

Please sign in to comment.