@opentelemetry/sdk-trace-web 1.0.1 → 1.2.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.
Files changed (41) hide show
  1. package/README.md +0 -6
  2. package/build/esm/StackContextManager.js +26 -5
  3. package/build/esm/StackContextManager.js.map +1 -1
  4. package/build/esm/WebTracerProvider.js.map +1 -1
  5. package/build/esm/utils.d.ts +27 -3
  6. package/build/esm/utils.js +29 -16
  7. package/build/esm/utils.js.map +1 -1
  8. package/build/esm/version.d.ts +1 -1
  9. package/build/esm/version.js +1 -1
  10. package/build/esm/version.js.map +1 -1
  11. package/build/esnext/StackContextManager.d.ts +50 -0
  12. package/build/esnext/StackContextManager.js +111 -0
  13. package/build/esnext/StackContextManager.js.map +1 -0
  14. package/build/esnext/WebTracerProvider.d.ts +24 -0
  15. package/build/esnext/WebTracerProvider.js +53 -0
  16. package/build/esnext/WebTracerProvider.js.map +1 -0
  17. package/build/esnext/enums/PerformanceTimingNames.d.ts +25 -0
  18. package/build/esnext/enums/PerformanceTimingNames.js +41 -0
  19. package/build/esnext/enums/PerformanceTimingNames.js.map +1 -0
  20. package/build/esnext/index.d.ts +6 -0
  21. package/build/esnext/index.js +21 -0
  22. package/build/esnext/index.js.map +1 -0
  23. package/build/esnext/types.d.ts +46 -0
  24. package/build/esnext/types.js +17 -0
  25. package/build/esnext/types.js.map +1 -0
  26. package/build/esnext/utils.d.ts +81 -0
  27. package/build/esnext/utils.js +312 -0
  28. package/build/esnext/utils.js.map +1 -0
  29. package/build/esnext/version.d.ts +2 -0
  30. package/build/esnext/version.js +18 -0
  31. package/build/esnext/version.js.map +1 -0
  32. package/build/src/StackContextManager.js +1 -0
  33. package/build/src/StackContextManager.js.map +1 -1
  34. package/build/src/WebTracerProvider.js.map +1 -1
  35. package/build/src/utils.d.ts +27 -3
  36. package/build/src/utils.js +39 -26
  37. package/build/src/utils.js.map +1 -1
  38. package/build/src/version.d.ts +1 -1
  39. package/build/src/version.js +1 -1
  40. package/build/src/version.js.map +1 -1
  41. package/package.json +34 -25
@@ -0,0 +1,25 @@
1
+ export declare enum PerformanceTimingNames {
2
+ CONNECT_END = "connectEnd",
3
+ CONNECT_START = "connectStart",
4
+ DECODED_BODY_SIZE = "decodedBodySize",
5
+ DOM_COMPLETE = "domComplete",
6
+ DOM_CONTENT_LOADED_EVENT_END = "domContentLoadedEventEnd",
7
+ DOM_CONTENT_LOADED_EVENT_START = "domContentLoadedEventStart",
8
+ DOM_INTERACTIVE = "domInteractive",
9
+ DOMAIN_LOOKUP_END = "domainLookupEnd",
10
+ DOMAIN_LOOKUP_START = "domainLookupStart",
11
+ ENCODED_BODY_SIZE = "encodedBodySize",
12
+ FETCH_START = "fetchStart",
13
+ LOAD_EVENT_END = "loadEventEnd",
14
+ LOAD_EVENT_START = "loadEventStart",
15
+ NAVIGATION_START = "navigationStart",
16
+ REDIRECT_END = "redirectEnd",
17
+ REDIRECT_START = "redirectStart",
18
+ REQUEST_START = "requestStart",
19
+ RESPONSE_END = "responseEnd",
20
+ RESPONSE_START = "responseStart",
21
+ SECURE_CONNECTION_START = "secureConnectionStart",
22
+ UNLOAD_EVENT_END = "unloadEventEnd",
23
+ UNLOAD_EVENT_START = "unloadEventStart"
24
+ }
25
+ //# sourceMappingURL=PerformanceTimingNames.d.ts.map
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export var PerformanceTimingNames;
17
+ (function (PerformanceTimingNames) {
18
+ PerformanceTimingNames["CONNECT_END"] = "connectEnd";
19
+ PerformanceTimingNames["CONNECT_START"] = "connectStart";
20
+ PerformanceTimingNames["DECODED_BODY_SIZE"] = "decodedBodySize";
21
+ PerformanceTimingNames["DOM_COMPLETE"] = "domComplete";
22
+ PerformanceTimingNames["DOM_CONTENT_LOADED_EVENT_END"] = "domContentLoadedEventEnd";
23
+ PerformanceTimingNames["DOM_CONTENT_LOADED_EVENT_START"] = "domContentLoadedEventStart";
24
+ PerformanceTimingNames["DOM_INTERACTIVE"] = "domInteractive";
25
+ PerformanceTimingNames["DOMAIN_LOOKUP_END"] = "domainLookupEnd";
26
+ PerformanceTimingNames["DOMAIN_LOOKUP_START"] = "domainLookupStart";
27
+ PerformanceTimingNames["ENCODED_BODY_SIZE"] = "encodedBodySize";
28
+ PerformanceTimingNames["FETCH_START"] = "fetchStart";
29
+ PerformanceTimingNames["LOAD_EVENT_END"] = "loadEventEnd";
30
+ PerformanceTimingNames["LOAD_EVENT_START"] = "loadEventStart";
31
+ PerformanceTimingNames["NAVIGATION_START"] = "navigationStart";
32
+ PerformanceTimingNames["REDIRECT_END"] = "redirectEnd";
33
+ PerformanceTimingNames["REDIRECT_START"] = "redirectStart";
34
+ PerformanceTimingNames["REQUEST_START"] = "requestStart";
35
+ PerformanceTimingNames["RESPONSE_END"] = "responseEnd";
36
+ PerformanceTimingNames["RESPONSE_START"] = "responseStart";
37
+ PerformanceTimingNames["SECURE_CONNECTION_START"] = "secureConnectionStart";
38
+ PerformanceTimingNames["UNLOAD_EVENT_END"] = "unloadEventEnd";
39
+ PerformanceTimingNames["UNLOAD_EVENT_START"] = "unloadEventStart";
40
+ })(PerformanceTimingNames || (PerformanceTimingNames = {}));
41
+ //# sourceMappingURL=PerformanceTimingNames.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PerformanceTimingNames.js","sourceRoot":"","sources":["../../../src/enums/PerformanceTimingNames.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,MAAM,CAAN,IAAY,sBAuBX;AAvBD,WAAY,sBAAsB;IAChC,oDAA0B,CAAA;IAC1B,wDAA8B,CAAA;IAC9B,+DAAqC,CAAA;IACrC,sDAA4B,CAAA;IAC5B,mFAAyD,CAAA;IACzD,uFAA6D,CAAA;IAC7D,4DAAkC,CAAA;IAClC,+DAAqC,CAAA;IACrC,mEAAyC,CAAA;IACzC,+DAAqC,CAAA;IACrC,oDAA0B,CAAA;IAC1B,yDAA+B,CAAA;IAC/B,6DAAmC,CAAA;IACnC,8DAAoC,CAAA;IACpC,sDAA4B,CAAA;IAC5B,0DAAgC,CAAA;IAChC,wDAA8B,CAAA;IAC9B,sDAA4B,CAAA;IAC5B,0DAAgC,CAAA;IAChC,2EAAiD,CAAA;IACjD,6DAAmC,CAAA;IACnC,iEAAuC,CAAA;AACzC,CAAC,EAvBW,sBAAsB,KAAtB,sBAAsB,QAuBjC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport enum PerformanceTimingNames {\n CONNECT_END = 'connectEnd',\n CONNECT_START = 'connectStart',\n DECODED_BODY_SIZE = 'decodedBodySize',\n DOM_COMPLETE = 'domComplete',\n DOM_CONTENT_LOADED_EVENT_END = 'domContentLoadedEventEnd',\n DOM_CONTENT_LOADED_EVENT_START = 'domContentLoadedEventStart',\n DOM_INTERACTIVE = 'domInteractive',\n DOMAIN_LOOKUP_END = 'domainLookupEnd',\n DOMAIN_LOOKUP_START = 'domainLookupStart',\n ENCODED_BODY_SIZE = 'encodedBodySize',\n FETCH_START = 'fetchStart',\n LOAD_EVENT_END = 'loadEventEnd',\n LOAD_EVENT_START = 'loadEventStart',\n NAVIGATION_START = 'navigationStart',\n REDIRECT_END = 'redirectEnd',\n REDIRECT_START = 'redirectStart',\n REQUEST_START = 'requestStart',\n RESPONSE_END = 'responseEnd',\n RESPONSE_START = 'responseStart',\n SECURE_CONNECTION_START = 'secureConnectionStart',\n UNLOAD_EVENT_END = 'unloadEventEnd',\n UNLOAD_EVENT_START = 'unloadEventStart',\n}\n"]}
@@ -0,0 +1,6 @@
1
+ export * from './WebTracerProvider';
2
+ export * from './StackContextManager';
3
+ export * from './enums/PerformanceTimingNames';
4
+ export * from './types';
5
+ export * from './utils';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,21 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export * from './WebTracerProvider';
17
+ export * from './StackContextManager';
18
+ export * from './enums/PerformanceTimingNames';
19
+ export * from './types';
20
+ export * from './utils';
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport * from './WebTracerProvider';\nexport * from './StackContextManager';\nexport * from './enums/PerformanceTimingNames';\nexport * from './types';\nexport * from './utils';\n"]}
@@ -0,0 +1,46 @@
1
+ import { PerformanceTimingNames } from './enums/PerformanceTimingNames';
2
+ export declare type PerformanceEntries = {
3
+ [PerformanceTimingNames.CONNECT_END]?: number;
4
+ [PerformanceTimingNames.CONNECT_START]?: number;
5
+ [PerformanceTimingNames.DECODED_BODY_SIZE]?: number;
6
+ [PerformanceTimingNames.DOM_COMPLETE]?: number;
7
+ [PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_END]?: number;
8
+ [PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_START]?: number;
9
+ [PerformanceTimingNames.DOM_INTERACTIVE]?: number;
10
+ [PerformanceTimingNames.DOMAIN_LOOKUP_END]?: number;
11
+ [PerformanceTimingNames.DOMAIN_LOOKUP_START]?: number;
12
+ [PerformanceTimingNames.ENCODED_BODY_SIZE]?: number;
13
+ [PerformanceTimingNames.FETCH_START]?: number;
14
+ [PerformanceTimingNames.LOAD_EVENT_END]?: number;
15
+ [PerformanceTimingNames.LOAD_EVENT_START]?: number;
16
+ [PerformanceTimingNames.REDIRECT_END]?: number;
17
+ [PerformanceTimingNames.REDIRECT_START]?: number;
18
+ [PerformanceTimingNames.REQUEST_START]?: number;
19
+ [PerformanceTimingNames.RESPONSE_END]?: number;
20
+ [PerformanceTimingNames.RESPONSE_START]?: number;
21
+ [PerformanceTimingNames.SECURE_CONNECTION_START]?: number;
22
+ [PerformanceTimingNames.UNLOAD_EVENT_END]?: number;
23
+ [PerformanceTimingNames.UNLOAD_EVENT_START]?: number;
24
+ };
25
+ /**
26
+ * This interface defines a fallback to read performance metrics,
27
+ * this happens for example on Safari Mac
28
+ */
29
+ export interface PerformanceLegacy {
30
+ timing?: PerformanceEntries;
31
+ }
32
+ /**
33
+ * This interface is used in {@link getResource} function to return
34
+ * main request and it's corresponding PreFlight request
35
+ */
36
+ export interface PerformanceResourceTimingInfo {
37
+ corsPreFlightRequest?: PerformanceResourceTiming;
38
+ mainRequest?: PerformanceResourceTiming;
39
+ }
40
+ declare type PropagateTraceHeaderCorsUrl = string | RegExp;
41
+ /**
42
+ * urls which should include trace headers when origin doesn't match
43
+ */
44
+ export declare type PropagateTraceHeaderCorsUrls = PropagateTraceHeaderCorsUrl | PropagateTraceHeaderCorsUrl[];
45
+ export {};
46
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { PerformanceTimingNames } from './enums/PerformanceTimingNames';
17
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { PerformanceTimingNames } from './enums/PerformanceTimingNames';\n\nexport type PerformanceEntries = {\n [PerformanceTimingNames.CONNECT_END]?: number;\n [PerformanceTimingNames.CONNECT_START]?: number;\n [PerformanceTimingNames.DECODED_BODY_SIZE]?: number;\n [PerformanceTimingNames.DOM_COMPLETE]?: number;\n [PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_END]?: number;\n [PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_START]?: number;\n [PerformanceTimingNames.DOM_INTERACTIVE]?: number;\n [PerformanceTimingNames.DOMAIN_LOOKUP_END]?: number;\n [PerformanceTimingNames.DOMAIN_LOOKUP_START]?: number;\n [PerformanceTimingNames.ENCODED_BODY_SIZE]?: number;\n [PerformanceTimingNames.FETCH_START]?: number;\n [PerformanceTimingNames.LOAD_EVENT_END]?: number;\n [PerformanceTimingNames.LOAD_EVENT_START]?: number;\n [PerformanceTimingNames.REDIRECT_END]?: number;\n [PerformanceTimingNames.REDIRECT_START]?: number;\n [PerformanceTimingNames.REQUEST_START]?: number;\n [PerformanceTimingNames.RESPONSE_END]?: number;\n [PerformanceTimingNames.RESPONSE_START]?: number;\n [PerformanceTimingNames.SECURE_CONNECTION_START]?: number;\n [PerformanceTimingNames.UNLOAD_EVENT_END]?: number;\n [PerformanceTimingNames.UNLOAD_EVENT_START]?: number;\n};\n\n/**\n * This interface defines a fallback to read performance metrics,\n * this happens for example on Safari Mac\n */\nexport interface PerformanceLegacy {\n timing?: PerformanceEntries;\n}\n\n/**\n * This interface is used in {@link getResource} function to return\n * main request and it's corresponding PreFlight request\n */\nexport interface PerformanceResourceTimingInfo {\n corsPreFlightRequest?: PerformanceResourceTiming;\n mainRequest?: PerformanceResourceTiming;\n}\n\ntype PropagateTraceHeaderCorsUrl = string | RegExp;\n\n/**\n * urls which should include trace headers when origin doesn't match\n */\nexport type PropagateTraceHeaderCorsUrls =\n | PropagateTraceHeaderCorsUrl\n | PropagateTraceHeaderCorsUrl[];\n"]}
@@ -0,0 +1,81 @@
1
+ import { PerformanceEntries, PerformanceResourceTimingInfo, PropagateTraceHeaderCorsUrls } from './types';
2
+ import * as api from '@opentelemetry/api';
3
+ /**
4
+ * Helper function to be able to use enum as typed key in type and in interface when using forEach
5
+ * @param obj
6
+ * @param key
7
+ */
8
+ export declare function hasKey<O>(obj: O, key: keyof any): key is keyof O;
9
+ /**
10
+ * Helper function for starting an event on span based on {@link PerformanceEntries}
11
+ * @param span
12
+ * @param performanceName name of performance entry for time start
13
+ * @param entries
14
+ */
15
+ export declare function addSpanNetworkEvent(span: api.Span, performanceName: string, entries: PerformanceEntries): api.Span | undefined;
16
+ /**
17
+ * Helper function for adding network events
18
+ * @param span
19
+ * @param resource
20
+ */
21
+ export declare function addSpanNetworkEvents(span: api.Span, resource: PerformanceEntries): void;
22
+ /**
23
+ * sort resources by startTime
24
+ * @param filteredResources
25
+ */
26
+ export declare function sortResources(filteredResources: PerformanceResourceTiming[]): PerformanceResourceTiming[];
27
+ /**
28
+ * Get closest performance resource ignoring the resources that have been
29
+ * already used.
30
+ * @param spanUrl
31
+ * @param startTimeHR
32
+ * @param endTimeHR
33
+ * @param resources
34
+ * @param ignoredResources
35
+ * @param initiatorType
36
+ */
37
+ export declare function getResource(spanUrl: string, startTimeHR: api.HrTime, endTimeHR: api.HrTime, resources: PerformanceResourceTiming[], ignoredResources?: WeakSet<PerformanceResourceTiming>, initiatorType?: string): PerformanceResourceTimingInfo;
38
+ /**
39
+ * The URLLike interface represents an URL and HTMLAnchorElement compatible fields.
40
+ */
41
+ export interface URLLike {
42
+ hash: string;
43
+ host: string;
44
+ hostname: string;
45
+ href: string;
46
+ readonly origin: string;
47
+ password: string;
48
+ pathname: string;
49
+ port: string;
50
+ protocol: string;
51
+ search: string;
52
+ username: string;
53
+ }
54
+ /**
55
+ * Parses url using URL constructor or fallback to anchor element.
56
+ * @param url
57
+ */
58
+ export declare function parseUrl(url: string): URLLike;
59
+ /**
60
+ * Parses url using URL constructor or fallback to anchor element and serialize
61
+ * it to a string.
62
+ *
63
+ * Performs the steps described in https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url
64
+ *
65
+ * @param url
66
+ */
67
+ export declare function normalizeUrl(url: string): string;
68
+ /**
69
+ * Get element XPath
70
+ * @param target - target element
71
+ * @param optimised - when id attribute of element is present the xpath can be
72
+ * simplified to contain id
73
+ */
74
+ export declare function getElementXPath(target: any, optimised?: boolean): string;
75
+ /**
76
+ * Checks if trace headers should be propagated
77
+ * @param spanUrl
78
+ * @private
79
+ */
80
+ export declare function shouldPropagateTraceHeaders(spanUrl: string, propagateTraceHeaderCorsUrls?: PropagateTraceHeaderCorsUrls): boolean;
81
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,312 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { PerformanceTimingNames as PTN } from './enums/PerformanceTimingNames';
17
+ import { hrTimeToNanoseconds, timeInputToHrTime, urlMatches, } from '@opentelemetry/core';
18
+ import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
19
+ // Used to normalize relative URLs
20
+ let urlNormalizingAnchor;
21
+ function getUrlNormalizingAnchor() {
22
+ if (!urlNormalizingAnchor) {
23
+ urlNormalizingAnchor = document.createElement('a');
24
+ }
25
+ return urlNormalizingAnchor;
26
+ }
27
+ /**
28
+ * Helper function to be able to use enum as typed key in type and in interface when using forEach
29
+ * @param obj
30
+ * @param key
31
+ */
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ export function hasKey(obj, key) {
34
+ return key in obj;
35
+ }
36
+ /**
37
+ * Helper function for starting an event on span based on {@link PerformanceEntries}
38
+ * @param span
39
+ * @param performanceName name of performance entry for time start
40
+ * @param entries
41
+ */
42
+ export function addSpanNetworkEvent(span, performanceName, entries) {
43
+ if (hasKey(entries, performanceName) &&
44
+ typeof entries[performanceName] === 'number') {
45
+ span.addEvent(performanceName, entries[performanceName]);
46
+ return span;
47
+ }
48
+ return undefined;
49
+ }
50
+ /**
51
+ * Helper function for adding network events
52
+ * @param span
53
+ * @param resource
54
+ */
55
+ export function addSpanNetworkEvents(span, resource) {
56
+ addSpanNetworkEvent(span, PTN.FETCH_START, resource);
57
+ addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_START, resource);
58
+ addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_END, resource);
59
+ addSpanNetworkEvent(span, PTN.CONNECT_START, resource);
60
+ addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource);
61
+ addSpanNetworkEvent(span, PTN.CONNECT_END, resource);
62
+ addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
63
+ addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
64
+ addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
65
+ const encodedLength = resource[PTN.ENCODED_BODY_SIZE];
66
+ if (encodedLength !== undefined) {
67
+ span.setAttribute(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, encodedLength);
68
+ }
69
+ const decodedLength = resource[PTN.DECODED_BODY_SIZE];
70
+ // Spec: Not set if transport encoding not used (in which case encoded and decoded sizes match)
71
+ if (decodedLength !== undefined && encodedLength !== decodedLength) {
72
+ span.setAttribute(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, decodedLength);
73
+ }
74
+ }
75
+ /**
76
+ * sort resources by startTime
77
+ * @param filteredResources
78
+ */
79
+ export function sortResources(filteredResources) {
80
+ return filteredResources.slice().sort((a, b) => {
81
+ const valueA = a[PTN.FETCH_START];
82
+ const valueB = b[PTN.FETCH_START];
83
+ if (valueA > valueB) {
84
+ return 1;
85
+ }
86
+ else if (valueA < valueB) {
87
+ return -1;
88
+ }
89
+ return 0;
90
+ });
91
+ }
92
+ /**
93
+ * Get closest performance resource ignoring the resources that have been
94
+ * already used.
95
+ * @param spanUrl
96
+ * @param startTimeHR
97
+ * @param endTimeHR
98
+ * @param resources
99
+ * @param ignoredResources
100
+ * @param initiatorType
101
+ */
102
+ export function getResource(spanUrl, startTimeHR, endTimeHR, resources, ignoredResources = new WeakSet(), initiatorType) {
103
+ // de-relativize the URL before usage (does no harm to absolute URLs)
104
+ const parsedSpanUrl = parseUrl(spanUrl);
105
+ spanUrl = parsedSpanUrl.toString();
106
+ const filteredResources = filterResourcesForSpan(spanUrl, startTimeHR, endTimeHR, resources, ignoredResources, initiatorType);
107
+ if (filteredResources.length === 0) {
108
+ return {
109
+ mainRequest: undefined,
110
+ };
111
+ }
112
+ if (filteredResources.length === 1) {
113
+ return {
114
+ mainRequest: filteredResources[0],
115
+ };
116
+ }
117
+ const sorted = sortResources(filteredResources);
118
+ if (parsedSpanUrl.origin !== location.origin && sorted.length > 1) {
119
+ let corsPreFlightRequest = sorted[0];
120
+ let mainRequest = findMainRequest(sorted, corsPreFlightRequest[PTN.RESPONSE_END], endTimeHR);
121
+ const responseEnd = corsPreFlightRequest[PTN.RESPONSE_END];
122
+ const fetchStart = mainRequest[PTN.FETCH_START];
123
+ // no corsPreFlightRequest
124
+ if (fetchStart < responseEnd) {
125
+ mainRequest = corsPreFlightRequest;
126
+ corsPreFlightRequest = undefined;
127
+ }
128
+ return {
129
+ corsPreFlightRequest,
130
+ mainRequest,
131
+ };
132
+ }
133
+ else {
134
+ return {
135
+ mainRequest: filteredResources[0],
136
+ };
137
+ }
138
+ }
139
+ /**
140
+ * Will find the main request skipping the cors pre flight requests
141
+ * @param resources
142
+ * @param corsPreFlightRequestEndTime
143
+ * @param spanEndTimeHR
144
+ */
145
+ function findMainRequest(resources, corsPreFlightRequestEndTime, spanEndTimeHR) {
146
+ const spanEndTime = hrTimeToNanoseconds(spanEndTimeHR);
147
+ const minTime = hrTimeToNanoseconds(timeInputToHrTime(corsPreFlightRequestEndTime));
148
+ let mainRequest = resources[1];
149
+ let bestGap;
150
+ const length = resources.length;
151
+ for (let i = 1; i < length; i++) {
152
+ const resource = resources[i];
153
+ const resourceStartTime = hrTimeToNanoseconds(timeInputToHrTime(resource[PTN.FETCH_START]));
154
+ const resourceEndTime = hrTimeToNanoseconds(timeInputToHrTime(resource[PTN.RESPONSE_END]));
155
+ const currentGap = spanEndTime - resourceEndTime;
156
+ if (resourceStartTime >= minTime && (!bestGap || currentGap < bestGap)) {
157
+ bestGap = currentGap;
158
+ mainRequest = resource;
159
+ }
160
+ }
161
+ return mainRequest;
162
+ }
163
+ /**
164
+ * Filter all resources that has started and finished according to span start time and end time.
165
+ * It will return the closest resource to a start time
166
+ * @param spanUrl
167
+ * @param startTimeHR
168
+ * @param endTimeHR
169
+ * @param resources
170
+ * @param ignoredResources
171
+ */
172
+ function filterResourcesForSpan(spanUrl, startTimeHR, endTimeHR, resources, ignoredResources, initiatorType) {
173
+ const startTime = hrTimeToNanoseconds(startTimeHR);
174
+ const endTime = hrTimeToNanoseconds(endTimeHR);
175
+ let filteredResources = resources.filter(resource => {
176
+ const resourceStartTime = hrTimeToNanoseconds(timeInputToHrTime(resource[PTN.FETCH_START]));
177
+ const resourceEndTime = hrTimeToNanoseconds(timeInputToHrTime(resource[PTN.RESPONSE_END]));
178
+ return (resource.initiatorType.toLowerCase() ===
179
+ (initiatorType || 'xmlhttprequest') &&
180
+ resource.name === spanUrl &&
181
+ resourceStartTime >= startTime &&
182
+ resourceEndTime <= endTime);
183
+ });
184
+ if (filteredResources.length > 0) {
185
+ filteredResources = filteredResources.filter(resource => {
186
+ return !ignoredResources.has(resource);
187
+ });
188
+ }
189
+ return filteredResources;
190
+ }
191
+ /**
192
+ * Parses url using URL constructor or fallback to anchor element.
193
+ * @param url
194
+ */
195
+ export function parseUrl(url) {
196
+ if (typeof URL === 'function') {
197
+ return new URL(url);
198
+ }
199
+ const element = getUrlNormalizingAnchor();
200
+ element.href = url;
201
+ return element;
202
+ }
203
+ /**
204
+ * Parses url using URL constructor or fallback to anchor element and serialize
205
+ * it to a string.
206
+ *
207
+ * Performs the steps described in https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url
208
+ *
209
+ * @param url
210
+ */
211
+ export function normalizeUrl(url) {
212
+ const urlLike = parseUrl(url);
213
+ return urlLike.href;
214
+ }
215
+ /**
216
+ * Get element XPath
217
+ * @param target - target element
218
+ * @param optimised - when id attribute of element is present the xpath can be
219
+ * simplified to contain id
220
+ */
221
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
222
+ export function getElementXPath(target, optimised) {
223
+ if (target.nodeType === Node.DOCUMENT_NODE) {
224
+ return '/';
225
+ }
226
+ const targetValue = getNodeValue(target, optimised);
227
+ if (optimised && targetValue.indexOf('@id') > 0) {
228
+ return targetValue;
229
+ }
230
+ let xpath = '';
231
+ if (target.parentNode) {
232
+ xpath += getElementXPath(target.parentNode, false);
233
+ }
234
+ xpath += targetValue;
235
+ return xpath;
236
+ }
237
+ /**
238
+ * get node index within the siblings
239
+ * @param target
240
+ */
241
+ function getNodeIndex(target) {
242
+ if (!target.parentNode) {
243
+ return 0;
244
+ }
245
+ const allowedTypes = [target.nodeType];
246
+ if (target.nodeType === Node.CDATA_SECTION_NODE) {
247
+ allowedTypes.push(Node.TEXT_NODE);
248
+ }
249
+ let elements = Array.from(target.parentNode.childNodes);
250
+ elements = elements.filter((element) => {
251
+ const localName = element.localName;
252
+ return (allowedTypes.indexOf(element.nodeType) >= 0 &&
253
+ localName === target.localName);
254
+ });
255
+ if (elements.length >= 1) {
256
+ return elements.indexOf(target) + 1; // xpath starts from 1
257
+ }
258
+ // if there are no other similar child xpath doesn't need index
259
+ return 0;
260
+ }
261
+ /**
262
+ * get node value for xpath
263
+ * @param target
264
+ * @param optimised
265
+ */
266
+ function getNodeValue(target, optimised) {
267
+ const nodeType = target.nodeType;
268
+ const index = getNodeIndex(target);
269
+ let nodeValue = '';
270
+ if (nodeType === Node.ELEMENT_NODE) {
271
+ const id = target.getAttribute('id');
272
+ if (optimised && id) {
273
+ return `//*[@id="${id}"]`;
274
+ }
275
+ nodeValue = target.localName;
276
+ }
277
+ else if (nodeType === Node.TEXT_NODE ||
278
+ nodeType === Node.CDATA_SECTION_NODE) {
279
+ nodeValue = 'text()';
280
+ }
281
+ else if (nodeType === Node.COMMENT_NODE) {
282
+ nodeValue = 'comment()';
283
+ }
284
+ else {
285
+ return '';
286
+ }
287
+ // if index is 1 it can be omitted in xpath
288
+ if (nodeValue && index > 1) {
289
+ return `/${nodeValue}[${index}]`;
290
+ }
291
+ return `/${nodeValue}`;
292
+ }
293
+ /**
294
+ * Checks if trace headers should be propagated
295
+ * @param spanUrl
296
+ * @private
297
+ */
298
+ export function shouldPropagateTraceHeaders(spanUrl, propagateTraceHeaderCorsUrls) {
299
+ let propagateTraceHeaderUrls = propagateTraceHeaderCorsUrls || [];
300
+ if (typeof propagateTraceHeaderUrls === 'string' ||
301
+ propagateTraceHeaderUrls instanceof RegExp) {
302
+ propagateTraceHeaderUrls = [propagateTraceHeaderUrls];
303
+ }
304
+ const parsedSpanUrl = parseUrl(spanUrl);
305
+ if (parsedSpanUrl.origin === location.origin) {
306
+ return true;
307
+ }
308
+ else {
309
+ return propagateTraceHeaderUrls.some(propagateTraceHeaderUrl => urlMatches(spanUrl, propagateTraceHeaderUrl));
310
+ }
311
+ }
312
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":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,CAAC;IAClE,IACE,OAAO,wBAAwB,KAAK,QAAQ;QAC5C,wBAAwB,YAAY,MAAM,EAC1C;QACA,wBAAwB,GAAG,CAAC,wBAAwB,CAAC,CAAC;KACvD;IACD,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;IAExC,IAAI,aAAa,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,EAAE;QAC5C,OAAO,IAAI,CAAC;KACb;SAAM;QACL,OAAO,wBAAwB,CAAC,IAAI,CAAC,uBAAuB,CAAC,EAAE,CAC7D,UAAU,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAC7C,CAAC;KACH;AACH,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n PerformanceEntries,\n PerformanceResourceTimingInfo,\n PropagateTraceHeaderCorsUrls,\n} from './types';\nimport { PerformanceTimingNames as PTN } from './enums/PerformanceTimingNames';\nimport * as api from '@opentelemetry/api';\nimport {\n hrTimeToNanoseconds,\n timeInputToHrTime,\n urlMatches,\n} from '@opentelemetry/core';\nimport { SemanticAttributes } from '@opentelemetry/semantic-conventions';\n\n// Used to normalize relative URLs\nlet urlNormalizingAnchor: HTMLAnchorElement | undefined;\nfunction getUrlNormalizingAnchor(): HTMLAnchorElement {\n if (!urlNormalizingAnchor) {\n urlNormalizingAnchor = document.createElement('a');\n }\n\n return urlNormalizingAnchor;\n}\n\n/**\n * Helper function to be able to use enum as typed key in type and in interface when using forEach\n * @param obj\n * @param key\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function hasKey<O>(obj: O, key: keyof any): key is keyof O {\n return key in obj;\n}\n\n/**\n * Helper function for starting an event on span based on {@link PerformanceEntries}\n * @param span\n * @param performanceName name of performance entry for time start\n * @param entries\n */\nexport function addSpanNetworkEvent(\n span: api.Span,\n performanceName: string,\n entries: PerformanceEntries\n): api.Span | undefined {\n if (\n hasKey(entries, performanceName) &&\n typeof entries[performanceName] === 'number'\n ) {\n span.addEvent(performanceName, entries[performanceName]);\n return span;\n }\n return undefined;\n}\n\n/**\n * Helper function for adding network events\n * @param span\n * @param resource\n */\nexport function addSpanNetworkEvents(\n span: api.Span,\n resource: PerformanceEntries\n): void {\n addSpanNetworkEvent(span, PTN.FETCH_START, resource);\n addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_START, resource);\n addSpanNetworkEvent(span, PTN.DOMAIN_LOOKUP_END, resource);\n addSpanNetworkEvent(span, PTN.CONNECT_START, resource);\n addSpanNetworkEvent(span, PTN.SECURE_CONNECTION_START, resource);\n addSpanNetworkEvent(span, PTN.CONNECT_END, resource);\n addSpanNetworkEvent(span, PTN.REQUEST_START, resource);\n addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);\n addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);\n const encodedLength = resource[PTN.ENCODED_BODY_SIZE];\n if (encodedLength !== undefined) {\n span.setAttribute(\n SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,\n encodedLength\n );\n }\n const decodedLength = resource[PTN.DECODED_BODY_SIZE];\n // Spec: Not set if transport encoding not used (in which case encoded and decoded sizes match)\n if (decodedLength !== undefined && encodedLength !== decodedLength) {\n span.setAttribute(\n SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,\n decodedLength\n );\n }\n}\n\n/**\n * sort resources by startTime\n * @param filteredResources\n */\nexport function sortResources(filteredResources: PerformanceResourceTiming[]): PerformanceResourceTiming[] {\n return filteredResources.slice().sort((a, b) => {\n const valueA = a[PTN.FETCH_START];\n const valueB = b[PTN.FETCH_START];\n if (valueA > valueB) {\n return 1;\n } else if (valueA < valueB) {\n return -1;\n }\n return 0;\n });\n}\n\n/**\n * Get closest performance resource ignoring the resources that have been\n * already used.\n * @param spanUrl\n * @param startTimeHR\n * @param endTimeHR\n * @param resources\n * @param ignoredResources\n * @param initiatorType\n */\nexport function getResource(\n spanUrl: string,\n startTimeHR: api.HrTime,\n endTimeHR: api.HrTime,\n resources: PerformanceResourceTiming[],\n ignoredResources: WeakSet<PerformanceResourceTiming> = new WeakSet<PerformanceResourceTiming>(),\n initiatorType?: string\n): PerformanceResourceTimingInfo {\n // de-relativize the URL before usage (does no harm to absolute URLs)\n const parsedSpanUrl = parseUrl(spanUrl);\n spanUrl = parsedSpanUrl.toString();\n\n const filteredResources = filterResourcesForSpan(\n spanUrl,\n startTimeHR,\n endTimeHR,\n resources,\n ignoredResources,\n initiatorType\n );\n\n if (filteredResources.length === 0) {\n return {\n mainRequest: undefined,\n };\n }\n if (filteredResources.length === 1) {\n return {\n mainRequest: filteredResources[0],\n };\n }\n const sorted = sortResources(filteredResources);\n\n if (parsedSpanUrl.origin !== location.origin && sorted.length > 1) {\n let corsPreFlightRequest: PerformanceResourceTiming | undefined = sorted[0];\n let mainRequest: PerformanceResourceTiming = findMainRequest(\n sorted,\n corsPreFlightRequest[PTN.RESPONSE_END],\n endTimeHR\n );\n\n const responseEnd = corsPreFlightRequest[PTN.RESPONSE_END];\n const fetchStart = mainRequest[PTN.FETCH_START];\n\n // no corsPreFlightRequest\n if (fetchStart < responseEnd) {\n mainRequest = corsPreFlightRequest;\n corsPreFlightRequest = undefined;\n }\n\n return {\n corsPreFlightRequest,\n mainRequest,\n };\n } else {\n return {\n mainRequest: filteredResources[0],\n };\n }\n}\n\n/**\n * Will find the main request skipping the cors pre flight requests\n * @param resources\n * @param corsPreFlightRequestEndTime\n * @param spanEndTimeHR\n */\nfunction findMainRequest(\n resources: PerformanceResourceTiming[],\n corsPreFlightRequestEndTime: number,\n spanEndTimeHR: api.HrTime\n): PerformanceResourceTiming {\n const spanEndTime = hrTimeToNanoseconds(spanEndTimeHR);\n const minTime = hrTimeToNanoseconds(\n timeInputToHrTime(corsPreFlightRequestEndTime)\n );\n\n let mainRequest: PerformanceResourceTiming = resources[1];\n let bestGap;\n\n const length = resources.length;\n for (let i = 1; i < length; i++) {\n const resource = resources[i];\n const resourceStartTime = hrTimeToNanoseconds(\n timeInputToHrTime(resource[PTN.FETCH_START])\n );\n\n const resourceEndTime = hrTimeToNanoseconds(\n timeInputToHrTime(resource[PTN.RESPONSE_END])\n );\n\n const currentGap = spanEndTime - resourceEndTime;\n\n if (resourceStartTime >= minTime && (!bestGap || currentGap < bestGap)) {\n bestGap = currentGap;\n mainRequest = resource;\n }\n }\n return mainRequest;\n}\n\n/**\n * Filter all resources that has started and finished according to span start time and end time.\n * It will return the closest resource to a start time\n * @param spanUrl\n * @param startTimeHR\n * @param endTimeHR\n * @param resources\n * @param ignoredResources\n */\nfunction filterResourcesForSpan(\n spanUrl: string,\n startTimeHR: api.HrTime,\n endTimeHR: api.HrTime,\n resources: PerformanceResourceTiming[],\n ignoredResources: WeakSet<PerformanceResourceTiming>,\n initiatorType?: string\n) {\n const startTime = hrTimeToNanoseconds(startTimeHR);\n const endTime = hrTimeToNanoseconds(endTimeHR);\n let filteredResources = resources.filter(resource => {\n const resourceStartTime = hrTimeToNanoseconds(\n timeInputToHrTime(resource[PTN.FETCH_START])\n );\n const resourceEndTime = hrTimeToNanoseconds(\n timeInputToHrTime(resource[PTN.RESPONSE_END])\n );\n\n return (\n resource.initiatorType.toLowerCase() ===\n (initiatorType || 'xmlhttprequest') &&\n resource.name === spanUrl &&\n resourceStartTime >= startTime &&\n resourceEndTime <= endTime\n );\n });\n\n if (filteredResources.length > 0) {\n filteredResources = filteredResources.filter(resource => {\n return !ignoredResources.has(resource);\n });\n }\n\n return filteredResources;\n}\n\n/**\n * The URLLike interface represents an URL and HTMLAnchorElement compatible fields.\n */\nexport interface URLLike {\n hash: string;\n host: string;\n hostname: string;\n href: string;\n readonly origin: string;\n password: string;\n pathname: string;\n port: string;\n protocol: string;\n search: string;\n username: string;\n}\n\n/**\n * Parses url using URL constructor or fallback to anchor element.\n * @param url\n */\nexport function parseUrl(url: string): URLLike {\n if (typeof URL === 'function') {\n return new URL(url);\n }\n const element = getUrlNormalizingAnchor();\n element.href = url;\n return element;\n}\n\n/**\n * Parses url using URL constructor or fallback to anchor element and serialize\n * it to a string.\n *\n * Performs the steps described in https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url\n *\n * @param url\n */\nexport function normalizeUrl(url: string): string {\n const urlLike = parseUrl(url);\n return urlLike.href;\n}\n\n/**\n * Get element XPath\n * @param target - target element\n * @param optimised - when id attribute of element is present the xpath can be\n * simplified to contain id\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types\nexport function getElementXPath(target: any, optimised?: boolean): string {\n if (target.nodeType === Node.DOCUMENT_NODE) {\n return '/';\n }\n const targetValue = getNodeValue(target, optimised);\n if (optimised && targetValue.indexOf('@id') > 0) {\n return targetValue;\n }\n let xpath = '';\n if (target.parentNode) {\n xpath += getElementXPath(target.parentNode, false);\n }\n xpath += targetValue;\n\n return xpath;\n}\n\n/**\n * get node index within the siblings\n * @param target\n */\nfunction getNodeIndex(target: HTMLElement): number {\n if (!target.parentNode) {\n return 0;\n }\n const allowedTypes = [target.nodeType];\n if (target.nodeType === Node.CDATA_SECTION_NODE) {\n allowedTypes.push(Node.TEXT_NODE);\n }\n let elements = Array.from(target.parentNode.childNodes);\n elements = elements.filter((element: Node) => {\n const localName = (element as HTMLElement).localName;\n return (\n allowedTypes.indexOf(element.nodeType) >= 0 &&\n localName === target.localName\n );\n });\n if (elements.length >= 1) {\n return elements.indexOf(target) + 1; // xpath starts from 1\n }\n // if there are no other similar child xpath doesn't need index\n return 0;\n}\n\n/**\n * get node value for xpath\n * @param target\n * @param optimised\n */\nfunction getNodeValue(target: HTMLElement, optimised?: boolean): string {\n const nodeType = target.nodeType;\n const index = getNodeIndex(target);\n let nodeValue = '';\n if (nodeType === Node.ELEMENT_NODE) {\n const id = target.getAttribute('id');\n if (optimised && id) {\n return `//*[@id=\"${id}\"]`;\n }\n nodeValue = target.localName;\n } else if (\n nodeType === Node.TEXT_NODE ||\n nodeType === Node.CDATA_SECTION_NODE\n ) {\n nodeValue = 'text()';\n } else if (nodeType === Node.COMMENT_NODE) {\n nodeValue = 'comment()';\n } else {\n return '';\n }\n // if index is 1 it can be omitted in xpath\n if (nodeValue && index > 1) {\n return `/${nodeValue}[${index}]`;\n }\n return `/${nodeValue}`;\n}\n\n/**\n * Checks if trace headers should be propagated\n * @param spanUrl\n * @private\n */\nexport function shouldPropagateTraceHeaders(\n spanUrl: string,\n propagateTraceHeaderCorsUrls?: PropagateTraceHeaderCorsUrls\n): boolean {\n let propagateTraceHeaderUrls = propagateTraceHeaderCorsUrls || [];\n if (\n typeof propagateTraceHeaderUrls === 'string' ||\n propagateTraceHeaderUrls instanceof RegExp\n ) {\n propagateTraceHeaderUrls = [propagateTraceHeaderUrls];\n }\n const parsedSpanUrl = parseUrl(spanUrl);\n\n if (parsedSpanUrl.origin === location.origin) {\n return true;\n } else {\n return propagateTraceHeaderUrls.some(propagateTraceHeaderUrl =>\n urlMatches(spanUrl, propagateTraceHeaderUrl)\n );\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export declare const VERSION = "1.2.0";
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ // this is autogenerated file, see scripts/version-update.js
17
+ export const VERSION = '1.2.0';
18
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,4DAA4D;AAC5D,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// this is autogenerated file, see scripts/version-update.js\nexport const VERSION = '1.2.0';\n"]}
@@ -37,6 +37,7 @@ class StackContextManager {
37
37
  * @param context
38
38
  * @param target Function to be executed within the context
39
39
  */
40
+ // eslint-disable-next-line @typescript-eslint/ban-types
40
41
  _bindFunction(context = api_1.ROOT_CONTEXT, target) {
41
42
  const manager = this;
42
43
  const contextWrapper = function (...args) {