Skip to content

Commit

Permalink
test: writes some tests for the sampler prop
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Jul 3, 2024
1 parent 7c78208 commit d8b82bd
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/rian.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as assert from 'uvu/assert';

import * as rian from 'rian';
import * as utils from 'rian/utils';
import { traceparent } from 'tctx';

const noop = () => {};

Expand Down Expand Up @@ -229,9 +230,71 @@ buffer('flush all', async () => {
assert.equal(spans[1].name, 'span 2');
});

const sampler = suite('sampler');

sampler('should allow a sampler', async () => {
let should_sample = false;

const tracer = rian.tracer('test', {
sampler: () => should_sample,
});

tracer.span('not sampled')(() => { });
should_sample = true;
tracer.span('sampled')(() => { });

const exporter = spy<rian.Exporter>(returns);
const scopedSpans: rian.ScopedSpans[] = await rian.report(exporter);

assert.equal(scopedSpans.length, 1);
assert.equal(scopedSpans.at(0)!.spans.length, 1);
assert.equal(scopedSpans.at(0)?.spans.at(0)?.name, 'sampled');
});

sampler('allow a sampler to make a decision from its parent', async () => {
let no_parent_sample = true;

const S: rian.Sampler = (_id, parentId) => {
if (!parentId) return no_parent_sample;
return traceparent.is_sampled(parentId);
}

const tracer = rian.tracer('test', { sampler: S });

tracer.span('sampled#1')((s) => {
no_parent_sample = false;
s.span('sampled#1.1')(() => { })
});

no_parent_sample = false;
tracer.span('not sampled#1')((s) => {
s.span('not sampled#1.1')(() => { })
});

no_parent_sample = true;
tracer.span('sampled#2')((s) => {
s.span('sampled#2.1')(() => { })
});

no_parent_sample = false;

const exporter = spy<rian.Exporter>(returns);
const scopedSpans: rian.ScopedSpans[] = await rian.report(exporter);

assert.equal(scopedSpans.length, 1);
assert.equal(scopedSpans.at(0)!.spans.length, 4);
assert.equal(scopedSpans.at(0)!.spans.map(s => s.name), [
'sampled#1',
'sampled#1.1',
'sampled#2',
'sampled#2.1',
]);
});

test.run();
fn.run();
measure.run();
sampled.run();
events.run();
buffer.run();
sampler.run();

0 comments on commit d8b82bd

Please sign in to comment.