Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check to events-to-s3 for label canary #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.1.18",
"version": "0.1.19",
"description": "An Automation App that handles all your GitHub Repository Activities",
"author": "Peter Zhu",
"homepage": "https://github.com/opensearch-project/automation-app",
Expand Down
23 changes: 23 additions & 0 deletions src/call/github-events-to-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

export default async function githubEventsToS3(app: Probot, context: any): Promise<void> {
// Removed validateResourceConfig to let this function listen on all repos, and filter for only the repos that are public.
Expand All @@ -23,6 +24,28 @@ export default async function githubEventsToS3(app: Probot, context: any): Promi
//
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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any small and simplified name please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that a long name for the label might be better in case someone adds this label randomly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can just use a random hash if you just want to test the addition and deletion.

try {
const cloudWatchClient = new CloudWatchClient({ region: String(process.env.REGION) });
const putMetricDataCommand = new PutMetricDataCommand({
Namespace: 'GitHubCanary',
MetricData: [
{
MetricName: 'LabelCanaryEvent',
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;
}

const eventName = context.payload.action === undefined ? context.name : `${context.name}.${context.payload.action}`;

context.uploaded_at = new Date().toISOString();
Expand Down
76 changes: 76 additions & 0 deletions test/call/github-events-to-s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import { Logger, Probot } from 'probot';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import githubEventsToS3 from '../../src/call/github-events-to-s3';
import { CloudWatchClient, PutMetricDataCommand } from '@aws-sdk/client-cloudwatch';

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

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

beforeEach(() => {
app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' });
Expand All @@ -41,6 +44,11 @@ describe('githubEventsToS3', () => {
send: jest.fn(),
};
(S3Client as jest.Mock).mockImplementation(() => mockS3Client);

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

afterEach(() => {
Expand Down Expand Up @@ -140,4 +148,72 @@ describe('githubEventsToS3', () => {
}),
);
});

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

mockCloudWatchClient.send.mockResolvedValue({});

await githubEventsToS3(app, context);

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 githubEventsToS3(app, context);

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: {
label: {
name: 's3-data-lake-app-canary-label',
},
repository: {
name: 'opensearch-metrics',
private: false,
},
},
};

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

await githubEventsToS3(app, context);

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