@@ -82,9 +82,6 @@ import { logs } from '@opentelemetry/api-logs';
8282const AWS_TRACES_OTLP_ENDPOINT_PATTERN = '^https://xray\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/traces$' ;
8383const AWS_LOGS_OTLP_ENDPOINT_PATTERN = '^https://logs\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/logs$' ;
8484
85- const AWS_OTLP_LOGS_GROUP_HEADER = 'x-aws-log-group' ;
86- const AWS_OTLP_LOGS_STREAM_HEADER = 'x-aws-log-stream' ;
87-
8885const APPLICATION_SIGNALS_ENABLED_CONFIG : string = 'OTEL_AWS_APPLICATION_SIGNALS_ENABLED' ;
8986const APPLICATION_SIGNALS_EXPORTER_ENDPOINT_CONFIG : string = 'OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT' ;
9087const METRIC_EXPORT_INTERVAL_CONFIG : string = 'OTEL_METRIC_EXPORT_INTERVAL' ;
@@ -104,8 +101,8 @@ const AWS_OTLP_LOGS_STREAM_HEADER = 'x-aws-log-stream';
104101const AWS_EMF_METRICS_NAMESPACE = 'x-aws-metric-namespace' ;
105102
106103interface OtlpLogHeaderSetting {
107- logGroup : string ;
108- logStream : string ;
104+ logGroup ? : string ;
105+ logStream ? : string ;
109106 namespace ?: string ;
110107 isValid : boolean ;
111108}
@@ -553,7 +550,7 @@ export class AwsLoggerProcessorProvider {
553550 if (
554551 otlpExporterLogsEndpoint &&
555552 isAwsOtlpEndpoint ( otlpExporterLogsEndpoint , 'logs' ) &&
556- validateLogsHeaders ( )
553+ validateAndFetchLogsHeader ( ) . isValid
557554 ) {
558555 diag . debug ( 'Detected CloudWatch Logs OTLP endpoint. Switching exporter to OTLPAwsLogExporter' ) ;
559556 exporters . push (
@@ -574,7 +571,7 @@ export class AwsLoggerProcessorProvider {
574571 if (
575572 otlpExporterLogsEndpoint &&
576573 isAwsOtlpEndpoint ( otlpExporterLogsEndpoint , 'logs' ) &&
577- validateLogsHeaders ( )
574+ validateAndFetchLogsHeader ( ) . isValid
578575 ) {
579576 diag . debug ( 'Detected CloudWatch Logs OTLP endpoint. Switching exporter to OTLPAwsLogExporter' ) ;
580577 exporters . push (
@@ -935,40 +932,60 @@ export function isAwsOtlpEndpoint(otlpEndpoint: string, service: string): boolea
935932 * Checks if x-aws-log-group and x-aws-log-stream are present in the headers in order to send logs to
936933 * AWS OTLP Logs endpoint.
937934 */
938- function validateLogsHeaders ( ) {
939- const logsHeaders = process . env [ ' OTEL_EXPORTER_OTLP_LOGS_HEADERS' ] ;
935+ export function validateAndFetchLogsHeader ( ) : OtlpLogHeaderSetting {
936+ const logHeaders = process . env . OTEL_EXPORTER_OTLP_LOGS_HEADERS ;
940937
941- if ( ! logsHeaders ) {
938+ if ( ! logHeaders ) {
942939 diag . warn (
943940 'Missing required configuration: The environment variable OTEL_EXPORTER_OTLP_LOGS_HEADERS must be set with ' +
944941 `required headers ${ AWS_OTLP_LOGS_GROUP_HEADER } and ${ AWS_OTLP_LOGS_STREAM_HEADER } . ` +
945942 `Example: OTEL_EXPORTER_OTLP_LOGS_HEADERS="${ AWS_OTLP_LOGS_GROUP_HEADER } =my-log-group,${ AWS_OTLP_LOGS_STREAM_HEADER } =my-log-stream"`
946943 ) ;
947- return false ;
944+ return {
945+ logGroup : '' ,
946+ logStream : '' ,
947+ namespace : '' ,
948+ isValid : false ,
949+ } ;
948950 }
949951
950- let hasLogGroup = false ;
951- let hasLogStream = false ;
952+ let logGroup : string | undefined = undefined ;
953+ let logStream : string | undefined = undefined ;
954+ let namespace : string | undefined = undefined ;
955+ let filteredLogHeadersCount : number = 0 ;
956+
957+ for ( const pair of logHeaders . split ( ',' ) ) {
958+ const splitIndex = pair . indexOf ( '=' ) ;
959+ if ( splitIndex > - 1 ) {
960+ const key = pair . substring ( 0 , splitIndex ) ;
961+ const value = pair . substring ( splitIndex + 1 ) ;
952962
953- for ( const pair of logsHeaders . split ( ',' ) ) {
954- if ( pair . includes ( '=' ) ) {
955- const [ key , value ] = pair . split ( '=' , 2 ) ;
956- if ( key === AWS_OTLP_LOGS_GROUP_HEADER && value ) {
957- hasLogGroup = true ;
958- } else if ( key === AWS_OTLP_LOGS_STREAM_HEADER && value ) {
959- hasLogStream = true ;
963+ if ( key === AWS_OTLP_LOGS_GROUP_HEADER && value !== '' ) {
964+ logGroup = value ;
965+ filteredLogHeadersCount ++ ;
966+ } else if ( key === AWS_OTLP_LOGS_STREAM_HEADER && value !== '' ) {
967+ logStream = value ;
968+ filteredLogHeadersCount ++ ;
969+ } else if ( key === AWS_EMF_METRICS_NAMESPACE && value !== '' ) {
970+ namespace = value ;
960971 }
961972 }
962973 }
963974
964- if ( ! hasLogGroup || ! hasLogStream ) {
975+ const isValid = filteredLogHeadersCount === 2 && ! ! logGroup && ! ! logStream ;
976+ if ( ! isValid ) {
965977 diag . warn (
966978 'Incomplete configuration: Please configure the environment variable OTEL_EXPORTER_OTLP_LOGS_HEADERS ' +
967979 `to have values for ${ AWS_OTLP_LOGS_GROUP_HEADER } and ${ AWS_OTLP_LOGS_STREAM_HEADER } `
968980 ) ;
969- return false ;
970981 }
971- return true ;
982+
983+ return {
984+ logGroup : logGroup ,
985+ logStream : logStream ,
986+ namespace : namespace ,
987+ isValid : isValid ,
988+ } ;
972989}
973990
974991export function checkEmfExporterEnabled ( ) : boolean {
@@ -1003,62 +1020,10 @@ export function createEmfExporter(): AWSCloudWatchEMFExporter | undefined {
10031020 return undefined ;
10041021 }
10051022
1006- return new AWSCloudWatchEMFExporter ( headersResult . namespace , headersResult . logGroup , headersResult . logStream ) ;
1007- }
1008-
1009- /**
1010- * Checks if x-aws-log-group and x-aws-log-stream are present in the headers in order to send logs to
1011- * AWS OTLP Logs endpoint.
1012- */
1013- export function validateAndFetchLogsHeader ( ) : OtlpLogHeaderSetting {
1014- const logHeaders = process . env . OTEL_EXPORTER_OTLP_LOGS_HEADERS ;
1015-
1016- if ( ! logHeaders ) {
1017- diag . warn (
1018- 'Improper configuration: Please configure the environment variable OTEL_EXPORTER_OTLP_LOGS_HEADERS to include x-aws-log-group and x-aws-log-stream'
1019- ) ;
1020- return {
1021- logGroup : '' ,
1022- logStream : '' ,
1023- namespace : '' ,
1024- isValid : false ,
1025- } ;
1026- }
1027-
1028- let logGroup : string | undefined = undefined ;
1029- let logStream : string | undefined = undefined ;
1030- let namespace : string | undefined = undefined ;
1031- let filteredLogHeadersCount : number = 0 ;
1032-
1033- for ( const pair of logHeaders . split ( ',' ) ) {
1034- const splitIndex = pair . indexOf ( '=' ) ;
1035- if ( splitIndex > - 1 ) {
1036- const key = pair . substring ( 0 , splitIndex ) ;
1037- const value = pair . substring ( splitIndex + 1 ) ;
1038-
1039- if ( key === AWS_OTLP_LOGS_GROUP_HEADER && value !== '' ) {
1040- logGroup = value ;
1041- filteredLogHeadersCount ++ ;
1042- } else if ( key === AWS_OTLP_LOGS_STREAM_HEADER && value !== '' ) {
1043- logStream = value ;
1044- filteredLogHeadersCount ++ ;
1045- } else if ( key === AWS_EMF_METRICS_NAMESPACE && value !== '' ) {
1046- namespace = value ;
1047- }
1048- }
1049- }
1050-
1051- const isValid = filteredLogHeadersCount === 2 && ! ! logGroup && ! ! logStream ;
1052- if ( ! isValid ) {
1053- diag . warn (
1054- 'Improper configuration: The environment variable OTEL_EXPORTER_OTLP_LOGS_HEADERS has invalid value(s) for x-aws-log-group or x-aws-log-stream'
1055- ) ;
1056- }
1057-
1058- return {
1059- logGroup : logGroup as string ,
1060- logStream : logStream as string ,
1061- namespace : namespace ,
1062- isValid : isValid ,
1063- } ;
1023+ // If headersResult.isValid is true, then headersResult.logGroup and headersResult.logStream are guaranteed to be strings
1024+ return new AWSCloudWatchEMFExporter (
1025+ headersResult . namespace ,
1026+ headersResult . logGroup as string ,
1027+ headersResult . logStream as string
1028+ ) ;
10641029}
0 commit comments