-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better tracing support in Stl.Rpc
- Loading branch information
1 parent
c82af31
commit 7bf5780
Showing
27 changed files
with
245 additions
and
65 deletions.
There are no files selected for viewing
8 changes: 6 additions & 2 deletions
8
src/Stl.Fusion.EntityFramework/Conversion/UlidToStringValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using System.Globalization; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
namespace Stl.Fusion.EntityFramework.Conversion; | ||
|
||
public class UlidToStringValueConverter(ConverterMappingHints mappingHints = null!) | ||
: ValueConverter<Ulid, string>(x => x.ToString(), x => Ulid.Parse(x), DefaultHints.With(mappingHints)) | ||
: ValueConverter<Ulid, string>( | ||
x => x.ToString()!, | ||
x => Ulid.Parse(x, CultureInfo.InvariantCulture), | ||
DefaultHints.With(mappingHints)) | ||
{ | ||
private static readonly ConverterMappingHints DefaultHints = new(26, unicode: false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Diagnostics; | ||
using Stl.Rpc.Infrastructure; | ||
|
||
namespace Stl.Rpc.Diagnostics; | ||
|
||
public class RpcMethodActivityTrace(RpcMethodActivityTracer tracer, Activity activity) : RpcMethodTrace | ||
{ | ||
public override void OnResultTaskReady(RpcInboundCall call) | ||
=> _ = call.UntypedResultTask.ContinueWith(Complete, | ||
CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); | ||
|
||
protected void Complete(Task resultTask) | ||
{ | ||
activity.Dispose(); | ||
tracer.CallCount?.Add(1); | ||
if (!resultTask.IsCompletedSuccessfully()) | ||
tracer.ErrorCount?.Add(1); | ||
tracer.CallDuration?.Record(activity.Duration.TotalMilliseconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using Stl.Rpc.Infrastructure; | ||
|
||
namespace Stl.Rpc.Diagnostics; | ||
|
||
public class RpcMethodActivityTracer : RpcMethodTracer | ||
{ | ||
protected readonly object Lock = new(); | ||
|
||
public string OperationName { get; init; } | ||
public ActivitySource ActivitySource { get; init; } | ||
public bool UseMeter { get; init; } = false; | ||
public Meter? Meter { get; protected set; } | ||
public Counter<long>? CallCount { get; protected set; } | ||
public Counter<long>? ErrorCount { get; protected set; } | ||
public Histogram<double>? CallDuration { get; protected set; } | ||
|
||
public RpcMethodActivityTracer(RpcMethodDef method) : base(method) | ||
{ | ||
OperationName = $"rpc:{method.Name.Value}@{method.Service.Name.Value}"; | ||
ActivitySource = GetType().GetActivitySource(); | ||
} | ||
|
||
public override RpcMethodTrace? TryStartTrace(RpcInboundCall call) | ||
{ | ||
if (UseMeter && Meter == null) | ||
CreateMeter(); | ||
var activity = ActivitySource.StartActivity(OperationName); | ||
return activity == null ? null : new RpcMethodActivityTrace(this, activity); | ||
} | ||
|
||
protected void CreateMeter() | ||
{ | ||
if (Meter != null) return; | ||
lock (Lock) { | ||
if (Meter != null) return; | ||
|
||
Meter = GetType().GetMeter(); | ||
CallCount = Meter.CreateCounter<long>("Call count: " + OperationName); | ||
ErrorCount = Meter.CreateCounter<long>("Error count: " + OperationName); | ||
CallDuration = Meter.CreateHistogram<double>("Call duration: " + OperationName, "ms"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Stl.Rpc.Infrastructure; | ||
|
||
namespace Stl.Rpc.Diagnostics; | ||
|
||
public abstract class RpcMethodTrace | ||
{ | ||
public abstract void OnResultTaskReady(RpcInboundCall call); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Stl.Rpc.Infrastructure; | ||
|
||
namespace Stl.Rpc.Diagnostics; | ||
|
||
public abstract class RpcMethodTracer(RpcMethodDef method) | ||
{ | ||
public RpcMethodDef Method { get; init; } = method; | ||
public Sampler Sampler { get; init; } = Sampler.Always; | ||
|
||
public abstract RpcMethodTrace? TryStartTrace(RpcInboundCall call); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/Stl.Rpc/Infrastructure/RpcInboundCallActivityMiddleware.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.