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

PPI-203 : deprecate context manager and implement attach/detach APIs #192

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main(List<String> args) {

In order to parent spans, context must be propagated. Propagation can be achieved by manually passing an instance of `Context` or by using Dart [`Zones`](https://dart.dev/libraries/async/zones).

See the [noop context manager example](./example/noop_context_manager.dart) and [zone context manager example](./example/zone_context_manager.dart) for more information.
See the [attach detach context example](./example/attach_detach_context)for more information.

### Inter-process

Expand Down
7 changes: 7 additions & 0 deletions example/attach_detach_context/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Attach Detach Context Example

This example demonstrates context propagation using the attach/detach context APIs through multiple zones.

The example produces two traces represented by the following diagram:

![./attach_detach_context.png](./attach_detach_context.png)
49 changes: 49 additions & 0 deletions example/attach_detach_context/attach_detach_context.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 'dart:async';

import 'package:opentelemetry/api.dart';
import 'package:opentelemetry/sdk.dart'
show ConsoleExporter, SimpleSpanProcessor, TracerProviderBase;

void main() {
final tp = TracerProviderBase(
processors: [SimpleSpanProcessor(ConsoleExporter())]),
tracer = tp.getTracer('instrumentation-name');

final span = tracer.startSpan('root-zone-attached-span')..end();
final token = attach(contextWithSpan(active, span));

final completer = Completer();

// zone A
zoneWithContext(active).run(() {
final span = tracer.startSpan('zone-a-attached-span')..end();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would advise against shadowing the variables.

span and token are both shadowed and that certainly makes the example a little harder to read.

One way you could tackle this is to split the zone function. The other is just using more unique names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I can update the variable names. Though I'm wondering if I should scrap this example for something more contrived. Like pretend to do an app "setup" or "load" or something 🤷

final context = contextWithSpan(active, span);

// zone B
zoneWithContext(context).run(() {
tracer.startSpan('zone-b-span').end();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this zone-b-span should be a child of zone-a-attached-span? But the README says it is not. I suggest to use the same span names in the README.

completer.future.then((_) {
tracer.startSpan('zone-b-post-detach-span').end();
});
});

final token = attach(context);
tracer.startSpan('zone-a-attached-child-span').end();
if (!detach(token)) {
throw Exception('Failed to detach context');
}

tracer.startSpan('zone-a-post-detach-span').end();
});

if (!detach(token)) {
throw Exception('Failed to detach context');
}

completer.complete();

tracer.startSpan('root-zone-post-detach-span').end();
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 10 additions & 7 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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 'dart:async';

import 'package:opentelemetry/api.dart';
import 'package:opentelemetry/sdk.dart';

Expand Down Expand Up @@ -28,20 +30,21 @@ final tracer = provider.getTracer('instrumentation-name');

/// Demonstrates creating a trace with a parent and child span.
void main() async {
// The current active span is available via the global context manager.
var context = globalContextManager.active;
// The current active context is available via the global getter, [active].
var context = active;

// A trace starts with a root span which has no parent.
final parentSpan = tracer.startSpan('parent-span');

// A new context can be created in order to propagate context manually.
context = contextWithSpan(context, parentSpan);

// The traceContext and traceContextSync functions will automatically
// propagate context, capture errors, and end the span.
await traceContext(
'child-span', (_context) => Future.delayed(Duration(milliseconds: 100)),
context: context);
// The [trace] and [traceSync] functions will automatically propagate
// context, capture errors, and end the span.
await trace('child-span', () {
tracer.startSpan('grandchild-span').end();
return Future.delayed(Duration(milliseconds: 100));
}, context: context, tracer: tracer);

// Spans must be ended or they will not be exported.
parentSpan.end();
Expand Down
28 changes: 0 additions & 28 deletions example/noop_context_manager.dart

This file was deleted.

10 changes: 3 additions & 7 deletions example/w3c_context_propagation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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
Expand All @@ -30,15 +29,12 @@ void main(List<String> args) async {
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());
tmp.inject(contextWithSpan(active, span), carrier, MapSetter());
await test(carrier);
span.end();
}
Expand All @@ -47,7 +43,7 @@ Future test(Map<String, String> carrier) async {
globalTracerProvider
.getTracer('instrumentation-name')
.startSpan('test-span-1',
context: globalTextMapPropagator.extract(
globalContextManager.active, carrier, MapGetter()))
context:
globalTextMapPropagator.extract(active, carrier, MapGetter()))
.end();
}
32 changes: 0 additions & 32 deletions example/zone_context_manager.dart

This file was deleted.

14 changes: 9 additions & 5 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ export 'src/api/common/resource_attributes.dart' show ResourceAttributes;
export 'src/api/common/semantic_attributes.dart' show SemanticAttributes;
export 'src/api/context/context.dart'
show
active,
attach,
Context,
ContextKey,
contextFromZone,
contextWithSpan,
contextWithSpanContext,
detach,
root,
blakeroberts-wk marked this conversation as resolved.
Show resolved Hide resolved
blakeroberts-wk marked this conversation as resolved.
Show resolved Hide resolved
spanContextFromContext,
spanFromContext;
export 'src/api/context/context_manager.dart'
show
globalContextManager,
registerGlobalContextManager;
spanFromContext,
zoneWithContext;
export 'src/api/context/context_manager.dart'
show globalContextManager, registerGlobalContextManager;
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
Loading