Skip to content
This repository was archived by the owner on Jan 1, 2021. It is now read-only.

Commit f234e0b

Browse files
committed
Added: Datadog request stats.
1 parent a36e791 commit f234e0b

6 files changed

Lines changed: 223 additions & 2 deletions

File tree

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Kernel extends HttpKernel
4040
'throttle:240,1',
4141
'bindings',
4242
\App\Http\Middleware\CheckDBMaintAPI::class,
43+
\App\Http\Middleware\DatadogMiddleware::class,
4344
],
4445
];
4546

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use ChaseConey\LaravelDatadogHelper\Datadog;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Http\Response;
9+
10+
class DatadogMiddleware
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @param \Closure $next
17+
* @return mixed
18+
*/
19+
public function handle($request, Closure $next)
20+
{
21+
$startTime = microtime(true);
22+
$response = $next($request);
23+
if (config('datadog-helper.enabled', false)) {
24+
static::logDuration($request, $response, $startTime);
25+
}
26+
return $response;
27+
}
28+
29+
/**
30+
* Logs the duration of a specific request through the application
31+
*
32+
* @param Request $request
33+
* @param Response $response
34+
* @param double $startTime
35+
*/
36+
protected static function logDuration(Request $request, Response $response, $startTime)
37+
{
38+
$duration = microtime(true) - $startTime;
39+
40+
$ua = $request->header("User-Agent");
41+
$re = '/Radarr\/(?P<version>(\d+\.)+\d+)\s\((?P<os_name>(Osx|Linux|Windows))\s(?P<os_version>(\d+\.)+\d+)\)/i';
42+
preg_match($re, $ua, $matches);
43+
44+
$tags = [
45+
"url" => $request->getSchemeAndHttpHost() . $request->getRequestUri(),
46+
"status_code" => $response->getStatusCode(),
47+
"radarr",
48+
"version" => $matches["version"],
49+
"os_name" => $matches["os_name"],
50+
"os_version" => $matches["os_version"]
51+
];
52+
53+
Datadog::timing('request_time', $duration, 1, $tags);
54+
55+
Datadog::set('unique_users', $request->ip(), 1.0, $tags);
56+
}
57+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"require": {
88
"php": ">=5.6.4",
99
"barryvdh/laravel-debugbar": "^2.4",
10+
"chaseconey/laravel-datadog-helper": "^0.9.0",
1011
"elcobvg/laravel-opcache": "^0.3.0",
1112
"graham-campbell/exceptions": "^9.3",
1213
"guzzlehttp/guzzle": "~6.0",

composer.lock

Lines changed: 110 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@
200200

201201
ElcoBvg\Opcache\ServiceProvider::class,
202202

203+
ChaseConey\LaravelDatadogHelper\LaravelDatadogHelperServiceProvider::class,
204+
203205
],
204206

205207
/*
@@ -249,7 +251,7 @@
249251
'URL' => Illuminate\Support\Facades\URL::class,
250252
'Validator' => Illuminate\Support\Facades\Validator::class,
251253
'View' => Illuminate\Support\Facades\View::class,
252-
254+
'Datadog' => ChaseConey\LaravelDatadogHelper\Datadog::class
253255
],
254256

255257
];

config/datadog-helper.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Datadog Tracking Enabled
8+
|--------------------------------------------------------------------------
9+
|
10+
| Set this option to enable or disable the datadog helper.
11+
|
12+
*/
13+
'enabled' => true,
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Datadog Tracking Prefix
18+
|--------------------------------------------------------------------------
19+
|
20+
| This is the prefix that will be placed in front of all of your metric entries. If you have multiple
21+
| applications being tracked in Datadog, it is recommended putting the application name somewhere
22+
| inside of your prefix. A common naming scheme is something like app.<app-name>.
23+
|
24+
*/
25+
'prefix' => 'radarr.api',
26+
27+
'api_key' => env("DATADOG_API_KEY", ""),
28+
29+
'application_key' => null,
30+
31+
'datadog_host' => 'https://app.datadoghq.com',
32+
33+
'statsd_server' => 'localhost',
34+
35+
'statsd_port' => 8125,
36+
37+
/*
38+
|--------------------------------------------------------------------------
39+
| Transport
40+
|--------------------------------------------------------------------------
41+
|
42+
| Submitting events via TCP vs UDP
43+
| TCP - High-confidence event submission. Will log errors on event submission error.
44+
| UDP - "Fire and forget" event submission. Will not log errors on event submission error.
45+
| No acknowledgement of submitted event from Datadog.
46+
| Since the UDP method uses the a local dogstatsd instance you don't need to setup
47+
| any additional application/api access.
48+
*/
49+
'transport' => 'UDP'
50+
51+
];

0 commit comments

Comments
 (0)