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

feat: change the sampler's signature for better decisions #6

Merged
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"prettier": "@marais/prettier",
"dependencies": {
"flattie": "^1.1.1",
"tctx": "^0.1.0"
"tctx": "^0.2.3"
},
"devDependencies": {
"@marais/prettier": "0.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AsyncLocalStorage } from 'node:async_hooks';
import { suite, test } from 'uvu';
import * as assert from 'uvu/assert';

import { make } from 'tctx';
import { make } from 'tctx/traceparent';

import type { Exporter } from 'rian';
import * as rian from 'rian/async';
Expand Down
13 changes: 7 additions & 6 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { CallableScope, ClockLike, Options, Sampler, Scope, Span } from 'ri
import type { Tracer } from 'rian/async';
import { measure } from 'rian/utils';

import { type Traceparent } from 'tctx';
import * as tctx from 'tctx';
import { type Traceparent } from 'tctx/traceparent';
import * as traceparent from 'tctx/traceparent';

import { span_buffer, wait_promises } from './_internal';

Expand Down Expand Up @@ -37,11 +37,12 @@ export function span(name: string, parent_id?: Traceparent | string) {
const should_sample = api.sampler;

// ---
const parent = (typeof parent_id === 'string' ? tctx.parse(parent_id) : (parent_id || current_span?.traceparent));
const id = parent?.child() || tctx.make();
const parent = (typeof parent_id === 'string' ? traceparent.parse(parent_id) : (parent_id || current_span?.traceparent));
const id = parent?.child() || traceparent.make();

const is_sampling = typeof should_sample == 'boolean' ? should_sample : should_sample(name, id, scope);
!is_sampling ? tctx.unsample(id) : tctx.sample(id);
const is_sampling = typeof should_sample == 'boolean' ? should_sample : should_sample(id.parent_id, parent, name, scope);
if (is_sampling) traceparent.sample(id);
else traceparent.unsample(id);

const span_obj: Span = {
id, parent, name,
Expand Down
12 changes: 8 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ export type Context = {
*/
export type Sampler = (
/**
* The name of the span.
* The id of the new span looking for a sampling decision.
*/
id: string,
/**
* The parent id of the new span looking for a sampling decision.
*/
name: string,
parent: Traceparent | undefined,
/**
* The traceparent id of the span.
* The name of the span.
*/
id: Traceparent,
name: string,
/**
* The tracer this span belongs to.
*/
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { CallableScope, Options, Span, Tracer } from 'rian';
import { measure } from 'rian/utils';
import { span_buffer, wait_promises } from './_internal';

import { type Traceparent } from 'tctx';
import * as tctx from 'tctx';
import { type Traceparent } from 'tctx/traceparent';
import * as traceparent from 'tctx/traceparent';

export { report, configure } from './_internal';

Expand All @@ -21,11 +21,12 @@ export function tracer(name: string, options?: Options): Tracer {
parent_id?: Traceparent | string,
): CallableScope => {
// ---
const parent = (typeof parent_id === 'string' ? tctx.parse(parent_id) : parent_id);
const id = parent?.child() || tctx.make();
const parent = (typeof parent_id === 'string' ? traceparent.parse(parent_id) : parent_id);
const id = parent?.child() || traceparent.make();

const is_sampling = typeof should_sample == 'boolean' ? should_sample : should_sample(name, id, scope);
!is_sampling ? tctx.unsample(id) : tctx.sample(id);
const is_sampling = typeof should_sample == 'boolean' ? should_sample : should_sample(id.parent_id, parent, name, scope);
if (is_sampling) traceparent.sample(id);
else traceparent.unsample(id);

const span_obj: Span = {
id, parent, name,
Expand Down
65 changes: 64 additions & 1 deletion src/rian.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { spy, spyOn } from 'nanospy';
import { is_sampled } from 'tctx';
import { is_sampled } from 'tctx/traceparent';
import { suite, test } from 'uvu';
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();