From 3976314d8f484126b2c34a0b1bdf3b570da1dd71 Mon Sep 17 00:00:00 2001 From: Christophe Chevalier Date: Wed, 31 Jul 2024 15:37:20 +0200 Subject: [PATCH] Aspire: add settinsg to configure the default FdbTracingOptions - Default tracing options in the AppHost will be applied to all processed that use the fdb resource. - ex: WithDefaults(...., tracing: FdbTracingOptions.All) can be used to temporarily enable ALL tracing during local dev --- .../FdbAspireHostingExtensions.cs | 4 +++- .../FdbClusterResource.cs | 9 +++++++++ .../FdbAspireComponentExtensions.cs | 18 ++++++++++++++++++ Aspire.FoundationDB/FdbClientSettings.cs | 11 +++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Aspire.FoundationDB.Hosting/FdbAspireHostingExtensions.cs b/Aspire.FoundationDB.Hosting/FdbAspireHostingExtensions.cs index 07a71e446..5eb91629c 100644 --- a/Aspire.FoundationDB.Hosting/FdbAspireHostingExtensions.cs +++ b/Aspire.FoundationDB.Hosting/FdbAspireHostingExtensions.cs @@ -267,14 +267,16 @@ public static IResourceBuilder WithNativeLibrary(this IResou /// Default transaction timeout (or TimeSpan.Zero for infinite timeout) /// Default transaction max retry limit (or 0 for infinite retries) /// If true, the process will only have read-only access to the FoundationDB cluster. + /// Default tracing options /// These settings will be applied to the default connection options, and can be overriden per transaction, or during the program startup. - public static IResourceBuilder WithDefaults(this IResourceBuilder builder, TimeSpan? timeout = null, int? retryLimit = null, bool? readOnly = null) + public static IResourceBuilder WithDefaults(this IResourceBuilder builder, TimeSpan? timeout = null, int? retryLimit = null, bool? readOnly = null, FdbTracingOptions? tracing = null) { Contract.NotNull(builder); var fdbCluster = builder.Resource; if (timeout != null) fdbCluster.DefaultTimeout = timeout; if (retryLimit != null) fdbCluster.DefaultRetryLimit = retryLimit; if (readOnly != null) fdbCluster.ReadOnly = readOnly; + if (tracing != null) fdbCluster.DefaultTracing = tracing; return builder; } diff --git a/Aspire.FoundationDB.Hosting/FdbClusterResource.cs b/Aspire.FoundationDB.Hosting/FdbClusterResource.cs index af5c1170f..e94d04924 100644 --- a/Aspire.FoundationDB.Hosting/FdbClusterResource.cs +++ b/Aspire.FoundationDB.Hosting/FdbClusterResource.cs @@ -60,6 +60,10 @@ public FdbClusterResource(string name, string? entrypoint = null) : base(name, e /// See for more information. public int? DefaultRetryLimit { get; set; } + /// Default tracing options + /// See for more information. + public FdbTracingOptions? DefaultTracing { get; set; } + /// Specifies if the FoundationDB cluster is mounted in read-only mode by default. /// See for more information. public bool? ReadOnly { get; set; } @@ -132,6 +136,11 @@ private string GetConnectionString() builder["DefaultRetryLimit"] = this.DefaultRetryLimit.Value.ToString(CultureInfo.InvariantCulture); } + if (this.DefaultTracing.HasValue) + { + builder["DefaultTracing"] = ((int) this.DefaultTracing.Value).ToString(CultureInfo.InvariantCulture); + } + if (this.ReadOnly == true) { builder["ReadOnly"] = true; diff --git a/Aspire.FoundationDB/FdbAspireComponentExtensions.cs b/Aspire.FoundationDB/FdbAspireComponentExtensions.cs index e805b923c..a657f0ff6 100644 --- a/Aspire.FoundationDB/FdbAspireComponentExtensions.cs +++ b/Aspire.FoundationDB/FdbAspireComponentExtensions.cs @@ -226,6 +226,24 @@ private static IHostApplicationBuilder AddFoundationDb(this IHostApplicationBuil options.ConnectionOptions.DefaultRetryLimit = settings.DefaultRetryLimit.Value; } + // DefaultTracing=(flags) + if (cnx != null && cnx.ContainsKey("DefaultTracing")) + { + if (!int.TryParse((string) cnx["DefaultTracing"], out var count)) + { + throw new InvalidOperationException("Malformed default tracing options"); + } + if (count < 0) + { + throw new InvalidOperationException("Default tracing options must be a positive value"); + } + options.ConnectionOptions.DefaultTracing = (FdbTracingOptions) count; + } + else if (settings.DefaultTracing != null) + { + options.ConnectionOptions.DefaultTracing = (FdbTracingOptions) settings.DefaultTracing.Value; + } + // run additional custom configuration configureProvider?.Invoke(options); }); diff --git a/Aspire.FoundationDB/FdbClientSettings.cs b/Aspire.FoundationDB/FdbClientSettings.cs index ca9548b21..3d1d7a712 100644 --- a/Aspire.FoundationDB/FdbClientSettings.cs +++ b/Aspire.FoundationDB/FdbClientSettings.cs @@ -54,16 +54,23 @@ public sealed class FdbClientSettings /// See for more information. public int? DefaultRetryLimit { get; set; } + /// Overrides the default tracing options that should be used by this instance + /// + /// See for more information. + /// The default tracing options are and + /// + public int? DefaultTracing { get; set; } + /// Gets or sets a boolean value that indicates whether the FoundationDB health check is disabled or not. /// Enabled by default. public bool DisableHealthChecks { get; set; } /// Gets or sets a boolean value that indicates whether the OpenTelemetry tracing is disabled or not. - /// Enabled by default. + /// Tracing is enabled by default. public bool DisableTracing { get; set; } /// Gets or sets a boolean value that indicates whether the OpenTelemetry metrics are disabled or not. - /// Enabled by default. + /// Metrics are enabled by default. public bool DisableMetrics { get; set; } }