@dash0/sdk-web 0.13.2 → 0.13.4

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.
Files changed (36) hide show
  1. package/README.md +14 -0
  2. package/dist/dash0.iife.js +1 -1
  3. package/dist/dash0.iife.js.map +1 -1
  4. package/dist/dash0.js +1 -1
  5. package/dist/dash0.js.map +1 -1
  6. package/dist/dash0.umd.cjs +1 -1
  7. package/dist/dash0.umd.cjs.map +1 -1
  8. package/dist/modules/api/log-level.js +1 -0
  9. package/dist/modules/attributes/common.js +2 -2
  10. package/dist/modules/attributes/url.js +11 -10
  11. package/dist/modules/entrypoint/npm-package.js +1 -0
  12. package/dist/modules/entrypoint/script.js +2 -0
  13. package/dist/modules/instrumentations/http/fetch.js +3 -3
  14. package/dist/modules/semantic-conventions.js +1 -0
  15. package/dist/modules/transport/fetch.js +2 -1
  16. package/dist/modules/utils/debug.js +20 -10
  17. package/dist/modules/utils/otel/attributes.js +8 -0
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/dist/types/api/log-level.d.ts +2 -0
  20. package/dist/types/attributes/common.d.ts +3 -0
  21. package/dist/types/attributes/url.d.ts +2 -1
  22. package/dist/types/entrypoint/npm-package.d.ts +1 -0
  23. package/dist/types/semantic-conventions.d.ts +1 -0
  24. package/dist/types/utils/debug.d.ts +11 -1
  25. package/dist/types/utils/otel/attributes.d.ts +2 -0
  26. package/package.json +1 -1
  27. package/src/api/log-level.ts +2 -0
  28. package/src/attributes/common.ts +5 -1
  29. package/src/attributes/url.ts +12 -10
  30. package/src/entrypoint/npm-package.ts +1 -0
  31. package/src/entrypoint/script.ts +2 -0
  32. package/src/instrumentations/http/fetch.ts +3 -3
  33. package/src/semantic-conventions.ts +1 -0
  34. package/src/transport/fetch.ts +3 -1
  35. package/src/utils/debug.ts +25 -10
  36. package/src/utils/otel/attributes.ts +11 -0
@@ -0,0 +1 @@
1
+ export { setActiveLogLevel } from "../utils/debug";
@@ -1,5 +1,5 @@
1
1
  import { generateUniqueId, nav, NO_VALUE_FALLBACK, WEB_EVENT_ID_BYTES, win } from "../utils";
2
- import { BROWSER_TAB_ID, NETWORK_CONNECTION_TYPE, SESSION_ID, WEB_EVENT_ID, WINDOW_HEIGHT, WINDOW_WIDTH, } from "../semantic-conventions";
2
+ import { BROWSER_TAB_ID, NETWORK_CONNECTION_TYPE, PAGE_URL_ATTR_PREFIX, SESSION_ID, WEB_EVENT_ID, WINDOW_HEIGHT, WINDOW_WIDTH, } from "../semantic-conventions";
3
3
  import { sessionId } from "../api/session";
4
4
  import { vars } from "../vars";
5
5
  import { addAttribute } from "../utils/otel";
@@ -12,7 +12,7 @@ export function addCommonAttributes(attributes, options) {
12
12
  for (let i = 0; i < vars.signalAttributes.length; i++) {
13
13
  attributes.push(vars.signalAttributes[i]);
14
14
  }
15
- addUrlAttributes(attributes, options?.url ?? win?.location.href ?? NO_VALUE_FALLBACK);
15
+ addUrlAttributes(attributes, options?.url ?? win?.location.href ?? NO_VALUE_FALLBACK, PAGE_URL_ATTR_PREFIX);
16
16
  if (sessionId) {
17
17
  addAttribute(attributes, SESSION_ID, sessionId);
18
18
  }
@@ -1,25 +1,26 @@
1
- import { addAttribute } from "../utils/otel";
1
+ import { addAttribute, withPrefix } from "../utils/otel";
2
2
  import { URL_DOMAIN, URL_FRAGMENT, URL_FULL, URL_PATH, URL_QUERY, URL_SCHEME } from "../semantic-conventions";
3
- import { parseUrl } from "../utils/url";
4
- export function addUrlAttributes(attributes, url) {
3
+ import { parseUrl } from "../utils";
4
+ export function addUrlAttributes(attributes, url, prefix) {
5
+ const applyPrefix = withPrefix(prefix);
5
6
  try {
6
7
  const parsed = parseUrl(url);
7
8
  if (parsed.username)
8
9
  parsed.username = "REDACTED";
9
10
  if (parsed.password)
10
11
  parsed.password = "REDACTED";
11
- addAttribute(attributes, URL_FULL, parsed.href);
12
- addAttribute(attributes, URL_PATH, parsed.pathname);
13
- addAttribute(attributes, URL_DOMAIN, parsed.hostname);
14
- addAttribute(attributes, URL_SCHEME, parsed.protocol.replace(":", ""));
12
+ addAttribute(attributes, applyPrefix(URL_FULL), parsed.href);
13
+ addAttribute(attributes, applyPrefix(URL_PATH), parsed.pathname);
14
+ addAttribute(attributes, applyPrefix(URL_DOMAIN), parsed.hostname);
15
+ addAttribute(attributes, applyPrefix(URL_SCHEME), parsed.protocol.replace(":", ""));
15
16
  if (parsed.hash) {
16
- addAttribute(attributes, URL_FRAGMENT, parsed.hash.replace("#", ""));
17
+ addAttribute(attributes, applyPrefix(URL_FRAGMENT), parsed.hash.replace("#", ""));
17
18
  }
18
19
  if (parsed.search) {
19
- addAttribute(attributes, URL_QUERY, parsed.search.replace("?", ""));
20
+ addAttribute(attributes, applyPrefix(URL_QUERY), parsed.search.replace("?", ""));
20
21
  }
21
22
  }
22
23
  catch (_e) {
23
- addAttribute(attributes, URL_FULL, String(url));
24
+ addAttribute(attributes, applyPrefix(URL_FULL), String(url));
24
25
  }
25
26
  }
@@ -4,6 +4,7 @@ export * from "../api/identify";
4
4
  export * from "../api/debug";
5
5
  export * from "../api/attributes";
6
6
  export * from "../api/events";
7
+ export * from "../api/log-level";
7
8
  export { terminateSession } from "../api/session";
8
9
  export { reportError } from "../api/report-error";
9
10
  export function init(opts) {
@@ -6,6 +6,7 @@ import { init as initApi } from "../api/init";
6
6
  import { terminateSession } from "../api/session";
7
7
  import { addSignalAttribute, removeSignalAttribute } from "../api/attributes";
8
8
  import { sendEvent } from "../api/events";
9
+ import { setActiveLogLevel } from "../api/log-level";
9
10
  /**
10
11
  * All the APIs exposed through the script tag via `dash0('{{api name}}')`
11
12
  */
@@ -17,6 +18,7 @@ const scriptApis = {
17
18
  reportError,
18
19
  addSignalAttribute,
19
20
  removeSignalAttribute,
21
+ setActiveLogLevel,
20
22
  sendEvent,
21
23
  };
22
24
  init();
@@ -7,7 +7,7 @@ import { vars } from "../../vars";
7
7
  import { httpRequestHeaderKey, httpResponseHeaderKey } from "../../utils/otel/http";
8
8
  import { sendSpan } from "../../transport";
9
9
  import { addResourceNetworkEvents, addResourceSize, HTTP_METHOD_OTHER, isWellKnownHttpMethod } from "./utils";
10
- import { addCommonAttributes } from "../../attributes";
10
+ import { addCommonAttributes, addUrlAttributes } from "../../attributes";
11
11
  export function instrumentFetch() {
12
12
  if (!win || !win.fetch || !win.Request) {
13
13
  debug("Browser does not support the Fetch API, skipping instrumentation");
@@ -40,8 +40,8 @@ function wrapFetch(original) {
40
40
  const isWellKnownMethodMatchingLeniently = isWellKnownHttpMethod(originalMethod.toUpperCase());
41
41
  const method = isWellKnownMethodMatchingLeniently ? originalMethod.toUpperCase() : HTTP_METHOD_OTHER;
42
42
  const span = startSpan(`HTTP ${method}`);
43
- // The url namespace of a http span has a different meaning than for common rum signals.
44
- addCommonAttributes(span.attributes, { url });
43
+ addCommonAttributes(span.attributes);
44
+ addUrlAttributes(span.attributes, url);
45
45
  addGraphQlProperties(input, init, span);
46
46
  addAttribute(span.attributes, HTTP_REQUEST_METHOD, method);
47
47
  if (!isWellKnownMethod) {
@@ -30,6 +30,7 @@ export const EXCEPTION_STACKTRACE = "exception.stacktrace";
30
30
  // Error Attribute Keys
31
31
  export const ERROR_TYPE = "error.type";
32
32
  // URL Attribute Keys
33
+ export const PAGE_URL_ATTR_PREFIX = "page";
33
34
  export const URL_DOMAIN = "url.domain";
34
35
  export const URL_FRAGMENT = "url.fragment";
35
36
  export const URL_FULL = "url.full";
@@ -1,7 +1,8 @@
1
1
  import { vars } from "../vars";
2
- import { noop, warn, fetch } from "../utils";
2
+ import { noop, warn, fetch, debug } from "../utils";
3
3
  const BEACON_BODY_SIZE_LIMIT = 60000;
4
4
  export async function send(path, body) {
5
+ debug("Transmitting telemetry to endpoints", body);
5
6
  const jsonString = JSON.stringify(body);
6
7
  let requestBody = jsonString;
7
8
  let byteLength = jsonString.length;
@@ -1,19 +1,29 @@
1
1
  import { noop } from "./fn";
2
- export const log = createLogger("log");
2
+ const logLevels = {
3
+ debug: 0,
4
+ info: 1,
5
+ warn: 2,
6
+ error: 3,
7
+ };
8
+ let activeLogLevel = logLevels.warn;
9
+ /**
10
+ * Changes the logging verbosity of Dash0's web SDK. By default, only warnings and errors are logged.
11
+ */
12
+ export function setActiveLogLevel(level) {
13
+ activeLogLevel = logLevels[level] ?? logLevels.warn;
14
+ }
3
15
  export const info = createLogger("info");
4
16
  export const warn = createLogger("warn");
5
17
  export const error = createLogger("error");
6
18
  export const debug = createLogger("debug");
7
- function createLogger(method) {
8
- if (typeof console === "undefined" || typeof console.log !== "function" || typeof console.log.apply !== "function") {
9
- return noop;
10
- }
11
- if (console[method] && typeof console[method].apply === "function") {
19
+ function createLogger(logLevel) {
20
+ if (typeof console !== "undefined" && console[logLevel] && typeof console[logLevel].apply === "function") {
21
+ const numericLogLevel = logLevels[logLevel];
12
22
  return function () {
13
- console[method].apply(console, arguments);
23
+ if (numericLogLevel >= activeLogLevel) {
24
+ console[logLevel].apply(console, arguments);
25
+ }
14
26
  };
15
27
  }
16
- return function () {
17
- console.log.apply(console, arguments);
18
- };
28
+ return noop;
19
29
  }
@@ -55,3 +55,11 @@ export function removeAttribute(attributes, key) {
55
55
  attributes.splice(index, 1);
56
56
  }
57
57
  }
58
+ export function withPrefix(prefix) {
59
+ if (!prefix)
60
+ return (attr) => attr;
61
+ if (Array.isArray(prefix)) {
62
+ return (attr) => [...prefix, attr].join(".");
63
+ }
64
+ return (attr) => `${prefix}.${attr}`;
65
+ }