@@ -39,7 +39,8 @@ class OTLPExporter {
3939
4040 constructor (
4141 private readonly _eventRepository : EventRepository ,
42- private readonly _verbose : boolean
42+ private readonly _verbose : boolean ,
43+ private readonly _spanAttributeValueLengthLimit : number
4344 ) {
4445 this . _tracer = trace . getTracer ( "otlp-exporter" ) ;
4546 }
@@ -52,7 +53,7 @@ class OTLPExporter {
5253 this . #logExportTracesVerbose( request ) ;
5354
5455 const events = this . #filterResourceSpans( request . resourceSpans ) . flatMap ( ( resourceSpan ) => {
55- return convertSpansToCreateableEvents ( resourceSpan ) ;
56+ return convertSpansToCreateableEvents ( resourceSpan , this . _spanAttributeValueLengthLimit ) ;
5657 } ) ;
5758
5859 const enrichedEvents = enrichCreatableEvents ( events ) ;
@@ -79,7 +80,7 @@ class OTLPExporter {
7980 this . #logExportLogsVerbose( request ) ;
8081
8182 const events = this . #filterResourceLogs( request . resourceLogs ) . flatMap ( ( resourceLog ) => {
82- return convertLogsToCreateableEvents ( resourceLog ) ;
83+ return convertLogsToCreateableEvents ( resourceLog , this . _spanAttributeValueLengthLimit ) ;
8384 } ) ;
8485
8586 const enrichedEvents = enrichCreatableEvents ( events ) ;
@@ -180,7 +181,10 @@ class OTLPExporter {
180181 }
181182}
182183
183- function convertLogsToCreateableEvents ( resourceLog : ResourceLogs ) : Array < CreatableEvent > {
184+ function convertLogsToCreateableEvents (
185+ resourceLog : ResourceLogs ,
186+ spanAttributeValueLengthLimit : number
187+ ) : Array < CreatableEvent > {
184188 const resourceAttributes = resourceLog . resource ?. attributes ?? [ ] ;
185189
186190 const resourceProperties = extractEventProperties ( resourceAttributes ) ;
@@ -213,10 +217,10 @@ function convertLogsToCreateableEvents(resourceLog: ResourceLogs): Array<Creatab
213217 status : logLevelToEventStatus ( log . severityNumber ) ,
214218 startTime : log . timeUnixNano ,
215219 properties : {
216- ...convertKeyValueItemsToMap ( log . attributes ?? [ ] , [
217- SemanticInternalAttributes . SPAN_ID ,
218- SemanticInternalAttributes . SPAN_PARTIAL ,
219- ] ) ,
220+ ...convertKeyValueItemsToMap (
221+ truncateAttributes ( log . attributes ?? [ ] , spanAttributeValueLengthLimit ) ,
222+ [ SemanticInternalAttributes . SPAN_ID , SemanticInternalAttributes . SPAN_PARTIAL ]
223+ ) ,
220224 } ,
221225 style : convertKeyValueItemsToMap (
222226 pickAttributes ( log . attributes ?? [ ] , SemanticInternalAttributes . STYLE ) ,
@@ -283,7 +287,10 @@ function convertLogsToCreateableEvents(resourceLog: ResourceLogs): Array<Creatab
283287 } ) ;
284288}
285289
286- function convertSpansToCreateableEvents ( resourceSpan : ResourceSpans ) : Array < CreatableEvent > {
290+ function convertSpansToCreateableEvents (
291+ resourceSpan : ResourceSpans ,
292+ spanAttributeValueLengthLimit : number
293+ ) : Array < CreatableEvent > {
287294 const resourceAttributes = resourceSpan . resource ?. attributes ?? [ ] ;
288295
289296 const resourceProperties = extractEventProperties ( resourceAttributes ) ;
@@ -323,10 +330,10 @@ function convertSpansToCreateableEvents(resourceSpan: ResourceSpans): Array<Crea
323330 events : spanEventsToEventEvents ( span . events ?? [ ] ) ,
324331 duration : span . endTimeUnixNano - span . startTimeUnixNano ,
325332 properties : {
326- ...convertKeyValueItemsToMap ( span . attributes ?? [ ] , [
327- SemanticInternalAttributes . SPAN_ID ,
328- SemanticInternalAttributes . SPAN_PARTIAL ,
329- ] ) ,
333+ ...convertKeyValueItemsToMap (
334+ truncateAttributes ( span . attributes ?? [ ] , spanAttributeValueLengthLimit ) ,
335+ [ SemanticInternalAttributes . SPAN_ID , SemanticInternalAttributes . SPAN_PARTIAL ]
336+ ) ,
330337 } ,
331338 style : convertKeyValueItemsToMap (
332339 pickAttributes ( span . attributes ?? [ ] , SemanticInternalAttributes . STYLE ) ,
@@ -852,7 +859,23 @@ function binaryToHex(buffer: Buffer | string | undefined): string | undefined {
852859 return Buffer . from ( Array . from ( buffer ) ) . toString ( "hex" ) ;
853860}
854861
862+ function truncateAttributes ( attributes : KeyValue [ ] , maximumLength : number = 1024 ) : KeyValue [ ] {
863+ return attributes . map ( ( attribute ) => {
864+ return isStringValue ( attribute . value )
865+ ? {
866+ key : attribute . key ,
867+ value : {
868+ stringValue : attribute . value . stringValue . slice ( 0 , maximumLength ) ,
869+ } ,
870+ }
871+ : attribute ;
872+ } ) ;
873+ }
874+
855875export const otlpExporter = new OTLPExporter (
856876 eventRepository ,
857- process . env . OTLP_EXPORTER_VERBOSE === "1"
877+ process . env . OTLP_EXPORTER_VERBOSE === "1" ,
878+ process . env . SERVER_OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT
879+ ? parseInt ( process . env . SERVER_OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT , 10 )
880+ : 8192
858881) ;
0 commit comments