Skip to content

Commit

Permalink
introduce: tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
riemannulus committed Aug 1, 2024
1 parent 9213082 commit fecb06f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions Bencodex/Bencodex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Collections.Immutable" Version="1.*" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions Bencodex/BencodexTracer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics;

namespace Bencodex
{
internal static class BencodexTracer
{
private static readonly ActivitySource _activitySource = new ActivitySource("Bencodex");

public static Activity? StartActivity(string name) =>
_activitySource.StartActivity(name);
}
}
10 changes: 10 additions & 0 deletions Bencodex/Decoder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Numerics;
Expand Down Expand Up @@ -39,30 +40,38 @@ public IValue Decode()

private IValue? DecodeValue()
{
using Activity? activity = BencodexTracer.StartActivity(nameof(Decode));
switch (ReadByte())
{
case 0x65: // 'e'
activity?.AddTag("type", "null");
return null;

case 0x6e: // 'n'
#pragma warning disable SA1129
activity?.AddTag("type", nameof(Types.Null));
return new Null();
#pragma warning restore SA1129

case 0x74: // 't'
activity?.AddTag("type", nameof(Types.Boolean));
return new Bencodex.Types.Boolean(true);

case 0x66: // 'f'
activity?.AddTag("type", nameof(Types.Boolean));
return new Bencodex.Types.Boolean(false);

case 0x69: // 'i'
activity?.AddTag("type", nameof(Types.Integer));
BigInteger integer = ReadInteger();
return new Integer(integer);

case 0x75: // 'u'
activity?.AddTag("type", nameof(Types.Text));
return ReadTextAfterPrefix();

case 0x6c: // 'l'
activity?.AddTag("type", nameof(Types.List));
var elements = new List<IValue>();
while (DecodeValue() is IValue element)
{
Expand All @@ -72,6 +81,7 @@ public IValue Decode()
return new Bencodex.Types.List(elements);

case 0x64: // 'd'
activity?.AddTag("type", nameof(Types.Dictionary));
var builder = ImmutableSortedDictionary.CreateBuilder<IKey, IValue>(KeyComparer.Instance);
IKey? lastKey = null;
while (DecodeKey() is IKey key)
Expand Down
3 changes: 3 additions & 0 deletions Bencodex/Encoder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -239,6 +240,8 @@ internal static long EncodeDigits(long positiveInt, byte[] buffer, long offset)
// TODO: Needs a unit test.
internal static long Encode(in IValue value, byte[] buffer, long offset)
{
using Activity? activity = BencodexTracer.StartActivity(nameof(Encode));
activity?.AddTag("type", value.GetType().ToString());
return value switch
{
Null _ => EncodeNull(buffer, offset),
Expand Down

0 comments on commit fecb06f

Please sign in to comment.