Skip to content

Commit

Permalink
Merge pull request #158 from mikebosland/custom-time-provider
Browse files Browse the repository at this point in the history
Allow a custom time provider in TracerProviderBase
  • Loading branch information
rmconsole5-wk authored Apr 17, 2024
2 parents f00bb98 + 45551db commit abf182f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/src/sdk/trace/tracer_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ class TracerProviderBase implements api.TracerProvider {
@protected
final sdk.SpanLimits spanLimits;

final sdk.TimeProvider _timeProvider;

TracerProviderBase(
{this.processors =
const [], // Default to a TracerProvider which does not emit traces.
resource,
sdk.TimeProvider? timeProvider,
this.sampler = const sdk.ParentBasedSampler(sdk.AlwaysOnSampler()),
this.idGenerator = const sdk.IdGenerator(),
this.spanLimits = const sdk.SpanLimits()})
: resource = resource ?? sdk.Resource([]);
: resource = resource ?? sdk.Resource([]),
_timeProvider = timeProvider ?? sdk.DateTimeTimeProvider();

List<sdk.SpanProcessor> get spanProcessors => processors;

Expand All @@ -50,7 +54,7 @@ class TracerProviderBase implements api.TracerProvider {
processors,
resource,
sampler,
sdk.DateTimeTimeProvider(),
_timeProvider,
idGenerator,
sdk.InstrumentationScope(name, version, schemaUrl, attributes),
spanLimits));
Expand Down
18 changes: 18 additions & 0 deletions test/unit/sdk/trace_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

@TestOn('vm')
import 'package:fixnum/src/int64.dart';
import 'package:mockito/mockito.dart';
import 'package:opentelemetry/src/sdk/time_providers/time_provider.dart';
import 'package:opentelemetry/src/sdk/trace/read_only_span.dart';
import 'package:opentelemetry/src/sdk/trace/span_processors/span_processor.dart';
import 'package:opentelemetry/src/sdk/trace/tracer_provider.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -36,6 +39,13 @@ void main() {
expect(provider.spanProcessors, [mockProcessor1, mockProcessor2]);
});

test('traceProvider custom timeProvider', () {
final mockTimeProvider = FakeTimeProvider(now: Int64(123));
final provider = TracerProviderBase(timeProvider: mockTimeProvider);
final span = provider.getTracer('foo').startSpan('bar') as ReadOnlySpan;
expect(span.startTime, Int64(123));
});

test('tracerProvider force flushes all processors', () {
final mockProcessor1 = MockSpanProcessor();
final mockProcessor2 = MockSpanProcessor();
Expand All @@ -55,3 +65,11 @@ void main() {
verify(mockProcessor2.shutdown()).called(1);
});
}

class FakeTimeProvider extends Mock implements TimeProvider {
FakeTimeProvider({required Int64 now}) : _now = now;
final Int64 _now;

@override
Int64 get now => _now;
}

0 comments on commit abf182f

Please sign in to comment.