-
Notifications
You must be signed in to change notification settings - Fork 767
/
util.ts
176 lines (155 loc) · 4.77 KB
/
util.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* 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 * as path from 'path';
import { URL } from 'url';
import * as fs from 'fs';
import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base';
import {
createInsecureCredentials,
createSslCredentials,
} from './grpc-exporter-transport';
// NOTE: do not change these type imports to actual imports. Doing so WILL break `@opentelemetry/instrumentation-http`,
// as they'd be imported before the http/https modules can be wrapped.
import type { ChannelCredentials } from '@grpc/grpc-js';
export const DEFAULT_COLLECTOR_URL = 'http://localhost:4317';
export function validateAndNormalizeUrl(url: string): string {
const hasProtocol = url.match(/^([\w]{1,8}):\/\//);
if (!hasProtocol) {
url = `https://${url}`;
}
const target = new URL(url);
if (target.protocol === 'unix:') {
return url;
}
if (target.pathname && target.pathname !== '/') {
diag.warn(
'URL path should not be set when using grpc, the path part of the URL will be ignored.'
);
}
if (target.protocol !== '' && !target.protocol?.match(/^(http)s?:$/)) {
diag.warn('URL protocol should be http(s)://. Using http://.');
}
return target.host;
}
export function configureCredentials(
credentials: ChannelCredentials | undefined,
endpoint: string
): ChannelCredentials {
let insecure: boolean;
if (credentials) {
return credentials;
} else if (endpoint.startsWith('https://')) {
insecure = false;
} else if (
endpoint.startsWith('http://') ||
endpoint === DEFAULT_COLLECTOR_URL
) {
insecure = true;
} else {
insecure = getSecurityFromEnv();
}
if (insecure) {
return createInsecureCredentials();
} else {
return getCredentialsFromEnvironment();
}
}
function getSecurityFromEnv(): boolean {
const definedInsecure =
getEnv().OTEL_EXPORTER_OTLP_TRACES_INSECURE ||
getEnv().OTEL_EXPORTER_OTLP_INSECURE;
if (definedInsecure) {
return definedInsecure.toLowerCase() === 'true';
} else {
return false;
}
}
/**
* Exported for testing
*/
export function getCredentialsFromEnvironment(): ChannelCredentials {
const rootCert = retrieveRootCert();
const privateKey = retrievePrivateKey();
const certChain = retrieveCertChain();
return createSslCredentials(rootCert, privateKey, certChain);
}
function retrieveRootCert(): Buffer | undefined {
const rootCertificate =
getEnv().OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE ||
getEnv().OTEL_EXPORTER_OTLP_CERTIFICATE;
if (rootCertificate) {
try {
return fs.readFileSync(path.resolve(process.cwd(), rootCertificate));
} catch {
diag.warn('Failed to read root certificate file');
return undefined;
}
} else {
return undefined;
}
}
function retrievePrivateKey(): Buffer | undefined {
const clientKey =
getEnv().OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY ||
getEnv().OTEL_EXPORTER_OTLP_CLIENT_KEY;
if (clientKey) {
try {
return fs.readFileSync(path.resolve(process.cwd(), clientKey));
} catch {
diag.warn('Failed to read client certificate private key file');
return undefined;
}
} else {
return undefined;
}
}
function retrieveCertChain(): Buffer | undefined {
const clientChain =
getEnv().OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE ||
getEnv().OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE;
if (clientChain) {
try {
return fs.readFileSync(path.resolve(process.cwd(), clientChain));
} catch {
diag.warn('Failed to read client certificate chain file');
return undefined;
}
} else {
return undefined;
}
}
export function configureCompression(
compression: CompressionAlgorithm | undefined
): CompressionAlgorithm {
if (compression != null) {
return compression;
}
const envCompression =
getEnv().OTEL_EXPORTER_OTLP_TRACES_COMPRESSION ||
getEnv().OTEL_EXPORTER_OTLP_COMPRESSION ||
'none';
if (envCompression === 'gzip') {
return CompressionAlgorithm.GZIP;
} else if (envCompression === 'none') {
return CompressionAlgorithm.NONE;
}
diag.warn(
'Unknown compression "' + envCompression + '", falling back to "none"'
);
return CompressionAlgorithm.NONE;
}