Skip to content

Commit

Permalink
Merge pull request #909 from humanmade/backport-897-to-v16-branch
Browse files Browse the repository at this point in the history
[Backport v16-branch] Reduce number of API calls to PutMetricData
  • Loading branch information
joehoyle authored Nov 12, 2024
2 parents 9c232a9 + 5647a27 commit 795a904
Showing 1 changed file with 126 additions and 24 deletions.
150 changes: 126 additions & 24 deletions inc/cavalcade_runner_to_cloudwatch/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ function bootstrap() {
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 );
}

/**
Expand All @@ -50,11 +44,6 @@ function on_job_started( Worker $worker, Job $job ) {
* @param Job $job The current Cavalcade cron 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( $worker, $job, 'fail' );
}
Expand All @@ -66,12 +55,6 @@ function on_job_failed( Worker $worker, Job $job ) {
* @param Job $job The current Cavalcade cron 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( $worker, $job, 'success' );
}

Expand All @@ -80,18 +63,120 @@ function on_job_completed( Worker $worker, Job $job ) {
*
* @param Worker $worker The Cavalcade runner process.
* @param Job $job The current Cavalcade cron job.
* @param string $status The job status.
* @param 'success'|'fail' $status The job status.
*/
function on_end_job( Worker $worker, Job $job, string $status ) {
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' );

$status_metric = $status === 'success' ? 'Completed' : 'Failed';

// Batch all the metrics together to avoid many API calls.
$data = [];
$data[] = [
'MetricName' => $status_metric,
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
[
'Name' => 'Job',
'Value' => $job->hook,
],
],
'Value' => 1,
];
$data[] = [
'MetricName' => $status_metric,
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
],
'Value' => 1,
];
$data[] = [
'MetricName' => $status_metric,
'Value' => 1,
];

$data[] = [
'MetricName' => 'Invocations',
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
[
'Name' => 'Job',
'Value' => $job->hook,
],
],
'Value' => 1,
];
$data[] = [
'MetricName' => 'Invocations',
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
],
'Value' => 1,
];
$data[] = [
'MetricName' => 'Invocations',
'Value' => 1,
];
$data[] = [
'MetricName' => 'Duration',
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
[
'Name' => 'Job',
'Value' => $job->hook,
],
],
'Value' => $duration,
];
$data[] = [
'MetricName' => 'Duration',
'Unit' => 'Seconds',
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
[
'Name' => 'Job',
'Value' => $job->hook,
],
],
'Value' => $duration,
];
$data[] = [
'MetricName' => 'Duration',
'Unit' => 'Seconds',
'Dimensions' => [
[
'Name' => 'Application',
'Value' => HM_ENV,
],
],
'Value' => $duration,
];
$data[] = [
'MetricName' => 'Duration',
'Unit' => 'Seconds',
'Value' => $duration,
];
put_metric_data_multiple( $data );

// Workaround to get the stdout / stderr for the job.
$reflection = new ReflectionClass( $worker );
Expand Down Expand Up @@ -152,6 +237,23 @@ function ( $name, $value ) {
}
}

/**
* Save metric data to CloudWatch.
*
* @param array{ MetricName: string, ?Unit: string, ?Dimensions: { Name: string, value: int}[], Value: float }[] $data The metric data to save.
*/
function put_metric_data_multiple( array $data ) {
try {
cloudwatch_client()->putMetricData([
'MetricData' => $data,
'Namespace' => 'Cavalcade',
]);
} catch ( Exception $e ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
trigger_error( sprintf( 'Error from CloudWatch API: %s', $e->getMessage() ), E_USER_WARNING );
}
}

/**
* Get a configured CloudWatchClient client.
*
Expand Down

0 comments on commit 795a904

Please sign in to comment.