Skip to content

Commit b6b2089

Browse files
Merge pull request #214 from Workiva/fix-new-root
Fix trace and traceSync not creating root spans when newRoot is true
2 parents 4c86a3b + 7a9c276 commit b6b2089

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

lib/src/api/open_telemetry.dart

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66
import 'package:meta/meta.dart';
77

88
import '../../api.dart' as api;
9+
import '../../src/sdk/trace/tracer.dart' as sdk show Tracer;
910
import '../experimental_api.dart';
1011
import 'propagation/noop_text_map_propagator.dart';
1112
import 'trace/noop_tracer_provider.dart';
@@ -102,12 +103,23 @@ Future<T> trace<T>(String name, Future<T> Function() fn,
102103
context ??= api.Context.current;
103104
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');
104105

105-
final span = tracer.startSpan(name,
106-
// TODO: use start span option `newRoot` instead
107-
context: newRoot ? api.Context.root : context,
108-
attributes: spanAttributes,
109-
kind: spanKind,
110-
links: spanLinks);
106+
// TODO: use start span option `newRoot` instead
107+
var span;
108+
if (tracer is sdk.Tracer) {
109+
span = tracer.startSpan(name,
110+
context: context,
111+
attributes: spanAttributes,
112+
kind: spanKind,
113+
links: spanLinks,
114+
newRoot: newRoot);
115+
} else {
116+
span = tracer.startSpan(name,
117+
context: newRoot ? api.Context.root : context,
118+
attributes: spanAttributes,
119+
kind: spanKind,
120+
links: spanLinks);
121+
}
122+
111123
try {
112124
return await Zone.current.fork().run(() {
113125
final token = api.Context.attach(api.contextWithSpan(context!, span));
@@ -139,12 +151,23 @@ T traceSync<T>(String name, T Function() fn,
139151
context ??= api.Context.current;
140152
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');
141153

142-
final span = tracer.startSpan(name,
143-
// TODO: use start span option `newRoot` instead
144-
context: newRoot ? api.Context.root : context,
145-
attributes: spanAttributes,
146-
kind: spanKind,
147-
links: spanLinks);
154+
// TODO: use start span option `newRoot` instead
155+
var span;
156+
if (tracer is sdk.Tracer) {
157+
span = tracer.startSpan(name,
158+
context: context,
159+
attributes: spanAttributes,
160+
kind: spanKind,
161+
links: spanLinks,
162+
newRoot: newRoot);
163+
} else {
164+
span = tracer.startSpan(name,
165+
context: newRoot ? api.Context.root : context,
166+
attributes: spanAttributes,
167+
kind: spanKind,
168+
links: spanLinks);
169+
}
170+
148171
try {
149172
return Zone.current.fork().run(() {
150173
final token = api.Context.attach(api.contextWithSpan(context!, span));

test/api/open_telemetry_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ void main() {
5858
}, tracer: tracer, context: contextWithSpan(Context.current, parent));
5959
});
6060

61+
test('trace creates a root span', () async {
62+
final parent = tracer.startSpan('parent')..end();
63+
final context = contextWithSpan(Context.current, parent);
64+
final token = Context.attach(context);
65+
66+
await trace('child', () async {
67+
final child = spanFromContext(Context.current);
68+
expect(child.parentSpanId.isValid, isFalse);
69+
}, tracer: tracer, context: context, newRoot: true);
70+
71+
Context.detach(token);
72+
});
73+
74+
test('traceSync creates a root span', () {
75+
final parent = tracer.startSpan('parent')..end();
76+
final context = contextWithSpan(Context.current, parent);
77+
final token = Context.attach(context);
78+
79+
traceSync('child', () {
80+
final child = spanFromContext(Context.current);
81+
expect(child.parentSpanId.isValid, isFalse);
82+
}, tracer: tracer, context: context, newRoot: true);
83+
84+
Context.detach(token);
85+
});
86+
6187
test('trace catches, records, and rethrows exception', () async {
6288
late sdk.Span span;
6389
var caught = false;

0 commit comments

Comments
 (0)