Skip to content

Commit dbd4ab2

Browse files
support zone context propagation in traceContext and traceContextSync
1 parent 449f214 commit dbd4ab2

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

example/zone_context_manager.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import 'dart:async';
33
import 'package:opentelemetry/api.dart';
44
import 'package:opentelemetry/sdk.dart'
55
show ConsoleExporter, SimpleSpanProcessor, TracerProviderBase;
6-
import 'package:opentelemetry/src/api/context/zone_context.dart';
76
import 'package:opentelemetry/src/experimental_api.dart'
8-
show globalContextManager, registerGlobalContextManager, ZoneContextManager;
7+
show
8+
globalContextManager,
9+
registerGlobalContextManager,
10+
ZoneContext,
11+
ZoneContextManager;
912

1013
void main(List<String> args) async {
1114
final tp =

lib/src/api/context/context.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ abstract class Context {
7171

7272
/// Execute a function [fn] within this [Context] and return its result.
7373
@Deprecated(
74-
'This method will be removed in 0.19.0. Propagate [Context] as an argument to [fn].')
74+
'This method will be removed in 0.19.0. Propagate [Context] as an '
75+
'argument to [fn] and call [fn] directly or use '
76+
'[(context as ZoneContext).run((_) => fn(this))] instead.')
7577
R execute<R>(R Function() fn);
7678

7779
/// Get the [Span] attached to this [Context], or an invalid, [Span] if no such

lib/src/api/context/map_context.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class MapContext implements Context {
4343
@override
4444
Span get span => spanFromContext(this);
4545

46+
/// Call [fn] with this [MapContext] and return its result.
47+
@experimental
4648
R run<R>(R Function(Context context) fn) => fn(this);
4749

4850
/// Get the [SpanContext] from this [MapContext], or an invalid [SpanContext] if no such

lib/src/api/context/zone_context.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class ZoneContext implements Context {
5959
@override
6060
R execute<R>(R Function() fn) => _zone.run(() => fn());
6161

62+
/// Call [fn] in this [ZoneContext]'s [Zone] and return its result.
63+
@experimental
6264
R run<R>(R Function(Context context) fn) => _zone.run(() => fn(this));
6365

6466
/// Get the [Span] attached to this [ZoneContext], or an invalid, [Span] if no such

lib/src/api/open_telemetry.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'propagation/noop_text_map_propagator.dart';
99
import 'trace/noop_tracer_provider.dart';
1010

1111
import '../../api.dart' as api;
12-
import '../experimental_api.dart' show globalContextManager;
12+
import '../experimental_api.dart' show globalContextManager, ZoneContext;
1313

1414
final api.TracerProvider _noopTracerProvider = NoopTracerProvider();
1515
final api.TextMapPropagator _noopTextMapPropagator = NoopTextMapPropagator();
@@ -79,8 +79,13 @@ Future<T> traceContext<T>(String name, Future<T> Function(api.Context) fn,
7979
}
8080

8181
final span = tracer.startSpan(name, context: context);
82+
context = api.contextWithSpan(context, span);
8283
try {
83-
return await fn(api.contextWithSpan(context, span));
84+
// TODO: remove this check once `run` exists on context interface
85+
if (context is ZoneContext) {
86+
return await context.run((context) => fn(context));
87+
}
88+
return await fn(context);
8489
} catch (e, s) {
8590
span
8691
..setStatus(api.StatusCode.error, e.toString())
@@ -93,7 +98,7 @@ Future<T> traceContext<T>(String name, Future<T> Function(api.Context) fn,
9398

9499
/// Use [traceSync] instead of [trace] when [fn] is not an async function.
95100
@Deprecated(
96-
'This method will be removed in 0.19.0. Use [traceSyncContext] instead.')
101+
'This method will be removed in 0.19.0. Use [traceContextSync] instead.')
97102
R traceSync<R>(String name, R Function() fn,
98103
{api.Context? context, api.Tracer? tracer}) {
99104
context ??= globalContextManager.active;
@@ -120,9 +125,9 @@ R traceSync<R>(String name, R Function() fn,
120125
}
121126
}
122127

123-
/// Use [traceSyncContext] instead of [traceContext] when [fn] is not an async function.
128+
/// Use [traceContextSync] instead of [traceContext] when [fn] is not an async function.
124129
@experimental
125-
R traceSyncContext<R>(String name, R Function(api.Context) fn,
130+
R traceContextSync<R>(String name, R Function(api.Context) fn,
126131
{api.Context? context,
127132
api.Tracer? tracer,
128133
bool newRoot = false,
@@ -136,12 +141,19 @@ R traceSyncContext<R>(String name, R Function(api.Context) fn,
136141
}
137142

138143
final span = tracer.startSpan(name, context: context);
144+
context = api.contextWithSpan(context, span);
139145
try {
140-
final r = fn(api.contextWithSpan(context, span));
146+
var r;
147+
// TODO: remove this check once `run` exists on context interface
148+
if (context is ZoneContext) {
149+
r = context.run((context) => fn(context));
150+
} else {
151+
r = fn(context);
152+
}
141153

142154
if (r is Future) {
143155
throw ArgumentError.value(fn, 'fn',
144-
'Use traceSyncContext to trace functions that do not return a [Future].');
156+
'Use traceContextSync to trace functions that do not return a [Future].');
145157
}
146158

147159
return r;

lib/src/experimental_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:meta/meta.dart';
99
export 'api/context/context_manager.dart'
1010
show ContextManager, registerGlobalContextManager, globalContextManager;
1111
export 'api/context/noop_context_manager.dart' show NoopContextManager;
12+
export 'api/context/zone_context.dart' show ZoneContext;
1213
export 'api/context/zone_context_manager.dart' show ZoneContextManager;
1314
export 'api/metrics/counter.dart' show Counter;
1415
export 'api/metrics/meter_provider.dart' show MeterProvider;

0 commit comments

Comments
 (0)