Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,14 @@ func httpClientResult(for taskResult: (Data?, URLResponse?, Error?)) -> Result<H
)
}

let statusCode = httpResponse.statusCode
guard (200..<300).contains(statusCode) else {
return .failure(
HTTPClientError(
description: "HTTP request failed with status code: \(statusCode)"
)
)
}

return .success(httpResponse)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {

public func export(spans: [SpanData], explicitTimeout: TimeInterval? = nil)
-> SpanExporterResultCode {
var resultValue: SpanExporterResultCode = .success
var sendingSpans: [SpanData] = []
exporterLock.withLockVoid {
pendingSpans.append(contentsOf: spans)
Expand All @@ -68,7 +69,12 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
$0.resourceSpans = SpanAdapter.toProtoResourceSpans(
spanDataList: sendingSpans)
}
let request = createRequest(body: body, endpoint: endpoint)
let semaphore = DispatchSemaphore(value: 0)
var request = createRequest(body: body, endpoint: endpoint)

let timeout = min(explicitTimeout ?? TimeInterval.greatestFiniteMagnitude, config.timeout)
request.timeoutInterval = timeout

exporterMetrics?.addSeen(value: sendingSpans.count)
httpClient.send(request: request) { [weak self] result in
switch result {
Expand All @@ -80,9 +86,17 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
self?.pendingSpans.append(contentsOf: sendingSpans)
}
print(error)
resultValue = .failure
}
semaphore.signal()
}

let waitResult = semaphore.wait(timeout: .now() + timeout)
if waitResult == .timedOut {
exporterMetrics?.addFailed(value: sendingSpans.count)
return .failure
}
return .success
return resultValue
}

public func flush(explicitTimeout: TimeInterval? = nil)
Expand All @@ -99,7 +113,9 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
spanDataList: pendingSpans)
}
let semaphore = DispatchSemaphore(value: 0)
let request = createRequest(body: body, endpoint: endpoint)
var request = createRequest(body: body, endpoint: endpoint)
let timeout = min(explicitTimeout ?? TimeInterval.greatestFiniteMagnitude, config.timeout)
request.timeoutInterval = timeout

httpClient.send(request: request) { [weak self] result in
switch result {
Expand All @@ -112,7 +128,12 @@ public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
}
semaphore.signal()
}
semaphore.wait()

let waitResult = semaphore.wait(timeout: .now() + timeout)
if waitResult == .timedOut {
exporterMetrics?.addFailed(value: pendingSpans.count)
return .failure
}
}
return resultValue
}
Expand Down
Loading