Skip to content

Commit db953cf

Browse files
author
Patrick Leckey
committed
added counting observer
1 parent 2989d51 commit db953cf

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright (c)2015 In-Touch Insight Systems Inc. All rights reserved.
4+
*
5+
* Date: 15-03-11
6+
* Time: 8:55 AM
7+
*
8+
* @author pleckey
9+
* @project laravel-newrelic
10+
*/
11+
12+
namespace Intouch\LaravelNewrelic\Observers;
13+
14+
15+
class NewrelicCountingObserver {
16+
17+
/**
18+
* Custom Metric name
19+
*
20+
* @var string|null
21+
*/
22+
protected $name;
23+
24+
/**
25+
* The list of observable events we care about
26+
*
27+
* @var array
28+
*/
29+
protected $care = [
30+
'created',
31+
'saved',
32+
'deleted',
33+
'updated',
34+
'restored',
35+
];
36+
37+
/**
38+
* @param null $name
39+
* @param array $care
40+
*/
41+
public function __construct( $name = null, array $care = [] )
42+
{
43+
$this->name = $name;
44+
$this->care = $care ?: $this->care;
45+
}
46+
47+
/**
48+
* Handle the observable events we get passed
49+
*
50+
* @param string $event
51+
* @param array $args
52+
*/
53+
public function __call( $event, array $args )
54+
{
55+
// ignore it if we don't care about this event
56+
if ( !in_array( $event, $this->care ) ) return;
57+
58+
$model = array_shift( $args );
59+
$name = 'Custom/Counts/' . ltrim( $this->name ?: get_class($model), '/' ) . '/' . $event;
60+
61+
/**
62+
* NewRelic assumes custom metrics to be in milliseconds, so 4 gets interpreted as
63+
* .004. So each "count" increment is 1000 to display properly in custom dashboards.
64+
*/
65+
\Newrelic::customMetric( $name, 1000 );
66+
}
67+
}

0 commit comments

Comments
 (0)