Skip to content

Commit

Permalink
feat: change the sampler's signature for better decisions
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Jul 2, 2024
1 parent 0db064f commit efa5bdd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { span_buffer, wait_promises } from './_internal';
export { configure, report } from './_internal';

type API = {
sampler: Sampler | boolean;
sampler: typeof Sampler | boolean;
scope: { name: string };
clock: ClockLike;
};
Expand Down Expand Up @@ -40,8 +40,9 @@ export function span(name: string, parent_id?: Traceparent | string) {
const parent = (typeof parent_id === 'string' ? tctx.parse(parent_id) : (parent_id || current_span?.traceparent));
const id = parent?.child() || tctx.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) tctx.sample(id);
else tctx.unsample(id);

const span_obj: Span = {
id, parent, name,
Expand Down
18 changes: 11 additions & 7 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type Options = {
/**
* @borrows {@link Sampler}
*/
sampler?: Sampler | boolean;
sampler?: typeof Sampler | boolean;

clock?: ClockLike;
};
Expand All @@ -47,20 +47,24 @@ export type Context = {
* Return true if the span should be sampled, and reported to the {@link Exporter}.
* Return false if the span should not be sampled, and not reported to the {@link Exporter}.
*/
export type Sampler = (
export function Sampler(
/**
* The name of the span.
* The id of the new span looking for a sampling decision.
*/
id: string,
/**
* The traceparent 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.
*/
tracer: { readonly name: string },
) => boolean;
): boolean;

// --- spans

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export function tracer(name: string, options?: Options): Tracer {
const parent = (typeof parent_id === 'string' ? tctx.parse(parent_id) : parent_id);
const id = parent?.child() || tctx.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) tctx.sample(id);
else tctx.unsample(id);

const span_obj: Span = {
id, parent, name,
Expand Down

0 comments on commit efa5bdd

Please sign in to comment.