ITracer GetDecoratedTracer(string flavor) { var originalTracer = new MockTracer(); var builder = new TracerDecoratorBuilder(originalTracer); switch (flavor) { case "StartedtAndFinished": builder .OnSpanStarted((span, name) => Interlocked.Increment(ref startedSpanCount)) .OnSpanFinished((span, name) => Interlocked.Decrement(ref startedSpanCount)) ; break; case "StartedWithCallback": builder .OnSpanStartedWithCallback((span, name) => { Interlocked.Increment(ref startedSpanCount); return((s, n) => Interlocked.Decrement(ref startedSpanCount)); }) ; break; case "Both": builder .OnSpanStarted((span, name) => Interlocked.Increment(ref startedSpanCount)) .OnSpanFinished((span, name) => Interlocked.Decrement(ref startedSpanCount)) .OnSpanStartedWithCallback((span, name) => { Interlocked.Increment(ref startedSpanCount); return((s, n) => Interlocked.Decrement(ref startedSpanCount)); }) ; break; case "Five!!": for (int i = 0; i < 5; i++) { builder .OnSpanStarted((span, name) => Interlocked.Increment(ref startedSpanCount)) .OnSpanFinished((span, name) => Interlocked.Decrement(ref startedSpanCount)) ; } break; default: throw new ArgumentOutOfRangeException(nameof(flavor), flavor, "Unknown"); } return(builder.Build()); }
public static ITracer Decorate( [NotNull] this ITracer source, [NotNull] TracerDecoration publicDecoration) { var decoration = publicDecoration as ITracerDecoration; var builder = new TracerDecoratorBuilder(source); // For performance, to avoid a level of indirection when not needed bool bypassBuilder = true; // Checking for null to avoid adding them if they're not implemented since the underlying library has optimizations if not subscribed if (decoration.OnSpanStarted != null) { builder = builder.OnSpanStarted(decoration.OnSpanStarted); bypassBuilder = false; } if (decoration.OnSpanFinished != null) { builder = builder.OnSpanFinished(decoration.OnSpanFinished); bypassBuilder = false; } if (decoration.OnSpanLog != null) { builder = builder.OnSpanLog(decoration.OnSpanLog); bypassBuilder = false; } if (decoration.OnSpanSetTag != null) { builder = builder.OnSpanSetTag(decoration.OnSpanSetTag); bypassBuilder = false; } if (decoration.OnSpanActivated != null) { builder = builder.OnSpanActivated(decoration.OnSpanActivated); bypassBuilder = false; } if (decoration.OnSpanStartedWithFinishCallback != null) { builder = builder.OnSpanStartedWithCallback(decoration.OnSpanStartedWithFinishCallback); bypassBuilder = false; } if (bypassBuilder) { return(source); } else { return(builder .Build()); } }
private static TracerDecoratorBuilder WithAppMetrics(this TracerDecoratorBuilder builder, IMetrics metrics, OpenTracingAppMetricsDecoratorOptions options, Action <OpenTracingAppMetricsDecoratorOptions> setupOptions) { options = options ?? throw new ArgumentNullException(nameof(options)); setupOptions?.Invoke(options); if (options.SpansCountersEnabled) { var spansCounter = new CounterOptions { Name = options.SpansCounterName }; builder.OnSpanStarted((span, operationName) => { metrics.Measure.Counter.Increment(spansCounter, operationName); if (options.DistinctOperationsCountersEnabled) { var distinctCounter = new CounterOptions { Name = options.DistinctOperationCountersName + operationName }; metrics.Measure.Counter.Increment(distinctCounter); } }); builder.OnSpanFinished((span, operationName) => { metrics.Measure.Counter.Decrement(spansCounter, operationName); if (options.DistinctOperationsCountersEnabled) { var distinctCounter = new CounterOptions { Name = options.DistinctOperationCountersName + operationName }; metrics.Measure.Counter.Decrement(distinctCounter); } }); } if (options.SpansMetersEnabled) { var spansMeter = new MeterOptions { Name = options.SpansMeterName }; builder.OnSpanStarted((span, operationName) => { metrics.Measure.Meter.Mark(spansMeter, operationName); if (options.DistinctOperationsMetersEnabled) { var distinctMeter = new MeterOptions { Name = options.DistinctOperationMetersName + operationName }; metrics.Measure.Meter.Mark(distinctMeter); } }); } if (options.SpansTimersEnabled) { var spansTimer = new TimerOptions { Name = options.SpansTimerName, DurationUnit = TimeUnit.Milliseconds, RateUnit = TimeUnit.Milliseconds }; builder.OnSpanStartedWithCallback((span, operationName) => { var timerContext = metrics.Measure.Timer.Time(spansTimer, operationName); TimerContext distinctTimerContext = default(TimerContext); if (options.DistinctOperationsTimersEnabled) { var distinctTimer = new TimerOptions { Name = options.DistinctOperationTimersName + operationName, DurationUnit = TimeUnit.Milliseconds, RateUnit = TimeUnit.Milliseconds }; distinctTimerContext = metrics.Measure.Timer.Time(distinctTimer); } return((sp, op) => { timerContext.Dispose(); distinctTimerContext.Dispose(); }); }); } return(builder); }