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

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
7 changes: 7 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,12 @@ events:
- all

tasks:
- name: Github Label Canary Monitor
call: github-label-canary-monitor@default
args:
nameSpace: GitHubLabelCanary
metricName: AutomationApp_EventDataLake
value: '1'
unit: Count
- 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
69 changes: 69 additions & 0 deletions src/call/github-label-canary-monitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 :
// - nameSpace: (string) The namespace of the CloudWatch Metric you want to send data to.
// - metricName: (string) The metric name of the CloudWatch Metric you want to send data to.
// - value: (string) The value of the CloudWatch Metric you want to send.
// - unit: (string) The unit of the CloudWatch Metric you want to send.

import { Probot } from 'probot';
import { CloudWatchClient, PutMetricDataCommand, StandardUnit } from '@aws-sdk/client-cloudwatch';
import { Resource } from '../service/resource/resource';

export interface LabelCanaryMonitorParams {
nameSpace: string;
metricName: string;
value: string;
unit: string;
}

export default async function githubLabelCanaryMonitor(
app: Probot,
context: any,
resource: Resource,
{ nameSpace, metricName, value, unit }: 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: nameSpace,
MetricData: [
{
MetricName: metricName,
Value: Number(value),
Unit: unit as StandardUnit,
},
],
});
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
}
}
}
139 changes: 139 additions & 0 deletions test/call/github-label-canary-monitor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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 resource: any;
let mockCloudWatchClient: any;
let args: LabelCanaryMonitorParams;

beforeEach(() => {
args = {
nameSpace: 'testNameSpace',
metricName: 'testMetric',
value: '1',
unit: 'Count',
};

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,
},
},
};

resource = {
organizations: new Map([
[
'org',
{
repositories: new Map([['repo', 'repo object']]),
},
],
]),
};

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, resource, 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, resource, 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, resource, args);

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