@@ -31,6 +31,8 @@ import {
3131 ExporterDefaultHistogramAggregation ,
3232 ExporterTemporalityPreference ,
3333} from './models/meterProviderModel' ;
34+ import { OtlpHttpEncoding } from './models/commonModel' ;
35+ import { diag } from '@opentelemetry/api' ;
3436
3537/**
3638 * EnvironmentConfigProvider provides a configuration based on environment variables.
@@ -456,57 +458,123 @@ export function setLoggerProvider(config: ConfigurationModel): void {
456458 batch . max_export_batch_size = maxExportBatchSize ;
457459 }
458460
459- const endpoint =
460- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT' ) ??
461- ( getStringFromEnv ( 'OTEL_EXPORTER_OTLP_ENDPOINT' )
462- ? `${ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_ENDPOINT' ) } /v1/logs`
463- : null ) ;
464- if ( endpoint && batch . exporter . otlp_http ) {
465- batch . exporter . otlp_http . endpoint = endpoint ;
466- }
467-
468- const certificateFile =
469- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE' ) ??
470- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_CERTIFICATE' ) ;
471- if ( certificateFile && batch . exporter . otlp_http ) {
472- batch . exporter . otlp_http . certificate_file = certificateFile ;
473- }
474-
475- const clientKeyFile =
476- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY' ) ??
477- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_CLIENT_KEY' ) ;
478- if ( clientKeyFile && batch . exporter . otlp_http ) {
479- batch . exporter . otlp_http . client_key_file = clientKeyFile ;
480- }
481-
482- const clientCertificateFile =
483- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE' ) ??
484- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE' ) ;
485- if ( clientCertificateFile && batch . exporter . otlp_http ) {
486- batch . exporter . otlp_http . client_certificate_file = clientCertificateFile ;
487- }
488-
489- const compression =
490- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_COMPRESSION' ) ??
491- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_COMPRESSION' ) ;
492- if ( compression && batch . exporter . otlp_http ) {
493- batch . exporter . otlp_http . compression = compression ;
494- }
495-
496- const timeout =
497- getNumberFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_TIMEOUT' ) ??
498- getNumberFromEnv ( 'OTEL_EXPORTER_OTLP_TIMEOUT' ) ;
499- if ( timeout && batch . exporter . otlp_http ) {
500- batch . exporter . otlp_http . timeout = timeout ;
501- }
502-
503- const headersList =
504- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_HEADERS' ) ??
505- getStringFromEnv ( 'OTEL_EXPORTER_OTLP_HEADERS' ) ;
506- if ( headersList && batch . exporter . otlp_http ) {
507- batch . exporter . otlp_http . headers_list = headersList ;
461+ const exportersType = Array . from (
462+ new Set ( getStringListFromEnv ( 'OTEL_LOGS_EXPORTER' ) )
463+ ) ;
464+ if ( exportersType . length === 0 ) {
465+ exportersType . push ( 'otlp' ) ;
466+ }
467+
468+ config . logger_provider . processors = [ ] ;
469+ if ( exportersType . includes ( 'none' ) ) {
470+ diag . info (
471+ `OTEL_LOGS_EXPORTER contains "none". Logger provider will not be initialized.`
472+ ) ;
473+ return ;
474+ }
475+
476+ for ( let i = 0 ; i < exportersType . length ; i ++ ) {
477+ const exporterType = exportersType [ i ] ;
478+ const batchInfo = { ...batch } ;
479+ if ( exporterType === 'console' ) {
480+ config . logger_provider . processors . push ( {
481+ simple : { exporter : { console : { } } } ,
482+ } ) ;
483+ } else {
484+ // 'otlp' and default
485+ const protocol =
486+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_PROTOCOL' ) ??
487+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_PROTOCOL' ) ??
488+ 'http/protobuf' ;
489+ const certificateFile =
490+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE' ) ??
491+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_CERTIFICATE' ) ;
492+ const clientKeyFile =
493+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY' ) ??
494+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_CLIENT_KEY' ) ;
495+ const clientCertificateFile =
496+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE' ) ??
497+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE' ) ;
498+ const compression =
499+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_COMPRESSION' ) ??
500+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_COMPRESSION' ) ;
501+ const timeout =
502+ getNumberFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_TIMEOUT' ) ??
503+ getNumberFromEnv ( 'OTEL_EXPORTER_OTLP_TIMEOUT' ) ??
504+ 10000 ;
505+ const headersList =
506+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_HEADERS' ) ??
507+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_HEADERS' ) ;
508+
509+ if ( protocol === 'grpc' ) {
510+ delete batchInfo . exporter . otlp_http ;
511+ batchInfo . exporter . otlp_grpc = { } ;
512+ const endpoint =
513+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT' ) ??
514+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_ENDPOINT' ) ??
515+ 'http://localhost:4317' ;
516+ if ( endpoint ) {
517+ batchInfo . exporter . otlp_grpc . endpoint = endpoint ;
518+ }
519+ if ( certificateFile ) {
520+ batchInfo . exporter . otlp_grpc . certificate_file = certificateFile ;
521+ }
522+ if ( clientKeyFile ) {
523+ batchInfo . exporter . otlp_grpc . client_key_file = clientKeyFile ;
524+ }
525+ if ( clientCertificateFile ) {
526+ batchInfo . exporter . otlp_grpc . client_certificate_file =
527+ clientCertificateFile ;
528+ }
529+ if ( compression ) {
530+ batchInfo . exporter . otlp_grpc . compression = compression ;
531+ }
532+ if ( timeout ) {
533+ batchInfo . exporter . otlp_grpc . timeout = timeout ;
534+ }
535+ if ( headersList ) {
536+ batchInfo . exporter . otlp_grpc . headers_list = headersList ;
537+ }
538+ } else {
539+ if ( batchInfo . exporter . otlp_http == null ) {
540+ batchInfo . exporter . otlp_http = { } ;
541+ }
542+ const endpoint =
543+ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT' ) ??
544+ ( getStringFromEnv ( 'OTEL_EXPORTER_OTLP_ENDPOINT' )
545+ ? `${ getStringFromEnv ( 'OTEL_EXPORTER_OTLP_ENDPOINT' ) } /v1/logs`
546+ : null ) ;
547+ if ( endpoint ) {
548+ batchInfo . exporter . otlp_http . endpoint = endpoint ;
549+ }
550+ if ( certificateFile ) {
551+ batchInfo . exporter . otlp_http . certificate_file = certificateFile ;
552+ }
553+ if ( clientKeyFile ) {
554+ batchInfo . exporter . otlp_http . client_key_file = clientKeyFile ;
555+ }
556+ if ( clientCertificateFile ) {
557+ batchInfo . exporter . otlp_http . client_certificate_file =
558+ clientCertificateFile ;
559+ }
560+ if ( compression ) {
561+ batchInfo . exporter . otlp_http . compression = compression ;
562+ }
563+ if ( timeout ) {
564+ batchInfo . exporter . otlp_http . timeout = timeout ;
565+ }
566+ if ( headersList ) {
567+ batchInfo . exporter . otlp_http . headers_list = headersList ;
568+ }
569+
570+ if ( protocol === 'http/json' ) {
571+ batchInfo . exporter . otlp_http . encoding = OtlpHttpEncoding . JSON ;
572+ } else if ( protocol === 'http/protobuf' ) {
573+ batchInfo . exporter . otlp_http . encoding = OtlpHttpEncoding . Protobuf ;
574+ }
575+ }
576+ config . logger_provider . processors . push ( { batch : batchInfo } ) ;
577+ }
508578 }
509-
510- config . logger_provider . processors [ 0 ] . batch = batch ;
511579 }
512580}
0 commit comments