Skip to content

Commit 0e09680

Browse files
feat(opentelemetry-configuration): add backup options for certificates and endpoints (#6038)
Co-authored-by: Jamie Danielson <[email protected]>
1 parent c67f1d4 commit 0e09680

File tree

3 files changed

+129
-30
lines changed

3 files changed

+129
-30
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
7373
* feat(opentelemetry-configuration): parse logger provider from config file [#5995](https://github.com/open-telemetry/opentelemetry-js/pull/5995) @maryliag
7474
* feat(opentelemetry-configuration): parse meter provider from config file [#6000](https://github.com/open-telemetry/opentelemetry-js/pull/6000) @maryliag
7575
* feat(opentelemetry-configuration): parse config file with format 1.0-rc.2 [#6029](https://github.com/open-telemetry/opentelemetry-js/pull/6029) @maryliag
76+
* feat(opentelemetry-configuration): add backup options for certificates and endpoints [#6038](https://github.com/open-telemetry/opentelemetry-js/pull/6038) @maryliag
7677

7778
### :bug: Bug Fixes
7879

experimental/packages/opentelemetry-configuration/src/EnvironmentConfigProvider.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -200,28 +200,32 @@ export function setTracerProvider(config: ConfigurationModel): void {
200200
batch.max_export_batch_size = maxExportBatchSize;
201201
}
202202

203-
const endpoint = getStringFromEnv('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT');
203+
const endpoint =
204+
getStringFromEnv('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT') ??
205+
(getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT')
206+
? `${getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT')}/v1/traces`
207+
: null);
204208
if (endpoint && batch.exporter.otlp_http) {
205209
batch.exporter.otlp_http.endpoint = endpoint;
206210
}
207211

208-
const certificateFile = getStringFromEnv(
209-
'OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE'
210-
);
212+
const certificateFile =
213+
getStringFromEnv('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE') ??
214+
getStringFromEnv('OTEL_EXPORTER_OTLP_CERTIFICATE');
211215
if (certificateFile && batch.exporter.otlp_http) {
212216
batch.exporter.otlp_http.certificate_file = certificateFile;
213217
}
214218

215-
const clientKeyFile = getStringFromEnv(
216-
'OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY'
217-
);
219+
const clientKeyFile =
220+
getStringFromEnv('OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY') ??
221+
getStringFromEnv('OTEL_EXPORTER_OTLP_CLIENT_KEY');
218222
if (clientKeyFile && batch.exporter.otlp_http) {
219223
batch.exporter.otlp_http.client_key_file = clientKeyFile;
220224
}
221225

222-
const clientCertificateFile = getStringFromEnv(
223-
'OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE'
224-
);
226+
const clientCertificateFile =
227+
getStringFromEnv('OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE') ??
228+
getStringFromEnv('OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE');
225229
if (clientCertificateFile && batch.exporter.otlp_http) {
226230
batch.exporter.otlp_http.client_certificate_file = clientCertificateFile;
227231
}
@@ -269,28 +273,32 @@ export function setMeterProvider(config: ConfigurationModel): void {
269273
readerPeriodic.exporter.otlp_http = {};
270274
}
271275

272-
const endpoint = getStringFromEnv('OTEL_EXPORTER_OTLP_METRICS_ENDPOINT');
276+
const endpoint =
277+
getStringFromEnv('OTEL_EXPORTER_OTLP_METRICS_ENDPOINT') ??
278+
(getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT')
279+
? `${getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT')}/v1/metrics`
280+
: null);
273281
if (endpoint) {
274282
readerPeriodic.exporter.otlp_http.endpoint = endpoint;
275283
}
276284

277-
const certificateFile = getStringFromEnv(
278-
'OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE'
279-
);
285+
const certificateFile =
286+
getStringFromEnv('OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE') ??
287+
getStringFromEnv('OTEL_EXPORTER_OTLP_CERTIFICATE');
280288
if (certificateFile) {
281289
readerPeriodic.exporter.otlp_http.certificate_file = certificateFile;
282290
}
283291

284-
const clientKeyFile = getStringFromEnv(
285-
'OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY'
286-
);
292+
const clientKeyFile =
293+
getStringFromEnv('OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY') ??
294+
getStringFromEnv('OTEL_EXPORTER_OTLP_CLIENT_KEY');
287295
if (clientKeyFile) {
288296
readerPeriodic.exporter.otlp_http.client_key_file = clientKeyFile;
289297
}
290298

291-
const clientCertificateFile = getStringFromEnv(
292-
'OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE'
293-
);
299+
const clientCertificateFile =
300+
getStringFromEnv('OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE') ??
301+
getStringFromEnv('OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE');
294302
if (clientCertificateFile) {
295303
readerPeriodic.exporter.otlp_http.client_certificate_file =
296304
clientCertificateFile;
@@ -430,28 +438,32 @@ export function setLoggerProvider(config: ConfigurationModel): void {
430438
batch.max_export_batch_size = maxExportBatchSize;
431439
}
432440

433-
const endpoint = getStringFromEnv('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT');
441+
const endpoint =
442+
getStringFromEnv('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT') ??
443+
(getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT')
444+
? `${getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT')}/v1/logs`
445+
: null);
434446
if (endpoint && batch.exporter.otlp_http) {
435447
batch.exporter.otlp_http.endpoint = endpoint;
436448
}
437449

438-
const certificateFile = getStringFromEnv(
439-
'OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE'
440-
);
450+
const certificateFile =
451+
getStringFromEnv('OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE') ??
452+
getStringFromEnv('OTEL_EXPORTER_OTLP_CERTIFICATE');
441453
if (certificateFile && batch.exporter.otlp_http) {
442454
batch.exporter.otlp_http.certificate_file = certificateFile;
443455
}
444456

445-
const clientKeyFile = getStringFromEnv(
446-
'OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY'
447-
);
457+
const clientKeyFile =
458+
getStringFromEnv('OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY') ??
459+
getStringFromEnv('OTEL_EXPORTER_OTLP_CLIENT_KEY');
448460
if (clientKeyFile && batch.exporter.otlp_http) {
449461
batch.exporter.otlp_http.client_key_file = clientKeyFile;
450462
}
451463

452-
const clientCertificateFile = getStringFromEnv(
453-
'OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE'
454-
);
464+
const clientCertificateFile =
465+
getStringFromEnv('OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE') ??
466+
getStringFromEnv('OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE');
455467
if (clientCertificateFile && batch.exporter.otlp_http) {
456468
batch.exporter.otlp_http.client_certificate_file = clientCertificateFile;
457469
}

experimental/packages/opentelemetry-configuration/test/ConfigProvider.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,92 @@ describe('ConfigProvider', function () {
10521052
);
10531053
});
10541054

1055+
it('should use backup option for certificates', function () {
1056+
process.env.OTEL_EXPORTER_OTLP_CERTIFICATE =
1057+
'backup_certificate_file.pem';
1058+
process.env.OTEL_EXPORTER_OTLP_CLIENT_KEY = 'backup_client_key.pem';
1059+
process.env.OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE =
1060+
'backup_client_certificate.pem';
1061+
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://backup.com:4318';
1062+
1063+
const expectedConfig: Configuration = {
1064+
...defaultConfig,
1065+
tracer_provider: {
1066+
processors: [
1067+
{
1068+
batch: {
1069+
...defaultConfig.tracer_provider?.processors[0].batch,
1070+
exporter: {
1071+
otlp_http: {
1072+
endpoint: 'http://backup.com:4318/v1/traces',
1073+
timeout: 10000,
1074+
encoding: OtlpHttpEncoding.Protobuf,
1075+
certificate_file: 'backup_certificate_file.pem',
1076+
client_certificate_file: 'backup_client_certificate.pem',
1077+
client_key_file: 'backup_client_key.pem',
1078+
},
1079+
},
1080+
},
1081+
},
1082+
],
1083+
limits: defaultConfig.tracer_provider?.limits,
1084+
sampler: defaultConfig.tracer_provider?.sampler,
1085+
},
1086+
meter_provider: {
1087+
...defaultConfig.meter_provider,
1088+
readers: [
1089+
{
1090+
periodic: {
1091+
interval: 60000,
1092+
timeout: 30000,
1093+
exporter: {
1094+
otlp_http: {
1095+
endpoint: 'http://backup.com:4318/v1/metrics',
1096+
timeout: 10000,
1097+
temporality_preference:
1098+
ExporterTemporalityPreference.Cumulative,
1099+
default_histogram_aggregation:
1100+
ExporterDefaultHistogramAggregation.ExplicitBucketHistogram,
1101+
certificate_file: 'backup_certificate_file.pem',
1102+
client_certificate_file: 'backup_client_certificate.pem',
1103+
client_key_file: 'backup_client_key.pem',
1104+
},
1105+
},
1106+
},
1107+
},
1108+
],
1109+
},
1110+
logger_provider: {
1111+
...defaultConfig.logger_provider,
1112+
processors: [
1113+
{
1114+
batch: {
1115+
schedule_delay: 1000,
1116+
export_timeout: 30000,
1117+
max_queue_size: 2048,
1118+
max_export_batch_size: 512,
1119+
exporter: {
1120+
otlp_http: {
1121+
endpoint: 'http://backup.com:4318/v1/logs',
1122+
timeout: 10000,
1123+
encoding: OtlpHttpEncoding.Protobuf,
1124+
certificate_file: 'backup_certificate_file.pem',
1125+
client_certificate_file: 'backup_client_certificate.pem',
1126+
client_key_file: 'backup_client_key.pem',
1127+
},
1128+
},
1129+
},
1130+
},
1131+
],
1132+
},
1133+
};
1134+
const configProvider = createConfigProvider();
1135+
assert.deepStrictEqual(
1136+
configProvider.getInstrumentationConfig(),
1137+
expectedConfig
1138+
);
1139+
});
1140+
10551141
it('checks to keep good code coverage', function () {
10561142
let config: ConfigurationModel = {};
10571143
setResources(config);

0 commit comments

Comments
 (0)