1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.isAttributeValue = exports.isAttributeKey = exports.sanitizeAttributes = void 0;
|
19 | const api_1 = require("@opentelemetry/api");
|
20 | function sanitizeAttributes(attributes) {
|
21 | const out = {};
|
22 | if (typeof attributes !== 'object' || attributes == null) {
|
23 | return out;
|
24 | }
|
25 | for (const [key, val] of Object.entries(attributes)) {
|
26 | if (!isAttributeKey(key)) {
|
27 | api_1.diag.warn(`Invalid attribute key: ${key}`);
|
28 | continue;
|
29 | }
|
30 | if (!isAttributeValue(val)) {
|
31 | api_1.diag.warn(`Invalid attribute value set for key: ${key}`);
|
32 | continue;
|
33 | }
|
34 | if (Array.isArray(val)) {
|
35 | out[key] = val.slice();
|
36 | }
|
37 | else {
|
38 | out[key] = val;
|
39 | }
|
40 | }
|
41 | return out;
|
42 | }
|
43 | exports.sanitizeAttributes = sanitizeAttributes;
|
44 | function isAttributeKey(key) {
|
45 | return typeof key === 'string' && key.length > 0;
|
46 | }
|
47 | exports.isAttributeKey = isAttributeKey;
|
48 | function isAttributeValue(val) {
|
49 | if (val == null) {
|
50 | return true;
|
51 | }
|
52 | if (Array.isArray(val)) {
|
53 | return isHomogeneousAttributeValueArray(val);
|
54 | }
|
55 | return isValidPrimitiveAttributeValue(val);
|
56 | }
|
57 | exports.isAttributeValue = isAttributeValue;
|
58 | function isHomogeneousAttributeValueArray(arr) {
|
59 | let type;
|
60 | for (const element of arr) {
|
61 |
|
62 | if (element == null)
|
63 | continue;
|
64 | if (!type) {
|
65 | if (isValidPrimitiveAttributeValue(element)) {
|
66 | type = typeof element;
|
67 | continue;
|
68 | }
|
69 |
|
70 | return false;
|
71 | }
|
72 | if (typeof element === type) {
|
73 | continue;
|
74 | }
|
75 | return false;
|
76 | }
|
77 | return true;
|
78 | }
|
79 | function isValidPrimitiveAttributeValue(val) {
|
80 | switch (typeof val) {
|
81 | case 'number':
|
82 | case 'boolean':
|
83 | case 'string':
|
84 | return true;
|
85 | }
|
86 | return false;
|
87 | }
|
88 |
|
\ | No newline at end of file |