-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added check to events-to-s3 for label canary
Signed-off-by: Brandon Shien <bshien@amazon.com>
- Loading branch information
Showing
5 changed files
with
215 additions
and
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,66 @@ | ||
/* | ||
* 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, 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 | ||
} | ||
} | ||
} |
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,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'); | ||
}); | ||
}); |