Skip to content

Commit 8cb6a7b

Browse files
committed
Expose Origin on IConfigurationSource
1 parent 5247cf4 commit 8cb6a7b

9 files changed

+61
-51
lines changed

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/CompositeConfigurationSource.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public CompositeConfigurationSource(IEnumerable<IConfigurationSource> sources)
3131
_sources = [..sources];
3232
}
3333

34+
public ConfigurationOrigins Origin => ConfigurationOrigins.Unknown;
35+
3436
/// <summary>
3537
/// Adds a new configuration source to this instance.
3638
/// </summary>

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/DictionaryConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public DictionaryConfigurationSource(IReadOnlyDictionary<string, string> diction
1919
_dictionary = dictionary;
2020
}
2121

22-
internal override ConfigurationOrigins Origin => ConfigurationOrigins.Code;
22+
public override ConfigurationOrigins Origin => ConfigurationOrigins.Code;
2323

2424
protected override string? GetString(string key)
2525
{

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/DictionaryObjectConfigurationSource.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ namespace Datadog.Trace.Configuration;
1515

1616
internal class DictionaryObjectConfigurationSource : IConfigurationSource
1717
{
18-
private readonly ConfigurationOrigins _origin;
19-
2018
public DictionaryObjectConfigurationSource(IReadOnlyDictionary<string, object?> dictionary)
2119
: this(dictionary, ConfigurationOrigins.Code)
2220
{
@@ -25,9 +23,11 @@ public DictionaryObjectConfigurationSource(IReadOnlyDictionary<string, object?>
2523
public DictionaryObjectConfigurationSource(IReadOnlyDictionary<string, object?> dictionary, ConfigurationOrigins origin)
2624
{
2725
Dictionary = dictionary;
28-
_origin = origin;
26+
Origin = origin;
2927
}
3028

29+
public ConfigurationOrigins Origin { get; }
30+
3131
protected IReadOnlyDictionary<string, object?> Dictionary { get; }
3232

3333
protected virtual bool TryGetValue(string key, out object? value)
@@ -39,17 +39,17 @@ public ConfigurationResult<string> GetString(string key, IConfigurationTelemetry
3939
{
4040
if (objValue is not string value)
4141
{
42-
telemetry.Record(key, objValue.ToString(), recordValue: true, _origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
42+
telemetry.Record(key, objValue.ToString(), recordValue: true, Origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
4343
return ConfigurationResult<string>.ParseFailure();
4444
}
4545

4646
if (validator is null || validator(value))
4747
{
48-
telemetry.Record(key, value, recordValue, _origin);
48+
telemetry.Record(key, value, recordValue, Origin);
4949
return ConfigurationResult<string>.Valid(value);
5050
}
5151

52-
telemetry.Record(key, value, recordValue, _origin, TelemetryErrorCode.FailedValidation);
52+
telemetry.Record(key, value, recordValue, Origin, TelemetryErrorCode.FailedValidation);
5353
return ConfigurationResult<string>.Invalid(value);
5454
}
5555

@@ -62,17 +62,17 @@ public ConfigurationResult<int> GetInt32(string key, IConfigurationTelemetry tel
6262
{
6363
if (objValue is not int value)
6464
{
65-
telemetry.Record(key, objValue.ToString(), recordValue: true, _origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
65+
telemetry.Record(key, objValue.ToString(), recordValue: true, Origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
6666
return ConfigurationResult<int>.ParseFailure();
6767
}
6868

6969
if (validator is null || validator(value))
7070
{
71-
telemetry.Record(key, value, _origin);
71+
telemetry.Record(key, value, Origin);
7272
return ConfigurationResult<int>.Valid(value);
7373
}
7474

75-
telemetry.Record(key, value, _origin, TelemetryErrorCode.FailedValidation);
75+
telemetry.Record(key, value, Origin, TelemetryErrorCode.FailedValidation);
7676
return ConfigurationResult<int>.Invalid(value);
7777
}
7878

@@ -85,17 +85,17 @@ public ConfigurationResult<double> GetDouble(string key, IConfigurationTelemetry
8585
{
8686
if (objValue is not double value)
8787
{
88-
telemetry.Record(key, objValue.ToString(), recordValue: true, _origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
88+
telemetry.Record(key, objValue.ToString(), recordValue: true, Origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
8989
return ConfigurationResult<double>.ParseFailure();
9090
}
9191

9292
if (validator is null || validator(value))
9393
{
94-
telemetry.Record(key, value, _origin);
94+
telemetry.Record(key, value, Origin);
9595
return ConfigurationResult<double>.Valid(value);
9696
}
9797

98-
telemetry.Record(key, value, _origin, TelemetryErrorCode.FailedValidation);
98+
telemetry.Record(key, value, Origin, TelemetryErrorCode.FailedValidation);
9999
return ConfigurationResult<double>.Invalid(value);
100100
}
101101

@@ -108,17 +108,17 @@ public ConfigurationResult<bool> GetBool(string key, IConfigurationTelemetry tel
108108
{
109109
if (objValue is not bool value)
110110
{
111-
telemetry.Record(key, objValue.ToString(), recordValue: true, _origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
111+
telemetry.Record(key, objValue.ToString(), recordValue: true, Origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
112112
return ConfigurationResult<bool>.ParseFailure();
113113
}
114114

115115
if (validator is null || validator(value))
116116
{
117-
telemetry.Record(key, value, _origin);
117+
telemetry.Record(key, value, Origin);
118118
return ConfigurationResult<bool>.Valid(value);
119119
}
120120

121-
telemetry.Record(key, value, _origin, TelemetryErrorCode.FailedValidation);
121+
telemetry.Record(key, value, Origin, TelemetryErrorCode.FailedValidation);
122122
return ConfigurationResult<bool>.Invalid(value);
123123
}
124124

@@ -134,18 +134,18 @@ public ConfigurationResult<IDictionary<string, string>> GetDictionary(string key
134134
{
135135
if (objValue is not IDictionary<string, string> value)
136136
{
137-
telemetry.Record(key, objValue.ToString(), recordValue: true, _origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
137+
telemetry.Record(key, objValue.ToString(), recordValue: true, Origin, TelemetryErrorCode.UnexpectedTypeInConfigurationSource);
138138
return ConfigurationResult<IDictionary<string, string>>.ParseFailure();
139139
}
140140

141141
var dictAsString = string.Join($"{separator}", value.Select(x => $"{key}:{value}"));
142142
if (validator is null || validator(value))
143143
{
144-
telemetry.Record(key, dictAsString, recordValue: true, _origin);
144+
telemetry.Record(key, dictAsString, recordValue: true, Origin);
145145
return ConfigurationResult<IDictionary<string, string>>.Valid(value);
146146
}
147147

148-
telemetry.Record(key, dictAsString, recordValue: true, _origin, TelemetryErrorCode.FailedValidation);
148+
telemetry.Record(key, dictAsString, recordValue: true, Origin, TelemetryErrorCode.FailedValidation);
149149
return ConfigurationResult<IDictionary<string, string>>.Invalid(value);
150150
}
151151

@@ -172,15 +172,15 @@ public ConfigurationResult<T> GetAs<T>(string key, IConfigurationTelemetry telem
172172
{
173173
if (validator is null || validator(result.Result))
174174
{
175-
telemetry.Record(key, valueAsString, recordValue, _origin);
175+
telemetry.Record(key, valueAsString, recordValue, Origin);
176176
return ConfigurationResult<T>.Valid(result.Result);
177177
}
178178

179-
telemetry.Record(key, valueAsString, recordValue, _origin, TelemetryErrorCode.FailedValidation);
179+
telemetry.Record(key, valueAsString, recordValue, Origin, TelemetryErrorCode.FailedValidation);
180180
return ConfigurationResult<T>.Invalid(result.Result);
181181
}
182182

183-
telemetry.Record(key, valueAsString, recordValue, _origin, TelemetryErrorCode.ParsingCustomError);
183+
telemetry.Record(key, valueAsString, recordValue, Origin, TelemetryErrorCode.ParsingCustomError);
184184
return ConfigurationResult<T>.ParseFailure();
185185
}
186186

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/EnvironmentConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Datadog.Trace.Configuration
1717
internal class EnvironmentConfigurationSource : StringConfigurationSource
1818
{
1919
/// <inheritdoc />
20-
internal override ConfigurationOrigins Origin => ConfigurationOrigins.EnvVars;
20+
public override ConfigurationOrigins Origin => ConfigurationOrigins.EnvVars;
2121

2222
/// <inheritdoc />
2323
protected override string? GetString(string key)

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/IConfigurationSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ namespace Datadog.Trace.Configuration;
1818
/// </summary>
1919
public interface IConfigurationSource
2020
{
21+
/// <summary>
22+
/// Gets the origin of the configuration source.
23+
/// </summary>
24+
public ConfigurationOrigins Origin { get; }
25+
2126
/// <summary>
2227
/// Gets the <see cref="string"/> value of
2328
/// the setting with the specified key.

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/JsonConfigurationSource.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ internal class JsonConfigurationSource : IConfigurationSource
2828
{
2929
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(JsonConfigurationSource));
3030
private readonly JToken? _configuration;
31-
private readonly ConfigurationOrigins _origin;
3231

3332
/// <summary>
3433
/// Initializes a new instance of the <see cref="JsonConfigurationSource"/>
@@ -60,9 +59,11 @@ private protected JsonConfigurationSource(string json, ConfigurationOrigins orig
6059
if (deserialize is null) { ThrowHelper.ThrowArgumentNullException(nameof(deserialize)); }
6160

6261
_configuration = deserialize(json);
63-
_origin = origin;
62+
Origin = origin;
6463
}
6564

65+
public ConfigurationOrigins Origin { get; }
66+
6667
internal string? JsonConfigurationFilePath { get; }
6768

6869
internal bool TreatNullDictionaryAsEmpty { get; set; } = true;
@@ -108,17 +109,17 @@ public ConfigurationResult<string> GetString(string key, IConfigurationTelemetry
108109
{
109110
if (validator is null || validator(value))
110111
{
111-
telemetry.Record(key, value, recordValue, _origin);
112+
telemetry.Record(key, value, recordValue, Origin);
112113
return ConfigurationResult<string>.Valid(value);
113114
}
114115

115-
telemetry.Record(key, value, recordValue, _origin, TelemetryErrorCode.FailedValidation);
116+
telemetry.Record(key, value, recordValue, Origin, TelemetryErrorCode.FailedValidation);
116117
return ConfigurationResult<string>.Invalid(value);
117118
}
118119
}
119120
catch (Exception)
120121
{
121-
telemetry.Record(key, token?.ToString(), recordValue, _origin, TelemetryErrorCode.JsonStringError);
122+
telemetry.Record(key, token?.ToString(), recordValue, Origin, TelemetryErrorCode.JsonStringError);
122123
throw; // Existing behaviour
123124
}
124125

@@ -137,17 +138,17 @@ public ConfigurationResult<int> GetInt32(string key, IConfigurationTelemetry tel
137138
{
138139
if (validator is null || validator(value.Value))
139140
{
140-
telemetry.Record(key, value.Value, _origin);
141+
telemetry.Record(key, value.Value, Origin);
141142
return ConfigurationResult<int>.Valid(value.Value);
142143
}
143144

144-
telemetry.Record(key, value.Value, _origin, TelemetryErrorCode.FailedValidation);
145+
telemetry.Record(key, value.Value, Origin, TelemetryErrorCode.FailedValidation);
145146
return ConfigurationResult<int>.Invalid(value.Value);
146147
}
147148
}
148149
catch (Exception)
149150
{
150-
telemetry.Record(key, token?.ToString(), recordValue: true, _origin, TelemetryErrorCode.JsonInt32Error);
151+
telemetry.Record(key, token?.ToString(), recordValue: true, Origin, TelemetryErrorCode.JsonInt32Error);
151152
throw; // Exising behaviour
152153
}
153154

@@ -166,17 +167,17 @@ public ConfigurationResult<double> GetDouble(string key, IConfigurationTelemetry
166167
{
167168
if (validator is null || validator(value.Value))
168169
{
169-
telemetry.Record(key, value.Value, _origin);
170+
telemetry.Record(key, value.Value, Origin);
170171
return ConfigurationResult<double>.Valid(value.Value);
171172
}
172173

173-
telemetry.Record(key, value.Value, _origin, TelemetryErrorCode.FailedValidation);
174+
telemetry.Record(key, value.Value, Origin, TelemetryErrorCode.FailedValidation);
174175
return ConfigurationResult<double>.Invalid(value.Value);
175176
}
176177
}
177178
catch (Exception)
178179
{
179-
telemetry.Record(key, token?.ToString(), recordValue: true, _origin, TelemetryErrorCode.JsonDoubleError);
180+
telemetry.Record(key, token?.ToString(), recordValue: true, Origin, TelemetryErrorCode.JsonDoubleError);
180181
throw; // Exising behaviour
181182
}
182183

@@ -195,17 +196,17 @@ public ConfigurationResult<bool> GetBool(string key, IConfigurationTelemetry tel
195196
{
196197
if (validator is null || validator(value.Value))
197198
{
198-
telemetry.Record(key, value.Value, _origin);
199+
telemetry.Record(key, value.Value, Origin);
199200
return ConfigurationResult<bool>.Valid(value.Value);
200201
}
201202

202-
telemetry.Record(key, value.Value, _origin, TelemetryErrorCode.FailedValidation);
203+
telemetry.Record(key, value.Value, Origin, TelemetryErrorCode.FailedValidation);
203204
return ConfigurationResult<bool>.Invalid(value.Value);
204205
}
205206
}
206207
catch (Exception)
207208
{
208-
telemetry.Record(key, token?.ToString(), recordValue: true, _origin, TelemetryErrorCode.JsonBooleanError);
209+
telemetry.Record(key, token?.ToString(), recordValue: true, Origin, TelemetryErrorCode.JsonBooleanError);
209210
throw; // Exising behaviour
210211
}
211212

@@ -228,20 +229,20 @@ public ConfigurationResult<T> GetAs<T>(string key, IConfigurationTelemetry telem
228229
{
229230
if (validator is null || validator(value.Result))
230231
{
231-
telemetry.Record(key, valueAsString, recordValue, _origin);
232+
telemetry.Record(key, valueAsString, recordValue, Origin);
232233
return ConfigurationResult<T>.Valid(value.Result);
233234
}
234235

235-
telemetry.Record(key, valueAsString, recordValue, _origin, TelemetryErrorCode.FailedValidation);
236+
telemetry.Record(key, valueAsString, recordValue, Origin, TelemetryErrorCode.FailedValidation);
236237
return ConfigurationResult<T>.Invalid(value.Result);
237238
}
238239

239-
telemetry.Record(key, valueAsString, recordValue, _origin, TelemetryErrorCode.ParsingCustomError);
240+
telemetry.Record(key, valueAsString, recordValue, Origin, TelemetryErrorCode.ParsingCustomError);
240241
}
241242
}
242243
catch (Exception)
243244
{
244-
telemetry.Record(key, token?.ToString(), recordValue, _origin, TelemetryErrorCode.JsonStringError);
245+
telemetry.Record(key, token?.ToString(), recordValue, Origin, TelemetryErrorCode.JsonStringError);
245246
throw; // Exising behaviour
246247
}
247248

@@ -301,7 +302,7 @@ public ConfigurationResult<IDictionary<string, string>> GetDictionary(string key
301302
catch (Exception e)
302303
{
303304
Log.Error(e, "Unable to parse configuration value for {ConfigurationKey} as key-value pairs of strings.", key);
304-
telemetry.Record(key, tokenAsString, recordValue: true, _origin, TelemetryErrorCode.JsonStringError);
305+
telemetry.Record(key, tokenAsString, recordValue: true, Origin, TelemetryErrorCode.JsonStringError);
305306
return ConfigurationResult<IDictionary<string, string>>.ParseFailure();
306307
}
307308
}
@@ -311,19 +312,19 @@ public ConfigurationResult<IDictionary<string, string>> GetDictionary(string key
311312
}
312313
catch (InvalidCastException)
313314
{
314-
telemetry.Record(key, tokenAsString, recordValue: true, _origin, TelemetryErrorCode.JsonStringError);
315+
telemetry.Record(key, tokenAsString, recordValue: true, Origin, TelemetryErrorCode.JsonStringError);
315316
throw; // Exising behaviour
316317
}
317318

318319
ConfigurationResult<IDictionary<string, string>> Validate(IDictionary<string, string> dictionary)
319320
{
320321
if (validator is null || validator(dictionary))
321322
{
322-
telemetry.Record(key, tokenAsString, recordValue: true, _origin);
323+
telemetry.Record(key, tokenAsString, recordValue: true, Origin);
323324
return ConfigurationResult<IDictionary<string, string>>.Valid(dictionary);
324325
}
325326

326-
telemetry.Record(key, tokenAsString, recordValue: true, _origin, TelemetryErrorCode.FailedValidation);
327+
telemetry.Record(key, tokenAsString, recordValue: true, Origin, TelemetryErrorCode.FailedValidation);
327328
return ConfigurationResult<IDictionary<string, string>>.Invalid(dictionary);
328329
}
329330
}
@@ -363,7 +364,7 @@ public ConfigurationResult<IDictionary<string, string>> GetDictionary(string key
363364
catch (Exception e)
364365
{
365366
Log.Error(e, "Unable to parse configuration value for {ConfigurationKey} as key-value pairs of strings.", key);
366-
telemetry.Record(key, tokenAsString, recordValue: true, _origin, TelemetryErrorCode.JsonStringError);
367+
telemetry.Record(key, tokenAsString, recordValue: true, Origin, TelemetryErrorCode.JsonStringError);
367368
return ConfigurationResult<IDictionary<string, string>>.ParseFailure();
368369
}
369370
}
@@ -373,19 +374,19 @@ public ConfigurationResult<IDictionary<string, string>> GetDictionary(string key
373374
}
374375
catch (InvalidCastException)
375376
{
376-
telemetry.Record(key, tokenAsString, recordValue: true, _origin, TelemetryErrorCode.JsonStringError);
377+
telemetry.Record(key, tokenAsString, recordValue: true, Origin, TelemetryErrorCode.JsonStringError);
377378
throw; // Exising behaviour
378379
}
379380

380381
ConfigurationResult<IDictionary<string, string>> Validate(IDictionary<string, string> dictionary)
381382
{
382383
if (validator is null || validator(dictionary))
383384
{
384-
telemetry.Record(key, tokenAsString, recordValue: true, _origin);
385+
telemetry.Record(key, tokenAsString, recordValue: true, Origin);
385386
return ConfigurationResult<IDictionary<string, string>>.Valid(dictionary);
386387
}
387388

388-
telemetry.Record(key, tokenAsString, recordValue: true, _origin, TelemetryErrorCode.FailedValidation);
389+
telemetry.Record(key, tokenAsString, recordValue: true, Origin, TelemetryErrorCode.FailedValidation);
389390
return ConfigurationResult<IDictionary<string, string>>.Invalid(dictionary);
390391
}
391392
}

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/NameValueConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal NameValueConfigurationSource(NameValueCollection nameValueCollection, C
3939
Origin = origin;
4040
}
4141

42-
internal override ConfigurationOrigins Origin { get; }
42+
public override ConfigurationOrigins Origin { get; }
4343

4444
/// <inheritdoc />
4545
protected override string? GetString(string key)

0 commit comments

Comments
 (0)