@dash0/sdk-web 0.18.0 → 0.18.2
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/api/init.js +6 -2
- package/dist/modules/api/init_test.js +63 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/api/init.ts +7 -1
- package/src/api/init_test.ts +77 -0
package/src/api/init.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
win,
|
|
20
20
|
NO_VALUE_FALLBACK,
|
|
21
21
|
pick,
|
|
22
|
+
loc,
|
|
22
23
|
} from "../utils";
|
|
23
24
|
import { trackSessions } from "./session";
|
|
24
25
|
import { startWebVitalsInstrumentation } from "../instrumentations/web-vitals";
|
|
@@ -44,10 +45,15 @@ export function init(opts: InitOptions) {
|
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
if (!isSupported()) {
|
|
47
|
-
debug("Stopping Dash0 Web SDK initialization. This browser does not support the necessary APIs");
|
|
48
|
+
debug("Stopping Dash0 Web SDK initialization. This browser does not support the necessary APIs.");
|
|
48
49
|
return;
|
|
49
50
|
}
|
|
50
51
|
|
|
52
|
+
if (!opts.serviceName.trim()) {
|
|
53
|
+
debug("Missing or empty serviceName value. Falling back to location.hostname.");
|
|
54
|
+
opts.serviceName = loc?.hostname ?? "unknown";
|
|
55
|
+
}
|
|
56
|
+
|
|
51
57
|
vars.endpoints = opts.endpoint instanceof Array ? opts.endpoint : [opts.endpoint];
|
|
52
58
|
if (vars.endpoints.length === 0) {
|
|
53
59
|
warn("No telemetry endpoint configured. Aborting Dash0 Web SDK initialization process.");
|
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
|
});
|