Skip to content
Open
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {
recordSpanError,
setSpanAttributesSafe,
spanErrorAttributes,
withSpan,
type WithSpanOptions,
Expand Down
40 changes: 40 additions & 0 deletions src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SpanStatusCode,
trace,
type Attributes,
type AttributeValue,
type Span,
type SpanOptions,
type Tracer,
Expand Down Expand Up @@ -42,12 +43,51 @@ export function recordSpanError(span: Span, err: unknown): void {
span.setAttributes(spanErrorAttributes(err));
}

export function setSpanAttributesSafe(
span: Span,
attributes: Record<string, unknown>,
): number {
let count = 0;

for (const [key, value] of Object.entries(attributes)) {
if (key.length === 0 || !isAttributeValue(value)) continue;
span.setAttribute(key, value);
count += 1;
Comment thread
superlog-responder[bot] marked this conversation as resolved.
}

return count;
}

export function spanErrorAttributes(err: unknown): Attributes {
return {
"error.type": errorType(err),
};
}

function isAttributeValue(value: unknown): value is AttributeValue {
if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean"
) {
return true;
}

if (!Array.isArray(value)) return false;
if (value.length === 0) return true;

const elementType = typeof value[0];
if (
elementType !== "string" &&
elementType !== "number" &&
elementType !== "boolean"
) {
return false;
}

return value.every((item) => typeof item === elementType);
}

function toException(err: unknown): Error {
if (err instanceof Error) return err;
return new Error(typeof err === "string" ? err : JSON.stringify(err));
Expand Down
45 changes: 44 additions & 1 deletion test/span.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { SpanStatusCode } from "@opentelemetry/api";
import { describe, expect, it, vi } from "vitest";
import { recordSpanError, spanErrorAttributes, withSpan } from "../src/index.js";
import {
recordSpanError,
setSpanAttributesSafe,
spanErrorAttributes,
withSpan,
} from "../src/index.js";

describe("span helpers", () => {
it("records low-cardinality error.type", () => {
Expand All @@ -26,6 +31,44 @@ describe("span helpers", () => {
expect(span.setAttributes).toHaveBeenCalledWith({ "error.type": "Error" });
});

it("sets valid span attributes and returns the number set", () => {
const span = {
setAttribute: vi.fn(),
};

const count = setSpanAttributesSafe(span as never, {
string: "value",
number: 42,
boolean: true,
strings: ["one", "two"],
numbers: [1, 2],
booleans: [true, false],
});

expect(count).toBe(6);
expect(span.setAttribute).toHaveBeenCalledTimes(6);
expect(span.setAttribute).toHaveBeenCalledWith("string", "value");
expect(span.setAttribute).toHaveBeenCalledWith("numbers", [1, 2]);
});

it("drops empty keys, null, undefined, and invalid attribute values", () => {
const span = {
setAttribute: vi.fn(),
};

const count = setSpanAttributesSafe(span as never, {
"": "invalid",
null: null,
undefined,
object: { value: "invalid" },
function: () => "invalid",
mixed: ["one", 2],
});

expect(count).toBe(0);
expect(span.setAttribute).not.toHaveBeenCalled();
});

it("runs callback using a supplied tracer and ends spans", async () => {
const span = {
end: vi.fn(),
Expand Down
Loading