Skip to content

Commit

Permalink
Fdb: rename FdbMetricsReporter to FdbClientInstrumentation and add OT…
Browse files Browse the repository at this point in the history
…EL instrumentation helpers

- Move the ActivitySource to FdbClientInstrumentation, so that it could be used by other parts of the library
- Standardize the name of the Trace, Metrics and HealthChecks to "FoundationDB.Client" (with uppercase 'B' !)
  • Loading branch information
KrzysFR committed Aug 1, 2024
1 parent 8a36f26 commit 4cba7a1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 58 deletions.
30 changes: 4 additions & 26 deletions Aspire.FoundationDB/FdbAspireComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ namespace Microsoft.Extensions.Hosting
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

/// <summary>Provides extension methods for adding FoundationDB to the local DI container.</summary>
[PublicAPI]
public static class FdbAspireComponentExtensions
{

private const string ActivitySourceName = "FoundationDb.Client";

private const string DefaultConfigSectionName = "Aspire:FoundationDb:Client";

/// <summary>Add support for connecting to a FoundationDB cluster</summary>
Expand Down Expand Up @@ -251,13 +249,13 @@ private static IHostApplicationBuilder AddFoundationDb(this IHostApplicationBuil
if (!settings.DisableTracing)
{
builder.Services.AddOpenTelemetry()
.WithTracing(traceBuilder => traceBuilder.AddSource(ActivitySourceName));
.WithTracing(traceBuilder => traceBuilder.AddFoundationDbInstrumentation());
}

if (!settings.DisableHealthChecks)
{
var check = new HealthCheckRegistration(
"FoundationDb.Client",
FdbClientInstrumentation.HealthCheckName,
sp =>
{
try
Expand Down Expand Up @@ -288,27 +286,7 @@ private static IHostApplicationBuilder AddFoundationDb(this IHostApplicationBuil
if (!settings.DisableMetrics)
{
builder.Services.AddOpenTelemetry()
.WithMetrics((meterProviderBuilder) =>
{
meterProviderBuilder
.AddMeter("FdbClient")
.AddView("db.fdb.client.transactions.duration",
new ExplicitBucketHistogramConfiguration
{
Boundaries = [ 0, 0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10 ],
})
.AddView("db.fdb.client.operations.duration",
new ExplicitBucketHistogramConfiguration
{
Boundaries = [ 0, 0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10 ],
})
.AddView("db.fdb.client.operations.size",
new ExplicitBucketHistogramConfiguration
{
Boundaries = [ 0, 10, 20, 50, 100, 200, 500, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000, 200_000, 500_000, 1_000_000, 2_000_000, 5_000_000, 10_000_000 ]
})
;
});
.WithMetrics((meterBuilder) => meterBuilder.AddFoundationDbInstrumentation());
}

return builder;
Expand Down
40 changes: 40 additions & 0 deletions Aspire.FoundationDB/FdbClientOpenTelemtryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#region Copyright (c) 2023-2024 SnowBank SAS
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
//
#endregion

namespace OpenTelemetry.Trace
{
using FoundationDB.Client;
using OpenTelemetry.Metrics;

public static class FdbClientOpenTelemtryExtensions
{

public static TracerProviderBuilder AddFoundationDbInstrumentation(this TracerProviderBuilder provider)
{
provider.AddSource(FdbClientInstrumentation.ActivityName);
return provider;
}

public static MeterProviderBuilder AddFoundationDbInstrumentation(this MeterProviderBuilder provider)
{
provider.AddMeter(FdbClientInstrumentation.MeterName)
.AddView(
"db.fdb.client.transactions.duration",
new ExplicitBucketHistogramConfiguration { Boundaries = [ 0, 0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10 ], })
.AddView(
"db.fdb.client.operations.duration",
new ExplicitBucketHistogramConfiguration { Boundaries = [ 0, 0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10 ], })
.AddView(
"db.fdb.client.operations.size",
new ExplicitBucketHistogramConfiguration { Boundaries = [ 0, 10, 20, 50, 100, 200, 500, 1_000, 2_000, 5_000, 10_000, 20_000, 50_000, 100_000, 200_000, 500_000, 1_000_000, 2_000_000, 5_000_000, 10_000_000 ] })
;
return provider;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ namespace FoundationDB.Client
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using OpenTelemetry.Metrics;

public static class FdbMetricsReporter
public static class FdbClientInstrumentation
{

private const string METER_NAME = "FdbClient";
private const string VERSION = "0.1.0"; //TODO: which version?
public const string ActivityName = "FoundationDB.Client";
public const string ActivityVersion = "0.1.0"; //TODO: which version?
public const string MeterName = "FoundationDB.Client";
public const string MeterVersion = "0.1.0"; //TODO: which version?
public const string HealthCheckName = "FoundationDB.Client";

private static Meter Meter { get; } = new(METER_NAME, VERSION); //TODO: version?
internal static readonly ActivitySource ActivitySource = new(ActivityName, ActivityVersion);

private static Meter Meter { get; } = new(MeterName, MeterVersion); //TODO: version?

private static UpDownCounter<int> TransactionsExecuting { get; } = Meter.CreateUpDownCounter<int>(
"db.fdb.client.transactions.executing",
Expand Down
31 changes: 14 additions & 17 deletions FoundationDB.Client/FdbOperationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ namespace FoundationDB.Client
[PublicAPI]
public sealed class FdbOperationContext : IDisposable
{
//REVIEW: maybe we should find a way to reduce the size of this class? (it's already almost at 100 bytes !)

private static readonly ActivitySource ActivitySource = new("FoundationDB.Client");

/// <summary>The database used by the operation</summary>
public IFdbDatabase Database => m_db;
Expand Down Expand Up @@ -746,7 +743,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
var targetName = PrettifyTargetName((handler.Target?.GetType() ?? handler.Method.DeclaringType)?.GetFriendlyName());
var methodName = PrettifyMethodName(handler.Method.Name);

using var mainActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordTransactions) ? ActivitySource.StartActivity(context.Mode == FdbTransactionMode.ReadOnly ? "FDB Read" : "FDB ReadWrite", kind: ActivityKind.Client) : null;
using var mainActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordTransactions) ? FdbClientInstrumentation.ActivitySource.StartActivity(context.Mode == FdbTransactionMode.ReadOnly ? "FDB Read" : "FDB ReadWrite", kind: ActivityKind.Client) : null;
context.Activity = mainActivity;

bool reportTransStarted = false;
Expand Down Expand Up @@ -781,7 +778,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
if (trans.IsSnapshot) mainActivity.SetTag("db.fdb.trans.snapshot", true);
}

FdbMetricsReporter.ReportTransactionStart(context, trans);
FdbClientInstrumentation.ReportTransactionStart(context, trans);
reportTransStarted = true;

while (!context.Committed && !context.Cancellation.IsCancellationRequested)
Expand All @@ -790,7 +787,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
bool hasRunValueChecks = false;
result = default!;

using var attemptActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordOperations) ? ActivitySource.StartActivity(targetName + "::" + methodName, kind: ActivityKind.Client) : null;
using var attemptActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordOperations) ? FdbClientInstrumentation.ActivitySource.StartActivity(targetName + "::" + methodName, kind: ActivityKind.Client) : null;
if (attemptActivity?.IsAllDataRequested == true)
{
attemptActivity.SetTag("db.system", "fdb");
Expand All @@ -805,7 +802,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
{
TIntermediate intermediate;

currentActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordSteps) ? ActivitySource.StartActivity("FDB Handler") : null;
currentActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordSteps) ? FdbClientInstrumentation.ActivitySource.StartActivity("FDB Handler") : null;
if (currentActivity != null)
{
context.Activity = currentActivity;
Expand All @@ -821,7 +818,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
}

Contract.Debug.Assert(!reportOpStarted);
FdbMetricsReporter.ReportOperationStarted(context, trans);
FdbClientInstrumentation.ReportOperationStarted(context, trans);
reportOpStarted = true;

// call the user provided lambda
Expand Down Expand Up @@ -1094,7 +1091,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
hasRunValueChecks = true;
if (context.HasPendingValueChecks(out var valueChecks))
{
currentActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordSteps) ? ActivitySource.StartActivity("FDB Value Checks") : null;
currentActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordSteps) ? FdbClientInstrumentation.ActivitySource.StartActivity("FDB Value Checks") : null;
if (currentActivity != null)
{
context.Activity = currentActivity;
Expand Down Expand Up @@ -1133,7 +1130,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
if (!trans.IsReadOnly)
{ // commit the transaction

currentActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordSteps) ? ActivitySource.StartActivity("FDB Commit") : null;
currentActivity = tracingOptions.HasFlag(FdbTracingOptions.RecordSteps) ? FdbClientInstrumentation.ActivitySource.StartActivity("FDB Commit") : null;
if (currentActivity != null)
{
context.Activity = currentActivity;
Expand Down Expand Up @@ -1163,7 +1160,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
context.Activity = mainActivity;
}

FdbMetricsReporter.ReportOperationCommitted(trans, context);
FdbClientInstrumentation.ReportOperationCommitted(trans, context);
}

// we are done
Expand Down Expand Up @@ -1416,7 +1413,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
{
Contract.Debug.Assert(reportOpStarted);
reportOpStarted = false;
FdbMetricsReporter.ReportOperationCompleted(context, trans, e2.Code);
FdbClientInstrumentation.ReportOperationCompleted(context, trans, e2.Code);
throw;
}
shouldRethrow = true;
Expand All @@ -1427,7 +1424,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
{
Contract.Debug.Assert(reportOpStarted);
reportOpStarted = false;
FdbMetricsReporter.ReportOperationCompleted(context, trans, context.PreviousError);
FdbClientInstrumentation.ReportOperationCompleted(context, trans, context.PreviousError);
throw;
}

Expand Down Expand Up @@ -1482,7 +1479,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
{
Contract.Debug.Assert(reportOpStarted);
reportOpStarted = false;
FdbMetricsReporter.ReportOperationCompleted(context, trans, e2.Code);
FdbClientInstrumentation.ReportOperationCompleted(context, trans, e2.Code);
throw;
}
}
Expand All @@ -1500,14 +1497,14 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu
{
Contract.Debug.Assert(reportOpStarted);
reportOpStarted = false;
FdbMetricsReporter.ReportOperationCompleted(context, trans, context.PreviousError);
FdbClientInstrumentation.ReportOperationCompleted(context, trans, context.PreviousError);
throw;
}
}

Contract.Debug.Assert(reportOpStarted);
reportOpStarted = false;
FdbMetricsReporter.ReportOperationCompleted(context, trans, context.PreviousError);
FdbClientInstrumentation.ReportOperationCompleted(context, trans, context.PreviousError);

// update the base time for the next attempt
context.BaseDuration = context.ElapsedTotal;
Expand Down Expand Up @@ -1548,7 +1545,7 @@ internal static async Task<TResult> ExecuteInternal<TState, TIntermediate, TResu

if (reportTransStarted)
{
FdbMetricsReporter.ReportTransactionStop(context);
FdbClientInstrumentation.ReportTransactionStop(context);
}

if (context.BaseDuration.TotalSeconds >= 10)
Expand Down
22 changes: 11 additions & 11 deletions FoundationDB.Client/FdbTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public Task<bool> TryGetAsync(ReadOnlySpan<byte> key, IBufferWriter<byte> valueW

private Task<Slice> PerformGetOperation(ReadOnlySpan<byte> key, bool snapshot)
{
FdbMetricsReporter.ReportGet(this);
FdbClientInstrumentation.ReportGet(this);

return m_log == null ? m_handler.GetAsync(key, snapshot: snapshot, m_cancellation) : ExecuteLogged(this, key, snapshot);

Expand All @@ -644,7 +644,7 @@ static Task<Slice> ExecuteLogged(FdbTransaction self, ReadOnlySpan<byte> key, bo

private Task<bool> PerformGetOperation(ReadOnlySpan<byte> key, IBufferWriter<byte> valueWriter, bool snapshot)
{
FdbMetricsReporter.ReportGet(this);
FdbClientInstrumentation.ReportGet(this);

return m_log == null ? m_handler.TryGetAsync(key, valueWriter, snapshot: snapshot, m_cancellation) : ExecuteLogged(this, key, snapshot, valueWriter);

Expand Down Expand Up @@ -672,7 +672,7 @@ static Task<bool> ExecuteLogged(FdbTransaction self, ReadOnlySpan<byte> key, boo

private Task<(FdbValueCheckResult Result, Slice Actual)> PerformValueCheckOperation(ReadOnlySpan<byte> key, Slice expected, bool snapshot)
{
FdbMetricsReporter.ReportGet(this); //REVIEW: use a specific operation type for this?
FdbClientInstrumentation.ReportGet(this); //REVIEW: use a specific operation type for this?

return m_log == null ? m_handler.CheckValueAsync(key, expected, snapshot: snapshot, m_cancellation) : ExecuteLogged(this, key, expected, snapshot);

Expand Down Expand Up @@ -704,7 +704,7 @@ public Task<Slice[]> GetValuesAsync(ReadOnlySpan<Slice> keys)

private Task<Slice[]> PerformGetValuesOperation(ReadOnlySpan<Slice> keys, bool snapshot)
{
FdbMetricsReporter.ReportGet(this, keys.Length);
FdbClientInstrumentation.ReportGet(this, keys.Length);

return m_log == null ? m_handler.GetValuesAsync(keys, snapshot: snapshot, m_cancellation) : ExecuteLogged(this, keys, snapshot);

Expand Down Expand Up @@ -755,7 +755,7 @@ private Task<FdbRangeChunk> PerformGetRangeOperation(
FdbReadMode read,
int iteration)
{
FdbMetricsReporter.ReportGetRange(this);
FdbClientInstrumentation.ReportGetRange(this);

return m_log == null
? m_handler.GetRangeAsync(beginInclusive, endExclusive, limit, reverse, targetBytes, mode, read, iteration, snapshot, m_cancellation)
Expand Down Expand Up @@ -843,7 +843,7 @@ public Task<Slice> GetKeyAsync(KeySelector selector)

private Task<Slice> PerformGetKeyOperation(KeySelector selector, bool snapshot)
{
FdbMetricsReporter.ReportGetKey(this);
FdbClientInstrumentation.ReportGetKey(this);

return m_log == null ? m_handler.GetKeyAsync(selector, snapshot: snapshot, m_cancellation) : ExecuteLogged(this, selector, snapshot);

Expand Down Expand Up @@ -879,7 +879,7 @@ public Task<Slice[]> GetKeysAsync(ReadOnlySpan<KeySelector> selectors)

private Task<Slice[]> PerformGetKeysOperation(ReadOnlySpan<KeySelector> selectors, bool snapshot)
{
FdbMetricsReporter.ReportGetKey(this, selectors.Length);
FdbClientInstrumentation.ReportGetKey(this, selectors.Length);

return m_log == null ? m_handler.GetKeysAsync(selectors, snapshot: snapshot, m_cancellation) : ExecuteLogged(this, selectors, snapshot);

Expand Down Expand Up @@ -911,7 +911,7 @@ public void Set(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value)

private void PerformSetOperation(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value)
{
FdbMetricsReporter.ReportSet(this);
FdbClientInstrumentation.ReportSet(this);

if (m_log == null)
{
Expand Down Expand Up @@ -1071,7 +1071,7 @@ public void Atomic(ReadOnlySpan<byte> key, ReadOnlySpan<byte> param, FdbMutation

private void PerformAtomicOperation(ReadOnlySpan<byte> key, ReadOnlySpan<byte> param, FdbMutationType type)
{
FdbMetricsReporter.ReportAtomicOp(this, type);
FdbClientInstrumentation.ReportAtomicOp(this, type);

if (m_log == null)
{
Expand Down Expand Up @@ -1110,7 +1110,7 @@ public void Clear(ReadOnlySpan<byte> key)

private void PerformClearOperation(ReadOnlySpan<byte> key)
{
FdbMetricsReporter.ReportClear(this);
FdbClientInstrumentation.ReportClear(this);

if (m_log == null)
{
Expand Down Expand Up @@ -1150,7 +1150,7 @@ public void ClearRange(ReadOnlySpan<byte> beginKeyInclusive, ReadOnlySpan<byte>

private void PerformClearRangeOperation(ReadOnlySpan<byte> beginKeyInclusive, ReadOnlySpan<byte> endKeyExclusive)
{
FdbMetricsReporter.ReportClearRange(this);
FdbClientInstrumentation.ReportClearRange(this);

if (m_log == null)
{
Expand Down

0 comments on commit 4cba7a1

Please sign in to comment.