Skip to content

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
23 changes: 19 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2 # use CircleCI 2.0

x-dockerbuild-7: &dockerbuild-7
x-dockerbuild-phpdbg: &dockerbuild-phpdbg
steps:
- checkout
- run: sudo docker-php-ext-install sockets
Expand All @@ -19,16 +19,28 @@ x-dockerbuild-5: &dockerbuild-5
- run: composer lint

jobs:
test-8.0:
Copy link
Member

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 💯

<<: *dockerbuild-phpdbg
docker:
- image: circleci/php:8.0-node-browsers
test-7.4:
<<: *dockerbuild-phpdbg
docker:
- image: circleci/php:7.4-node-browsers
test-7.3:
<<: *dockerbuild-phpdbg
docker:
- image: circleci/php:7.3-node-browsers
test-7.2:
<<: *dockerbuild-7
<<: *dockerbuild-phpdbg
docker:
- image: circleci/php:7.2-node-browsers
test-7.1:
<<: *dockerbuild-7
<<: *dockerbuild-phpdbg
docker:
- image: circleci/php:7.1-node-browsers
test-7.0:
<<: *dockerbuild-7
<<: *dockerbuild-phpdbg
docker:
- image: circleci/php:7.0-node-browsers
test-5.6:
Expand All @@ -40,6 +52,9 @@ workflows:
version: 2
check_compile:
jobs:
- test-8.0
- test-7.4
- test-7.3
- test-7.2
- test-7.1
- test-7.0
Expand Down
11 changes: 11 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.php text eol=lf
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 docs folder with this as a markdown document and link to it from the readme?

In this readme you could have a brief section

If you want more control over how transports are constructed see custom-transport-factory.md on how to implement a custom transport factory.


In some setups, the use of environment variables can cause surprises, such as warnings, test-failures, etc.
Copy link
Member

Choose a reason for hiding this comment

The 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):

Using environment variables for configuration may not always be ideal. If you want extra control over how transports are constructed or validated consider implementing a custom TransportFactory. You can achieve this by implanting the DataDog\TransportFactoryinterface and suppling it to thetransport_factory` configuration.


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: reword to something like:

Custom transport factory that validates a host and port are not empty. This extends the default behavior where these errors would otherwise fail silently.

* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##### Instantiating datadog with your new class
##### Instantiating DogStatsd with your custom transport factory

```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?
Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"sort-packages": true
},
"require-dev": {
"phpunit/phpunit": "5.7.27",
"yoast/phpunit-polyfills": "^1.0.1",
"squizlabs/php_codesniffer": "^3.3"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/BatchedDogStatsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class BatchedDogStatsd extends DogStatsd
public static $maxBufferLength = 50;


public function __construct(array $config = array())
public function __construct(array $config = array(), $transportFactory = null)
{
// by default the telemetry is enabled for BatchedDogStatsd
if (!isset($config["disable_telemetry"])) {
$config["disable_telemetry"] = false;
}
parent::__construct($config);
parent::__construct($config, $transportFactory);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/BridgingTransportFactory.php
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);
}
}
26 changes: 14 additions & 12 deletions src/DogStatsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class DogStatsd
*/
private $decimalPrecision;

/**
* @var TransportFactory
*/
private $transportFactory;

private static $eventUrl = '/api/v1/events';

// Used for the telemetry tags
Expand All @@ -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')
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -465,15 +477,7 @@ public function flush($message)
$message .= $this->flushTelemetry();

// Non - Blocking UDP I/O - Use IP Addresses!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: This comment can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment?

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Expand All @@ -483,8 +487,6 @@ public function flush($message)
$this->bytes_dropped += strlen($message);
$this->packets_dropped += 1;
}

socket_close($socket);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Ipv4UdpTransport.php
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);
}
}
16 changes: 16 additions & 0 deletions src/TransportFactory.php
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
{
/**
* @return TransportMechanism
*/
public function create();
}
15 changes: 15 additions & 0 deletions src/TransportMechanism.php
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);
}
26 changes: 26 additions & 0 deletions src/UnixSocketTransport.php
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);
}
}
7 changes: 3 additions & 4 deletions tests/TestHelpers/CurlSpyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DataDog\TestHelpers;

use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

$curlSpy = new CurlSpy();

Expand All @@ -11,13 +11,12 @@ class CurlSpyTestCase extends TestCase
/**
* Set up a spy object to capture calls to built in curl functions
*/
protected function setUp()
protected function set_up()
{
global $curlSpy;

$curlSpy = new CurlSpy();

parent::setUp();
parent::set_up();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TestHelpers/SocketSpyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DataDog\TestHelpers;

use DataDog\DogStatsd;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* Making this variable global to this file is necessary for interacting with
Expand All @@ -24,13 +24,13 @@ class SocketSpyTestCase extends TestCase
/**
* Set up a spy object to capture calls to global built in socket functions
*/
protected function setUp()
protected function set_up()
{
global $socketSpy;

$socketSpy = new SocketSpy();

parent::setUp();
parent::set_up();
}

/**
Expand Down
Loading