Skip to content

Commit 9d7a386

Browse files
add warning log on detach mismatch
1 parent d1536f1 commit 9d7a386

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/src/api/context/context.dart

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
import 'dart:async';
55

6+
import 'package:logging/logging.dart';
67
import 'package:meta/meta.dart';
78

89
import '../../../api.dart';
910
import '../trace/nonrecording_span.dart' show NonRecordingSpan;
1011

12+
final Logger _log = Logger('opentelemetry');
13+
1114
typedef ContextKey = Object;
1215

1316
final ContextKey contextKey = ContextKey();
@@ -83,13 +86,20 @@ bool detach(ContextToken token) {
8386
Zone.current[contextStackKey] ?? rootStack;
8487

8588
final index = stack.indexWhere((c) => c.token == token);
86-
final success = index == stack.length - 1;
89+
90+
// the expected context to detach is the last one in the stack
91+
final match = index == stack.length - 1;
92+
if (!match) {
93+
_log.warning('unexpected (mismatched) token given to detach');
94+
}
95+
96+
// if the context associated with the given token exists, remove it regardless
97+
// of mismatch
8798
if (index != -1) {
8899
stack.removeAt(index);
89100
}
90101

91-
// TODO: log attach/detach mismatch warning
92-
return success;
102+
return match;
93103
}
94104

95105
class Context {

0 commit comments

Comments
 (0)