-
Notifications
You must be signed in to change notification settings - Fork 81
Feature/injectable transport #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.php text eol=lf |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,6 +46,64 @@ $statsd = new DogStatsd( | |||||
|
||||||
DogStatsd constructor, takes a configuration array. See the full list of available [DogStatsD Client instantiation parameters](https://docs.datadoghq.com/developers/dogstatsd/?code-lang=php#client-instantiation-parameters). | ||||||
|
||||||
#### Migrating from legacy configuration | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd call this section "Using a custom transport factory" Overall I think this whole section is valuable but perhaps a bit verbose for the readme. Could you create a root level In this readme you could have a brief section
|
||||||
|
||||||
In some setups, the use of environment variables can cause surprises, such as warnings, test-failures, etc. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: just want to reword this a bit. We prefer to be brief in the readme with a focus on usage. If you feel that the extended explanation is valuable, please add it in the code comments or in a dedicated document (as suggested above) Perhaps something like (replacing lines 51-53):
|
||||||
|
||||||
There is a new argument `transport_factory`, which takes an argument that _implements_ `DataDog\TransportFactory` interface. This has one method `create`, which can return, one or more classes implementing `DataDog\TransportMechanism`. This can be particularly useful if you experience warnings, errors or other output from the PHP built-in's this library uses for communicating stats. | ||||||
|
||||||
##### Defining custom implementation of transport factory | ||||||
|
||||||
```php | ||||||
<?php | ||||||
|
||||||
namespace YourOrg\Metrics; | ||||||
|
||||||
/** | ||||||
* This class works around the decisions the constructor of DogStatsd class | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: reword to something like:
|
||||||
* Which has default convenience functionality, which can make your code | ||||||
* more difficult to reason about. | ||||||
*/ | ||||||
final class CustomTransportFactory implements DataDog\TransportFactory | ||||||
{ | ||||||
private $host; | ||||||
private $port; | ||||||
|
||||||
public function __construct($host, $port) | ||||||
{ | ||||||
if (empty($host) || empty($port)) { | ||||||
throw new \ArgumentException("Must specify host and port"); | ||||||
} | ||||||
} | ||||||
|
||||||
public function create() | ||||||
{ | ||||||
return new Ipv4UdpTransport( | ||||||
$this->host, | ||||||
$this->port | ||||||
); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
##### Instantiating datadog with your new class | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
```php | ||||||
<?php | ||||||
|
||||||
require __DIR__ . '/vendor/autoload.php'; | ||||||
|
||||||
use DataDog\DogStatsd; | ||||||
use YourOrg\Metrics\CustomTransportFactory; | ||||||
|
||||||
$statsd = new DogStatsd( | ||||||
array('transport_factory' =>, new CustomTransportFactory()) | ||||||
); | ||||||
``` | ||||||
|
||||||
#### Why this structure? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can drop this section from the readme. Perhaps add this as a code comment (or feel free to include it in a separate document) |
||||||
|
||||||
The reason a factory is used to create an instance, is that DogStatsD opens sockets on the machine running this code. If sockets are left open and unclosed, then the second time a socket is to be created, more problems arise. | ||||||
|
||||||
### Origin detection over UDP in Kubernetes | ||||||
|
||||||
Origin detection is a method to detect which pod DogStatsD packets are coming from in order to add the pod's tags to the tag list. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace DataDog; | ||
|
||
final class BridgingTransportFactory implements TransportFactory | ||
{ | ||
private $socketPath; | ||
private $host; | ||
private $port; | ||
|
||
public function __construct($socketPath = null, $host = null, $port = null) | ||
{ | ||
$this->socketPath = $socketPath; | ||
$this->host = $host; | ||
$this->port = $port; | ||
} | ||
|
||
/** | ||
* @return TransportMechanism | ||
*/ | ||
public function create() | ||
{ | ||
if (is_null($this->socketPath)) { | ||
return new Ipv4UdpTransport( | ||
$this->host, | ||
$this->port | ||
); | ||
} | ||
|
||
return new UnixSocketTransport($this->socketPath); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,11 @@ class DogStatsd | |
*/ | ||
private $decimalPrecision; | ||
|
||
/** | ||
* @var TransportFactory | ||
*/ | ||
private $transportFactory; | ||
|
||
private static $eventUrl = '/api/v1/events'; | ||
|
||
// Used for the telemetry tags | ||
|
@@ -75,10 +80,11 @@ class DogStatsd | |
* curl_ssl_verify_peer, | ||
* api_key and app_key, | ||
* decimal_precision | ||
* transport_factory | ||
* | ||
* @param array $config | ||
*/ | ||
public function __construct(array $config = array()) | ||
public function __construct(array $config = array(), $transportFactory = null) | ||
{ | ||
$this->host = isset($config['host']) | ||
? $config['host'] : (getenv('DD_AGENT_HOST') | ||
|
@@ -90,6 +96,12 @@ public function __construct(array $config = array()) | |
|
||
$this->socketPath = isset($config['socket_path']) ? $config['socket_path'] : null; | ||
|
||
$this->transportFactory = ( | ||
isset($config['transport_factory']) ? | ||
$transportFactory : | ||
new BridgingTransportFactory($this->socketPath, $this->host, $this->port) | ||
); | ||
|
||
Comment on lines
+99
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I had hoped might be pointed out is that this could be an alternative to https://github.com/DataDog/php-datadogstatsd/pull/116/files#diff-baf3cb376e36dfcf767f6389163de7d731798b1b108a44f33a2047e8e98bae3bR88-R98 I didn't want to leap too far in a single refactoring; and I think this should be part of a Configuration, that specific factory implementations could offer. So that implementations of transport are simple, and do not have cascading logic, or magic coming from the environment; but another layer can add that for them, or omit that, as the example in the README.md does. |
||
$this->datadogHost = isset($config['datadog_host']) ? $config['datadog_host'] : 'https://app.datadoghq.com'; | ||
$this->curlVerifySslHost = isset($config['curl_ssl_verify_host']) ? $config['curl_ssl_verify_host'] : 2; | ||
$this->curlVerifySslPeer = isset($config['curl_ssl_verify_peer']) ? $config['curl_ssl_verify_peer'] : 1; | ||
|
@@ -465,15 +477,7 @@ public function flush($message) | |
$message .= $this->flushTelemetry(); | ||
|
||
// Non - Blocking UDP I/O - Use IP Addresses! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: This comment can be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep - this comment is no longer relevant since the implementation moved (plus It's not a great comment to begin with) |
||
$socket = is_null($this->socketPath) ? socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) | ||
: socket_create(AF_UNIX, SOCK_DGRAM, 0); | ||
socket_set_nonblock($socket); | ||
|
||
if (!is_null($this->socketPath)) { | ||
$res = socket_sendto($socket, $message, strlen($message), 0, $this->socketPath); | ||
} else { | ||
$res = socket_sendto($socket, $message, strlen($message), 0, $this->host, $this->port); | ||
} | ||
Comment on lines
-468
to
-476
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, this is a win. |
||
$res = $this->transportFactory->create()->sendMessage($message); | ||
|
||
if ($res !== false) { | ||
$this->resetTelemetry(); | ||
|
@@ -483,8 +487,6 @@ public function flush($message) | |
$this->bytes_dropped += strlen($message); | ||
$this->packets_dropped += 1; | ||
} | ||
|
||
socket_close($socket); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace DataDog; | ||
|
||
final class Ipv4UdpTransport implements TransportMechanism | ||
{ | ||
private $host; | ||
private $port; | ||
private $socket; | ||
|
||
public function __construct($host, $port) | ||
{ | ||
$this->host = $host; | ||
$this->port = $port; | ||
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | ||
socket_set_nonblock($this->socket); | ||
} | ||
|
||
public function sendMessage($message) | ||
{ | ||
return (bool) socket_sendto($this->socket, $message, strlen($message), 0, $this->host, $this->port); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
socket_close($this->socket); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace DataDog; | ||
|
||
/** | ||
* Contract for creating an instance of TransportMechanism. | ||
* For Runtime creation of implementations of the TransportMechanism contract. | ||
* Used for statsd. Can be used to re-factor constructor logic. | ||
*/ | ||
interface TransportFactory | ||
Lewiscowles1986 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @return TransportMechanism | ||
*/ | ||
public function create(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace DataDog; | ||
|
||
/** | ||
* Contract for all-transports of DataDog statsd flush. | ||
* Can be used to for test-doubles, fallback implementations; future iterations. | ||
*/ | ||
interface TransportMechanism | ||
{ | ||
/** | ||
* @return bool | ||
*/ | ||
public function sendMessage($message); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace DataDog; | ||
|
||
final class UnixSocketTransport implements TransportMechanism | ||
{ | ||
private $socket; | ||
private $socketPath; | ||
|
||
public function __construct($socketPath) | ||
{ | ||
$this->socket = socket_create(AF_UNIX, SOCK_DGRAM, 0); | ||
socket_set_nonblock($this->socket); | ||
$this->socketPath = $socketPath; | ||
} | ||
|
||
public function sendMessage($message) | ||
{ | ||
return (bool) socket_sendto($this->socket, $message, strlen($message), 0, $this->socketPath); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
socket_close($this->socket); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the extended test coverage 💯