Skip to content

Commit

Permalink
show globalContextManager
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeroberts-wk committed Jul 17, 2024
1 parent a227a5d commit 9328236
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion example/w3c_context_propagation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:opentelemetry/api.dart';
import 'package:opentelemetry/sdk.dart'
show ConsoleExporter, SimpleSpanProcessor, TracerProviderBase;
import 'package:opentelemetry/src/experimental_api.dart'
show globalContextManager, NoopContextManager, registerGlobalContextManager;
show NoopContextManager, registerGlobalContextManager;

class MapSetter implements TextMapSetter<Map> {
@override
Expand Down
6 changes: 1 addition & 5 deletions example/zone_context_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import 'package:opentelemetry/api.dart';
import 'package:opentelemetry/sdk.dart'
show ConsoleExporter, SimpleSpanProcessor, TracerProviderBase;
import 'package:opentelemetry/src/experimental_api.dart'
show
globalContextManager,
registerGlobalContextManager,
ZoneContext,
ZoneContextManager;
show registerGlobalContextManager, ZoneContext, ZoneContextManager;

void main(List<String> args) async {
final tp =
Expand Down
1 change: 1 addition & 0 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export 'src/api/context/context.dart'
contextWithSpanContext,
spanContextFromContext,
spanFromContext;
export 'src/api/context/context_manager.dart' show globalContextManager;
export 'src/api/exporters/span_exporter.dart' show SpanExporter;
export 'src/api/instrumentation_library.dart' show InstrumentationLibrary;
export 'src/api/open_telemetry.dart'
Expand Down
1 change: 0 additions & 1 deletion lib/src/api/context/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information

import '../../../api.dart';
import '../../experimental_api.dart' show globalContextManager;
import '../trace/nonrecording_span.dart' show NonRecordingSpan;

class ContextKey {}
Expand Down
9 changes: 4 additions & 5 deletions lib/src/api/open_telemetry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:meta/meta.dart';
import '../../api.dart' as api;
import 'propagation/noop_text_map_propagator.dart';
import 'trace/noop_tracer_provider.dart';
import 'context/context_manager.dart';
import 'context/zone_context.dart';

final api.TracerProvider _noopTracerProvider = NoopTracerProvider();
Expand Down Expand Up @@ -46,7 +45,7 @@ void registerGlobalTextMapPropagator(api.TextMapPropagator textMapPropagator) {
'This method will be removed in 0.19.0. Use [traceContext] instead.')
Future<T> trace<T>(String name, Future<T> Function() fn,
{api.Context? context, api.Tracer? tracer}) async {
context ??= globalContextManager.active;
context ??= api.globalContextManager.active;
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

final span = tracer.startSpan(name, context: context);
Expand All @@ -71,7 +70,7 @@ Future<T> traceContext<T>(String name, Future<T> Function(api.Context) fn,
bool newRoot = false,
api.SpanKind spanKind = api.SpanKind.internal,
List<api.SpanLink> spanLinks = const []}) async {
context ??= globalContextManager.active;
context ??= api.globalContextManager.active;
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

// TODO: use start span option `newRoot` instead
Expand Down Expand Up @@ -103,7 +102,7 @@ Future<T> traceContext<T>(String name, Future<T> Function(api.Context) fn,
'This method will be removed in 0.19.0. Use [traceContextSync] instead.')
R traceSync<R>(String name, R Function() fn,
{api.Context? context, api.Tracer? tracer}) {
context ??= globalContextManager.active;
context ??= api.globalContextManager.active;
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

final span = tracer.startSpan(name, context: context);
Expand Down Expand Up @@ -135,7 +134,7 @@ R traceContextSync<R>(String name, R Function(api.Context) fn,
bool newRoot = false,
api.SpanKind spanKind = api.SpanKind.internal,
List<api.SpanLink> spanLinks = const []}) {
context ??= globalContextManager.active;
context ??= api.globalContextManager.active;
tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

// TODO: use start span option `newRoot` instead
Expand Down
2 changes: 1 addition & 1 deletion lib/src/experimental_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ library experimental_api;
import 'package:meta/meta.dart';

export 'api/context/context_manager.dart'
show ContextManager, registerGlobalContextManager, globalContextManager;
show ContextManager, registerGlobalContextManager;
export 'api/context/noop_context_manager.dart' show NoopContextManager;
export 'api/context/zone_context.dart' show ZoneContext;
export 'api/context/zone_context_manager.dart' show ZoneContextManager;
Expand Down
3 changes: 1 addition & 2 deletions lib/src/sdk/trace/tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:meta/meta.dart';

import '../../../api.dart' as api;
import '../../../sdk.dart' as sdk;
import '../../experimental_api.dart' show globalContextManager;
import '../common/limits.dart' show applyLinkLimits;
import 'span.dart';

Expand Down Expand Up @@ -38,7 +37,7 @@ class Tracer implements api.Tracer {
List<api.SpanLink> links = const [],
Int64? startTime,
bool newRoot = false}) {
context ??= globalContextManager.active;
context ??= api.globalContextManager.active;
startTime ??= _timeProvider.now;

// If a valid, active Span is present in the context, use it as this Span's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@TestOn('vm')
import 'package:opentelemetry/api.dart' as api;
import 'package:opentelemetry/sdk.dart' as sdk;
import 'package:opentelemetry/src/experimental_api.dart';
import 'package:opentelemetry/src/sdk/trace/span.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -50,7 +49,7 @@ void main() {
final testPropagator = api.W3CTraceContextPropagator();
final testCarrier = <String, String>{};
final testContext =
api.contextWithSpan(globalContextManager.active, testSpan);
api.contextWithSpan(api.globalContextManager.active, testSpan);

testPropagator.inject(testContext, testCarrier, TestingInjector());
final resultSpan = api.spanFromContext(
Expand Down Expand Up @@ -89,7 +88,7 @@ void main() {
final testPropagator = api.W3CTraceContextPropagator();
final testCarrier = <String, String>{};
final testContext =
api.contextWithSpan(globalContextManager.active, testSpan);
api.contextWithSpan(api.globalContextManager.active, testSpan);

testPropagator.inject(testContext, testCarrier, TestingInjector());
final resultSpan = api.spanFromContext(
Expand Down Expand Up @@ -133,7 +132,7 @@ void main() {
// Inject and extract a test Span from a Context, as when an outbound
// call is made and received by another service.
final testContext =
api.contextWithSpan(globalContextManager.active, testSpan);
api.contextWithSpan(api.globalContextManager.active, testSpan);
testPropagator.inject(testContext, testCarrier, TestingInjector());
final parentSpan = api.spanFromContext(
testPropagator.extract(testContext, testCarrier, TestingExtractor()));
Expand All @@ -142,7 +141,7 @@ void main() {

// Use the transmitted Span as a receiver.
final resultSpan = tracer.startSpan('doWork',
context: api.contextWithSpan(globalContextManager.active, testSpan))
context: api.contextWithSpan(api.globalContextManager.active, testSpan))
..end();

// Verify that data from the original Span propagates to the child.
Expand Down
3 changes: 1 addition & 2 deletions test/integration/sdk/tracer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@TestOn('vm')
import 'package:opentelemetry/api.dart' as api;
import 'package:opentelemetry/sdk.dart' as sdk;
import 'package:opentelemetry/src/experimental_api.dart';
import 'package:opentelemetry/src/sdk/trace/span.dart';
import 'package:opentelemetry/src/sdk/trace/tracer.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -42,7 +41,7 @@ void main() {

final parentSpan = tracer.startSpan('foo');
final context =
api.contextWithSpan(globalContextManager.active, parentSpan);
api.contextWithSpan(api.globalContextManager.active, parentSpan);

final childSpan = tracer.startSpan('bar', context: context) as Span;

Expand Down
9 changes: 5 additions & 4 deletions test/unit/api/context/context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ void main() {
group('spanFromContext', () {
test('returns Span when exists', () {
final context =
api.contextWithSpan(globalContextManager.active, testSpan);
api.contextWithSpan(api.globalContextManager.active, testSpan);

expect(api.spanFromContext(context), same(testSpan));
});

test('returns an invalid Span when a Span does not exist in the Context',
() {
final context = globalContextManager.active;
final context = api.globalContextManager.active;

expect(api.spanFromContext(context), isA<NonRecordingSpan>());
expect(api.spanContextFromContext(context).isValid, isFalse);
Expand All @@ -45,15 +45,16 @@ void main() {
group('spanContextFromContext', () {
test('returns SpanContext when Span exists', () {
final testContext =
api.contextWithSpan(globalContextManager.active, testSpan);
api.contextWithSpan(api.globalContextManager.active, testSpan);

expect(api.spanContextFromContext(testContext), same(testSpanContext));
});

test(
'returns an invalid SpanContext when a Span does not exist in the Context',
() {
expect(api.spanContextFromContext(globalContextManager.active).isValid,
expect(
api.spanContextFromContext(api.globalContextManager.active).isValid,
isFalse);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

@TestOn('vm')
import 'package:opentelemetry/api.dart';
import 'package:opentelemetry/src/experimental_api.dart';
import 'package:test/test.dart';

void main() {
Expand Down
7 changes: 4 additions & 3 deletions test/unit/sdk/trace/tracer_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@TestOn('vm')

import 'package:opentelemetry/api.dart' as api;
import 'package:opentelemetry/src/experimental_api.dart';
import 'package:opentelemetry/src/sdk/trace/tracer.dart';
import 'package:opentelemetry/src/sdk/trace/tracer_provider.dart';
import 'package:test/test.dart';
Expand All @@ -12,7 +11,8 @@ void main() {

test('with newRoot true', () {
final parent = tracer.startSpan('parent');
final context = api.contextWithSpan(globalContextManager.active, parent);
final context =
api.contextWithSpan(api.globalContextManager.active, parent);
final span =
(tracer as Tracer).startSpan('', newRoot: true, context: context);
expect(span.parentSpanId.isValid, isFalse);
Expand All @@ -23,7 +23,8 @@ void main() {

test('with newRoot false', () {
final parent = tracer.startSpan('parent');
final context = api.contextWithSpan(globalContextManager.active, parent);
final context =
api.contextWithSpan(api.globalContextManager.active, parent);
final span =
(tracer as Tracer).startSpan('', newRoot: false, context: context);
expect(span.parentSpanId.isValid, isTrue);
Expand Down

0 comments on commit 9328236

Please sign in to comment.