UNPKG

2.69 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.isAttributeValue = exports.isAttributeKey = exports.sanitizeAttributes = void 0;
19const api_1 = require("@opentelemetry/api");
20function 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}
43exports.sanitizeAttributes = sanitizeAttributes;
44function isAttributeKey(key) {
45 return typeof key === 'string' && key.length > 0;
46}
47exports.isAttributeKey = isAttributeKey;
48function 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}
57exports.isAttributeValue = isAttributeValue;
58function isHomogeneousAttributeValueArray(arr) {
59 let type;
60 for (const element of arr) {
61 // null/undefined elements are allowed
62 if (element == null)
63 continue;
64 if (!type) {
65 if (isValidPrimitiveAttributeValue(element)) {
66 type = typeof element;
67 continue;
68 }
69 // encountered an invalid primitive
70 return false;
71 }
72 if (typeof element === type) {
73 continue;
74 }
75 return false;
76 }
77 return true;
78}
79function isValidPrimitiveAttributeValue(val) {
80 switch (typeof val) {
81 case 'number':
82 case 'boolean':
83 case 'string':
84 return true;
85 }
86 return false;
87}
88//# sourceMappingURL=attributes.js.map
\No newline at end of file