Skip to content

Commit a906580

Browse files
Merge pull request #219 from StanleyCocos/feat/timeout
2 parents ed86747 + e6aac52 commit a906580

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/src/sdk/trace/exporters/collector_exporter.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ class CollectorExporter implements sdk.SpanExporter {
2323
final Uri uri;
2424
final http.Client client;
2525
final Map<String, String> headers;
26+
27+
/// Timeout duration for the request in milliseconds.
28+
/// Default is 10000ms.
29+
/// Set to 0 or a negative value to disable timeout.
30+
final int timeoutMilliseconds;
2631
var _isShutdown = false;
2732

28-
CollectorExporter(this.uri,
29-
{http.Client? httpClient, this.headers = const {}})
30-
: client = httpClient ?? http.Client();
33+
CollectorExporter(
34+
this.uri, {
35+
http.Client? httpClient,
36+
this.headers = const {},
37+
this.timeoutMilliseconds = 10000,
38+
}) : client = httpClient ?? http.Client();
3139

3240
@override
3341
void export(List<sdk.ReadOnlySpan> spans) {
@@ -58,8 +66,11 @@ class CollectorExporter implements sdk.SpanExporter {
5866

5967
while (retries < maxRetries) {
6068
try {
61-
final response = await client.post(uri,
62-
body: body.writeToBuffer(), headers: headers);
69+
final request =
70+
client.post(uri, body: body.writeToBuffer(), headers: headers);
71+
final response = timeoutMilliseconds > 0
72+
? await request.timeout(Duration(milliseconds: timeoutMilliseconds))
73+
: await request;
6374
if (response.statusCode == 200) {
6475
return;
6576
}

0 commit comments

Comments
 (0)