-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from Workiva/attach-detach-v3
PPI-203 : deprecate context manager and implement attach/detach APIs
- Loading branch information
Showing
30 changed files
with
700 additions
and
566 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Attach Detach Context Example | ||
|
||
This example demonstrates context propagation using the attach/detach context APIs. | ||
|
||
The example produces two traces represented by the following diagram: | ||
|
||
```mermaid | ||
flowchart LR | ||
r1[Root 1 Span] --> c[Child Span] | ||
c --> g1[Grandchild 1 Span] | ||
c --> g2[Grandchild 2 Span] | ||
r2[Root 2 Span] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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 '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'); | ||
|
||
// Attach the root span to the current context (the root context) making the | ||
// span the current span until it is detached. | ||
final rootToken = Context.attach( | ||
contextWithSpan(Context.current, tracer.startSpan('root-1')..end())); | ||
|
||
// Starting a child span will automatically parent the span to the span held | ||
// by the attached context. | ||
final child1 = tracer.startSpan('child')..end(); | ||
final context = contextWithSpan(Context.current, child1); | ||
|
||
// Starting a span doesn't automatically attach the span. So to make the | ||
// parent span actually parent a span, its context needs to be attached. | ||
final childToken = Context.attach(context); | ||
tracer.startSpan('grandchild-1').end(); | ||
if (!Context.detach(childToken)) { | ||
throw Exception('Failed to detach context'); | ||
} | ||
|
||
// Alternatively, manually specifying the desired parent context avoids the | ||
// need to attach and detach the context. | ||
tracer.startSpan('grandchild-2', context: context).end(); | ||
|
||
if (!Context.detach(rootToken)) { | ||
throw Exception('Failed to detach context'); | ||
} | ||
|
||
// Since the previous root span context was detached, spans will no longer be | ||
// automatically parented. | ||
tracer.startSpan('root-2').end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Stream Context Propagation Example | ||
|
||
This example demonstrates context propagation over a `Stream` or `StreamController`. | ||
|
||
The example produces three traces represented by the following diagram: | ||
|
||
```mermaid | ||
flowchart LR | ||
subgraph a[Zone A] | ||
direction LR | ||
ap[Zone A Parent Span] --> ac[Zone A Child Span] | ||
r[New Root Span] | ||
ec[Event Child Span] | ||
end | ||
ep[Event Parent Span] --> ec | ||
``` | ||
|
||
Note: When registering a stream listener, A stream listener callback is executed in the Zone where the callback was registered. This can lead to long running traces if the stream gets an event long after it was registered. To avoid this issue, the `trace` and `traceSync` functions will attach and detach the span they create before and after invoking the callback given to them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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; | ||
|
||
mixin EventContext { | ||
final Context context = Context.current; | ||
} | ||
|
||
class MyEvent with EventContext { | ||
MyEvent(); | ||
} | ||
|
||
void main() async { | ||
final tp = TracerProviderBase( | ||
processors: [SimpleSpanProcessor(ConsoleExporter())]), | ||
tracer = tp.getTracer('instrumentation-name'); | ||
|
||
final controller = StreamController<MyEvent>(); | ||
|
||
traceSync('zone-a-parent', () { | ||
tracer.startSpan('zone-a-child').end(); | ||
|
||
controller.stream.listen((e) { | ||
tracer.startSpan('new-root').end(); | ||
tracer.startSpan('event-child', context: e.context).end(); | ||
}); | ||
}, tracer: tracer); | ||
|
||
traceSync('event-parent', () => controller.add(MyEvent()), tracer: tracer); | ||
|
||
await controller.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.