-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from humanmade/cavalcade-runner-to-cloudwatch
Send cavalcade metrics to cloudwatch.
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace HM\Cavalcade\CloudWatch; | ||
|
||
use Aws\CloudWatch\CloudWatchClient; | ||
use Exception; | ||
use HM\Cavalcade\Runner\Hooks; | ||
use HM\Cavalcade\Runner\Job; | ||
use HM\Cavalcade\Runner\Runner; | ||
use HM\Cavalcade\Runner\Worker; | ||
|
||
Runner::instance()->hooks->register( 'Runner.run_job.started', __NAMESPACE__ . '\\on_job_started' ); | ||
Runner::instance()->hooks->register( 'Runner.check_workers.job_failed', __NAMESPACE__ . '\\on_job_failed' ); | ||
Runner::instance()->hooks->register( 'Runner.check_workers.job_completed', __NAMESPACE__ . '\\on_job_completed' ); | ||
|
||
/** | ||
* Called when a new job is started via Cavalcade, and sends an Invocation metric to CloudWatch. | ||
* | ||
* @param Worker $worker | ||
* @param Job $job | ||
*/ | ||
function on_job_started( Worker $worker, Job $job ) { | ||
global $job_start_times; | ||
$job_start_times[ $job->id ] = microtime( true ); | ||
put_metric_data( 'Invocations', 1, [ 'Application' => HM_ENV, 'Job' => $job->hook ] ); | ||
put_metric_data( 'Invocations', 1, [ 'Application' => HM_ENV ] ); | ||
put_metric_data( 'Invocations', 1 ); | ||
} | ||
|
||
/** | ||
* Called when a job is failed via Cavalcade, and sends an Completed metric to CloudWatch. | ||
* | ||
* @param Worker $worker | ||
* @param Job $job | ||
*/ | ||
function on_job_failed( Worker $worker, Job $job ) { | ||
put_metric_data( 'Failed', 1, [ 'Application' => HM_ENV, 'Job' => $job->hook ] ); | ||
put_metric_data( 'Failed', 1, [ 'Application' => HM_ENV ] ); | ||
put_metric_data( 'Failed', 1 ); | ||
on_end_job( $job ); | ||
} | ||
|
||
/** | ||
* Called when a job is completed via Cavalcade, and sends an Completed metric to CloudWatch. | ||
* | ||
* @param Worker $worker | ||
* @param Job $job | ||
*/ | ||
function on_job_completed( Worker $worker, Job $job ) { | ||
put_metric_data( 'Completed', 1, [ 'Application' => HM_ENV, 'Job' => $job->hook ] ); | ||
put_metric_data( 'Completed', 1, [ 'Application' => HM_ENV ] ); | ||
put_metric_data( 'Completed', 1 ); | ||
on_end_job( $job ); | ||
} | ||
|
||
/** | ||
* Called when a job completed or failed via Cavalcade, and sends a Dureaction metric to CloudWatch. | ||
* | ||
* @param Job $job | ||
*/ | ||
function on_end_job( Job $job ) { | ||
global $job_start_times; | ||
$duration = microtime( true ) - $job_start_times[ $job->id ]; | ||
unset( $job_start_times[ $job->id ] ); | ||
put_metric_data( 'Duration', $duration, [ 'Application' => HM_ENV, 'Job' => $job->hook ], 'Seconds' ); | ||
put_metric_data( 'Duration', $duration, [ 'Application' => HM_ENV ], 'Seconds' ); | ||
put_metric_data( 'Duration', $duration, [], 'Seconds' ); | ||
} | ||
|
||
/** | ||
* Save metric data to CloudWatch. | ||
* | ||
* @param string $metric_name | ||
* @param float $value | ||
* @param array $dimensions | ||
* @param string $unit | ||
*/ | ||
function put_metric_data( $metric_name, $value, $dimensions = [], $unit = 'None' ) { | ||
$client = CloudWatchClient::factory( [ | ||
'profile' => 'default', | ||
'version' => '2010-08-01', | ||
'region' => HM_ENV_REGION, | ||
'http' => [ | ||
'synchronous' => false, | ||
], | ||
] ); | ||
|
||
try { | ||
$client->putMetricData([ | ||
'MetricData' => [ | ||
[ | ||
'Dimensions' => array_map( | ||
function ( $name, $value ) { | ||
return [ 'Name' => $name, 'Value' => $value ]; | ||
}, | ||
array_keys( $dimensions ), | ||
$dimensions | ||
), | ||
'MetricName' => $metric_name, | ||
'Unit' => $unit, | ||
'Value' => $value, | ||
], | ||
], | ||
'Namespace' => 'Cavalcade', | ||
]); | ||
} catch ( Exception $e ) { | ||
trigger_error( sprintf( 'Error from CloudWatch API: %s', $e->getMessage() ), E_USER_WARNING ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters