Skip to content

Commit 89ef835

Browse files
author
Justin Hayes
committed
Initial commit
0 parents  commit 89ef835

20 files changed

+1888
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
3+
matrix:
4+
include:
5+
- php: 7.1
6+
env: ILLUMINATE_VERSION=5.5.*
7+
- php: 7.1
8+
env: ILLUMINATE_VERSION=5.6.*
9+
- php: 7.1
10+
env: ILLUMINATE_VERSION=5.7.*
11+
- php: 7.2
12+
env: ILLUMINATE_VERSION=5.5.*
13+
- php: 7.2
14+
env: ILLUMINATE_VERSION=5.6.*
15+
- php: 7.2
16+
env: ILLUMINATE_VERSION=5.7.*
17+
18+
before_install:
19+
- composer require "illuminate/support:${ILLUMINATE_VERSION}" --no-update
20+
21+
before_script:
22+
- composer update --prefer-source --no-interaction
23+
24+
script:
25+
- vendor/bin/phpunit

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "jshayes/laravel-datadogstatsd",
3+
"type": "library",
4+
"description": "A Laravel library for DataDog Statsd",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Justin Hayes"
9+
}
10+
],
11+
"require": {
12+
"datadog/php-datadogstatsd": "^1.3",
13+
"illuminate/support": "^5.5"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"JSHayes\\LaravelDataDogStatsd\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"JSHayes\\LaravelDataDogStatsd\\Tests\\": "tests/"
23+
}
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^7.4",
27+
"orchestra/testbench": "^3.0"
28+
},
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"JSHayes\\LaravelDataDogStatsd\\ServiceProvider"
33+
]
34+
}
35+
}
36+
}

phpunit.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false">
10+
<testsuites>
11+
<testsuite name="Unit">
12+
<directory suffix="Test.php">./tests/Unit</directory>
13+
</testsuite>
14+
</testsuites>
15+
</phpunit>

src/DataDog.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace JSHayes\LaravelDataDogStatsd;
4+
5+
use DataDog\DogStatsd;
6+
use DataDog\BatchedDogStatsd;
7+
use Illuminate\Contracts\Foundation\Application;
8+
9+
class DataDog
10+
{
11+
/**
12+
* The DogStatsd instance
13+
*
14+
* @var \DataDog\DogStatsd
15+
*/
16+
protected $instance;
17+
18+
/**
19+
* Create a new DataDog wrapper
20+
*
21+
* @param \Illuminate\Contracts\Foundation\Application $app
22+
*/
23+
public function __construct(Application $app)
24+
{
25+
$this->app = $app;
26+
}
27+
28+
/**
29+
* Create a StatBuilder for the given stat to fluently send stats
30+
*
31+
* @param string $stat
32+
*
33+
* @return \JSHayes\LaravelDataDogStatsd\StatBuilder
34+
*/
35+
public function stat(string $stat): StatBuilder
36+
{
37+
return new StatBuilder($this->getStatsdInstance(), $stat);
38+
}
39+
40+
/**
41+
* Delegate calls to the DogStatsd instance
42+
*
43+
* @param string $method
44+
* @param array $arguments
45+
*
46+
* @return mixed
47+
*/
48+
public function __call(string $method, array $parameters = [])
49+
{
50+
return call_user_func_array([$this->getStatsdInstance(), $method], $parameters);
51+
}
52+
53+
/**
54+
* Create the single DogStatsd driver
55+
*
56+
* @param array $config
57+
*
58+
* @return \DataDog\DogStatsd
59+
*/
60+
private function createSingleDriver(array $config): DogStatsd
61+
{
62+
return new DogStatsd($config);
63+
}
64+
65+
/**
66+
* Create the BatchedDogStatsd driver
67+
*
68+
* @param array $config
69+
*
70+
* @return \DataDog\BatchedDogStatsd
71+
*/
72+
private function createBatchedDriver(array $config): BatchedDogStatsd
73+
{
74+
$statsd = new BatchedDogStatsd($config);
75+
$this->app->terminating(function () use ($statsd) {
76+
$statsd->flush_buffer();
77+
});
78+
return $statsd;
79+
}
80+
81+
/**
82+
* Make the statsd driver based on the configuration
83+
*
84+
* @return \DataDog\DogStatsd
85+
*/
86+
private function makeStatsdInstance(): DogStatsd
87+
{
88+
$driver = $this->app['config']['datadog.driver'];
89+
$method = "create{$driver}Driver";
90+
return $this->$method($this->app['config']["datadog.drivers.$driver"]);
91+
}
92+
93+
/**
94+
* Get the statsd driver
95+
*
96+
* @return \DataDog\DogStatsd
97+
*/
98+
protected function getStatsdInstance(): DogStatsd
99+
{
100+
return $this->instance = $this->instance ?? $this->makeStatsdInstance();
101+
}
102+
}

src/Facades/DataDog.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace JSHayes\LaravelDataDogStatsd\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use JSHayes\LaravelDataDogStatsd\Testing\DataDogFake;
7+
8+
class DataDog extends Facade
9+
{
10+
/**
11+
* Get the registered name of the component.
12+
*
13+
* @return string
14+
*/
15+
protected static function getFacadeAccessor()
16+
{
17+
return 'datadog';
18+
}
19+
20+
/**
21+
* Replace the bound instance with a fake.
22+
*
23+
* @return void
24+
*/
25+
public static function fake()
26+
{
27+
static::swap(new DataDogFake(static::getFacadeApplication()));
28+
}
29+
}

src/ServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace JSHayes\LaravelDataDogStatsd;
4+
5+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6+
7+
class ServiceProvider extends BaseServiceProvider
8+
{
9+
/**
10+
* Register any package services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
$this->app->singleton('datadog', DataDog::class);
17+
}
18+
}

src/StatBuilder.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace JSHayes\LaravelDataDogStatsd;
4+
5+
use DataDog\DogStatsd;
6+
7+
class StatBuilder
8+
{
9+
private $statsd;
10+
private $stat;
11+
private $sampleRate = 1.0;
12+
private $tags = null;
13+
14+
/**
15+
* Create a new metric builder
16+
*
17+
* @param \DataDog\DogStatsd $statsd
18+
* @param string $stat
19+
*/
20+
public function __construct(DogStatsd $statsd, string $stat)
21+
{
22+
$this->statsd = $statsd;
23+
$this->stat = $stat;
24+
}
25+
26+
/**
27+
* Apply the given tags to this stat
28+
*
29+
* @param array|string $tags
30+
*
31+
* @return self
32+
*/
33+
public function withTags($tags): self
34+
{
35+
$this->tags = $tags;
36+
return $this;
37+
}
38+
39+
/**
40+
* Use the given sample rate for this stat
41+
*
42+
* @param array|string $tags
43+
*
44+
* @return self
45+
*/
46+
public function withSampleRate(float $sampleRate): self
47+
{
48+
$this->sampleRate = $sampleRate;
49+
return $this;
50+
}
51+
52+
/**
53+
* Log timing information
54+
*
55+
* @param float $time
56+
*
57+
* @return void
58+
*/
59+
public function timing(float $time): void
60+
{
61+
$this->statsd->timing($this->stat, $time, $this->sampleRate, $this->tags);
62+
}
63+
64+
/**
65+
* A convenient alias for the timing function when used with micro-timing
66+
*
67+
* @param float $time
68+
*
69+
* @return void
70+
*/
71+
public function microtiming(float $time): void
72+
{
73+
$this->statsd->microtiming($this->stat, $time, $this->sampleRate, $this->tags);
74+
}
75+
76+
/**
77+
* Gauge
78+
*
79+
* @param float $value
80+
*
81+
* @return void
82+
*/
83+
public function gauge(float $value): void
84+
{
85+
$this->statsd->gauge($this->stat, $value, $this->sampleRate, $this->tags);
86+
}
87+
88+
/**
89+
* Histogram
90+
*
91+
* @param float $value
92+
*
93+
* @return void
94+
*/
95+
public function histogram(float $value): void
96+
{
97+
$this->statsd->histogram($this->stat, $value, $this->sampleRate, $this->tags);
98+
}
99+
100+
/**
101+
* Distribution
102+
*
103+
* @param float $value
104+
*
105+
* @return void
106+
*/
107+
public function distribution(float $value): void
108+
{
109+
$this->statsd->distribution($this->stat, $value, $this->sampleRate, $this->tags);
110+
}
111+
112+
/**
113+
* Set
114+
*
115+
* @param float $value
116+
*
117+
* @return void
118+
*/
119+
public function set(float $value): void
120+
{
121+
$this->statsd->set($this->stat, $value, $this->sampleRate, $this->tags);
122+
}
123+
124+
/**
125+
* Increment a stat counter
126+
*
127+
* @param int $value
128+
*
129+
* @return void
130+
*/
131+
public function increment(int $value = 1): void
132+
{
133+
$this->statsd->increment($this->stat, $this->sampleRate, $this->tags, $value);
134+
}
135+
136+
/**
137+
* Decrement a stat counter
138+
*
139+
* @param int $value
140+
*
141+
* @return void
142+
*/
143+
public function decrement(int $value = -1): void
144+
{
145+
$this->statsd->decrement($this->stat, $this->sampleRate, $this->tags, $value);
146+
}
147+
}

0 commit comments

Comments
 (0)