Skip to content

Commit

Permalink
Aspire: enable Metrics for the FoundationDB componeent
Browse files Browse the repository at this point in the history
- report the FdbClient metrics to the Aspire Host via OpenTelemetry
- add 'DisableMetrics' setting
  • Loading branch information
KrzysFR committed Jul 26, 2024
1 parent 2d5a407 commit a3ac669
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
19 changes: 18 additions & 1 deletion Aspire.FoundationDB/FdbAspireComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Microsoft.Extensions.Hosting
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using OpenTelemetry.Metrics;

/// <summary>Provides extension methods for adding FoundationDB to the local DI container.</summary>
[PublicAPI]
Expand Down Expand Up @@ -265,6 +266,23 @@ private static IHostApplicationBuilder AddFoundationDb(this IHostApplicationBuil
builder.Services.AddHealthChecks().Add(check);
}
}

if (!settings.DisableMetrics)
{
builder.Services.AddOpenTelemetry()
.WithMetrics((meterProviderBuilder) =>
{
meterProviderBuilder
.AddMeter("FdbClient")
.AddView("db.client.operation.duration",
new ExplicitBucketHistogramConfiguration
{
Boundaries = [0, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10],
})
;
});
}

return builder;
}

Expand All @@ -284,5 +302,4 @@ public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, Canc
}

}

}
4 changes: 4 additions & 0 deletions Aspire.FoundationDB/FdbClientSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public sealed class FdbClientSettings
/// <remarks><para>Enabled by default.</para></remarks>
public bool DisableTracing { get; set; }

/// <summary>Gets or sets a boolean value that indicates whether the OpenTelemetry metrics are disabled or not.</summary>
/// <remarks><para>Enabled by default.</para></remarks>
public bool DisableMetrics { get; set; }

}

}
8 changes: 5 additions & 3 deletions Aspire.FoundationDB/FdbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public FdbHealthCheck(FdbHealthCheckOptions options)

public FdbHealthCheckOptions Options { get; set; }

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct = default)
{
cancellationToken.ThrowIfCancellationRequested();
ct.ThrowIfCancellationRequested();
try
{
var rv = await this.Options.Provider.ReadAsync(tr => tr.GetReadVersionAsync(), cancellationToken);
//TODO: change this to get the "status json" ?

var rv = await this.Options.Provider.ReadAsync(tr => tr.GetReadVersionAsync(), ct);
return HealthCheckResult.Healthy(data: new Dictionary<string, object>()
{
["readVersion"] = rv,
Expand Down

0 comments on commit a3ac669

Please sign in to comment.