Skip to content

Commit 29f02cb

Browse files
committed
fix(cls): add alarm flow for deploy and remove
1 parent 1355bcb commit 29f02cb

File tree

4 files changed

+212
-111
lines changed

4 files changed

+212
-111
lines changed

__tests__/cls/cls.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ describe('Cls', () => {
1313

1414
let outputs: DeployOutputs;
1515

16+
const alarms = [
17+
{
18+
name: 'cls-alarm-test',
19+
targets: [
20+
{
21+
period: 15,
22+
query: '* | select count(*) as errCount',
23+
},
24+
],
25+
monitor: {
26+
type: 'Period',
27+
time: 1,
28+
},
29+
trigger: {
30+
condition: '$1.count > 1',
31+
count: 2,
32+
period: 15,
33+
},
34+
noticeId: 'notice-4271ef11-1b09-459f-8dd1-b0a411757663',
35+
},
36+
];
37+
1638
const inputs: DeployInputs = {
1739
region: 'ap-guangzhou',
1840
name: 'cls-test',
@@ -32,14 +54,36 @@ describe('Cls', () => {
3254
},
3355
};
3456

35-
test('should deploy cls success', async () => {
57+
test('deploy cls', async () => {
58+
const res = await client.deploy(inputs);
59+
expect(res).toEqual({
60+
region: process.env.REGION,
61+
name: inputs.name,
62+
topic: inputs.topic,
63+
logsetId: expect.any(String),
64+
topicId: expect.any(String),
65+
});
66+
67+
outputs = res;
68+
});
69+
70+
test('deploy cls with alarms', async () => {
71+
inputs.alarms = alarms;
3672
const res = await client.deploy(inputs);
3773
expect(res).toEqual({
3874
region: process.env.REGION,
3975
name: inputs.name,
4076
topic: inputs.topic,
4177
logsetId: expect.any(String),
4278
topicId: expect.any(String),
79+
alarms: [
80+
{
81+
id: expect.stringContaining('alarm-'),
82+
logsetId: expect.any(String),
83+
topicId: expect.any(String),
84+
...alarms[0],
85+
},
86+
],
4387
});
4488

4589
outputs = res;

src/modules/cls/alarm.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ export default class Alarm {
101101
} else {
102102
console.log(`Alarm ${id} not exist`);
103103
}
104-
}
105-
if (name) {
104+
} else if (name) {
106105
const detail = await this.get({ name });
107106
if (detail) {
108107
await this.request({

src/modules/cls/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ import {
1212
RemoveDashboardInputs,
1313
Dashboard,
1414
DashboardItem,
15+
AlarmInputs,
1516
} from './interface';
1617
import { CapiCredentials, RegionType } from './../interface';
1718
import { ApiError } from '../../utils/error';
1819
import { dtz, TIME_FORMAT, Dayjs } from '../../utils/dayjs';
1920
import { createLogset, createTopic, updateIndex, getSearchSql } from './utils';
21+
import Alarm from './alarm';
2022

2123
export default class Cls {
2224
credentials: CapiCredentials;
2325
region: RegionType;
2426
cls: ClsClient;
27+
alarm: Alarm;
2528

2629
constructor(credentials: CapiCredentials, region: RegionType = 'ap-guangzhou', expire?: number) {
2730
this.region = region;
@@ -34,6 +37,8 @@ export default class Cls {
3437
debug: false,
3538
expire: expire,
3639
});
40+
41+
this.alarm = new Alarm(credentials, this.region);
3742
}
3843

3944
// 创建/更新 日志集
@@ -170,11 +175,25 @@ export default class Cls {
170175
outputs.topicId = inputs.topicId = topicOutput.topicId;
171176
await this.deployIndex(inputs);
172177

178+
// 部署告警
179+
const { alarms = [] } = inputs;
180+
if (alarms.length > 0) {
181+
outputs.alarms = [];
182+
for (let i = 0, len = alarms.length; i < len; i++) {
183+
const res = await this.alarm.create({
184+
...alarms[i],
185+
logsetId: outputs.logsetId,
186+
topicId: outputs.topicId,
187+
});
188+
outputs.alarms.push(res);
189+
}
190+
}
191+
173192
return outputs;
174193
}
175194

176195
// 删除
177-
async remove(inputs: { topicId?: string; logsetId?: string } = {}) {
196+
async remove(inputs: { topicId?: string; logsetId?: string; alarms?: AlarmInputs[] } = {}) {
178197
try {
179198
console.log(`Start removing cls`);
180199
console.log(`Removing cls topic id ${inputs.topicId}`);
@@ -198,6 +217,15 @@ export default class Cls {
198217
message: res2.error.message,
199218
});
200219
}
220+
const { alarms = [] } = inputs;
221+
if (alarms && alarms.length > 0) {
222+
for (let i = 0, len = alarms.length; i < len; i++) {
223+
const cur = alarms[i];
224+
console.log(`Removing alarm name ${cur.name}, id ${cur.id}`);
225+
await this.alarm.delete({ id: cur.id, name: cur.name });
226+
console.log(`Remove alarm name ${cur.name}, id ${cur.id} success`);
227+
}
228+
}
201229
console.log(`Removed cls id ${inputs.logsetId} success`);
202230
} catch (e) {
203231
console.log(e);

0 commit comments

Comments
 (0)