Skip to content

Commit 373139b

Browse files
Merge pull request #12 from michaelyeager-wf/myeager-wf/O11Y-1019
O11Y-1019: Inject and Extract context from a message to feed into span creation
2 parents 4821f21 + 354a6d1 commit 373139b

Some content is hidden

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

41 files changed

+1372
-106
lines changed

lib/api.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
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;
4+
export 'src/api/propagation/extractors/text_map_getter.dart' show TextMapGetter;
5+
export 'src/api/propagation/injectors/text_map_setter.dart' show TextMapSetter;
6+
export 'src/api/propagation/text_map_propagator.dart' show TextMapPropagator;
47
export 'src/api/trace/context_utils.dart'
58
show getSpan, getSpanContext, setSpan, withContext;
69
export 'src/api/trace/id_generator.dart' show IdGenerator;
710
export 'src/api/trace/span.dart' show Span;
811
export 'src/api/trace/span_context.dart' show SpanContext;
12+
export 'src/api/trace/span_id.dart' show SpanId;
13+
export 'src/api/trace/trace_flags.dart' show TraceFlags;
14+
export 'src/api/trace/trace_id.dart' show TraceId;
915
export 'src/api/trace/trace_state.dart' show TraceState;
1016
export 'src/api/trace/tracer.dart' show Tracer;
1117
export 'src/api/trace/tracer_provider.dart' show TracerProvider;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import '../text_map_propagator.dart';
2+
3+
/// Interface that allows a [TextMapPropagator] to read propagated fields from a carrier.
4+
abstract class TextMapGetter<C> {
5+
/// Returns all the keys in the given carrier.
6+
Iterable<String> keys(C carrier);
7+
8+
/// Returns the first value of the given propagation [key] or returns null.
9+
String get(C carrier, String key);
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import '../text_map_propagator.dart';
2+
3+
/// Class that allows a [TextMapPropagator] to set propagated fields into a carrier.
4+
abstract class TextMapSetter<C> {
5+
/// Sets [value] for [key] on [carrier].
6+
void set(C carrier, String key, String value);
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import '../context/context.dart';
2+
import 'extractors/text_map_getter.dart';
3+
import 'injectors/text_map_setter.dart';
4+
5+
/// A class responsible for performing the injection and extraction of a
6+
/// cross-cutting concern value as string key/values pairs into carriers that
7+
/// travel across process boundaries.
8+
///
9+
/// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md#textmap-propagator
10+
/// for full specification.
11+
abstract class TextMapPropagator<C> {
12+
void inject(Context context, C carrier, TextMapSetter<C> setter);
13+
14+
Context extract(Context context, C carrier, TextMapGetter<C> getter);
15+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'package:fixnum/fixnum.dart';
2+
3+
import '../../sdk/common/attributes.dart' as sdk_attributes;
4+
import '../../sdk/trace/span.dart';
5+
import '../../sdk/trace/span_context.dart' as sdk_spancontext;
6+
import '../../sdk/trace/span_id.dart';
7+
import '../common/attributes.dart';
8+
import 'noop_tracer.dart';
9+
import 'span.dart' as api;
10+
import 'span_context.dart';
11+
import 'span_status.dart';
12+
import 'tracer.dart';
13+
14+
/// A class representing a [Span] which should not be sampled or recorded.
15+
///
16+
/// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#wrapping-a-spancontext-in-a-span
17+
/// for more information.
18+
///
19+
/// This class should not be exposed to consumers and is used internally to wrap
20+
/// [SpanContext] being injected or extracted for external calls.
21+
class NonRecordingSpan extends Span implements api.Span {
22+
final Attributes _attributes = sdk_attributes.Attributes.empty();
23+
final SpanStatus _status = SpanStatus()..code = StatusCode.OK;
24+
final Tracer _tracer = NoopTracer();
25+
final SpanContext _spanContext;
26+
27+
NonRecordingSpan(this._spanContext)
28+
: super('NON_RECORDING', _spanContext, null, [], NoopTracer());
29+
30+
@override
31+
Attributes get attributes => _attributes;
32+
33+
@override
34+
set attributes(Attributes attributes) {
35+
return;
36+
}
37+
38+
@override
39+
void end() {
40+
return;
41+
}
42+
43+
@override
44+
Int64 get endTime => Int64.ZERO;
45+
46+
@override
47+
String get name => 'NON_RECORDING';
48+
49+
@override
50+
SpanId get parentSpanId => null;
51+
52+
@override
53+
void setStatus(StatusCode status, {String description}) {
54+
return;
55+
}
56+
57+
@override
58+
sdk_spancontext.SpanContext get spanContext => _spanContext;
59+
60+
@override
61+
Int64 get startTime => Int64.ZERO;
62+
63+
@override
64+
SpanStatus get status => _status;
65+
66+
@override
67+
Tracer get tracer => _tracer;
68+
}

lib/src/api/trace/noop_tracer.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import '../../../api.dart' as api;
2+
import '../../sdk/trace/span_context.dart';
3+
import 'nonrecording_span.dart';
4+
import 'tracer.dart' as api;
5+
6+
/// A [api.Tracer] class which yields [NonRecordingSpan]s and no-ops for most
7+
/// operations.
8+
class NoopTracer implements api.Tracer {
9+
@override
10+
String get name => 'NOOP';
11+
12+
@override
13+
api.Span startSpan(String name,
14+
{api.Context context, api.Attributes attributes}) {
15+
final SpanContext parentContext = api.getSpanContext(context);
16+
17+
return NonRecordingSpan(
18+
(parentContext.isValid) ? parentContext : SpanContext.invalid());
19+
}
20+
}

lib/src/api/trace/sampler.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import 'package:opentelemetry/src/api/context/context.dart';
2-
import 'package:opentelemetry/src/api/instrumentation_library.dart';
3-
import 'package:opentelemetry/src/api/trace/sampling_result.dart';
4-
import 'package:opentelemetry/src/api/trace/span.dart';
1+
import '../context/context.dart';
2+
import '../instrumentation_library.dart';
3+
import 'sampling_result.dart';
4+
import 'span.dart';
55

66
/// Represents an entity which determines whether a [Span] should be sampled
77
/// and sent for collection.

lib/src/api/trace/span.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'package:fixnum/fixnum.dart';
2-
import 'span_status.dart';
3-
import '../common/attributes.dart';
42

3+
import '../common/attributes.dart';
54
import 'span_context.dart';
5+
import 'span_id.dart';
6+
import 'span_status.dart';
67
import 'tracer.dart';
78

89
/// A representation of a single operation within a trace.
@@ -26,11 +27,15 @@ abstract class Span {
2627
Int64 get startTime;
2728

2829
/// The parent span id.
29-
List<int> get parentSpanId;
30+
SpanId get parentSpanId;
3031

3132
/// The name of the span.
3233
String get name;
3334

35+
/// Whether this Span is recording information like events with the
36+
/// addEvent operation, status with setStatus, etc.
37+
bool get isRecording;
38+
3439
/// Sets the status to the [Span].
3540
///
3641
/// If used, this will override the default [Span] status. Default status code
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
import 'span_id.dart';
2+
import 'trace_flags.dart';
3+
import 'trace_id.dart';
14
import 'trace_state.dart';
25

36
/// Representation of the context of the context of an individual span.
47
abstract class SpanContext {
58
/// Get the ID of the span.
6-
List<int> get spanId;
9+
SpanId get spanId;
710

811
/// Get the ID of the trace the span is a part of.
9-
List<int> get traceId;
12+
TraceId get traceId;
13+
14+
/// Get flags (sampling, trace level, etc.) set for the trace the span is a
15+
/// part of.
16+
TraceFlags get traceFlags;
1017

1118
/// Get the state of the entire trace.
1219
TraceState get traceState;
20+
21+
bool get isValid;
1322
}

lib/src/api/trace/span_id.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'span.dart';
2+
3+
/// Class representing an ID for a single [Span].
4+
/// See https://www.w3.org/TR/trace-context/#parent-id for full specification.
5+
abstract class SpanId {
6+
static const SIZE_BITS = 16;
7+
static const SIZE_BYTES = 8;
8+
static final List<int> INVALID =
9+
List<int>.filled(SIZE_BYTES, 0); // 0000000000000000
10+
static final List<int> ROOT = [];
11+
12+
/// Retrieve this SpanId as a list of byte values.
13+
List<int> get();
14+
15+
/// Whether this ID represents a valid [Span].
16+
bool get isValid;
17+
18+
/// Retrieve this SpanId as a human-readable ID.
19+
@override
20+
String toString();
21+
}

0 commit comments

Comments
 (0)