@dash0/sdk-web 0.17.0 → 0.18.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/src/api/events.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { EVENT_NAME, EVENT_NAMES, WEB_EVENT_TITLE, LOG_SEVERITIES, LOG_SEVERITY_TEXT } from "../semantic-conventions";
2
2
  import { sendLog } from "../transport";
3
3
  import { addCommonAttributes } from "../attributes";
4
- import { addAttribute, AttributeValueType, toAnyValue } from "../utils/otel";
4
+ import { addAttribute, addAttributes, AttributeValueType, toAnyValue } from "../utils/otel";
5
5
  import { nowNanos, TimeInput, toNanosTs, warn } from "../utils";
6
6
  import { AnyValue, KeyValue } from "../types/otlp";
7
7
 
@@ -49,14 +49,18 @@ export function sendEvent(name: string, opts?: EventOptions) {
49
49
  }
50
50
 
51
51
  const attributes: KeyValue[] = [];
52
+
52
53
  addCommonAttributes(attributes);
53
- Object.entries(opts?.attributes ?? {}).forEach(([key, value]) => addAttribute(attributes, key, value));
54
54
 
55
55
  addAttribute(attributes, EVENT_NAME, name);
56
+
56
57
  if (opts?.title) {
57
58
  addAttribute(attributes, WEB_EVENT_TITLE, opts.title);
58
59
  }
59
60
 
61
+ // Add custom attributes last to allow overrides
62
+ addAttributes(attributes, opts?.attributes);
63
+
60
64
  sendLog({
61
65
  timeUnixNano: opts?.timestamp != null ? toNanosTs(opts.timestamp) : nowNanos(),
62
66
  attributes,
@@ -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[] = opts?.attributes ? [...opts.attributes] : [];
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
- addCommonAttributes(attributes);
136
+
137
+ // Add custom attributes last to allow overrides
138
+ addAttributes(attributes, opts?.attributes);
133
139
 
134
140
  trackedError = {
135
141
  seenCount: 1,
@@ -1,4 +1,5 @@
1
- import { KeyValue } from "./otlp";
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: string | null | undefined;
11
+ componentStack?: string | null | undefined;
11
12
  /**
12
13
  * Additional attributes to add to the error span
13
14
  */
14
- attributes?: KeyValue[];
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) {