-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from michaelyeager-wf/myeager-wf/O11Y-1508
O11Y-1508: Change Span to use high resolution time instead of DateTime.now()
- Loading branch information
Showing
40 changed files
with
491 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
lib/src/sdk/platforms/web/time_providers/web_time_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'dart:html'; | ||
import 'dart:js'; | ||
|
||
import 'package:fixnum/fixnum.dart'; | ||
|
||
import '../../../../../sdk.dart' as sdk; | ||
|
||
/// BrowserTimeProvider retrieves high-resolution timestamps utilizing the | ||
/// `window.performance` API. | ||
/// | ||
/// See [DOMHighResTimeStamp](https://www.w3.org/TR/hr-time/#sec-DOMHighResTimeStamp) | ||
/// for more information. | ||
/// | ||
/// Note that this time may be inaccurate if the executing system is suspended | ||
/// for sleep. See https://github.com/open-telemetry/opentelemetry-js/issues/852 | ||
/// for more information. | ||
class WebTimeProvider implements sdk.TimeProvider { | ||
static final Int64 _timeOrigin = _fromDouble( | ||
JsObject.fromBrowserObject(window)['performance']['timeOrigin'] ?? | ||
// fallback for browsers that don't support timeOrigin, like Dartium | ||
window.performance.timing.navigationStart.toDouble()); | ||
|
||
/// Derive a time, in nanoseconds, from a floating-point time, in milliseconds. | ||
static Int64 _fromDouble(double time) => | ||
Int64((time * sdk.TimeProvider.nanosecondsPerMillisecond).round()); | ||
|
||
/// The current time, in nanoseconds since Unix Epoch. | ||
/// | ||
/// Note that this time may be inaccurate if the executing system is suspended | ||
/// for sleep. See https://github.com/open-telemetry/opentelemetry-js/issues/852 | ||
/// for more information. | ||
@override | ||
Int64 get now => | ||
// .now() returns an int in Dartium, requiring .toDouble() | ||
_timeOrigin + _fromDouble(window.performance.now().toDouble()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import '../../../../../api.dart' as api; | ||
import '../../../../../sdk.dart' as sdk; | ||
import '../../../trace/tracer.dart'; | ||
|
||
/// A [api.TracerProvider] which implements features specific to `dart:html`. | ||
/// | ||
/// Use of [WebTracerProvider] with this provider results in a [api.Tracer] | ||
/// which uses the `window.performance` API for high-precision timestamps | ||
/// on the [api.Span]s it creates. | ||
/// | ||
/// Note that these timestamps may be inaccurate if the executing system is | ||
/// suspended for sleep. | ||
/// See https://github.com/open-telemetry/opentelemetry-js/issues/852 | ||
/// for more information. | ||
class WebTracerProvider extends sdk.TracerProviderBase { | ||
final Map<String, api.Tracer> _tracers = {}; | ||
final List<api.SpanProcessor> _processors; | ||
final sdk.Resource _resource; | ||
final sdk.Sampler _sampler; | ||
final sdk.TimeProvider _timeProvider; | ||
final api.IdGenerator _idGenerator; | ||
final sdk.SpanLimits _spanLimits; | ||
|
||
WebTracerProvider( | ||
{List<api.SpanProcessor> processors, | ||
sdk.Resource resource, | ||
sdk.Sampler sampler, | ||
sdk.TimeProvider timeProvider, | ||
api.IdGenerator idGenerator, | ||
sdk.SpanLimits spanLimits}) | ||
: | ||
// Default to a no-op TracerProvider. | ||
_processors = processors ?? [], | ||
_resource = resource ?? sdk.Resource([]), | ||
_sampler = sampler ?? sdk.ParentBasedSampler(sdk.AlwaysOnSampler()), | ||
_timeProvider = timeProvider ?? sdk.DateTimeTimeProvider(), | ||
_idGenerator = idGenerator ?? sdk.IdGenerator(), | ||
_spanLimits = spanLimits ?? sdk.SpanLimits(), | ||
super( | ||
processors: processors, | ||
resource: resource, | ||
sampler: sampler, | ||
idGenerator: idGenerator, | ||
spanLimits: spanLimits); | ||
|
||
@override | ||
api.Tracer getTracer(String name, {String version = ''}) { | ||
return _tracers.putIfAbsent( | ||
'$name@$version', | ||
() => Tracer(_processors, _resource, _sampler, _timeProvider, | ||
_idGenerator, sdk.InstrumentationLibrary(name, version), | ||
spanLimits: _spanLimits)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:fixnum/fixnum.dart'; | ||
import 'time_provider.dart'; | ||
|
||
/// DateTimeTimeProvider retrieves timestamps using DateTime. | ||
class DateTimeTimeProvider implements TimeProvider { | ||
@override | ||
Int64 get now => | ||
Int64(DateTime.now().microsecondsSinceEpoch) * | ||
TimeProvider.nanosecondsPerMicrosecond; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:fixnum/fixnum.dart'; | ||
|
||
abstract class TimeProvider { | ||
// The smallest increment that DateTime can report is in microseconds, while | ||
// OpenTelemetry expects time in nanoseconds. | ||
static const int nanosecondsPerMicrosecond = 1000; | ||
|
||
// window.performance API reports time in fractional milliseconds, while | ||
// OpenTelemetry expects time in nanoseconds. | ||
static const int nanosecondsPerMillisecond = 1000000; | ||
|
||
/// The current time, in nanoseconds since Unix Epoch. | ||
Int64 get now; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export 'src/sdk/platforms/web/time_providers/web_time_provider.dart' | ||
show WebTimeProvider; | ||
export 'src/sdk/platforms/web/trace/web_tracer_provider.dart' | ||
show WebTracerProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@TestOn('vm') | ||
import 'package:opentelemetry/api.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
|
Oops, something went wrong.