-
Notifications
You must be signed in to change notification settings - Fork 33
/
w3c_context_propagation.dart
53 lines (44 loc) · 1.62 KB
/
w3c_context_propagation.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2021-2022 Workiva.
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information
import 'package:opentelemetry/api.dart';
import 'package:opentelemetry/sdk.dart'
show ConsoleExporter, SimpleSpanProcessor, TracerProviderBase;
import 'package:opentelemetry/src/experimental_api.dart' show NoopContextManager;
class MapSetter implements TextMapSetter<Map> {
@override
void set(Map carrier, String key, String value) {
carrier[key] = value;
}
}
class MapGetter implements TextMapGetter<Map> {
@override
String? get(Map? carrier, String key) {
return (carrier == null) ? null : carrier[key];
}
@override
Iterable<String> keys(Map carrier) {
return carrier.keys.map((key) => key.toString());
}
}
void main(List<String> args) async {
final tp =
TracerProviderBase(processors: [SimpleSpanProcessor(ConsoleExporter())]);
registerGlobalTracerProvider(tp);
final cm = NoopContextManager();
registerGlobalContextManager(cm);
final tmp = W3CTraceContextPropagator();
registerGlobalTextMapPropagator(tmp);
final span = tp.getTracer('instrumentation-name').startSpan('test-span-0');
final carrier = <String, String>{};
tmp.inject(contextWithSpan(cm.active, span), carrier, MapSetter());
await test(carrier);
span.end();
}
Future test(Map<String, String> carrier) async {
globalTracerProvider
.getTracer('instrumentation-name')
.startSpan('test-span-1',
context: globalTextMapPropagator.extract(
globalContextManager.active, carrier, MapGetter()))
.end();
}