@@ -35,12 +35,13 @@ data WatDoOnEOF = StopOnEOF | SleepAndRetryOnEOF
35
35
36
36
data State = S
37
37
{ originTimestamp :: ! Timestamp ,
38
- threadMap :: IM. IntMap ThreadId ,
38
+ cap2thread :: IM. IntMap ThreadId ,
39
39
spans :: HM. HashMap SpanId Span ,
40
40
instrumentMap :: HM. HashMap InstrumentId CaptureInstrument ,
41
41
traceMap :: HM. HashMap ThreadId TraceId ,
42
42
serial2sid :: HM. HashMap Word64 SpanId ,
43
43
thread2sid :: HM. HashMap ThreadId SpanId ,
44
+ thread2displayThread :: HM. HashMap ThreadId ThreadId , -- https://github.com/ethercrow/opentelemetry-haskell/issues/40
44
45
gcRequestedAt :: ! Timestamp ,
45
46
gcStartedAt :: ! Timestamp ,
46
47
gcGeneration :: ! Int ,
@@ -52,7 +53,7 @@ data State = S
52
53
deriving (Show )
53
54
54
55
initialState :: Word64 -> R. SMGen -> State
55
- initialState timestamp = S timestamp mempty mempty mempty mempty mempty mempty 0 0 0 0 0 0
56
+ initialState timestamp = S timestamp mempty mempty mempty mempty mempty mempty mempty 0 0 0 0 0 0
56
57
57
58
data EventSource
58
59
= EventLogHandle Handle WatDoOnEOF
@@ -142,9 +143,9 @@ parseOpenTelemetry UserBinaryMessage {payload} = parseByteString payload
142
143
parseOpenTelemetry _ = Nothing
143
144
144
145
processEvent :: Event -> State -> (State , [Span ], [Metric ])
145
- processEvent (Event ts ev m_cap) st@ ( S {.. }) =
146
+ processEvent (Event ts ev m_cap) st@ S {.. } =
146
147
let now = originTimestamp + ts
147
- m_thread_id = m_cap >>= flip IM. lookup threadMap
148
+ m_thread_id = m_cap >>= flip IM. lookup cap2thread
148
149
m_trace_id = m_thread_id >>= flip HM. lookup traceMap
149
150
in case (ev, m_cap, m_thread_id) of
150
151
(WallClockTime {sec, nsec}, _, _) ->
@@ -158,12 +159,13 @@ processEvent (Event ts ev m_cap) st@(S {..}) =
158
159
[Metric threadsI [MetricDatapoint now 1 ]]
159
160
)
160
161
(RunThread tid, Just cap, _) ->
161
- (st {threadMap = IM. insert cap tid threadMap }, [] , [] )
162
+ (st {cap2thread = IM. insert cap tid cap2thread }, [] , [] )
162
163
(StopThread tid tstatus, Just cap, _)
163
164
| isTerminalThreadStatus tstatus ->
164
165
( st
165
- { threadMap = IM. delete cap threadMap,
166
- traceMap = HM. delete tid traceMap
166
+ { cap2thread = IM. delete cap cap2thread,
167
+ traceMap = HM. delete tid traceMap,
168
+ thread2displayThread = HM. delete tid thread2displayThread
167
169
},
168
170
[] ,
169
171
[Metric threadsI [MetricDatapoint now (- 1 )]]
@@ -187,6 +189,7 @@ processEvent (Event ts ev m_cap) st@(S {..}) =
187
189
spanStartedAt = gcStartedAt,
188
190
spanFinishedAt = now,
189
191
spanThreadId = maxBound ,
192
+ spanDisplayThreadId = maxBound ,
190
193
spanTags = mempty ,
191
194
spanEvents = [] ,
192
195
spanParentId = Nothing ,
@@ -200,6 +203,7 @@ processEvent (Event ts ev m_cap) st@(S {..}) =
200
203
spanStartedAt = gcRequestedAt,
201
204
spanFinishedAt = gcStartedAt,
202
205
spanThreadId = maxBound ,
206
+ spanDisplayThreadId = maxBound ,
203
207
spanTags = mempty ,
204
208
spanEvents = [] ,
205
209
spanParentId = Nothing ,
@@ -284,12 +288,14 @@ handleOpenTelemetryEventlogEvent m st (tid, now, m_trace_id) =
284
288
case HM. lookup serial $ serial2sid st of
285
289
Nothing ->
286
290
let (st', span_id) = inventSpanId serial st
291
+ (st'', display_tid) = inventDisplayTid tid st'
287
292
parent = HM. lookup tid (thread2sid st)
288
293
sp =
289
294
Span
290
295
{ spanContext = SpanContext span_id (fromMaybe (TId 42 ) m_trace_id),
291
296
spanOperation = " " ,
292
297
spanThreadId = tid,
298
+ spanDisplayThreadId = display_tid,
293
299
spanStartedAt = 0 ,
294
300
spanFinishedAt = now,
295
301
spanTags = mempty ,
@@ -298,20 +304,23 @@ handleOpenTelemetryEventlogEvent m st (tid, now, m_trace_id) =
298
304
spanNanosecondsSpentInGC = 0 ,
299
305
spanParentId = parent
300
306
}
301
- in (createSpan span_id sp st', [] , [] )
307
+ in (createSpan span_id sp st'' , [] , [] )
302
308
Just span_id ->
303
309
let (st', sp) = emitSpan serial span_id st
304
- in (st', [sp {spanFinishedAt = now}], [] )
310
+ (st'', display_tid) = inventDisplayTid tid st'
311
+ in (st'', [sp {spanFinishedAt = now, spanDisplayThreadId = display_tid}], [] )
305
312
BeginSpanEv (SpanInFlight serial) (SpanName operation) ->
306
313
case HM. lookup serial (serial2sid st) of
307
314
Nothing ->
308
315
let (st', span_id) = inventSpanId serial st
309
316
parent = HM. lookup tid (thread2sid st)
317
+ (st'', display_tid) = inventDisplayTid tid st'
310
318
sp =
311
319
Span
312
320
{ spanContext = SpanContext span_id (fromMaybe (TId 42 ) m_trace_id),
313
321
spanOperation = operation,
314
322
spanThreadId = tid,
323
+ spanDisplayThreadId = display_tid,
315
324
spanStartedAt = now,
316
325
spanFinishedAt = 0 ,
317
326
spanTags = mempty ,
@@ -320,10 +329,11 @@ handleOpenTelemetryEventlogEvent m st (tid, now, m_trace_id) =
320
329
spanNanosecondsSpentInGC = 0 ,
321
330
spanParentId = parent
322
331
}
323
- in (createSpan span_id sp st', [] , [] )
332
+ in (createSpan span_id sp st'' , [] , [] )
324
333
Just span_id ->
325
334
let (st', sp) = emitSpan serial span_id st
326
- in (st', [sp {spanOperation = operation, spanStartedAt = now, spanThreadId = tid}], [] )
335
+ (st'', display_tid) = inventDisplayTid tid st'
336
+ in (st'', [sp {spanOperation = operation, spanStartedAt = now, spanThreadId = tid, spanDisplayThreadId = display_tid}], [] )
327
337
DeclareInstrumentEv iType iId iName ->
328
338
(st {instrumentMap = HM. insert iId (CaptureInstrument iType iName) (instrumentMap st)}, [] , [] )
329
339
MetricCaptureEv instrumentId val -> case HM. lookup instrumentId (instrumentMap st) of
@@ -396,6 +406,14 @@ inventSpanId serial st = (st', sid)
396
406
(SId -> sid, randomGen') = R. nextWord64 randomGen
397
407
st' = st {serial2sid = HM. insert serial sid serial2sid, randomGen = randomGen'}
398
408
409
+ inventDisplayTid :: ThreadId -> State -> (State , ThreadId )
410
+ inventDisplayTid tid st@ (S {thread2displayThread}) =
411
+ case HM. lookup tid thread2displayThread of
412
+ Nothing ->
413
+ let new_dtid = fromIntegral (HM. size thread2displayThread)
414
+ in (st {thread2displayThread = HM. insert tid new_dtid thread2displayThread}, new_dtid)
415
+ Just dtid -> (st, dtid)
416
+
399
417
parseText :: [T. Text ] -> Maybe OpenTelemetryEventlogEvent
400
418
parseText =
401
419
\ case
0 commit comments