Skip to content

Commit 3e199a3

Browse files
Merge pull request #36 from Workiva/O11Y-1528
O11Y-1528 : update sdk to use api where possible
2 parents 0fe130a + 213f739 commit 3e199a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+701
-971
lines changed

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,71 +20,80 @@ The current options are:
2020
### Span Exporters
2121

2222
#### CollectorExporter
23+
2324
The CollectorExporter requires a Uri of the opentelemetry-collector instance's trace collector.
24-
```
25+
26+
```dart
2527
import 'package:opentelemetry/sdk.dart' as otel_sdk;
2628
2729
final exporter = otel_sdk.CollectorExporter(Uri.parse('https://my-collector.com/v1/traces'));
2830
```
2931

3032
#### ConsoleExporter
33+
3134
The ConsoleExporter has no requirements, and has no configuration options.
32-
```
35+
36+
```dart
3337
import 'package:opentelemetry/sdk.dart' as otel_sdk;
3438
3539
final exporter = otel_sdk.ConsoleExporter();
3640
```
3741

3842
### Span Processors
3943

40-
Next, you will need a at least one span processor. A span processor is responsible for collectoring the spans you create and feeding them to the exporter.
44+
Next, you will need at least one span processor. A span processor is responsible for collecting the spans you create and feeding them to the exporter.
4145
The current options are:
4246

4347
| SpanProcessor | Description |
4448
| -------- | ----------- |
4549
| [BatchSpanProcessor](#batchspanprocessor) | Batches spans to be exported on a configured time interval. |
4650
| [SimpleSpanProcessor](#simplespanprocessor) | Executes the provided exporter immediately upon closing the span. |
4751

48-
4952
#### BatchSpanProcessor
53+
5054
BatchSpanProcessors collect up to 2048 spans per interval, and executes the provided exporter on a timer.
5155
| Option | Description | Default |
5256
| ------ | ----------- | ------- |
5357
| maxExportBatchSize | At most, how many spans are processed per batch. | 512 |
5458
| scheduledDelay | How long to collect spans before processing them. | 5000 ms |
55-
```
59+
60+
```dart
5661
import 'package:opentelemetry/sdk.dart' as otel_sdk;
5762
5863
final exporter = otel_sdk.ConsoleExporter();
5964
final processor = otel_sdk.BatchSpanProcessor(exporter, scheduledDelay: 10000);
6065
```
6166

6267
#### SimpleSpanProcessor
68+
6369
A SimpleSpanProcessor has no configuration options, and executes the exporter when each span is closed.
64-
```
70+
71+
```dart
6572
import 'package:opentelemetry/sdk.dart' as otel_sdk;
6673
6774
final exporter = otel_sdk.ConsoleExporter();
6875
final processor = otel_sdk.SimpleSpanProcessor(exporter);
6976
```
7077

7178
### Tracer Provider
79+
7280
A trace provider registers your span processors, and is responsible for managing any tracers.
7381
| Option | Description | Default |
7482
| ------ | ----------- | ------- |
7583
| processors | A list of SpanProcessors to register. | A [SimpleSpanProcessor](#simplespanprocessor) configured with a [ConsoleExporter](#consoleexporter). |
76-
```
84+
85+
```dart
7786
import 'package:opentelemetry/sdk.dart' as otel_sdk;
7887
7988
final exporter = otel_sdk.CollectorExporter(Uri.parse('https://my-collector.com/v1/traces'));
80-
final processor = otel_sdk.BatchSpanProcesor(exporter);
89+
final processor = otel_sdk.BatchSpanProcessor(exporter);
8190
8291
// Send spans to a collector every 5 seconds
8392
final provider = otel_sdk.TracerProvider([processor]);
8493
8594
// Optionally, multiple processors can be registered
8695
final provider = otel_sdk.TracerProvider([
87-
otel_sdk.BatchSpanProcesor(otel_sdk.CollectorExporter(Uri.parse('https://my-collector.com/v1/traces'))),
96+
otel_sdk.BatchSpanProcessor(otel_sdk.CollectorExporter(Uri.parse('https://my-collector.com/v1/traces'))),
8897
otel_sdk.SimpleSpanProcessor(otel_sdk.ConsoleExporter())
8998
]);
9099
@@ -97,14 +106,18 @@ final tracer = otel_sdk.globalTracerProvider.getTracer('instrumentation-name');
97106
```
98107

99108
## Collecting Spans
109+
100110
To start a span, execute `startSpan` on the tracer with the name of what you are tracing. When complete, call `end` on the span.
101-
```
111+
112+
```dart
102113
final span = tracer.startSpan('doingWork');
103114
...
104115
span.end();
105116
```
117+
106118
To create children spans, you must set the parent span as "current", and execute work within `withContext`.
107-
```
119+
120+
```dart
108121
final checkoutSpan = tracer.startSpan('checkout');
109122
withContext(setSpan(Context.current, checkoutSpan), () {
110123
final ringUpSpan = tracer.startSpan('ringUp');
@@ -121,7 +134,8 @@ checkoutSpan.end();
121134
```
122135

123136
To avoid needing to pass spans around as arguments to other functions, you can get the current span with `Context.current.span`.
124-
```
137+
138+
```dart
125139
doWork() {
126140
Span parentSpan = Context.current.span;
127141

lib/api.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ export 'src/api/common/attribute.dart' show Attribute;
22
export 'src/api/common/attributes.dart' show Attributes;
33
export 'src/api/context/context.dart' show Context;
44
export 'src/api/exporters/span_exporter.dart' show SpanExporter;
5+
export 'src/api/instrumentation_library.dart' show InstrumentationLibrary;
56
export 'src/api/propagation/extractors/text_map_getter.dart' show TextMapGetter;
67
export 'src/api/propagation/injectors/text_map_setter.dart' show TextMapSetter;
78
export 'src/api/propagation/text_map_propagator.dart' show TextMapPropagator;
9+
export 'src/api/resource/resource.dart' show Resource;
810
export 'src/api/span_processors/span_processor.dart' show SpanProcessor;
911
export 'src/api/trace/id_generator.dart' show IdGenerator;
12+
export 'src/api/trace/nonrecording_span.dart' show NonRecordingSpan;
13+
export 'src/api/trace/sampler.dart' show Sampler;
14+
export 'src/api/trace/sampling_result.dart' show Decision, SamplingResult;
1015
export 'src/api/trace/span.dart' show Span;
1116
export 'src/api/trace/span_context.dart' show SpanContext;
1217
export 'src/api/trace/span_id.dart' show SpanId;
13-
export 'src/api/trace/span_status.dart' show StatusCode;
18+
export 'src/api/trace/span_status.dart' show SpanStatus, StatusCode;
1419
export 'src/api/trace/trace_flags.dart' show TraceFlags;
1520
export 'src/api/trace/trace_id.dart' show TraceId;
1621
export 'src/api/trace/trace_state.dart' show TraceState;

lib/sdk.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
export 'src/sdk/common/attribute.dart' show Attribute;
2-
export 'src/sdk/common/attributes.dart' show Attributes;
1+
export 'src/sdk/instrumentation_library.dart' show InstrumentationLibrary;
32
export 'src/sdk/open_telemetry.dart'
4-
show registerGlobalTracerProvider, globalTracerProvider;
3+
show
4+
registerGlobalTracerProvider,
5+
globalTracerProvider,
6+
registerGlobalTextMapPropagator,
7+
globalTextMapPropagator;
58
export 'src/sdk/resource/resource.dart' show Resource;
69
export 'src/sdk/trace/exporters/collector_exporter.dart' show CollectorExporter;
710
export 'src/sdk/trace/exporters/console_exporter.dart' show ConsoleExporter;
11+
export 'src/sdk/trace/id_generator.dart' show IdGenerator;
812
export 'src/sdk/trace/propagation/w3c_trace_context_propagator.dart'
913
show W3CTraceContextPropagator;
1014
export 'src/sdk/trace/samplers/always_off_sampler.dart' show AlwaysOffSampler;
1115
export 'src/sdk/trace/samplers/always_on_sampler.dart' show AlwaysOnSampler;
16+
export 'src/sdk/trace/samplers/parent_based_sampler.dart'
17+
show ParentBasedSampler;
18+
export 'src/sdk/trace/samplers/sampling_result.dart' show SamplingResult;
19+
export 'src/sdk/trace/span.dart' show Span;
20+
export 'src/sdk/trace/span_context.dart' show SpanContext;
1221
export 'src/sdk/trace/span_processors/batch_processor.dart'
1322
show BatchSpanProcessor;
1423
export 'src/sdk/trace/span_processors/simple_processor.dart'
1524
show SimpleSpanProcessor;
25+
export 'src/sdk/trace/tracer.dart' show Tracer;
1626
export 'src/sdk/trace/tracer_provider.dart' show TracerProvider;
27+
export 'src/sdk/trace/trace_state.dart' show TraceState;

lib/src/api/common/attribute.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// A representation of a single piece of metadata attached to trace span.
2-
abstract class Attribute {
2+
class Attribute {
33
final String key;
44
final Object value;
55

lib/src/api/common/attributes.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import 'attribute.dart';
22

33
/// A representation of a collection of metadata attached to a trace span.
4-
abstract class Attributes {
5-
/// Instantiate an empty Attributes.
6-
Attributes.empty();
7-
8-
/// The size of this collection of Attributes.
9-
int get length;
4+
class Attributes {
5+
Map<String, Object> _attributes = {};
106

11-
/// Whether this collection of Attributes is empty.
12-
bool get isEmpty;
7+
/// Instantiate an empty Attributes.
8+
Attributes.empty() {
9+
_attributes = {};
10+
}
1311

1412
/// Retrieve the value associated with the Attribute with key [key].
15-
Object get(String key);
13+
Object get(String key) => _attributes[key];
1614

1715
/// Retrieve the keys of all Attributes in this collection.
18-
Iterable<String> get keys;
16+
Iterable<String> get keys => _attributes.keys;
1917

2018
/// Add an Attribute [attribute].
2119
/// If an Attribute with the same key already exists, it will be overwritten.
22-
void add(Attribute attribute);
20+
void add(Attribute attribute) {
21+
_attributes[attribute.key] = attribute.value;
22+
}
2323

2424
/// Add all Attributes in List [attributes].
2525
/// If an Attribute with the same key already exists, it will be overwritten.
26-
void addAll(List<Attribute> attributes);
26+
void addAll(List<Attribute> attributes) {
27+
attributes.forEach(add);
28+
}
2729
}

lib/src/api/context/context.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
/// OpenTelemetry SDKs.
2424
import 'dart:async';
2525

26-
import 'package:opentelemetry/src/sdk/trace/span.dart';
27-
import 'package:opentelemetry/src/sdk/trace/span_context.dart';
26+
import '../../../api.dart' as api;
2827

2928
/// [ContextKey] used to store spans in a [Context].
3029
final ContextKey spanKey = Context.createKey('OpenTelemetry Context Key SPAN');
@@ -65,20 +64,20 @@ class Context {
6564
Context setValue(ContextKey key, Object value) =>
6665
Context._(_zone.fork(zoneValues: {key: value}));
6766

68-
/// Returns a new [Context] created from this one with the given [Span]
67+
/// Returns a new [Context] created from this one with the given [api.Span]
6968
/// set.
70-
Context withSpan(Span span) => setValue(spanKey, span);
69+
Context withSpan(api.Span span) => setValue(spanKey, span);
7170

7271
/// Execute a function [fn] within this [Context] and return its result.
7372
R execute<R>(R Function() fn) => _zone.run(fn);
7473

75-
/// Get the [Span] attached to this [Context], or null if no such
76-
/// [Span] exists.
77-
Span get span => getValue(spanKey);
74+
/// Get the [api.Span] attached to this [Context], or null if no such
75+
/// [api.Span] exists.
76+
api.Span get span => getValue(spanKey);
7877

79-
/// Get the [SpanContext] from this [Context], or null if no such
80-
/// [SpanContext] exists.
81-
SpanContext get spanContext => getValue(spanKey)?.spanContext;
78+
/// Get the [api.SpanContext] from this [Context], or null if no such
79+
/// [api.SpanContext] exists.
80+
api.SpanContext get spanContext => getValue(spanKey)?.spanContext;
8281
}
8382

8483
class ContextKey {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import '../trace/span.dart';
1+
import '../../../api.dart' as api;
22

33
abstract class SpanExporter {
4-
void export(List<Span> spans);
4+
void export(List<api.Span> spans);
55

66
void shutdown();
77
}

lib/src/api/propagation/extractors/text_map_getter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import '../text_map_propagator.dart';
1+
import '../../../../api.dart' as api;
22

3-
/// Interface that allows a [TextMapPropagator] to read propagated fields from a carrier.
3+
/// Interface that allows a [api.TextMapPropagator] to read propagated fields from a carrier.
44
abstract class TextMapGetter<C> {
55
/// Returns all the keys in the given carrier.
66
Iterable<String> keys(C carrier);

lib/src/api/propagation/injectors/text_map_setter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import '../text_map_propagator.dart';
1+
import '../../../../api.dart' as api;
22

3-
/// Class that allows a [TextMapPropagator] to set propagated fields into a carrier.
3+
/// Class that allows a [api.TextMapPropagator] to set propagated fields into a carrier.
44
abstract class TextMapSetter<C> {
55
/// Sets [value] for [key] on [carrier].
66
void set(C carrier, String key, String value);
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import '../context/context.dart';
2-
import 'extractors/text_map_getter.dart';
3-
import 'injectors/text_map_setter.dart';
1+
import '../../../api.dart' as api;
42

53
/// A class responsible for performing the injection and extraction of a
64
/// cross-cutting concern value as string key/values pairs into carriers that
@@ -9,7 +7,8 @@ import 'injectors/text_map_setter.dart';
97
/// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md#textmap-propagator
108
/// for full specification.
119
abstract class TextMapPropagator<C> {
12-
void inject(Context context, C carrier, TextMapSetter<C> setter);
10+
void inject(api.Context context, C carrier, api.TextMapSetter<C> setter);
1311

14-
Context extract(Context context, C carrier, TextMapGetter<C> getter);
12+
api.Context extract(
13+
api.Context context, C carrier, api.TextMapGetter<C> getter);
1514
}

0 commit comments

Comments
 (0)