From 695c3780c13c8127b4986a86382971652a950f51 Mon Sep 17 00:00:00 2001 From: Mike Bosland Date: Tue, 19 Mar 2024 13:10:09 +0000 Subject: [PATCH 1/2] Allow a custom time provider in TracerProviderBase This mirrors the capability in WebTracerProvider. --- lib/src/sdk/trace/tracer_provider.dart | 9 +++++++-- test/unit/sdk/trace_provider_test.dart | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/src/sdk/trace/tracer_provider.dart b/lib/src/sdk/trace/tracer_provider.dart index 63720640..fe00b111 100644 --- a/lib/src/sdk/trace/tracer_provider.dart +++ b/lib/src/sdk/trace/tracer_provider.dart @@ -27,14 +27,19 @@ class TracerProviderBase implements api.TracerProvider { @protected final sdk.SpanLimits spanLimits; + @protected + 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 get spanProcessors => processors; @@ -50,7 +55,7 @@ class TracerProviderBase implements api.TracerProvider { processors, resource, sampler, - sdk.DateTimeTimeProvider(), + _timeProvider, idGenerator, sdk.InstrumentationScope(name, version, schemaUrl, attributes), spanLimits)); diff --git a/test/unit/sdk/trace_provider_test.dart b/test/unit/sdk/trace_provider_test.dart index 82b8a324..36d28370 100644 --- a/test/unit/sdk/trace_provider_test.dart +++ b/test/unit/sdk/trace_provider_test.dart @@ -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'; @@ -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(); @@ -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; +} From 45551db82406637a8b7c90afc3e730727edb6532 Mon Sep 17 00:00:00 2001 From: mikebosland <146096721+mikebosland@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:20:13 +0100 Subject: [PATCH 2/2] Update lib/src/sdk/trace/tracer_provider.dart Remove redundant @protected annotation on private member. Co-authored-by: Blake Roberts --- lib/src/sdk/trace/tracer_provider.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/sdk/trace/tracer_provider.dart b/lib/src/sdk/trace/tracer_provider.dart index fe00b111..ccced25f 100644 --- a/lib/src/sdk/trace/tracer_provider.dart +++ b/lib/src/sdk/trace/tracer_provider.dart @@ -27,7 +27,6 @@ class TracerProviderBase implements api.TracerProvider { @protected final sdk.SpanLimits spanLimits; - @protected final sdk.TimeProvider _timeProvider; TracerProviderBase(