Skip to content

Commit

Permalink
Added check to events-to-s3 for label canary
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Shien <bshien@amazon.com>
  • Loading branch information
bshien committed Feb 17, 2025
1 parent 3b1ecd9 commit 5ade3ff
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 3 deletions.
4 changes: 4 additions & 0 deletions configs/operations/github-events-to-s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ events:
- all

tasks:
- name: Github Label Canary Monitor
call: github-label-canary-monitor@default
args:
metricName: AutomationApp_EventDataLake
- name: Github Events To S3 Operation
call: github-events-to-s3@default
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opensearch-automation-app",
"version": "0.3.5",
"version": "0.3.6",
"description": "An Automation App that handles all your GitHub Repository Activities",
"author": "Peter Zhu",
"homepage": "https://github.com/opensearch-project/automation-app",
Expand Down
57 changes: 57 additions & 0 deletions src/call/github-label-canary-monitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

// Name : githubLabelCanaryMonitor
// Description : Handle canary event by sending CloudWatch Metrics for monitoring purposes
// Arguments :
// - metricName: (string) The name of the CloudWatch Metric you want to send data to.

import { Probot } from 'probot';
import { CloudWatchClient, PutMetricDataCommand } from '@aws-sdk/client-cloudwatch';

export interface LabelCanaryMonitorParams {
metricName: string;
}

export default async function githubLabelCanaryMonitor(app: Probot, context: any, { metricName }: LabelCanaryMonitorParams): Promise<void> {
// Removed validateResourceConfig to let this function listen on all repos, and filter for only the repos that are public.
// This is done so when a new repo is made public, this app can automatically start processing its events.
//
// This is only for the s3 data lake specific case, everything else should still specify repos required to be listened in resource config.
//
// if (!(await validateResourceConfig(app, context, resource))) return;
//
const repoName = context.payload.repository?.name;
if (context.payload.repository?.private === false) {
// Handle canary event for monitoring purposes
if (repoName === 'opensearch-metrics' && context.name === 'label' && context.payload.label?.name === 's3-data-lake-app-canary-label') {
// Ignore if label was created
if (context.payload.action === 'deleted') {
try {
const cloudWatchClient = new CloudWatchClient({ region: String(process.env.REGION) });
const putMetricDataCommand = new PutMetricDataCommand({
Namespace: 'GitHubLabelCanary',
MetricData: [
{
MetricName: `${metricName}`,
Value: 1,
Unit: 'Count',
},
],
});
await cloudWatchClient.send(putMetricDataCommand);
app.log.info('CloudWatch metric for monitoring published.');
} catch (error) {
app.log.error(`Error Publishing CloudWatch metric for monitoring : ${error}`);
}
}
return; // In the future, add `exit` right here to prevent subsequent tasks from running
}
}
}
124 changes: 124 additions & 0 deletions test/call/github-label-canary-monitor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { Logger, Probot } from 'probot';
import githubLabelCanaryMonitor, { LabelCanaryMonitorParams } from '../../src/call/github-label-canary-monitor';
import { CloudWatchClient, PutMetricDataCommand } from '@aws-sdk/client-cloudwatch';

jest.mock('@aws-sdk/client-cloudwatch');

describe('githubEventsToS3', () => {
let app: Probot;
let context: any;
let mockCloudWatchClient: any;
let args: LabelCanaryMonitorParams;

beforeEach(() => {
args = {
metricName: 'testMetric',
};

app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' });
app.log = {
info: jest.fn(),
error: jest.fn(),
} as unknown as Logger;

context = {
name: 'name',
id: 'id',
payload: {
repository: {
name: 'repo',
owner: { login: 'org' },
private: false,
},
},
};

mockCloudWatchClient = {
send: jest.fn(),
};
(CloudWatchClient as jest.Mock).mockImplementation(() => mockCloudWatchClient);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should publish CloudWatch metric if event is label canary', async () => {
context = {
name: 'label',
id: 'id',
payload: {
action: 'deleted',
label: {
name: 's3-data-lake-app-canary-label',
},
repository: {
name: 'opensearch-metrics',
private: false,
},
},
};

mockCloudWatchClient.send.mockResolvedValue({});

await githubLabelCanaryMonitor(app, context, args);

expect(mockCloudWatchClient.send).toHaveBeenCalledWith(expect.any(PutMetricDataCommand));
expect(app.log.info).toHaveBeenCalledWith('CloudWatch metric for monitoring published.');
});

it('should not publish CloudWatch metric if event is not label canary', async () => {
context = {
name: 'label',
id: 'id',
payload: {
label: {
name: 'normal-label',
},
repository: {
name: 'opensearch-metrics',
private: false,
},
},
};

mockCloudWatchClient.send.mockResolvedValue({});

await githubLabelCanaryMonitor(app, context, args);

expect(mockCloudWatchClient.send).not.toHaveBeenCalledWith(expect.any(PutMetricDataCommand));
expect(app.log.info).not.toHaveBeenCalledWith('CloudWatch metric for monitoring published.');
});

it('should log an error if CloudWatch metric publishing fails', async () => {
context = {
name: 'label',
id: 'id',
payload: {
action: 'deleted',
label: {
name: 's3-data-lake-app-canary-label',
},
repository: {
name: 'opensearch-metrics',
private: false,
},
},
};

mockCloudWatchClient.send.mockRejectedValue(new Error('CloudWatch error'));

await githubLabelCanaryMonitor(app, context, args);

expect(app.log.error).toHaveBeenCalledWith('Error Publishing CloudWatch metric for monitoring : Error: CloudWatch error');
});
});

0 comments on commit 5ade3ff

Please sign in to comment.