Skip to content

Commit

Permalink
Merge pull request #48 from michaelyeager-wf/myeager-wf/O11Y-1709
Browse files Browse the repository at this point in the history
O11Y-1709: Move Sampler to SDK
  • Loading branch information
rmconsole2-wf authored Apr 18, 2022
2 parents a3b0085 + e1839fa commit ccd15bb
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 53 deletions.
2 changes: 0 additions & 2 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export 'src/api/resource/resource.dart' show Resource;
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/sampler.dart' show Sampler;
export 'src/api/trace/sampling_result.dart' show Decision, SamplingResult;
export 'src/api/trace/span.dart' show Span, SpanKind;
export 'src/api/trace/span_context.dart' show SpanContext;
export 'src/api/trace/span_id.dart' show SpanId;
Expand Down
10 changes: 6 additions & 4 deletions lib/sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ 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/samplers/always_off_sampler.dart' show AlwaysOffSampler;
export 'src/sdk/trace/samplers/always_on_sampler.dart' show AlwaysOnSampler;
export 'src/sdk/trace/samplers/parent_based_sampler.dart'
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/samplers/sampling_result.dart' show SamplingResult;
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.dart' show Span;
export 'src/sdk/trace/span_context.dart' show SpanContext;
export 'src/sdk/trace/span_processors/batch_processor.dart'
Expand Down
13 changes: 0 additions & 13 deletions lib/src/sdk/trace/samplers/sampling_result.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import '../../../../api.dart' as api;
import '../../../../sdk.dart' as sdk;

class AlwaysOffSampler implements api.Sampler {
class AlwaysOffSampler implements sdk.Sampler {
@override
String get description => 'AlwaysOffSampler';

@override
api.SamplingResult shouldSample(
sdk.SamplingResult shouldSample(
api.Context context,
api.TraceId traceId,
String spanName,
api.SpanKind spanKind,
bool spanIsRemote,
List<api.Attribute> spanAttributes,
List<api.SpanLink> links) {
return sdk.SamplingResult(api.Decision.drop, spanAttributes,
return sdk.SamplingResult(sdk.Decision.drop, spanAttributes,
context.spanContext?.traceState ?? sdk.TraceState.empty());
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import '../../../../api.dart' as api;
import '../../../../sdk.dart' as sdk;

class AlwaysOnSampler implements api.Sampler {
class AlwaysOnSampler implements sdk.Sampler {
@override
String get description => 'AlwaysOnSampler';

@override
api.SamplingResult shouldSample(
sdk.SamplingResult shouldSample(
api.Context context,
api.TraceId traceId,
String spanName,
api.SpanKind spanKind,
bool spanIsRemote,
List<api.Attribute> spanAttributes,
List<api.SpanLink> spanLinks) {
return sdk.SamplingResult(api.Decision.recordAndSample, spanAttributes,
return sdk.SamplingResult(sdk.Decision.recordAndSample, spanAttributes,
context.spanContext?.traceState ?? sdk.TraceState.empty());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import '../../../../api.dart' as api;
import '../../../../sdk.dart' as sdk;

class ParentBasedSampler implements api.Sampler {
final api.Sampler _root;
final api.Sampler _remoteParentSampled;
final api.Sampler _remoteParentNotSampled;
final api.Sampler _localParentSampled;
final api.Sampler _localParentNotSampled;
class ParentBasedSampler implements sdk.Sampler {
final sdk.Sampler _root;
final sdk.Sampler _remoteParentSampled;
final sdk.Sampler _remoteParentNotSampled;
final sdk.Sampler _localParentSampled;
final sdk.Sampler _localParentNotSampled;

ParentBasedSampler(this._root,
{remoteParentSampled,
Expand All @@ -24,7 +24,7 @@ class ParentBasedSampler implements api.Sampler {
String get description => 'ParentBasedSampler{root=${_root.description}}';

@override
api.SamplingResult shouldSample(
sdk.SamplingResult shouldSample(
api.Context context,
api.TraceId traceId,
String spanName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import '../../../api.dart' as api;
import '../../../../api.dart' as api;
import '../../../../sdk.dart' as sdk;

/// Represents an entity which determines whether a [api.Span] should be sampled
/// and sent for collection.
abstract class Sampler {
api.SamplingResult shouldSample(
sdk.SamplingResult shouldSample(
api.Context context,
api.TraceId traceId,
String spanName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import '../../../api.dart' as api;
import '../../../../api.dart' as api;

enum Decision {
drop,
recordOnly,
recordAndSample,
}

/// Represents the result of a Sampler as to whether a Span should
/// be processed for collection.
abstract class SamplingResult {
final api.Decision decision;
class SamplingResult {
final Decision decision;
final List<api.Attribute> spanAttributes;
final api.TraceState traceState;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/sdk/trace/tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../../../sdk.dart' as sdk;
class Tracer implements api.Tracer {
final List<api.SpanProcessor> _processors;
final api.Resource _resource;
final api.Sampler _sampler;
final sdk.Sampler _sampler;
final api.IdGenerator _idGenerator;
final api.InstrumentationLibrary _instrumentationLibrary;
sdk.SpanLimits _spanLimits;
Expand Down Expand Up @@ -49,7 +49,7 @@ class Tracer implements api.Tracer {

final samplerResult = _sampler.shouldSample(
context, traceId, name, kind, false, attributes, links);
final traceFlags = (samplerResult.decision == api.Decision.recordAndSample)
final traceFlags = (samplerResult.decision == sdk.Decision.recordAndSample)
? api.TraceFlags.sampled
: api.TraceFlags.none;
final spanContext =
Expand Down
4 changes: 2 additions & 2 deletions lib/src/sdk/trace/tracer_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class TracerProvider implements api.TracerProvider {
final Map<String, api.Tracer> _tracers = {};
List<api.SpanProcessor> _processors;
api.Resource _resource;
api.Sampler _sampler;
sdk.Sampler _sampler;
api.IdGenerator _idGenerator;
sdk.SpanLimits _spanLimits;

TracerProvider(
{List<api.SpanProcessor> processors,
api.Resource resource,
api.Sampler sampler,
sdk.Sampler sampler,
api.IdGenerator idGenerator,
sdk.SpanLimits spanLimits}) {
_processors = processors ?? []; // Default to a no-op TracerProvider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
final result = sdk.AlwaysOffSampler().shouldSample(testContext, traceId,
testSpan.name, api.SpanKind.internal, false, [], []);

expect(result.decision, equals(api.Decision.drop));
expect(result.decision, equals(sdk.Decision.drop));
expect(result.spanAttributes, equals([]));
expect(result.traceState, same(traceState));
});
Expand Down Expand Up @@ -49,7 +49,7 @@ void main() {
false,
attributesList, []);

expect(result.decision, equals(api.Decision.drop));
expect(result.decision, equals(sdk.Decision.drop));
expect(result.spanAttributes, attributesList);
expect(result.traceState.isEmpty, isTrue);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
final result = sdk.AlwaysOnSampler().shouldSample(testContext, traceId,
testSpan.name, api.SpanKind.internal, false, [], []);

expect(result.decision, equals(api.Decision.recordAndSample));
expect(result.decision, equals(sdk.Decision.recordAndSample));
expect(result.spanAttributes, equals([]));
expect(result.traceState, same(traceState));
});
Expand All @@ -39,7 +39,7 @@ void main() {
final result = sdk.AlwaysOnSampler().shouldSample(api.Context.root, traceId,
testSpan.name, api.SpanKind.internal, false, [], []);

expect(result.decision, equals(api.Decision.recordAndSample));
expect(result.decision, equals(sdk.Decision.recordAndSample));
expect(result.spanAttributes, equals([]));
expect(result.traceState.isEmpty, isTrue);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
final result = testSampler.shouldSample(testContext, traceId, testSpan.name,
api.SpanKind.internal, false, null, []);

expect(result.decision, equals(api.Decision.recordAndSample));
expect(result.decision, equals(sdk.Decision.recordAndSample));
expect(result.spanAttributes, equals(null));
expect(result.traceState.isEmpty, isTrue);
});
Expand All @@ -45,7 +45,7 @@ void main() {
final result = testSampler.shouldSample(api.Context.root, traceId,
testSpan.name, api.SpanKind.internal, false, null, []);

expect(result.decision, equals(api.Decision.recordAndSample));
expect(result.decision, equals(sdk.Decision.recordAndSample));
expect(result.spanAttributes, equals(null));
expect(result.traceState.isEmpty, isTrue);
});
Expand All @@ -67,7 +67,7 @@ void main() {
final result = testSampler.shouldSample(testContext, traceId, testSpan.name,
api.SpanKind.internal, false, null, []);

expect(result.decision, equals(api.Decision.recordAndSample));
expect(result.decision, equals(sdk.Decision.recordAndSample));
expect(result.spanAttributes, equals(null));
expect(result.traceState, same(traceState));
});
Expand All @@ -89,7 +89,7 @@ void main() {
final result = testSampler.shouldSample(testContext, traceId, testSpan.name,
api.SpanKind.internal, false, [], []);

expect(result.decision, equals(api.Decision.drop));
expect(result.decision, equals(sdk.Decision.drop));
expect(result.spanAttributes, equals([]));
expect(result.traceState, same(traceState));
});
Expand All @@ -111,7 +111,7 @@ void main() {
final result = testSampler.shouldSample(testContext, traceId, testSpan.name,
api.SpanKind.internal, false, [], []);

expect(result.decision, equals(api.Decision.recordAndSample));
expect(result.decision, equals(sdk.Decision.recordAndSample));
expect(result.spanAttributes, equals([]));
expect(result.traceState, same(traceState));
});
Expand All @@ -133,7 +133,7 @@ void main() {
final result = testSampler.shouldSample(testContext, traceId, testSpan.name,
api.SpanKind.internal, false, [], []);

expect(result.decision, equals(api.Decision.drop));
expect(result.decision, equals(sdk.Decision.drop));
expect(result.spanAttributes, equals([]));
expect(result.traceState, same(traceState));
});
Expand Down

0 comments on commit ccd15bb

Please sign in to comment.