@dash0/sdk-web 0.17.0 → 0.18.1
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/README.md +41 -25
- 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/api/events.js +3 -2
- package/dist/modules/api/init.js +6 -2
- package/dist/modules/api/init_test.js +63 -0
- package/dist/modules/instrumentations/errors/unhandled-error.js +5 -3
- package/dist/modules/utils/otel/attributes.js +23 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/types/errors.d.ts +4 -3
- package/dist/types/utils/otel/attributes.d.ts +19 -0
- package/package.json +1 -1
- package/src/api/events.ts +6 -2
- package/src/api/init.ts +7 -1
- package/src/api/init_test.ts +77 -0
- package/src/instrumentations/errors/unhandled-error.ts +9 -3
- package/src/types/errors.ts +4 -3
- package/src/utils/otel/attributes.ts +24 -0
package/src/api/init_test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { InitOptions, InstrumentationName } from "../types/options";
|
|
3
3
|
import { PropagatorConfig, Vars } from "../vars";
|
|
4
|
+
import { SERVICE_NAME } from "../semantic-conventions";
|
|
4
5
|
import { init as initFun } from "./init";
|
|
5
6
|
|
|
6
7
|
// Mock all the instrumentation modules
|
|
@@ -20,6 +21,15 @@ vi.mock("../instrumentations/navigation", () => ({
|
|
|
20
21
|
startNavigationInstrumentation: vi.fn(),
|
|
21
22
|
}));
|
|
22
23
|
|
|
24
|
+
// Mock the utils module to control loc.hostname
|
|
25
|
+
vi.mock("../utils", async () => {
|
|
26
|
+
const actual = await vi.importActual("../utils");
|
|
27
|
+
return {
|
|
28
|
+
...actual,
|
|
29
|
+
loc: { hostname: "test-hostname.example.com" },
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
|
|
23
33
|
import { startErrorInstrumentation } from "../instrumentations/errors";
|
|
24
34
|
import { instrumentFetch } from "../instrumentations/http/fetch";
|
|
25
35
|
import { startNavigationInstrumentation } from "../instrumentations/navigation";
|
|
@@ -226,4 +236,71 @@ describe("init", () => {
|
|
|
226
236
|
expect(spyOnWarn).not.toHaveBeenCalled();
|
|
227
237
|
});
|
|
228
238
|
});
|
|
239
|
+
|
|
240
|
+
describe("serviceName fallback", () => {
|
|
241
|
+
it("should use provided serviceName when it is valid", async () => {
|
|
242
|
+
init({
|
|
243
|
+
...baseOptions,
|
|
244
|
+
serviceName: "my-service",
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
248
|
+
expect(serviceNameAttr?.value.stringValue).toBe("my-service");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("should fallback to location.hostname when serviceName is empty string", async () => {
|
|
252
|
+
init({
|
|
253
|
+
...baseOptions,
|
|
254
|
+
serviceName: "",
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
258
|
+
expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("should fallback to location.hostname when serviceName is only whitespace", async () => {
|
|
262
|
+
init({
|
|
263
|
+
...baseOptions,
|
|
264
|
+
serviceName: " ",
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
268
|
+
expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("should fallback to location.hostname when serviceName is tabs and spaces", async () => {
|
|
272
|
+
init({
|
|
273
|
+
...baseOptions,
|
|
274
|
+
serviceName: " \t \n ",
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
278
|
+
expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("should fallback to 'unknown' when serviceName is empty and location.hostname is not available", async () => {
|
|
282
|
+
vi.resetModules();
|
|
283
|
+
|
|
284
|
+
// Mock utils module with loc as undefined
|
|
285
|
+
vi.doMock("../utils", async () => {
|
|
286
|
+
const actual = await vi.importActual("../utils");
|
|
287
|
+
return {
|
|
288
|
+
...actual,
|
|
289
|
+
loc: undefined,
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const { vars: importedVars } = await import("../vars");
|
|
294
|
+
const { init: importedInit } = await import("./init");
|
|
295
|
+
const { SERVICE_NAME: importedServiceName } = await import("../semantic-conventions");
|
|
296
|
+
|
|
297
|
+
importedInit({
|
|
298
|
+
...baseOptions,
|
|
299
|
+
serviceName: "",
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const serviceNameAttr = importedVars.resource.attributes.find((attr) => attr.key === importedServiceName);
|
|
303
|
+
expect(serviceNameAttr?.value.stringValue).toBe("unknown");
|
|
304
|
+
});
|
|
305
|
+
});
|
|
229
306
|
});
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
EXCEPTION_TYPE,
|
|
11
11
|
LOG_SEVERITIES,
|
|
12
12
|
} from "../../semantic-conventions";
|
|
13
|
-
import { addAttribute } from "../../utils/otel";
|
|
13
|
+
import { addAttribute, addAttributes } from "../../utils/otel";
|
|
14
14
|
import { addCommonAttributes } from "../../attributes";
|
|
15
15
|
import { KeyValue, LogRecord } from "../../types/otlp";
|
|
16
16
|
import { ReportErrorOpts, ErrorLike } from "../../types/errors";
|
|
@@ -117,9 +117,13 @@ function onUnhandledError({ message, type, stack, opts }: UnhandledErrorArgs) {
|
|
|
117
117
|
if (trackedError) {
|
|
118
118
|
trackedError.seenCount++;
|
|
119
119
|
} else {
|
|
120
|
-
const attributes: KeyValue[] =
|
|
120
|
+
const attributes: KeyValue[] = [];
|
|
121
|
+
|
|
122
|
+
addCommonAttributes(attributes);
|
|
123
|
+
|
|
121
124
|
addAttribute(attributes, EVENT_NAME, EVENT_NAMES.ERROR);
|
|
122
125
|
addAttribute(attributes, EXCEPTION_MESSAGE, message);
|
|
126
|
+
|
|
123
127
|
if (type) {
|
|
124
128
|
addAttribute(attributes, EXCEPTION_TYPE, type);
|
|
125
129
|
}
|
|
@@ -129,7 +133,9 @@ function onUnhandledError({ message, type, stack, opts }: UnhandledErrorArgs) {
|
|
|
129
133
|
if (opts?.componentStack) {
|
|
130
134
|
addAttribute(attributes, EXCEPTION_COMPONENT_STACK, opts?.componentStack.substring(0, 2048));
|
|
131
135
|
}
|
|
132
|
-
|
|
136
|
+
|
|
137
|
+
// Add custom attributes last to allow overrides
|
|
138
|
+
addAttributes(attributes, opts?.attributes);
|
|
133
139
|
|
|
134
140
|
trackedError = {
|
|
135
141
|
seenCount: 1,
|
package/src/types/errors.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AttributeValueType } from "../utils/otel";
|
|
2
|
+
import { AnyValue } from "./otlp";
|
|
2
3
|
|
|
3
4
|
export type ErrorLike = {
|
|
4
5
|
message: string;
|
|
@@ -7,9 +8,9 @@ export type ErrorLike = {
|
|
|
7
8
|
};
|
|
8
9
|
|
|
9
10
|
export type ReportErrorOpts = {
|
|
10
|
-
componentStack
|
|
11
|
+
componentStack?: string | null | undefined;
|
|
11
12
|
/**
|
|
12
13
|
* Additional attributes to add to the error span
|
|
13
14
|
*/
|
|
14
|
-
attributes?:
|
|
15
|
+
attributes?: Record<string, AttributeValueType | AnyValue>;
|
|
15
16
|
};
|
|
@@ -55,12 +55,36 @@ export function toKeyValue(key: string, value: AttributeValueType | AnyValue): K
|
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Adds a single attribute to the provided attributes array
|
|
60
|
+
*/
|
|
58
61
|
export function addAttribute(attributes: KeyValue[], key: string, value: AttributeValueType | AnyValue) {
|
|
59
62
|
if (!key) return;
|
|
60
63
|
|
|
61
64
|
attributes.push(toKeyValue(key, value));
|
|
62
65
|
}
|
|
63
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Adds multiple attributes to the provided attributes array
|
|
69
|
+
*/
|
|
70
|
+
export function addAttributes(
|
|
71
|
+
/**
|
|
72
|
+
* The attributes array to add to
|
|
73
|
+
*/
|
|
74
|
+
attributes: KeyValue[],
|
|
75
|
+
/**
|
|
76
|
+
* Additional attributes to add
|
|
77
|
+
* If no attributes are provided, nothing is added
|
|
78
|
+
*/
|
|
79
|
+
attrs: Record<string, AttributeValueType | AnyValue> | undefined
|
|
80
|
+
) {
|
|
81
|
+
if (!attrs) return;
|
|
82
|
+
Object.entries(attrs).forEach(([key, value]) => addAttribute(attributes, key, value));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Removes an attribute from the provided attributes array by its key
|
|
87
|
+
*/
|
|
64
88
|
export function removeAttribute(attributes: KeyValue[], key: string) {
|
|
65
89
|
const index = attributes.findIndex((attr) => attr["key"] === key);
|
|
66
90
|
if (index !== -1) {
|