-
Notifications
You must be signed in to change notification settings - Fork 825
/
EnvDetectorSync.ts
160 lines (143 loc) · 5.35 KB
/
EnvDetectorSync.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { diag } from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { Resource } from '../Resource';
import { DetectorSync, ResourceAttributes } from '../types';
import { ResourceDetectionConfig } from '../config';
import { IResource } from '../IResource';
/**
* EnvDetectorSync can be used to detect the presence of and create a Resource
* from the OTEL_RESOURCE_ATTRIBUTES environment variable.
*/
class EnvDetectorSync implements DetectorSync {
// Type, attribute keys, and attribute values should not exceed 256 characters.
private readonly _MAX_LENGTH = 255;
// OTEL_RESOURCE_ATTRIBUTES is a comma-separated list of attributes.
private readonly _COMMA_SEPARATOR = ',';
// OTEL_RESOURCE_ATTRIBUTES contains key value pair separated by '='.
private readonly _LABEL_KEY_VALUE_SPLITTER = '=';
private readonly _ERROR_MESSAGE_INVALID_CHARS =
'should be a ASCII string with a length greater than 0 and not exceed ' +
this._MAX_LENGTH +
' characters.';
private readonly _ERROR_MESSAGE_INVALID_VALUE =
'should be a ASCII string with a length not exceed ' +
this._MAX_LENGTH +
' characters.';
/**
* Returns a {@link Resource} populated with attributes from the
* OTEL_RESOURCE_ATTRIBUTES environment variable. Note this is an async
* function to conform to the Detector interface.
*
* @param config The resource detection config
*/
detect(_config?: ResourceDetectionConfig): IResource {
const attributes: ResourceAttributes = {};
const env = getEnv();
const rawAttributes = env.OTEL_RESOURCE_ATTRIBUTES;
const serviceName = env.OTEL_SERVICE_NAME;
if (rawAttributes) {
try {
const parsedAttributes = this._parseResourceAttributes(rawAttributes);
Object.assign(attributes, parsedAttributes);
} catch (e) {
diag.debug(`EnvDetector failed: ${e.message}`);
}
}
if (serviceName) {
attributes[SEMRESATTRS_SERVICE_NAME] = serviceName;
}
return new Resource(attributes);
}
/**
* Creates an attribute map from the OTEL_RESOURCE_ATTRIBUTES environment
* variable.
*
* OTEL_RESOURCE_ATTRIBUTES: A comma-separated list of attributes describing
* the source in more detail, e.g. “key1=val1,key2=val2”. Domain names and
* paths are accepted as attribute keys. Values may be quoted or unquoted in
* general. If a value contains whitespace, =, or " characters, it must
* always be quoted.
*
* @param rawEnvAttributes The resource attributes as a comma-separated list
* of key/value pairs.
* @returns The sanitized resource attributes.
*/
private _parseResourceAttributes(
rawEnvAttributes?: string
): ResourceAttributes {
if (!rawEnvAttributes) return {};
const attributes: ResourceAttributes = {};
const rawAttributes: string[] = rawEnvAttributes.split(
this._COMMA_SEPARATOR,
-1
);
for (const rawAttribute of rawAttributes) {
const keyValuePair: string[] = rawAttribute.split(
this._LABEL_KEY_VALUE_SPLITTER,
-1
);
if (keyValuePair.length !== 2) {
continue;
}
let [key, value] = keyValuePair;
// Leading and trailing whitespaces are trimmed.
key = key.trim();
value = value.trim().split(/^"|"$/).join('');
if (!this._isValidAndNotEmpty(key)) {
throw new Error(`Attribute key ${this._ERROR_MESSAGE_INVALID_CHARS}`);
}
if (!this._isValid(value)) {
throw new Error(`Attribute value ${this._ERROR_MESSAGE_INVALID_VALUE}`);
}
attributes[key] = decodeURIComponent(value);
}
return attributes;
}
/**
* Determines whether the given String is a valid printable ASCII string with
* a length not exceed _MAX_LENGTH characters.
*
* @param str The String to be validated.
* @returns Whether the String is valid.
*/
private _isValid(name: string): boolean {
return name.length <= this._MAX_LENGTH && this._isBaggageOctetString(name);
}
// https://www.w3.org/TR/baggage/#definition
private _isBaggageOctetString(str: string): boolean {
for (let i = 0; i < str.length; i++) {
const ch = str.charCodeAt(i);
if (ch < 0x21 || ch === 0x2c || ch === 0x3b || ch === 0x5c || ch > 0x7e) {
return false;
}
}
return true;
}
/**
* Determines whether the given String is a valid printable ASCII string with
* a length greater than 0 and not exceed _MAX_LENGTH characters.
*
* @param str The String to be validated.
* @returns Whether the String is valid and not empty.
*/
private _isValidAndNotEmpty(str: string): boolean {
return str.length > 0 && this._isValid(str);
}
}
export const envDetectorSync = new EnvDetectorSync();