-
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.
add sampling support to span processors
- Loading branch information
1 parent
4b6145a
commit b4dd92b
Showing
13 changed files
with
358 additions
and
149 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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:collection/collection.dart'; | ||
import 'package:opentelemetry/api.dart' | ||
show | ||
Attribute, | ||
Context, | ||
SpanKind, | ||
SpanLink, | ||
TraceId, | ||
registerGlobalTracerProvider, | ||
spanContextFromContext; | ||
import 'package:opentelemetry/sdk.dart' | ||
show | ||
ConsoleExporter, | ||
Decision, | ||
ParentBasedSampler, | ||
ReadOnlySpan, | ||
ReadWriteSpan, | ||
Sampler, | ||
SamplingResult, | ||
SimpleSpanProcessor, | ||
TracerProviderBase; | ||
|
||
final Attribute samplingOffAttribute = | ||
Attribute.fromInt('sampling.priority', 0); | ||
|
||
class SpanSamplingPrioritySampler implements Sampler { | ||
@override | ||
SamplingResult shouldSample( | ||
Context parentContext, | ||
TraceId traceId, | ||
String name, | ||
SpanKind spanKind, | ||
List<Attribute> attributes, | ||
List<SpanLink> links) { | ||
final decision = attributes.firstWhereOrNull((element) => | ||
element.key == 'sampling.priority' && element.value == 0) != | ||
null | ||
? Decision.recordOnly | ||
: Decision.recordAndSample; | ||
|
||
return SamplingResult( | ||
decision, attributes, spanContextFromContext(parentContext).traceState); | ||
} | ||
|
||
@override | ||
String get description => 'SpanSamplingPrioritySampler'; | ||
} | ||
|
||
class PrintingSpanProcessor extends SimpleSpanProcessor { | ||
PrintingSpanProcessor(super.exporter); | ||
|
||
@override | ||
void onStart(ReadWriteSpan span, Context parentContext) { | ||
print('Span started: ${span.name}'); | ||
super.onStart(span, parentContext); | ||
} | ||
|
||
@override | ||
void onEnd(ReadOnlySpan span) { | ||
print('Span ended: ${span.name}'); | ||
super.onEnd(span); | ||
} | ||
|
||
@override | ||
void shutdown() { | ||
print('Shutting down'); | ||
super.shutdown(); | ||
} | ||
|
||
@override | ||
void forceFlush() {} | ||
} | ||
|
||
void main(List<String> args) async { | ||
final sampler = ParentBasedSampler(SpanSamplingPrioritySampler()); | ||
final tp = TracerProviderBase( | ||
processors: [PrintingSpanProcessor(ConsoleExporter())], sampler: sampler); | ||
registerGlobalTracerProvider(tp); | ||
|
||
final tracer = tp.getTracer('instrumentation-name'); | ||
|
||
tracer.startSpan('span-not-sampled', attributes: [ | ||
samplingOffAttribute, | ||
]).end(); | ||
tracer.startSpan('span-sampled').end(); | ||
|
||
tp.shutdown(); | ||
} |
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
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
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.