@@ -27,6 +27,13 @@ class NewRelicHandler extends AbstractProcessingHandler
2727 */
2828 protected $ appName ;
2929
30+ /**
31+ * Name of the current transaction
32+ *
33+ * @var string
34+ */
35+ protected $ transactionName ;
36+
3037 /**
3138 * Some context and extra data is passed into the handler as arrays of values. Do we send them as is
3239 * (useful if we are using the API), or explode them for display on the NewRelic RPM website?
@@ -39,14 +46,21 @@ class NewRelicHandler extends AbstractProcessingHandler
3946 * {@inheritDoc}
4047 *
4148 * @param string $appName
42- * @param boolean $implodeArrays
49+ * @param boolean $explodeArrays
50+ * @param string $transactionName
4351 */
44- public function __construct ($ level = Logger::ERROR , $ bubble = true , $ appName = null , $ explodeArrays = false )
45- {
52+ public function __construct (
53+ $ level = Logger::ERROR ,
54+ $ bubble = true ,
55+ $ appName = null ,
56+ $ explodeArrays = false ,
57+ $ transactionName = null
58+ ) {
4659 parent ::__construct ($ level , $ bubble );
4760
4861 $ this ->appName = $ appName ;
4962 $ this ->explodeArrays = $ explodeArrays ;
63+ $ this ->transactionName = $ transactionName ;
5064 }
5165
5266 /**
@@ -62,6 +76,11 @@ protected function write(array $record)
6276 $ this ->setNewRelicAppName ($ appName );
6377 }
6478
79+ if ($ transactionName = $ this ->getTransactionName ($ record ['context ' ])) {
80+ $ this ->setNewRelicTransactionName ($ transactionName );
81+ unset($ record ['context ' ]['transaction_name ' ]);
82+ }
83+
6584 if (isset ($ record ['context ' ]['exception ' ]) && $ record ['context ' ]['exception ' ] instanceof \Exception) {
6685 newrelic_notice_error ($ record ['message ' ], $ record ['context ' ]['exception ' ]);
6786 unset($ record ['context ' ]['exception ' ]);
@@ -102,7 +121,7 @@ protected function isNewRelicEnabled()
102121
103122 /**
104123 * Returns the appname where this log should be sent. Each log can override the default appname, set in this
105- * handler's constructor, by providing the appname in its context.
124+ * handler's constructor, by providing the appname in it's context.
106125 *
107126 * @param array $context
108127 * @return null|string
@@ -116,6 +135,23 @@ protected function getAppName(array $context)
116135 return $ this ->appName ;
117136 }
118137
138+ /**
139+ * Returns the name of the current transaction. Each log can override the default transaction name, set in this
140+ * handler's constructor, by providing the transaction_name in it's context
141+ *
142+ * @param array $context
143+ *
144+ * @return null|string
145+ */
146+ protected function getTransactionName (array $ context )
147+ {
148+ if (isset ($ context ['transaction_name ' ])) {
149+ return $ context ['transaction_name ' ];
150+ }
151+
152+ return $ this ->transactionName ;
153+ }
154+
119155 /**
120156 * Sets the NewRelic application that should receive this log.
121157 *
@@ -125,4 +161,14 @@ protected function setNewRelicAppName($appName)
125161 {
126162 newrelic_set_appname ($ appName );
127163 }
164+
165+ /**
166+ * Overwrites the name of the current transaction
167+ *
168+ * @param $transactionName
169+ */
170+ protected function setNewRelicTransactionName ($ transactionName )
171+ {
172+ newrelic_name_transaction ($ transactionName );
173+ }
128174}
0 commit comments