@@ -56,6 +56,7 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
5656
5757 public func export( spans: [ SpanData ] , explicitTimeout: TimeInterval ? = nil )
5858 -> SpanExporterResultCode {
59+ var resultValue : SpanExporterResultCode = . success
5960 var sendingSpans : [ SpanData ] = [ ]
6061 exporterLock. withLockVoid {
6162 pendingSpans. append ( contentsOf: spans)
@@ -68,7 +69,12 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
6869 $0. resourceSpans = SpanAdapter . toProtoResourceSpans (
6970 spanDataList: sendingSpans)
7071 }
71- let request = createRequest ( body: body, endpoint: endpoint)
72+ let semaphore = DispatchSemaphore ( value: 0 )
73+ var request = createRequest ( body: body, endpoint: endpoint)
74+
75+ let timeout = min ( explicitTimeout ?? TimeInterval . greatestFiniteMagnitude, config. timeout)
76+ request. timeoutInterval = timeout
77+
7278 exporterMetrics? . addSeen ( value: sendingSpans. count)
7379 httpClient. send ( request: request) { [ weak self] result in
7480 switch result {
@@ -80,9 +86,17 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
8086 self ? . pendingSpans. append ( contentsOf: sendingSpans)
8187 }
8288 OpenTelemetry . instance. feedbackHandler ? ( " \( error) " )
89+ resultValue = . failure
8390 }
91+ semaphore. signal ( )
92+ }
93+
94+ let waitResult = semaphore. wait ( timeout: . now( ) + timeout)
95+ if waitResult == . timedOut {
96+ exporterMetrics? . addFailed ( value: sendingSpans. count)
97+ return . failure
8498 }
85- return . success
99+ return resultValue
86100 }
87101
88102 public func flush( explicitTimeout: TimeInterval ? = nil )
@@ -99,7 +113,9 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
99113 spanDataList: pendingSpans)
100114 }
101115 let semaphore = DispatchSemaphore ( value: 0 )
102- let request = createRequest ( body: body, endpoint: endpoint)
116+ var request = createRequest ( body: body, endpoint: endpoint)
117+ let timeout = min ( explicitTimeout ?? TimeInterval . greatestFiniteMagnitude, config. timeout)
118+ request. timeoutInterval = timeout
103119
104120 httpClient. send ( request: request) { [ weak self] result in
105121 switch result {
@@ -112,7 +128,12 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
112128 }
113129 semaphore. signal ( )
114130 }
115- semaphore. wait ( )
131+
132+ let waitResult = semaphore. wait ( timeout: . now( ) + timeout)
133+ if waitResult == . timedOut {
134+ exporterMetrics? . addFailed ( value: pendingSpans. count)
135+ return . failure
136+ }
116137 }
117138 return resultValue
118139 }
0 commit comments