11// Copyright 2021-2022 Workiva.
22// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information
3-
4- /// The OpenTelemetry SDKs require a mechanism for propagating context and the
5- /// OpenTelemetry specification outlines the requirements for this context
6- /// implementation: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/context/context.md
7- ///
8- /// The spec notes that "languages are expected to use the single, widely used
9- /// Context implementation if one exists for them." Fortunately, the Dart SDK
10- /// provides just that with [Zone] - a representation of "an environment that
11- /// remains stable across asynchronous calls." [Zone] also meets the core
12- /// requirements of immutability and being able to read and write values:
13- ///
14- /// - Immutable: a Zone's values are set when the Zone is created and cannot be
15- /// changed afterwards.
16- /// - Reading and writing values: a Zone implements the `[]` operator, allowing
17- /// values to be read directly from it like a [Map] , and writing values is
18- /// possible only by forking another Zone and providing values to add/override
19- /// (the rest of the values will be inherited from the forked Zone).
20- ///
21- /// This library provides a simple abstraction over [Zone] for the purpose of
22- /// implementing the rest of the Context specification. OpenTelemetry SDKs and
23- /// instrumentation libraries should use this [Context] API instead of a [Zone]
24- /// directly. Other users should usually not interact with Context at all and
25- /// should instead manipulate it through cross-cutting concerns APIs provided by
26- /// OpenTelemetry SDKs.
27- import 'dart:async' ;
28-
293import '../../../api.dart' as api;
30- import '../trace/nonrecording_span.dart' ;
31-
32- /// [ContextKey] used to store spans in a [Context] .
33- final ContextKey spanKey = Context .createKey ('OpenTelemetry Context Key SPAN' );
34-
35- class Context {
36- final Zone _zone;
4+ import 'context_manager.dart' ;
375
38- Context ._( this ._zone);
6+ class ContextKey {}
397
8+ abstract class Context {
409 /// The active context.
41- static Context get current => Context ._(Zone .current);
10+ @Deprecated ('This method will be removed in the future.' )
11+ static Context get current => globalContextManager.active;
4212
4313 /// The root context which all other contexts are derived from.
4414 ///
@@ -47,50 +17,47 @@ class Context {
4717 /// Only use this context if you are certain you need to disregard the
4818 /// current [Context] . For example, when instrumenting an asynchronous
4919 /// event handler which may fire while an unrelated [Context] is "current".
50- static Context get root => Context ._(Zone .root);
20+ @Deprecated ('We are planning to remove this in the future.' )
21+ static Context get root => globalContextManager.root;
5122
5223 /// Returns a key to be used to read and/or write values to a context.
5324 ///
5425 /// [name] is for debug purposes only and does not uniquely identify the key.
5526 /// Multiple calls to this function with the same [name] will not return
5627 /// identical keys.
57- static ContextKey createKey (String name) => ContextKey (name);
28+ @Deprecated (
29+ 'This method will be removed in the future. Please use ContextKey() directly.' )
30+ static ContextKey createKey (String name) => ContextKey ();
5831
5932 /// Returns the value from this context identified by [key] , or null if no
6033 /// such value is set.
61- T ? getValue <T >(ContextKey key) => _zone[key] ;
34+ T ? getValue <T >(ContextKey key);
6235
6336 /// Returns a new context created from this one with the given key/value pair
6437 /// set.
6538 ///
6639 /// If [key] was already set in this context, it will be overridden. The rest
6740 /// of the context values will be inherited.
68- Context setValue (ContextKey key, Object value) =>
69- Context ._(_zone.fork (zoneValues: {key: value}));
41+ Context setValue (ContextKey key, Object value);
7042
7143 /// Returns a new [Context] created from this one with the given [api.Span]
7244 /// set.
73- Context withSpan (api.Span span) => setValue (spanKey, span);
45+ @Deprecated (
46+ 'This method will be removed in the future, new method will be added.' )
47+ Context withSpan (api.Span span);
7448
7549 /// Execute a function [fn] within this [Context] and return its result.
76- R execute <R >(R Function () fn) => _zone.run (fn);
50+ @Deprecated ('This method will be removed in the future.' )
51+ R execute <R >(R Function () fn);
7752
7853 /// Get the [api.Span] attached to this [Context] , or an invalid, [api.Span] if no such
7954 /// [api.Span] exists.
80- api.Span get span =>
81- getValue (spanKey) ?? NonRecordingSpan (api.SpanContext .invalid ());
55+ @Deprecated (
56+ 'This method will be removed in the future, new method will be added.' )
57+ api.Span get span;
8258
8359 /// Get the [api.SpanContext] from this [Context] , or an invalid [api.SpanContext] if no such
8460 /// [api.SpanContext] exists.
85- api.SpanContext get spanContext =>
86- (getValue (spanKey) ?? NonRecordingSpan (api.SpanContext .invalid ()))
87- .spanContext;
88- }
89-
90- class ContextKey {
91- /// Name of the context key.
92- final String name;
93-
94- /// Construct a [ContextKey] with a given [name] .
95- ContextKey (this .name);
61+ @Deprecated ('This method will be removed in the future.' )
62+ api.SpanContext get spanContext;
9663}
0 commit comments