Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Commit 3395351

Browse files
authored
Open configuration API (#128)
* Added read-only properties for configurations * Unified configurations to have GetX methods * Made GetX methods virtual for overriding * Add Tracer.Builder read-only properties * Added obsolete message
1 parent f833a2a commit 3395351

File tree

3 files changed

+61
-57
lines changed

3 files changed

+61
-57
lines changed

src/Jaeger/Configuration.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,12 @@ public enum Propagation
129129
/// <summary>
130130
/// The serviceName that the tracer will use.
131131
/// </summary>
132-
private readonly string _serviceName;
132+
public string ServiceName { get; }
133+
133134
private readonly ILoggerFactory _loggerFactory;
134135
private readonly ILogger _logger;
135-
private CodecConfiguration _codecConfig;
136136
private IMetricsFactory _metricsFactory;
137137
private Dictionary<string, string> _tracerTags;
138-
private bool _useTraceId128Bit;
139138

140139
/// <summary>
141140
/// Lazy singleton <see cref="Tracer"/> initialized in <see cref="GetTracer()"/> method.
@@ -144,10 +143,13 @@ public enum Propagation
144143

145144
public SamplerConfiguration SamplerConfig { get; private set; }
146145
public ReporterConfiguration ReporterConfig { get; private set; }
146+
public CodecConfiguration CodecConfig { get; private set; }
147+
public bool UseTraceId128Bit { get; private set; }
148+
public IReadOnlyDictionary<string, string> TracerTags => _tracerTags;
147149

148150
public Configuration(string serviceName, ILoggerFactory loggerFactory)
149151
{
150-
_serviceName = Tracer.Builder.CheckValidServiceName(serviceName);
152+
ServiceName = Tracer.Builder.CheckValidServiceName(serviceName);
151153
_loggerFactory = loggerFactory;
152154
_logger = _loggerFactory.CreateLogger<Configuration>();
153155
}
@@ -188,30 +190,30 @@ public Tracer.Builder GetTracerBuilder()
188190
{
189191
SamplerConfig = new SamplerConfiguration(_loggerFactory);
190192
}
191-
if (_codecConfig == null)
193+
if (CodecConfig == null)
192194
{
193-
_codecConfig = new CodecConfiguration(_loggerFactory);
195+
CodecConfig = new CodecConfiguration(_loggerFactory);
194196
}
195197
if (_metricsFactory == null)
196198
{
197199
_metricsFactory = NoopMetricsFactory.Instance;
198200
}
199201
IMetrics metrics = new MetricsImpl(_metricsFactory);
200202
IReporter reporter = ReporterConfig.GetReporter(metrics);
201-
ISampler sampler = SamplerConfig.CreateSampler(_serviceName, metrics);
202-
Tracer.Builder builder = new Tracer.Builder(_serviceName)
203+
ISampler sampler = SamplerConfig.GetSampler(ServiceName, metrics);
204+
Tracer.Builder builder = new Tracer.Builder(ServiceName)
203205
.WithLoggerFactory(_loggerFactory)
204206
.WithSampler(sampler)
205207
.WithReporter(reporter)
206208
.WithMetrics(metrics)
207209
.WithTags(_tracerTags);
208210

209-
if (_useTraceId128Bit)
211+
if (UseTraceId128Bit)
210212
{
211213
builder = builder.WithTraceId128Bit();
212214
}
213215

214-
_codecConfig.Apply(builder);
216+
CodecConfig.Apply(builder);
215217

216218
return builder;
217219
}
@@ -265,13 +267,13 @@ public Configuration WithSampler(SamplerConfiguration samplerConfig)
265267

266268
public Configuration WithCodec(CodecConfiguration codecConfig)
267269
{
268-
_codecConfig = codecConfig;
270+
CodecConfig = codecConfig;
269271
return this;
270272
}
271273

272274
public Configuration WithTraceId128Bit(bool useTraceId128Bit)
273275
{
274-
_useTraceId128Bit = useTraceId128Bit;
276+
UseTraceId128Bit = useTraceId128Bit;
275277
return this;
276278
}
277279

@@ -284,6 +286,7 @@ public Configuration WithTracerTags(Dictionary<string, string> tracerTags)
284286
return this;
285287
}
286288

289+
[Obsolete("Use the property 'TracerTags' instead.")]
287290
public IReadOnlyDictionary<string, string> GetTracerTags()
288291
{
289292
return _tracerTags;
@@ -342,8 +345,7 @@ public static SamplerConfiguration FromEnv(ILoggerFactory loggerFactory)
342345
return FromIConfiguration(loggerFactory, configuration);
343346
}
344347

345-
// for tests
346-
internal ISampler CreateSampler(string serviceName, IMetrics metrics)
348+
public virtual ISampler GetSampler(string serviceName, IMetrics metrics)
347349
{
348350
string samplerType = StringOrDefault(Type, RemoteControlledSampler.Type);
349351
double samplerParam = Param.GetValueOrDefault(ProbabilisticSampler.DefaultSamplingProbability);
@@ -555,7 +557,7 @@ public ReporterConfiguration WithSender(SenderConfiguration senderConfiguration)
555557
return this;
556558
}
557559

558-
public IReporter GetReporter(IMetrics metrics)
560+
public virtual IReporter GetReporter(IMetrics metrics)
559561
{
560562
IReporter reporter = new RemoteReporter.Builder()
561563
.WithLoggerFactory(_loggerFactory)
@@ -664,7 +666,7 @@ public SenderConfiguration WithAuthPassword(string password)
664666
/// configuration's state.
665667
/// </summary>
666668
/// <returns>The sender passed via the constructor or a properly configured sender.</returns>
667-
public ISender GetSender()
669+
public virtual ISender GetSender()
668670
{
669671
// if we have a sender, that's the one we return
670672
if (Sender != null)

src/Jaeger/Tracer.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,28 @@ private static string LoadVersion()
199199

200200
public sealed class Builder
201201
{
202-
private ILoggerFactory _loggerFactory;
203-
private ISampler _sampler;
204-
private IReporter _reporter;
205202
private PropagationRegistry _registry;
206-
private IMetrics _metrics = new MetricsImpl(NoopMetricsFactory.Instance);
207-
private readonly string _serviceName;
208-
private IClock _clock = new SystemClock();
209203
private readonly Dictionary<string, object> _tags = new Dictionary<string, object>();
210-
private bool _zipkinSharedRpcSpan;
211-
private IScopeManager _scopeManager = new AsyncLocalScopeManager();
212-
private IBaggageRestrictionManager _baggageRestrictionManager = new DefaultBaggageRestrictionManager();
213-
private bool _expandExceptionLogs;
214-
private bool _useTraceId128Bit;
215204

216205
// We need the loggerFactory for the PropagationRegistry so we have to defer these invocations.
217206
private readonly List<Action<PropagationRegistry>> _registryActions = new List<Action<PropagationRegistry>>();
218207

208+
public string ServiceName { get; }
209+
public ILoggerFactory LoggerFactory { get; private set; }
210+
public IBaggageRestrictionManager BaggageRestrictionManager { get; private set; } = new DefaultBaggageRestrictionManager();
211+
public IMetrics Metrics { get; private set; } = new MetricsImpl(NoopMetricsFactory.Instance);
212+
public bool ZipkinSharedRpcSpan { get; private set; }
213+
public ISampler Sampler { get; private set; }
214+
public IReporter Reporter { get; private set; }
215+
public IClock Clock { get; private set; } = new SystemClock();
216+
public IScopeManager ScopeManager { get; private set; } = new AsyncLocalScopeManager();
217+
public bool ExpandExceptionLogs { get; private set; }
218+
public bool UseTraceId128Bit { get; private set; }
219+
public IReadOnlyDictionary<string, object> Tags => _tags;
220+
219221
public Builder(string serviceName)
220222
{
221-
_serviceName = CheckValidServiceName(serviceName);
223+
ServiceName = CheckValidServiceName(serviceName);
222224

223225
_registryActions.Add(registry =>
224226
{
@@ -229,19 +231,19 @@ public Builder(string serviceName)
229231

230232
public Builder WithLoggerFactory(ILoggerFactory loggerFactory)
231233
{
232-
_loggerFactory = loggerFactory;
234+
LoggerFactory = loggerFactory;
233235
return this;
234236
}
235237

236238
public Builder WithReporter(IReporter reporter)
237239
{
238-
_reporter = reporter;
240+
Reporter = reporter;
239241
return this;
240242
}
241243

242244
public Builder WithSampler(ISampler sampler)
243245
{
244-
_sampler = sampler;
246+
Sampler = sampler;
245247
return this;
246248
}
247249
public Builder RegisterInjector<TCarrier>(IFormat<TCarrier> format, Injector<TCarrier> injector)
@@ -264,43 +266,43 @@ public Builder RegisterCodec<TCarrier>(IFormat<TCarrier> format, Codec<TCarrier>
264266

265267
public Builder WithMetrics(IMetrics metrics)
266268
{
267-
_metrics = metrics;
269+
Metrics = metrics;
268270
return this;
269271
}
270272

271273
public Builder WithMetricsFactory(IMetricsFactory factory)
272274
{
273-
_metrics = new MetricsImpl(factory);
275+
Metrics = new MetricsImpl(factory);
274276
return this;
275277
}
276278

277279
public Builder WithScopeManager(IScopeManager scopeManager)
278280
{
279-
_scopeManager = scopeManager;
281+
ScopeManager = scopeManager;
280282
return this;
281283
}
282284

283285
public Builder WithClock(IClock clock)
284286
{
285-
_clock = clock;
287+
Clock = clock;
286288
return this;
287289
}
288290

289291
public Builder WithZipkinSharedRpcSpan()
290292
{
291-
_zipkinSharedRpcSpan = true;
293+
ZipkinSharedRpcSpan = true;
292294
return this;
293295
}
294296

295297
public Builder WithExpandExceptionLogs()
296298
{
297-
_expandExceptionLogs = true;
299+
ExpandExceptionLogs = true;
298300
return this;
299301
}
300302

301303
public Builder WithTraceId128Bit()
302304
{
303-
_useTraceId128Bit = true;
305+
UseTraceId128Bit = true;
304306
return this;
305307
}
306308

@@ -342,45 +344,45 @@ public Builder WithTags(IEnumerable<KeyValuePair<string, string>> tags)
342344

343345
public Builder WithBaggageRestrictionManager(IBaggageRestrictionManager baggageRestrictionManager)
344346
{
345-
_baggageRestrictionManager = baggageRestrictionManager;
347+
BaggageRestrictionManager = baggageRestrictionManager;
346348
return this;
347349
}
348350

349351
public Tracer Build()
350352
{
351-
if (_loggerFactory == null)
353+
if (LoggerFactory == null)
352354
{
353-
_loggerFactory = NullLoggerFactory.Instance;
355+
LoggerFactory = NullLoggerFactory.Instance;
354356
}
355357

356-
_registry = new PropagationRegistry(_loggerFactory);
358+
_registry = new PropagationRegistry(LoggerFactory);
357359
foreach (var configureRegistry in _registryActions)
358360
{
359361
configureRegistry(_registry);
360362
}
361363

362-
if (_metrics == null)
364+
if (Metrics == null)
363365
{
364-
_metrics = new MetricsImpl(NoopMetricsFactory.Instance);
366+
Metrics = new MetricsImpl(NoopMetricsFactory.Instance);
365367
}
366368

367-
if (_reporter == null)
369+
if (Reporter == null)
368370
{
369-
_reporter = new RemoteReporter.Builder()
370-
.WithLoggerFactory(_loggerFactory)
371-
.WithMetrics(_metrics)
371+
Reporter = new RemoteReporter.Builder()
372+
.WithLoggerFactory(LoggerFactory)
373+
.WithMetrics(Metrics)
372374
.Build();
373375
}
374-
if (_sampler == null)
376+
if (Sampler == null)
375377
{
376-
_sampler = new RemoteControlledSampler.Builder(_serviceName)
377-
.WithLoggerFactory(_loggerFactory)
378-
.WithMetrics(_metrics)
378+
Sampler = new RemoteControlledSampler.Builder(ServiceName)
379+
.WithLoggerFactory(LoggerFactory)
380+
.WithMetrics(Metrics)
379381
.Build();
380382
}
381383

382-
return new Tracer(_serviceName, _reporter, _sampler, _registry, _clock, _metrics, _loggerFactory,
383-
_tags, _zipkinSharedRpcSpan, _scopeManager, _baggageRestrictionManager, _expandExceptionLogs, _useTraceId128Bit);
384+
return new Tracer(ServiceName, Reporter, Sampler, _registry, Clock, Metrics, LoggerFactory,
385+
_tags, ZipkinSharedRpcSpan, ScopeManager, BaggageRestrictionManager, ExpandExceptionLogs, UseTraceId128Bit);
384386
}
385387

386388
public static String CheckValidServiceName(String serviceName)

test/Jaeger.Tests/ConfigurationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public void TestConstSampler()
492492
{
493493
SamplerConfiguration samplerConfiguration = new SamplerConfiguration(_loggerFactory)
494494
.WithType(ConstSampler.Type);
495-
ISampler sampler = samplerConfiguration.CreateSampler("name",
495+
ISampler sampler = samplerConfiguration.GetSampler("name",
496496
new MetricsImpl(NoopMetricsFactory.Instance));
497497
Assert.True(sampler is ConstSampler);
498498
}
@@ -502,7 +502,7 @@ public void TestProbabilisticSampler()
502502
{
503503
SamplerConfiguration samplerConfiguration = new SamplerConfiguration(_loggerFactory)
504504
.WithType(ProbabilisticSampler.Type);
505-
ISampler sampler = samplerConfiguration.CreateSampler("name",
505+
ISampler sampler = samplerConfiguration.GetSampler("name",
506506
new MetricsImpl(NoopMetricsFactory.Instance));
507507
Assert.True(sampler is ProbabilisticSampler);
508508
}
@@ -512,7 +512,7 @@ public void TestRateLimitingSampler()
512512
{
513513
SamplerConfiguration samplerConfiguration = new SamplerConfiguration(_loggerFactory)
514514
.WithType(RateLimitingSampler.Type);
515-
ISampler sampler = samplerConfiguration.CreateSampler("name",
515+
ISampler sampler = samplerConfiguration.GetSampler("name",
516516
new MetricsImpl(NoopMetricsFactory.Instance));
517517
Assert.True(sampler is RateLimitingSampler);
518518
}

0 commit comments

Comments
 (0)