@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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EVENT_NAME, EVENT_NAMES, WEB_EVENT_TITLE, LOG_SEVERITIES } from "../semantic-conventions";
|
|
2
2
|
import { sendLog } from "../transport";
|
|
3
3
|
import { addCommonAttributes } from "../attributes";
|
|
4
|
-
import { addAttribute, toAnyValue } from "../utils/otel";
|
|
4
|
+
import { addAttribute, addAttributes, toAnyValue } from "../utils/otel";
|
|
5
5
|
import { nowNanos, toNanosTs, warn } from "../utils";
|
|
6
6
|
/**
|
|
7
7
|
* Sends a custom event.
|
|
@@ -15,11 +15,12 @@ export function sendEvent(name, opts) {
|
|
|
15
15
|
}
|
|
16
16
|
const attributes = [];
|
|
17
17
|
addCommonAttributes(attributes);
|
|
18
|
-
Object.entries(opts?.attributes ?? {}).forEach(([key, value]) => addAttribute(attributes, key, value));
|
|
19
18
|
addAttribute(attributes, EVENT_NAME, name);
|
|
20
19
|
if (opts?.title) {
|
|
21
20
|
addAttribute(attributes, WEB_EVENT_TITLE, opts.title);
|
|
22
21
|
}
|
|
22
|
+
// Add custom attributes last to allow overrides
|
|
23
|
+
addAttributes(attributes, opts?.attributes);
|
|
23
24
|
sendLog({
|
|
24
25
|
timeUnixNano: opts?.timestamp != null ? toNanosTs(opts.timestamp) : nowNanos(),
|
|
25
26
|
attributes,
|
package/dist/modules/api/init.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { vars } from "../vars";
|
|
2
2
|
import { DEPLOYMENT_ENVIRONMENT_NAME, DEPLOYMENT_ID, DEPLOYMENT_NAME, PAGE_LOAD_ID, SERVICE_NAME, SERVICE_VERSION, USER_AGENT, } from "../semantic-conventions";
|
|
3
|
-
import { fetch, generateUniqueId, PAGE_LOAD_ID_BYTES, warn, debug, perf, nav, win, NO_VALUE_FALLBACK, pick, } from "../utils";
|
|
3
|
+
import { fetch, generateUniqueId, PAGE_LOAD_ID_BYTES, warn, debug, perf, nav, win, NO_VALUE_FALLBACK, pick, loc, } from "../utils";
|
|
4
4
|
import { trackSessions } from "./session";
|
|
5
5
|
import { startWebVitalsInstrumentation } from "../instrumentations/web-vitals";
|
|
6
6
|
import { startErrorInstrumentation } from "../instrumentations/errors";
|
|
@@ -20,9 +20,13 @@ export function init(opts) {
|
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
if (!isSupported()) {
|
|
23
|
-
debug("Stopping Dash0 Web SDK initialization. This browser does not support the necessary APIs");
|
|
23
|
+
debug("Stopping Dash0 Web SDK initialization. This browser does not support the necessary APIs.");
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
|
+
if (!opts.serviceName.trim()) {
|
|
27
|
+
debug("Missing or empty serviceName value. Falling back to location.hostname.");
|
|
28
|
+
opts.serviceName = loc?.hostname ?? "unknown";
|
|
29
|
+
}
|
|
26
30
|
vars.endpoints = opts.endpoint instanceof Array ? opts.endpoint : [opts.endpoint];
|
|
27
31
|
if (vars.endpoints.length === 0) {
|
|
28
32
|
warn("No telemetry endpoint configured. Aborting Dash0 Web SDK initialization process.");
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { SERVICE_NAME } from "../semantic-conventions";
|
|
2
3
|
// Mock all the instrumentation modules
|
|
3
4
|
vi.mock("../instrumentations/web-vitals", () => ({
|
|
4
5
|
startWebVitalsInstrumentation: vi.fn(),
|
|
@@ -12,6 +13,14 @@ vi.mock("../instrumentations/http/fetch", () => ({
|
|
|
12
13
|
vi.mock("../instrumentations/navigation", () => ({
|
|
13
14
|
startNavigationInstrumentation: vi.fn(),
|
|
14
15
|
}));
|
|
16
|
+
// Mock the utils module to control loc.hostname
|
|
17
|
+
vi.mock("../utils", async () => {
|
|
18
|
+
const actual = await vi.importActual("../utils");
|
|
19
|
+
return {
|
|
20
|
+
...actual,
|
|
21
|
+
loc: { hostname: "test-hostname.example.com" },
|
|
22
|
+
};
|
|
23
|
+
});
|
|
15
24
|
import { startErrorInstrumentation } from "../instrumentations/errors";
|
|
16
25
|
import { instrumentFetch } from "../instrumentations/http/fetch";
|
|
17
26
|
import { startNavigationInstrumentation } from "../instrumentations/navigation";
|
|
@@ -179,4 +188,58 @@ describe("init", () => {
|
|
|
179
188
|
expect(spyOnWarn).not.toHaveBeenCalled();
|
|
180
189
|
});
|
|
181
190
|
});
|
|
191
|
+
describe("serviceName fallback", () => {
|
|
192
|
+
it("should use provided serviceName when it is valid", async () => {
|
|
193
|
+
init({
|
|
194
|
+
...baseOptions,
|
|
195
|
+
serviceName: "my-service",
|
|
196
|
+
});
|
|
197
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
198
|
+
expect(serviceNameAttr?.value.stringValue).toBe("my-service");
|
|
199
|
+
});
|
|
200
|
+
it("should fallback to location.hostname when serviceName is empty string", async () => {
|
|
201
|
+
init({
|
|
202
|
+
...baseOptions,
|
|
203
|
+
serviceName: "",
|
|
204
|
+
});
|
|
205
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
206
|
+
expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
|
|
207
|
+
});
|
|
208
|
+
it("should fallback to location.hostname when serviceName is only whitespace", async () => {
|
|
209
|
+
init({
|
|
210
|
+
...baseOptions,
|
|
211
|
+
serviceName: " ",
|
|
212
|
+
});
|
|
213
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
214
|
+
expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
|
|
215
|
+
});
|
|
216
|
+
it("should fallback to location.hostname when serviceName is tabs and spaces", async () => {
|
|
217
|
+
init({
|
|
218
|
+
...baseOptions,
|
|
219
|
+
serviceName: " \t \n ",
|
|
220
|
+
});
|
|
221
|
+
const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
|
|
222
|
+
expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
|
|
223
|
+
});
|
|
224
|
+
it("should fallback to 'unknown' when serviceName is empty and location.hostname is not available", async () => {
|
|
225
|
+
vi.resetModules();
|
|
226
|
+
// Mock utils module with loc as undefined
|
|
227
|
+
vi.doMock("../utils", async () => {
|
|
228
|
+
const actual = await vi.importActual("../utils");
|
|
229
|
+
return {
|
|
230
|
+
...actual,
|
|
231
|
+
loc: undefined,
|
|
232
|
+
};
|
|
233
|
+
});
|
|
234
|
+
const { vars: importedVars } = await import("../vars");
|
|
235
|
+
const { init: importedInit } = await import("./init");
|
|
236
|
+
const { SERVICE_NAME: importedServiceName } = await import("../semantic-conventions");
|
|
237
|
+
importedInit({
|
|
238
|
+
...baseOptions,
|
|
239
|
+
serviceName: "",
|
|
240
|
+
});
|
|
241
|
+
const serviceNameAttr = importedVars.resource.attributes.find((attr) => attr.key === importedServiceName);
|
|
242
|
+
expect(serviceNameAttr?.value.stringValue).toBe("unknown");
|
|
243
|
+
});
|
|
244
|
+
});
|
|
182
245
|
});
|
|
@@ -2,7 +2,7 @@ import { hasOwnProperty, nowNanos, win } from "../../utils";
|
|
|
2
2
|
import { isErrorMessageIgnored } from "../../utils/ignore-rules";
|
|
3
3
|
import { sendLog } from "../../transport";
|
|
4
4
|
import { EVENT_NAME, EVENT_NAMES, EXCEPTION_COMPONENT_STACK, EXCEPTION_MESSAGE, EXCEPTION_STACKTRACE, EXCEPTION_TYPE, LOG_SEVERITIES, } from "../../semantic-conventions";
|
|
5
|
-
import { addAttribute } from "../../utils/otel";
|
|
5
|
+
import { addAttribute, addAttributes } from "../../utils/otel";
|
|
6
6
|
import { addCommonAttributes } from "../../attributes";
|
|
7
7
|
const MAX_ERRORS_TO_REPORT = 100;
|
|
8
8
|
const MAX_STACK_SIZE = 30;
|
|
@@ -75,7 +75,8 @@ function onUnhandledError({ message, type, stack, opts }) {
|
|
|
75
75
|
trackedError.seenCount++;
|
|
76
76
|
}
|
|
77
77
|
else {
|
|
78
|
-
const attributes =
|
|
78
|
+
const attributes = [];
|
|
79
|
+
addCommonAttributes(attributes);
|
|
79
80
|
addAttribute(attributes, EVENT_NAME, EVENT_NAMES.ERROR);
|
|
80
81
|
addAttribute(attributes, EXCEPTION_MESSAGE, message);
|
|
81
82
|
if (type) {
|
|
@@ -87,7 +88,8 @@ function onUnhandledError({ message, type, stack, opts }) {
|
|
|
87
88
|
if (opts?.componentStack) {
|
|
88
89
|
addAttribute(attributes, EXCEPTION_COMPONENT_STACK, opts?.componentStack.substring(0, 2048));
|
|
89
90
|
}
|
|
90
|
-
|
|
91
|
+
// Add custom attributes last to allow overrides
|
|
92
|
+
addAttributes(attributes, opts?.attributes);
|
|
91
93
|
trackedError = {
|
|
92
94
|
seenCount: 1,
|
|
93
95
|
transmittedCount: 0,
|
|
@@ -44,11 +44,34 @@ export function toKeyValue(key, value) {
|
|
|
44
44
|
value: toAnyValue(value),
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Adds a single attribute to the provided attributes array
|
|
49
|
+
*/
|
|
47
50
|
export function addAttribute(attributes, key, value) {
|
|
48
51
|
if (!key)
|
|
49
52
|
return;
|
|
50
53
|
attributes.push(toKeyValue(key, value));
|
|
51
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Adds multiple attributes to the provided attributes array
|
|
57
|
+
*/
|
|
58
|
+
export function addAttributes(
|
|
59
|
+
/**
|
|
60
|
+
* The attributes array to add to
|
|
61
|
+
*/
|
|
62
|
+
attributes,
|
|
63
|
+
/**
|
|
64
|
+
* Additional attributes to add
|
|
65
|
+
* If no attributes are provided, nothing is added
|
|
66
|
+
*/
|
|
67
|
+
attrs) {
|
|
68
|
+
if (!attrs)
|
|
69
|
+
return;
|
|
70
|
+
Object.entries(attrs).forEach(([key, value]) => addAttribute(attributes, key, value));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Removes an attribute from the provided attributes array by its key
|
|
74
|
+
*/
|
|
52
75
|
export function removeAttribute(attributes, key) {
|
|
53
76
|
const index = attributes.findIndex((attr) => attr["key"] === key);
|
|
54
77
|
if (index !== -1) {
|