@@ -229,31 +229,22 @@ export function toCollectorExportTraceServiceRequest<
229229export function groupSpans (
230230 spans : ReadableSpan [ ]
231231) : Map < Resource , Map < core . InstrumentationLibrary , ReadableSpan [ ] > > {
232- const spanMap : Map <
233- Resource ,
234- Map < core . InstrumentationLibrary , ReadableSpan [ ] >
235- > = new Map ( ) ;
236- let resourceSpans :
237- | Map < core . InstrumentationLibrary , ReadableSpan [ ] >
238- | undefined ;
239- let libSpans : ReadableSpan [ ] | undefined ;
240-
241- spans . forEach ( span => {
232+ return spans . reduce ( ( spanMap , span ) => {
242233 //group by resource
243- resourceSpans = spanMap . get ( span . resource ) ;
234+ let resourceSpans = spanMap . get ( span . resource ) ;
244235 if ( ! resourceSpans ) {
245236 resourceSpans = new Map < core . InstrumentationLibrary , ReadableSpan [ ] > ( ) ;
246237 spanMap . set ( span . resource , resourceSpans ) ;
247238 }
248239 //group by instrumentation library
249- libSpans = resourceSpans . get ( span . instrumentationLibrary ) ;
240+ let libSpans = resourceSpans . get ( span . instrumentationLibrary ) ;
250241 if ( ! libSpans ) {
251242 libSpans = new Array < ReadableSpan > ( ) ;
252243 resourceSpans . set ( span . instrumentationLibrary , libSpans ) ;
253244 }
254245 libSpans . push ( span ) ;
255- } ) ;
256- return spanMap ;
246+ return spanMap ;
247+ } , new Map < Resource , Map < core . InstrumentationLibrary , ReadableSpan [ ] > > ( ) ) ;
257248}
258249
259250function toCollectorInstrumentationLibrarySpans (
@@ -270,27 +261,14 @@ function toCollectorResourceSpans(
270261 groupedSpans : Map < Resource , Map < core . InstrumentationLibrary , ReadableSpan [ ] > > ,
271262 baseAttributes : Attributes
272263) : opentelemetryProto . trace . v1 . ResourceSpans [ ] {
273- const collectorResourceSpans : opentelemetryProto . trace . v1 . ResourceSpans [ ] = [ ] ;
274-
275- groupedSpans . forEach ( ( libSpans , resource ) => {
276- const collectorResource : opentelemetryProto . resource . v1 . Resource = toCollectorResource (
277- resource ,
278- baseAttributes
279- ) ;
280-
281- const collectorLibSpans : opentelemetryProto . trace . v1 . InstrumentationLibrarySpans [ ] = [ ] ;
282-
283- libSpans . forEach ( ( spans , instrumentationLibrary ) => {
284- collectorLibSpans . push (
285- toCollectorInstrumentationLibrarySpans ( instrumentationLibrary , spans )
286- ) ;
287- } ) ;
288-
289- collectorResourceSpans . push ( {
290- resource : collectorResource ,
291- instrumentationLibrarySpans : collectorLibSpans ,
292- } ) ;
264+ return Array . from ( groupedSpans , ( [ resource , libSpans ] ) => {
265+ return {
266+ resource : toCollectorResource ( resource , baseAttributes ) ,
267+ instrumentationLibrarySpans : Array . from (
268+ libSpans ,
269+ ( [ instrumentationLibrary , spans ] ) =>
270+ toCollectorInstrumentationLibrarySpans ( instrumentationLibrary , spans )
271+ ) ,
272+ } ;
293273 } ) ;
294-
295- return collectorResourceSpans ;
296274}
0 commit comments