Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry-dart 0.17.0 #135

Merged
merged 14 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM dart:2.19
ADD pubspec.yaml ./
RUN dart pub get
FROM scratch
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
init:
git submodule init
# Pull opentelemetry-proto at the stored commit.
# To upgrade, execute `git submodule update --remote --merge`
# and commit the result.
git submodule update
dart pub get
dart pub global activate protoc_plugin 20.0.1
# Generate mocks for unit tests. For config, see build.yaml.
dart run build_runner build --delete-conflicting-outputs
dart pub global activate protoc_plugin 21.1.2
cd lib/src/sdk/proto && \
protoc --proto_path opentelemetry-proto --dart_out . \
opentelemetry-proto/opentelemetry/proto/common/v1/common.proto \
Expand Down
8 changes: 4 additions & 4 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
targets:
$default:
builders:
# mockito's builder is expensive and is not needed until this package is
# migrated to null-safety. At that point, it should be scoped only to
# relevant files.
mockito:mockBuilder:
enabled: false
enabled: true
generate_for:
include:
- test/unit/mocks.dart
1 change: 0 additions & 1 deletion lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export 'src/api/propagation/w3c_trace_context_propagator.dart'
show W3CTraceContextPropagator;
export 'src/api/span_processors/span_processor.dart' show SpanProcessor;
export 'src/api/trace/id_generator.dart' show IdGenerator;
export 'src/api/trace/nonrecording_span.dart' show NonRecordingSpan;
export 'src/api/trace/span_context.dart' show SpanContext;
export 'src/api/trace/span_id.dart' show SpanId;
export 'src/api/trace/span_link.dart' show SpanLink;
Expand Down
10 changes: 5 additions & 5 deletions lib/sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

export 'src/sdk/instrumentation_library.dart' show InstrumentationLibrary;
export 'src/sdk/common/instrumentation_scope.dart' show InstrumentationScope;
export 'src/sdk/resource/resource.dart' show Resource;
export 'src/sdk/time_providers/datetime_time_provider.dart'
show DateTimeTimeProvider;
export 'src/sdk/time_providers/time_provider.dart' show TimeProvider;
export 'src/sdk/trace/exporters/span_exporter.dart' show SpanExporter;
export 'src/sdk/trace/exporters/collector_exporter.dart' show CollectorExporter;
export 'src/sdk/trace/exporters/console_exporter.dart' show ConsoleExporter;
export 'src/sdk/trace/id_generator.dart' show IdGenerator;
export 'src/sdk/trace/propagation/w3c_trace_context_propagator.dart'
show W3CTraceContextPropagator;
export 'src/sdk/trace/sampling/always_off_sampler.dart' show AlwaysOffSampler;
export 'src/sdk/trace/sampling/always_on_sampler.dart' show AlwaysOnSampler;
export 'src/sdk/trace/sampling/parent_based_sampler.dart'
show ParentBasedSampler;
export 'src/sdk/trace/sampling/sampler.dart' show Sampler;
export 'src/sdk/trace/sampling/sampling_result.dart'
show Decision, SamplingResult;
export 'src/sdk/trace/span_context.dart' show SpanContext;
export 'src/sdk/trace/read_only_span.dart' show ReadOnlySpan;
export 'src/sdk/trace/read_write_span.dart' show ReadWriteSpan;
export 'src/sdk/trace/span_limits.dart' show SpanLimits;
export 'src/sdk/trace/span_processors/span_processor.dart' show SpanProcessor;
export 'src/sdk/trace/span_processors/batch_processor.dart'
show BatchSpanProcessor;
export 'src/sdk/trace/span_processors/simple_processor.dart'
show SimpleSpanProcessor;
export 'src/sdk/trace/span.dart' show Span;
export 'src/sdk/trace/trace_state.dart' show TraceState;
export 'src/sdk/trace/tracer_provider.dart' show TracerProviderBase;
48 changes: 8 additions & 40 deletions lib/src/api/common/attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,27 @@ class Attribute {
final Object value;

/// Create an Attribute from a String value.
Attribute.fromString(this.key, String this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromString(this.key, String this.value);

/// Create an Attribute from a boolean value.
// ignore: avoid_positional_boolean_parameters
Attribute.fromBoolean(this.key, bool this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromBoolean(this.key, bool this.value);

/// Create an Attribute from a double-precision floating-point value.
Attribute.fromDouble(this.key, double this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromDouble(this.key, double this.value);

/// Create an Attribute from an integer value.
Attribute.fromInt(this.key, int this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromInt(this.key, int this.value);

/// Create an Attribute from a list of String values.
Attribute.fromStringList(this.key, List<String> this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromStringList(this.key, List<String> this.value);

/// Create an Attribute from a list of boolean values.
Attribute.fromBooleanList(this.key, List<bool> this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromBooleanList(this.key, List<bool> this.value);

/// Create an Attribute from a list of double-precision floating-point values.
Attribute.fromDoubleList(this.key, List<double> this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromDoubleList(this.key, List<double> this.value);

/// Create an Attribute from a list of integer values.
Attribute.fromIntList(this.key, List<int> this.value) {
if (key == null) {
throw ArgumentError("key can't be null.");
}
}
Attribute.fromIntList(this.key, List<int> this.value);
}
14 changes: 9 additions & 5 deletions lib/src/api/context/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import 'dart:async';

import '../../../api.dart' as api;
import '../trace/nonrecording_span.dart';

/// [ContextKey] used to store spans in a [Context].
final ContextKey spanKey = Context.createKey('OpenTelemetry Context Key SPAN');
Expand Down Expand Up @@ -57,7 +58,7 @@ class Context {

/// Returns the value from this context identified by [key], or null if no
/// such value is set.
T getValue<T>(ContextKey key) => _zone[key];
T? getValue<T>(ContextKey key) => _zone[key];

/// Returns a new context created from this one with the given key/value pair
/// set.
Expand All @@ -74,13 +75,16 @@ class Context {
/// Execute a function [fn] within this [Context] and return its result.
R execute<R>(R Function() fn) => _zone.run(fn);

/// Get the [api.Span] attached to this [Context], or null if no such
/// Get the [api.Span] attached to this [Context], or an invalid, [api.Span] if no such
/// [api.Span] exists.
api.Span get span => getValue(spanKey);
api.Span get span =>
getValue(spanKey) ?? NonRecordingSpan(api.SpanContext.invalid());

/// Get the [api.SpanContext] from this [Context], or null if no such
/// Get the [api.SpanContext] from this [Context], or an invalid [api.SpanContext] if no such
/// [api.SpanContext] exists.
api.SpanContext get spanContext => getValue(spanKey)?.spanContext;
api.SpanContext get spanContext =>
(getValue(spanKey) ?? NonRecordingSpan(api.SpanContext.invalid()))
.spanContext;
}

class ContextKey {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/exporters/span_exporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import '../../../api.dart' as api;

@Deprecated(
'This class will be moved to the SDK package in v0.18.0. Use [SpanExporter] from SDK instead.')
abstract class SpanExporter {
void export(List<api.Span> spans);

Expand Down
1 change: 1 addition & 0 deletions lib/src/api/instrumentation_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// Represents versioning metadata for this library within applications
/// which use multiple implementations of OpenTelemetry.
// See https://github.com/open-telemetry/oteps/blob/main/text/0083-component.md#instrumentationlibrary
@Deprecated('This class will be removed in 0.18.0.')
abstract class InstrumentationLibrary {
String get name;
String get version;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/metrics/noop/noop_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import 'package:opentelemetry/src/experimental_api.dart';
/// A no-op instance of a [Counter]
class NoopCounter<T extends num> extends Counter<T> {
@override
void add(T value, {List<Attribute> attributes, Context context}) {}
void add(T value, {List<Attribute>? attributes, Context? context}) {}
}
2 changes: 1 addition & 1 deletion lib/src/api/metrics/noop/noop_meter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:opentelemetry/src/experimental_api.dart';
class NoopMeter implements Meter {
@override
Counter<T> createCounter<T extends num>(String name,
{String description, String unit}) {
{String? description, String? unit}) {
return NoopCounter<T>();
}
}
8 changes: 4 additions & 4 deletions lib/src/api/open_telemetry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void registerGlobalTextMapPropagator(api.TextMapPropagator textMapPropagator) {
/// Records a span of the given [name] for the given function with a given
/// [api.Tracer] and marks the span as errored if an exception occurs.
Future<T> trace<T>(String name, Future<T> Function() fn,
{api.Context context, api.Tracer tracer}) async {
{api.Context? context, api.Tracer? tracer}) async {
context ??= api.Context.current;
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

Expand All @@ -50,7 +50,7 @@ Future<T> trace<T>(String name, Future<T> Function() fn,
return await context.withSpan(span).execute(fn);
} catch (e, s) {
span
..setStatus(api.StatusCode.error, description: e.toString())
..setStatus(api.StatusCode.error, e.toString())
..recordException(e, stackTrace: s);
rethrow;
} finally {
Expand All @@ -60,7 +60,7 @@ Future<T> trace<T>(String name, Future<T> Function() fn,

/// Use [traceSync] instead of [trace] when [fn] is not an async function.
R traceSync<R>(String name, R Function() fn,
{api.Context context, api.Tracer tracer}) {
{api.Context? context, api.Tracer? tracer}) {
context ??= api.Context.current;
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

Expand All @@ -77,7 +77,7 @@ R traceSync<R>(String name, R Function() fn,
return r;
} catch (e, s) {
span
..setStatus(api.StatusCode.error, description: e.toString())
..setStatus(api.StatusCode.error, e.toString())
..recordException(e, stackTrace: s);
rethrow;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/propagation/extractors/text_map_getter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ abstract class TextMapGetter<C> {
Iterable<String> keys(C carrier);

/// Returns the first value of the given propagation [key] or returns null.
String get(C carrier, String key);
String? get(C carrier, String key);
}
32 changes: 18 additions & 14 deletions lib/src/api/propagation/w3c_trace_context_propagator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,32 @@ class W3CTraceContextPropagator implements api.TextMapPropagator {
// Carrier did not contain a trace header. Do nothing.
return context;
}
if (!traceParentHeaderRegEx.hasMatch(traceParentHeader)) {

final parentHeaderMatch =
traceParentHeaderRegEx.firstMatch(traceParentHeader);

if (parentHeaderMatch == null) {
// Encountered a malformed or unknown trace header. Do nothing.
return context;
}

final parentHeaderMatch =
traceParentHeaderRegEx.firstMatch(traceParentHeader);
final parentHeaderFields = Map<String, String>.fromIterable(
parentHeaderMatch.groupNames,
key: (element) => element.toString(),
value: (element) => parentHeaderMatch.namedGroup(element));

final traceId =
api.TraceId.fromString(parentHeaderFields[_traceIdFieldKey]) ??
api.TraceId.invalid();
final parentId =
api.SpanId.fromString(parentHeaderFields[_parentIdFieldKey]) ??
api.SpanId.invalid();
final traceFlags =
int.parse(parentHeaderFields[_traceFlagsFieldKey], radix: 16) ??
api.TraceFlags.none;
value: (element) => parentHeaderMatch.namedGroup(element)!);

final traceIdHeader = parentHeaderFields[_traceIdFieldKey];
final traceId = (traceIdHeader != null)
? api.TraceId.fromString(traceIdHeader)
: api.TraceId.invalid();
final parentIdHeader = parentHeaderFields[_parentIdFieldKey];
final parentId = (parentIdHeader != null)
? api.SpanId.fromString(parentIdHeader)
: api.SpanId.invalid();
final traceFlagsHeader = parentHeaderFields[_traceFlagsFieldKey];
final traceFlags = (traceFlagsHeader != null)
? int.parse(traceFlagsHeader, radix: 16)
: api.TraceFlags.none;
final traceStateHeader = getter.get(carrier, _traceStateHeaderKey);
final traceState = (traceStateHeader != null)
? api.TraceState.fromString(traceStateHeader)
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/span_processors/span_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import '../../../api.dart' as api;

@Deprecated(
'This class will be moved to the SDK package in v0.18.0. Use [SpanExporter] from SDK instead.')
abstract class SpanProcessor {
void onStart(api.Span span, api.Context parentContext);

Expand Down
37 changes: 6 additions & 31 deletions lib/src/api/trace/nonrecording_span.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import '../../../api.dart' as api;
///
/// This class should not be exposed to consumers and is used internally to wrap
/// [api.SpanContext] being injected or extracted for external calls.
@Deprecated(
'This class will stop being exported in v0.17.0. Please use [api.Span] instead.')
class NonRecordingSpan implements api.Span {
final api.SpanStatus _status = api.SpanStatus()..code = api.StatusCode.ok;
final api.SpanId _parentSpanId = api.SpanId.invalid();
final api.SpanContext _spanContext;

NonRecordingSpan(this._spanContext);
Expand All @@ -30,44 +28,21 @@ class NonRecordingSpan implements api.Span {
void end() {}

@override
Int64 get endTime => null;
void setName(String _name) {}

@override
String get name => 'NON_RECORDING';
api.SpanId get parentSpanId => _parentSpanId;

@override
set name(String _name) {}

@override
bool get isRecording => false;

@override
api.SpanId get parentSpanId => api.SpanId.invalid();

@override
@Deprecated(
'This method will be updated to use positional optional parameters in v0.17.0.')
void setStatus(api.StatusCode status, {String description}) {}
void setStatus(api.StatusCode status, [String? description]) {}

@override
api.SpanContext get spanContext => _spanContext;

@override
Int64 get startTime => null;

@override
api.SpanStatus get status => _status;

@override
api.InstrumentationLibrary get instrumentationLibrary => null;

@override
void recordException(dynamic exception, {StackTrace stackTrace}) {}
void recordException(dynamic exception, {StackTrace? stackTrace}) {}

@override
void addEvent(String name, Int64 timestamp,
{List<api.Attribute> attributes}) {}

@override
api.SpanKind get kind => api.SpanKind.internal;
{List<api.Attribute>? attributes}) {}
}
17 changes: 6 additions & 11 deletions lib/src/api/trace/noop_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ import 'nonrecording_span.dart';
class NoopTracer implements api.Tracer {
@override
api.Span startSpan(String name,
{api.Context context,
api.SpanKind kind,
List<api.Attribute> attributes,
List<api.SpanLink> links,
Int64 startTime}) {
final parentContext =
(context.spanContext != null && context.spanContext.isValid)
? context.spanContext
: api.SpanContext.invalid();

return NonRecordingSpan(parentContext);
{api.Context? context,
api.SpanKind? kind,
List<api.Attribute>? attributes,
List<api.SpanLink>? links,
Int64? startTime}) {
return NonRecordingSpan(context?.spanContext ?? api.SpanContext.invalid());
}
}
Loading