Skip to content

Commit 5bc1599

Browse files
author
Patrick Leckey
committed
added timing observer
1 parent db953cf commit 5bc1599

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/**
3+
* Copyright (c)2015 In-Touch Insight Systems Inc. All rights reserved.
4+
*
5+
* Date: 15-03-11
6+
* Time: 11:28 AM
7+
*
8+
* @author pleckey
9+
* @project laravel-newrelic
10+
*/
11+
12+
namespace Intouch\LaravelNewrelic\Observers;
13+
14+
class NewrelicTimingObserver
15+
{
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+
* The list of observable events we are able to care about
39+
*
40+
* @var array
41+
*/
42+
protected $valid = [
43+
'created',
44+
'saved',
45+
'deleted',
46+
'updated',
47+
'restored',
48+
];
49+
50+
/**
51+
* Static storage for the timing values
52+
*
53+
* @var array
54+
*/
55+
protected static $times = [ ];
56+
57+
/**
58+
* @param string|null $name
59+
* @param array $care
60+
*/
61+
public function __construct( $name = null, array $care = [ ] )
62+
{
63+
$this->name = $name;
64+
if (count( $care ) > 0) {
65+
$this->care = array_intersect( $this->valid, $care );
66+
}
67+
}
68+
69+
protected function getMetricName( $model, $event )
70+
{
71+
return 'Custom/Timing/' . ltrim( $this->name ?: get_class( $model ), '/' ) . '/' . $event;
72+
}
73+
74+
/**
75+
* Grab the time creating started
76+
*
77+
* @param Illuminate\Database\Eloquent\Model $model
78+
*/
79+
public function creating( $model )
80+
{
81+
static::$times['create'] = -microtime( true );
82+
}
83+
84+
/**
85+
* Grab the time saving started
86+
*
87+
* @param Illuminate\Database\Eloquent\Model $model
88+
*/
89+
public function saving( $model )
90+
{
91+
static::$times['save'] = -microtime( true );
92+
}
93+
94+
/**
95+
* Grab the time deleting started
96+
*
97+
* @param Illuminate\Database\Eloquent\Model $model
98+
*/
99+
public function deleting( $model )
100+
{
101+
static::$times['delete'] = -microtime( true );
102+
}
103+
104+
/**
105+
* Grab the time updating started
106+
*
107+
* @param Illuminate\Database\Eloquent\Model $model
108+
*/
109+
public function updating( $model )
110+
{
111+
static::$times['update'] = -microtime( true );
112+
}
113+
114+
/**
115+
* Grab the time restoring started
116+
*
117+
* @param Illuminate\Database\Eloquent\Model $model
118+
*/
119+
public function restoring( $model )
120+
{
121+
static::$times['restore'] = -microtime( true );
122+
}
123+
124+
/**
125+
* Record the time it took to create
126+
*
127+
* @param Illuminate\Database\Eloquent\Model $model
128+
*/
129+
public function created( $model )
130+
{
131+
$ms = round( static::$times['create'] + microtime( true ), 3 ) * 1000;
132+
\Newrelic::customMetric( $this->getMetricName( $model, 'created' ), $ms );
133+
}
134+
135+
/**
136+
* Record the time it took to save
137+
*
138+
* @param Illuminate\Database\Eloquent\Model $model
139+
*/
140+
public function saved( $model )
141+
{
142+
$ms = round( static::$times['save'] + microtime( true ), 3 ) * 1000;
143+
\Newrelic::customMetric( $this->getMetricName( $model, 'saved' ), $ms );
144+
}
145+
146+
/**
147+
* Record the time it took to delete
148+
*
149+
* @param Illuminate\Database\Eloquent\Model $model
150+
*/
151+
public function deleted( $model )
152+
{
153+
$ms = round( static::$times['delete'] + microtime( true ), 3 ) * 1000;
154+
\Newrelic::customMetric( $this->getMetricName( $model, 'deleted' ), $ms );
155+
}
156+
157+
/**
158+
* Record the time it took to update
159+
*
160+
* @param Illuminate\Database\Eloquent\Model $model
161+
*/
162+
public function updated( $model )
163+
{
164+
$ms = round( static::$times['update'] + microtime( true ), 3 ) * 1000;
165+
\Newrelic::customMetric( $this->getMetricName( $model, 'updated' ), $ms );
166+
}
167+
168+
/**
169+
* Record the time it took to restore
170+
*
171+
* @param Illuminate\Database\Eloquent\Model $model
172+
*/
173+
public function restored( $model )
174+
{
175+
$ms = round( static::$times['restore'] + microtime( true ), 3 ) * 1000;
176+
\Newrelic::customMetric( $this->getMetricName( $model, 'restored' ), $ms );
177+
}
178+
}

0 commit comments

Comments
 (0)