Skip to content

Commit

Permalink
PR feedback - include initial value in OverrideValue<T> helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Mar 1, 2024
1 parent e8a0b2a commit fa0e4ff
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ namespace Datadog.Trace.Configuration;
/// </summary>
public sealed class IntegrationSettings
{
private readonly bool? _enabledInitial;
private readonly bool? _analyticsEnabledInitial;
private readonly double _analyticsSampleRateInitial;
private OverrideValue<bool?> _enabledOverride = new();
private OverrideValue<bool?> _analyticsEnabledOverride = new();
private OverrideValue<double> _analyticsSampleRateOverride = new();
private OverrideValue<bool?> _enabled;
private OverrideValue<bool?> _analyticsEnabled;
private OverrideValue<double> _analyticsSampleRate;

internal IntegrationSettings(string integrationName, bool? enabled, bool? analyticsEnabled, double analyticsSampleRate)
{
IntegrationName = integrationName;
_enabledInitial = enabled;
_analyticsEnabledInitial = analyticsEnabled;
_analyticsSampleRateInitial = analyticsSampleRate;
_enabled = new(enabled);
_analyticsEnabled = new(analyticsEnabled);
_analyticsSampleRate = new(analyticsSampleRate);
}

/// <summary>
Expand All @@ -41,8 +38,8 @@ internal IntegrationSettings(string integrationName, bool? enabled, bool? analyt
public bool? Enabled
{
[Instrumented]
get => _enabledOverride.IsOverridden ? _enabledOverride.Value : _enabledInitial;
set => _enabledOverride = new(value);
get => _enabled.Value;
set => _enabled = _enabled.Override(value);
}

/// <summary>
Expand All @@ -52,8 +49,8 @@ public bool? Enabled
public bool? AnalyticsEnabled
{
[Instrumented]
get => _analyticsEnabledOverride.IsOverridden ? _analyticsEnabledOverride.Value : _analyticsEnabledInitial;
set => _analyticsEnabledOverride = new(value);
get => _analyticsEnabled.Value;
set => _analyticsEnabled = _analyticsEnabled.Override(value);
}

/// <summary>
Expand All @@ -63,19 +60,19 @@ public bool? AnalyticsEnabled
public double AnalyticsSampleRate
{
[Instrumented]
get => _analyticsSampleRateOverride.IsOverridden ? _analyticsSampleRateOverride.Value : _analyticsSampleRateInitial;
set => _analyticsSampleRateOverride = new(value);
get => _analyticsSampleRate.Value;
set => _analyticsSampleRate = _analyticsSampleRate.Override(value);
}

/// <summary>
/// "Serializes" the values, if changed, to an array. If there are no updates, returns null
/// </summary>
internal object?[]? GetChangeDetails()
=> IntegrationSettingsSerializationHelper.SerializeFromManual(
_enabledOverride.IsOverridden,
_enabledOverride.IsOverridden ? _enabledOverride.Value : null,
_analyticsEnabledOverride.IsOverridden,
_analyticsEnabledOverride.IsOverridden ? _analyticsEnabledOverride.Value : null,
_analyticsSampleRateOverride.IsOverridden,
_analyticsSampleRateOverride.IsOverridden ? _analyticsSampleRateOverride.Value : null);
_enabled.IsOverridden,
_enabled.IsOverridden ? _enabled.Value : null,
_analyticsEnabled.IsOverridden,
_analyticsEnabled.IsOverridden ? _analyticsEnabled.Value : null,
_analyticsSampleRate.IsOverridden,
_analyticsSampleRate.IsOverridden ? _analyticsSampleRate.Value : null);
}
23 changes: 16 additions & 7 deletions tracer/src/Datadog.Trace.Manual/Configuration/OverrideValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ namespace Datadog.Trace.Configuration;

internal readonly struct OverrideValue<T>
{
public readonly T Value;
public readonly T Initial;
public readonly bool IsOverridden;

public OverrideValue(T value)
private readonly T _override;

public OverrideValue(T initial)
{
Value = value;
IsOverridden = true;
Initial = initial;
_override = default!;
IsOverridden = false;
}

public OverrideValue()
public OverrideValue(T initial, T @override)
{
Value = default!;
IsOverridden = false;
Initial = initial;
_override = @override;
IsOverridden = true;
}

public T Value => IsOverridden ? _override : Initial;

public OverrideValue<T> Override(T value)
=> new(Initial, value);
}
Loading

0 comments on commit fa0e4ff

Please sign in to comment.