1
1
import {
2
2
BatchGetTracesCommand ,
3
3
GetTraceSummariesCommand ,
4
+ type Trace ,
4
5
XRayClient ,
5
6
} from '@aws-sdk/client-xray' ;
6
7
import promiseRetry from 'promise-retry' ;
@@ -87,10 +88,49 @@ const retriableGetTraceIds = (options: GetXRayTraceIdsOptions) =>
87
88
}
88
89
} ) ;
89
90
91
+ /**
92
+ * Parse and sort the trace segments by start time
93
+ *
94
+ * @param trace - The trace to parse and sort
95
+ * @param expectedSegmentsCount - The expected segments count for the trace
96
+ */
97
+ const parseAndSortTrace = ( trace : Trace , expectedSegmentsCount : number ) => {
98
+ const { Id : id , Segments : segments } = trace ;
99
+ if ( segments === undefined || segments . length !== expectedSegmentsCount ) {
100
+ throw new Error (
101
+ `Expected ${ expectedSegmentsCount } segments, got ${ segments ? segments . length : 0 } for traceId ${ trace . Id } `
102
+ ) ;
103
+ }
104
+
105
+ const parsedSegments : XRaySegmentParsed [ ] = [ ] ;
106
+ for ( const segment of segments ) {
107
+ const { Id, Document } = segment ;
108
+ if ( Document === undefined || Id === undefined ) {
109
+ throw new Error (
110
+ `Segment document or id are missing for traceId ${ trace . Id } `
111
+ ) ;
112
+ }
113
+
114
+ parsedSegments . push ( {
115
+ Id,
116
+ Document : JSON . parse ( Document ) as XRayTraceDocumentParsed ,
117
+ } ) ;
118
+ }
119
+ const sortedSegments = parsedSegments . sort (
120
+ ( a , b ) => a . Document . start_time - b . Document . start_time
121
+ ) ;
122
+
123
+ return {
124
+ Id : id as string ,
125
+ Segments : sortedSegments ,
126
+ } ;
127
+ } ;
128
+
90
129
/**
91
130
* Get the trace details for a given trace ID from the AWS X-Ray API.
92
131
*
93
- * When the trace is returned, the segments are parsed, since the document is returned as a string.
132
+ * When the trace is returned, the segments are parsed, since the document is returned
133
+ * stringified, and then sorted by start time.
94
134
*
95
135
* @param options - The options to get trace details, including the trace IDs and expected segments count
96
136
*/
@@ -112,37 +152,15 @@ const getTraceDetails = async (
112
152
) ;
113
153
}
114
154
115
- const parsedTraces : XRayTraceParsed [ ] = [ ] ;
155
+ const parsedAndSortedTraces : XRayTraceParsed [ ] = [ ] ;
116
156
for ( const trace of traces ) {
117
- const { Id : id , Segments : segments } = trace ;
118
- if ( segments === undefined || segments . length !== expectedSegmentsCount ) {
119
- throw new Error (
120
- `Expected ${ expectedSegmentsCount } segments, got ${ segments ? segments . length : 0 } for traceId ${ trace . Id } `
121
- ) ;
122
- }
123
-
124
- const parsedSegments : XRaySegmentParsed [ ] = [ ] ;
125
- for ( const segment of segments ) {
126
- const { Id, Document } = segment ;
127
- if ( Document === undefined || Id === undefined ) {
128
- throw new Error (
129
- `Segment document or id are missing for traceId ${ trace . Id } `
130
- ) ;
131
- }
132
-
133
- parsedSegments . push ( {
134
- Id,
135
- Document : JSON . parse ( Document ) as XRayTraceDocumentParsed ,
136
- } ) ;
137
- }
138
-
139
- parsedTraces . push ( {
140
- Id : id as string ,
141
- Segments : parsedSegments ,
142
- } ) ;
157
+ parsedAndSortedTraces . push ( parseAndSortTrace ( trace , expectedSegmentsCount ) ) ;
143
158
}
144
159
145
- return parsedTraces ;
160
+ return parsedAndSortedTraces . sort (
161
+ ( a , b ) =>
162
+ a . Segments [ 0 ] . Document . start_time - b . Segments [ 0 ] . Document . start_time
163
+ ) ;
146
164
} ;
147
165
148
166
/**
0 commit comments