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

fix: reading sampling priority will error when not available #483

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
34 changes: 33 additions & 1 deletion src/trace/span-context-wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SpanContextWrapper } from "./span-context-wrapper";
import { TraceSource } from "./trace-context-service";
import { SampleMode, TraceSource } from "./trace-context-service";

describe("SpanContextWrapper", () => {
beforeEach(() => {});
Expand All @@ -20,4 +20,36 @@ describe("SpanContextWrapper", () => {
expect(spanContext?.spanContext._traceId.toArray()).toEqual([121, 177, 18, 140, 107, 104, 185, 240]);
expect(spanContext?.spanContext._spanId.toArray()).toEqual([96, 4, 217, 174, 182, 29, 220, 172]);
});

describe("sampleMode", () => {
it("should return AUTO_KEEP when sampling priority is not available in spanContext", () => {
const spanContext = new SpanContextWrapper(
{
toSpanId: () => "1234",
toTraceId: () => "5678",
_sampling: {},
},
TraceSource.Event,
);

const sampleMode = spanContext.sampleMode();
expect(sampleMode).toBe(SampleMode.AUTO_KEEP);
expect(sampleMode.toString()).toBe("1");
});

it("should return sampling priority when available in spanContext", () => {
const spanContext = new SpanContextWrapper(
{
toSpanId: () => "1234",
toTraceId: () => "5678",
_sampling: { priority: 2 },
},
TraceSource.Event,
);

const sampleMode = spanContext.sampleMode();
expect(sampleMode).toBe(2);
expect(sampleMode.toString()).toBe("2");
});
});
});
4 changes: 2 additions & 2 deletions src/trace/span-context-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logDebug } from "../utils";
import { TraceContext, TraceSource } from "./trace-context-service";
import { SampleMode, TraceContext, TraceSource } from "./trace-context-service";
import { SpanContext } from "./tracer-wrapper";

/**
Expand All @@ -18,7 +18,7 @@ export class SpanContextWrapper implements SpanContext {
}

public sampleMode(): number {
return this.spanContext._sampling?.priority;
return this.spanContext._sampling?.priority ?? SampleMode.AUTO_KEEP;
}

public toString = (): string => {
Expand Down
Loading