Skip to content

Commit c0ead2e

Browse files
committed
feat: timeouts
1 parent c274a99 commit c0ead2e

4 files changed

Lines changed: 82 additions & 4 deletions

File tree

src/Logger/Adapter/AppSignal.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@
1111

1212
class AppSignal extends Adapter
1313
{
14+
private const DEFAULT_TIMEOUT = 5;
15+
16+
private const DEFAULT_CONNECT_TIMEOUT = 1;
17+
1418
/**
1519
* @var string (required, can be found in Appsignal -> Project -> App Settings -> Push & deploy -> Push Key)
1620
*/
1721
protected string $apiKey;
1822

23+
/**
24+
* Timeout (seconds) for the complete request.
25+
*/
26+
protected int $timeout;
27+
28+
/**
29+
* Timeout (seconds) for establishing the connection.
30+
*/
31+
protected int $connectTimeout;
32+
1933
/**
2034
* AppSignal constructor.
2135
*
2236
* @param string $key
37+
* @param int $timeout
38+
* @param int $connectTimeout
2339
*/
24-
public function __construct(string $key)
40+
public function __construct(string $key, int $timeout = self::DEFAULT_TIMEOUT, int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT)
2541
{
2642
$this->apiKey = $key;
43+
$this->timeout = $timeout > 0 ? $timeout : self::DEFAULT_TIMEOUT;
44+
$this->connectTimeout = $connectTimeout > 0 ? $connectTimeout : self::DEFAULT_CONNECT_TIMEOUT;
2745
}
2846

2947
/**
@@ -114,6 +132,8 @@ public function push(Log $log): int
114132
CURLOPT_RETURNTRANSFER => true,
115133
CURLOPT_POST => true,
116134
CURLOPT_POSTFIELDS => \json_encode($requestBody),
135+
CURLOPT_TIMEOUT => $this->timeout,
136+
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
117137
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED,
118138
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
119139
];

src/Logger/Adapter/LogOwl.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
class LogOwl extends Adapter
1414
{
15+
private const DEFAULT_TIMEOUT = 3;
16+
17+
private const DEFAULT_CONNECT_TIMEOUT = 1;
18+
1519
/**
1620
* @var string (required, can be found in LogOwl -> All Services -> Project -> Ticket -> Service Ticket)
1721
*/
@@ -23,20 +27,34 @@ class LogOwl extends Adapter
2327
*/
2428
protected string $logOwlHost;
2529

30+
/**
31+
* Timeout (seconds) for the complete request.
32+
*/
33+
protected int $timeout;
34+
35+
/**
36+
* Timeout (seconds) for establishing the connection.
37+
*/
38+
protected int $connectTimeout;
39+
2640
/**
2741
* LogOwl constructor.
2842
*
2943
* @param string $ticket
3044
* @param string $host
45+
* @param int $timeout
46+
* @param int $connectTimeout
3147
*/
32-
public function __construct(string $ticket, string $host = '')
48+
public function __construct(string $ticket, string $host = '', int $timeout = self::DEFAULT_TIMEOUT, int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT)
3349
{
3450
if (empty($host)) {
3551
$host = 'https://api.logowl.io/logging/';
3652
}
3753

3854
$this->ticket = $ticket;
3955
$this->logOwlHost = $host;
56+
$this->timeout = $timeout > 0 ? $timeout : self::DEFAULT_TIMEOUT;
57+
$this->connectTimeout = $connectTimeout > 0 ? $connectTimeout : self::DEFAULT_CONNECT_TIMEOUT;
4058
}
4159

4260
/**

src/Logger/Adapter/Raygun.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@
1111

1212
class Raygun extends Adapter
1313
{
14+
private const DEFAULT_TIMEOUT = 5;
15+
16+
private const DEFAULT_CONNECT_TIMEOUT = 1;
17+
1418
/**
1519
* @var string (required, can be found in Appsignal -> Project -> App Settings -> Push & deploy -> Push Key)
1620
*/
1721
protected string $apiKey;
1822

23+
/**
24+
* Timeout (seconds) for the complete request.
25+
*/
26+
protected int $timeout;
27+
28+
/**
29+
* Timeout (seconds) for establishing the connection.
30+
*/
31+
protected int $connectTimeout;
32+
1933
/**
2034
* Raygun constructor.
2135
*
2236
* @param string $key
37+
* @param int $timeout
38+
* @param int $connectTimeout
2339
*/
24-
public function __construct(string $key)
40+
public function __construct(string $key, int $timeout = self::DEFAULT_TIMEOUT, int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT)
2541
{
2642
$this->apiKey = $key;
43+
$this->timeout = $timeout > 0 ? $timeout : self::DEFAULT_TIMEOUT;
44+
$this->connectTimeout = $connectTimeout > 0 ? $connectTimeout : self::DEFAULT_CONNECT_TIMEOUT;
2745
}
2846

2947
/**
@@ -99,6 +117,8 @@ public function push(Log $log): int
99117
CURLOPT_RETURNTRANSFER => true,
100118
CURLOPT_POST => true,
101119
CURLOPT_POSTFIELDS => \json_encode($requestBody),
120+
CURLOPT_TIMEOUT => $this->timeout,
121+
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
102122
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED,
103123
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'X-ApiKey: '.$this->apiKey],
104124
];

src/Logger/Adapter/Sentry.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
class Sentry extends Adapter
1414
{
15+
private const DEFAULT_TIMEOUT = 5;
16+
17+
private const DEFAULT_CONNECT_TIMEOUT = 1;
18+
1519
/**
1620
* @var string (required, this part of Sentry DSN: 'https://{{THIS_PART}}@blabla.ingest.sentry.io/blabla')
1721
*/
@@ -28,14 +32,26 @@ class Sentry extends Adapter
2832
*/
2933
protected string $sentryHost;
3034

35+
/**
36+
* Timeout (seconds) for the complete request.
37+
*/
38+
protected int $timeout;
39+
40+
/**
41+
* Timeout (seconds) for establishing the connection.
42+
*/
43+
protected int $connectTimeout;
44+
3145
/**
3246
* Sentry constructor.
3347
*
3448
* @param string $projectId
3549
* @param string $key
3650
* @param string $host
51+
* @param int $timeout
52+
* @param int $connectTimeout
3753
*/
38-
public function __construct(string $projectId, string $key, string $host = '')
54+
public function __construct(string $projectId, string $key, string $host = '', int $timeout = self::DEFAULT_TIMEOUT, int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT)
3955
{
4056
if (empty($host)) {
4157
$host = 'https://sentry.io';
@@ -44,6 +60,8 @@ public function __construct(string $projectId, string $key, string $host = '')
4460
$this->sentryHost = $host;
4561
$this->sentryKey = $key;
4662
$this->projectId = $projectId;
63+
$this->timeout = $timeout > 0 ? $timeout : self::DEFAULT_TIMEOUT;
64+
$this->connectTimeout = $connectTimeout > 0 ? $connectTimeout : self::DEFAULT_CONNECT_TIMEOUT;
4765
}
4866

4967
/**
@@ -141,6 +159,8 @@ public function push(Log $log): int
141159
CURLOPT_RETURNTRANSFER => true,
142160
CURLOPT_POST => true,
143161
CURLOPT_POSTFIELDS => \json_encode($requestBody),
162+
CURLOPT_TIMEOUT => $this->timeout,
163+
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
144164
CURLOPT_HEADEROPT => \CURLHEADER_UNIFIED,
145165
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'X-Sentry-Auth: Sentry sentry_version=7, sentry_key='.$this->sentryKey.', sentry_client=utopia-logger/'.Logger::LIBRARY_VERSION],
146166
];

0 commit comments

Comments
 (0)