1616
1717package com.exactpro.th2.http.client.api.decorators
1818
19+ import com.exactpro.th2.common.event.EventUtils.generateUUID
1920import com.exactpro.th2.common.grpc.EventID
2021import rawhttp.core.EagerHttpRequest
2122import rawhttp.core.RawHttpHeaders
@@ -24,16 +25,22 @@ import rawhttp.core.RequestLine
2425import rawhttp.core.body.BodyReader
2526import rawhttp.core.body.HttpMessageBody
2627import java.net.InetAddress
28+ import java.time.Instant
29+ import java.util.concurrent.TimeUnit.SECONDS
30+ import java.util.concurrent.atomic.AtomicLong
2731
2832class Th2RawHttpRequest (
2933 requestLine : RequestLine ,
3034 headers : RawHttpHeaders ,
3135 bodyReader : BodyReader ? ,
3236 senderAddress : InetAddress ? ,
3337 val parentEventId : EventID ? ,
34- val metadataProperties : Map <String , String >
38+ val metadataProperties : Map <String , String >,
39+ val th2RequestId : String = nextRequestId(),
3540) : RawHttpRequest(requestLine, headers, bodyReader, senderAddress) {
3641
42+
43+
3744 override fun withBody (body : HttpMessageBody ? , adjustHeaders : Boolean ): Th2RawHttpRequest =
3845 withBody(body, adjustHeaders) { headers: RawHttpHeaders , bodyReader: BodyReader ? ->
3946 Th2RawHttpRequest (
@@ -42,7 +49,8 @@ class Th2RawHttpRequest(
4249 bodyReader,
4350 senderAddress.orElse(null ),
4451 parentEventId,
45- metadataProperties
52+ metadataProperties,
53+ th2RequestId,
4654 )
4755 }
4856
@@ -61,7 +69,8 @@ class Th2RawHttpRequest(
6169 body.orElse(null ),
6270 senderAddress.orElse(null ),
6371 parentEventId,
64- metadataProperties
72+ metadataProperties,
73+ th2RequestId,
6574 )
6675 }
6776
@@ -73,13 +82,22 @@ class Th2RawHttpRequest(
7382 body.orElse(null ),
7483 senderAddress.orElse(null ),
7584 parentEventId,
76- metadataProperties
85+ metadataProperties,
86+ th2RequestId,
7787 )
7888
7989 override fun eagerly (): EagerHttpRequest {
8090 throw UnsupportedOperationException (" Unsupported eagerly call of request. It's client side, no need to eager request" )
8191 }
8292
93+ companion object {
94+ private val BASE_REQUEST_ID = generateUUID()
95+ private val REQUEST_ID_COUNTER = Instant .now().run {
96+ AtomicLong (epochSecond * SECONDS .toNanos(1 ) + nano)
97+ }
98+
99+ private fun nextRequestId () = " $BASE_REQUEST_ID -${REQUEST_ID_COUNTER .incrementAndGet()} "
100+ }
83101}
84102
85103
0 commit comments