@dash0/sdk-web 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dash0.iife.js +1 -1
- package/dist/dash0.iife.js.map +1 -1
- package/dist/dash0.js +1 -1
- package/dist/dash0.js.map +1 -1
- package/dist/dash0.umd.cjs +1 -1
- package/dist/dash0.umd.cjs.map +1 -1
- package/dist/modules/utils/crc32.js +17 -0
- package/dist/modules/utils/otel/attributes.js +2 -0
- package/dist/modules/utils/otel/attributes_test.js +13 -1
- package/dist/modules/utils/otel/span.js +4 -3
- package/dist/modules/utils/session-id_test.js +0 -2
- package/dist/modules/utils/span-id.js +7 -0
- package/dist/modules/utils/span-id_test.js +24 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/crc32.d.ts +1 -0
- package/dist/types/utils/span-id.d.ts +1 -0
- package/dist/types/utils/span-id_test.d.ts +1 -0
- package/package.json +1 -1
- package/src/utils/crc32.ts +19 -0
- package/src/utils/otel/attributes.ts +2 -0
- package/src/utils/otel/attributes_test.ts +20 -2
- package/src/utils/otel/span.ts +4 -3
- package/src/utils/session-id_test.ts +0 -2
- package/src/utils/span-id.ts +9 -0
- package/src/utils/span-id_test.ts +30 -0
|
@@ -56,6 +56,8 @@ export function toKeyValue(key: string, value: AttributeValueType | AnyValue): K
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export function addAttribute(attributes: KeyValue[], key: string, value: AttributeValueType | AnyValue) {
|
|
59
|
+
if (!key) return;
|
|
60
|
+
|
|
59
61
|
attributes.push(toKeyValue(key, value));
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expect, describe, it } from "vitest";
|
|
2
|
-
import { toAnyValue } from "./attributes";
|
|
3
|
-
import { AnyValue } from "../../../types/otlp";
|
|
2
|
+
import { addAttribute, toAnyValue } from "./attributes";
|
|
3
|
+
import { AnyValue, KeyValue } from "../../../types/otlp";
|
|
4
4
|
|
|
5
5
|
describe("toAnyValue", () => {
|
|
6
6
|
describe("primitive values", () => {
|
|
@@ -178,4 +178,22 @@ describe("toAnyValue", () => {
|
|
|
178
178
|
expect(result).toBeUndefined();
|
|
179
179
|
});
|
|
180
180
|
});
|
|
181
|
+
|
|
182
|
+
describe("addAttribute", () => {
|
|
183
|
+
it("adds attributes to attribute set", () => {
|
|
184
|
+
const attributes: KeyValue[] = [];
|
|
185
|
+
|
|
186
|
+
addAttribute(attributes, "some.attribute", { stringValue: "a value" });
|
|
187
|
+
|
|
188
|
+
expect(attributes).toEqual(expect.arrayContaining([expect.objectContaining({ key: "some.attribute" })]));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("ignores attributes without key", () => {
|
|
192
|
+
const attributes: KeyValue[] = [];
|
|
193
|
+
|
|
194
|
+
addAttribute(attributes, "", { stringValue: "a value" });
|
|
195
|
+
|
|
196
|
+
expect(attributes).toHaveLength(0);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
181
199
|
});
|
package/src/utils/otel/span.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { KeyValue, Span, SpanStatus } from "../../../types/otlp";
|
|
2
2
|
import { nowNanos } from "../time";
|
|
3
|
-
import { generateUniqueId, SPAN_ID_BYTES } from "../id";
|
|
4
3
|
import { SPAN_KIND_CLIENT, SPAN_STATUS_UNSET } from "../../semantic-conventions";
|
|
5
4
|
import { sessionId } from "../../api/session";
|
|
6
5
|
import { generateTraceId } from "../trace-id";
|
|
6
|
+
import { generateSpanId } from "../span-id";
|
|
7
7
|
|
|
8
8
|
export type InProgressSpan = Omit<Span, "endTimeUnixNano">;
|
|
9
9
|
|
|
10
10
|
export function startSpan(name: string): InProgressSpan {
|
|
11
|
+
const traceId = generateTraceId(sessionId);
|
|
11
12
|
return {
|
|
12
|
-
traceId
|
|
13
|
-
spanId:
|
|
13
|
+
traceId,
|
|
14
|
+
spanId: generateSpanId(traceId),
|
|
14
15
|
name,
|
|
15
16
|
// Always CLIENT for now https://github.com/open-telemetry/opentelemetry-proto/blob/ac3242b03157295e4ee9e616af53b81517b06559/opentelemetry/proto/trace/v1/trace.proto#L143-L169
|
|
16
17
|
// Note: we directly define otlp here, this differs from the values used by oteljs internally.
|
|
@@ -12,7 +12,6 @@ describe("generateSessionId", () => {
|
|
|
12
12
|
vi.spyOn(localStorage, "isSupported", "get").mockReturnValue(true);
|
|
13
13
|
|
|
14
14
|
const sessionId = generateSessionId();
|
|
15
|
-
console.log(sessionId);
|
|
16
15
|
expect(sessionId.startsWith("00")).toBe(true);
|
|
17
16
|
});
|
|
18
17
|
|
|
@@ -20,7 +19,6 @@ describe("generateSessionId", () => {
|
|
|
20
19
|
vi.spyOn(localStorage, "isSupported", "get").mockReturnValue(false);
|
|
21
20
|
|
|
22
21
|
const sessionId = generateSessionId();
|
|
23
|
-
console.log(sessionId);
|
|
24
22
|
expect(sessionId.startsWith("01")).toBe(true);
|
|
25
23
|
});
|
|
26
24
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { generateUniqueId, SPAN_ID_BYTES } from "./id";
|
|
2
|
+
import { crc32 } from "./crc32";
|
|
3
|
+
|
|
4
|
+
export function generateSpanId(traceId: string): string {
|
|
5
|
+
const checksum = crc32(traceId);
|
|
6
|
+
const prefix = checksum.toString(16).padStart(8, "0");
|
|
7
|
+
|
|
8
|
+
return `${prefix}${generateUniqueId(SPAN_ID_BYTES - 4)}`;
|
|
9
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect } from "vitest";
|
|
2
|
+
import { generateSpanId } from "./span-id";
|
|
3
|
+
|
|
4
|
+
describe("generateSpanId", () => {
|
|
5
|
+
it("returns a span ID of the expected length", () => {
|
|
6
|
+
expect(generateSpanId("abcdef1234567890abcdef1234567890")).toHaveLength(16);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("returns the same prefix for the same trace ID", () => {
|
|
10
|
+
const traceId = "abcdef1234567890abcdef1234567890";
|
|
11
|
+
const spanId1 = generateSpanId(traceId);
|
|
12
|
+
const spanId2 = generateSpanId(traceId);
|
|
13
|
+
|
|
14
|
+
expect(spanId1.substring(0, 8)).toBe(spanId2.substring(0, 8));
|
|
15
|
+
expect(spanId1).not.equals(spanId2);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("returns different prefix for span ID for different trace IDs", () => {
|
|
19
|
+
const spanId1 = generateSpanId("abcdef1234567890abcdef1234567890");
|
|
20
|
+
const spanId2 = generateSpanId("1234567890abcdef1234567890abcdef");
|
|
21
|
+
|
|
22
|
+
expect(spanId1.substring(0, 8)).not.equals(spanId2.substring(0, 8));
|
|
23
|
+
expect(spanId1).not.equals(spanId2);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("return a span id with the correct prefix", () => {
|
|
27
|
+
expect(generateSpanId("abcdef1234567890abcdef1234567890").substring(0, 8)).toEqual("3d5a507a");
|
|
28
|
+
expect(generateSpanId("1234567890abcdef1234567890abcdef").substring(0, 8)).toEqual("c24261eb");
|
|
29
|
+
});
|
|
30
|
+
});
|