From 4ad6e6e018cc38a35f51335289fb68c434670a1b Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Thu, 19 Sep 2024 13:09:20 +0200 Subject: [PATCH 1/5] updated examples --- src/common_types.ts | 1 + src/dsl/monitor.ts | 1 + src/push/monitor.ts | 20 ++++++++++++++++++++ templates/journeys/example.journey.ts | 1 + templates/lightweight/heartbeat.yml | 1 + 5 files changed, 24 insertions(+) diff --git a/src/common_types.ts b/src/common_types.ts index 33a59f48..7d1f5e7c 100644 --- a/src/common_types.ts +++ b/src/common_types.ts @@ -255,6 +255,7 @@ export type PushOptions = Partial & kibanaVersion?: string; yes?: boolean; tags?: Array; + labels?: Record; alert?: AlertConfig; retestOnFailure?: MonitorConfig['retestOnFailure']; enabled?: boolean; diff --git a/src/dsl/monitor.ts b/src/dsl/monitor.ts index fdcf098e..c093e897 100644 --- a/src/dsl/monitor.ts +++ b/src/dsl/monitor.ts @@ -58,6 +58,7 @@ export type MonitorConfig = { name?: string; type?: string; tags?: string[]; + labels?: Record; schedule?: typeof ALLOWED_SCHEDULES[number]; enabled?: boolean; locations?: SyntheticsLocationsType[]; diff --git a/src/push/monitor.ts b/src/push/monitor.ts index 80996ea2..24685ddf 100644 --- a/src/push/monitor.ts +++ b/src/push/monitor.ts @@ -268,6 +268,7 @@ export function buildMonitorFromYaml( enabled: config.enabled ?? options.enabled, locations: options.locations, tags: options.tags, + labels: parseLabels(config, options.labels), ...normalizeConfig(config), retestOnFailure, privateLocations, @@ -311,6 +312,25 @@ export const parseAlertConfig = ( return Object.keys(result).length > 0 ? result : undefined; }; +export const parseLabels = ( + config: MonitorConfig, + gLabels?: Record +) => { + // get all keys starting with `label.` + const keys = Object.keys(config).filter(key => key.startsWith('labels.')); + const labels = {}; + for (const key of keys) { + labels[key.replace('labels.', '')] = config[key]; + delete config[key]; + } + if (gLabels) { + for (const key of Object.keys(gLabels)) { + labels[key] = gLabels[key]; + } + } + return Object.keys(labels).length > 0 ? labels : undefined; +}; + export function getAlertKeyValue( key: 'status' | 'tls', config: MonitorConfig, diff --git a/templates/journeys/example.journey.ts b/templates/journeys/example.journey.ts index ae52c00c..a75782a9 100644 --- a/templates/journeys/example.journey.ts +++ b/templates/journeys/example.journey.ts @@ -6,6 +6,7 @@ journey('My Example Journey', ({ page, params }) => { monitor.use({ id: 'example-monitor', schedule: 10, + labels: { foo: 'bar', team: 'frontend' }, }); step('launch application', async () => { await page.goto(params.url); diff --git a/templates/lightweight/heartbeat.yml b/templates/lightweight/heartbeat.yml index c1032c1c..3d7bb8d8 100644 --- a/templates/lightweight/heartbeat.yml +++ b/templates/lightweight/heartbeat.yml @@ -7,3 +7,4 @@ heartbeat.monitors: schedule: '@every 3m' timeout: 16s alert.status.enabled: true + labels.environment: "production" From 7dfc1de1ab59a90f98ec3a154ae42eb895963d62 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Thu, 19 Sep 2024 13:09:38 +0200 Subject: [PATCH 2/5] revert --- templates/journeys/example.journey.ts | 1 - templates/lightweight/heartbeat.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/templates/journeys/example.journey.ts b/templates/journeys/example.journey.ts index a75782a9..ae52c00c 100644 --- a/templates/journeys/example.journey.ts +++ b/templates/journeys/example.journey.ts @@ -6,7 +6,6 @@ journey('My Example Journey', ({ page, params }) => { monitor.use({ id: 'example-monitor', schedule: 10, - labels: { foo: 'bar', team: 'frontend' }, }); step('launch application', async () => { await page.goto(params.url); diff --git a/templates/lightweight/heartbeat.yml b/templates/lightweight/heartbeat.yml index 3d7bb8d8..c1032c1c 100644 --- a/templates/lightweight/heartbeat.yml +++ b/templates/lightweight/heartbeat.yml @@ -7,4 +7,3 @@ heartbeat.monitors: schedule: '@every 3m' timeout: 16s alert.status.enabled: true - labels.environment: "production" From 200e52f7622ad28985d46c42c4723399ddd54533 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Thu, 19 Sep 2024 18:40:06 +0200 Subject: [PATCH 3/5] add unit test --- __tests__/push/monitor.test.ts | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/__tests__/push/monitor.test.ts b/__tests__/push/monitor.test.ts index 0c0fe9bb..f28ebfea 100644 --- a/__tests__/push/monitor.test.ts +++ b/__tests__/push/monitor.test.ts @@ -472,6 +472,50 @@ heartbeat.monitors: hosts: ['elastic.co:443'], }); }); + + it('supports labels in config', async () => { + await writeHBFile(` +heartbeat.monitors: +- type: icmp + schedule: @every 5m + id: "test-icmp" + name: "test-icmp" + privateLocations: + - baz + tags: + - ltag1 + - ltag2 + labels.foo: bar + labels.baz: qux + `); + + const [mon] = await createLightweightMonitors(PROJECT_DIR, { + auth: 'foo', + params: { foo: 'bar' }, + kibanaVersion: '8.8.0', + locations: ['australia_east'], + tags: ['gtag1', 'gtag2'], + privateLocations: ['gbaz'], + schedule: 10, + retestOnFailure: false, + }); + + expect(mon.config).toEqual({ + id: 'test-icmp', + name: 'test-icmp', + locations: ['australia_east'], + privateLocations: ['baz'], + type: 'icmp', + params: { foo: 'bar' }, + schedule: 5, + tags: ['ltag1', 'ltag2'], + retestOnFailure: false, + labels: { + baz: 'qux', + foo: 'bar', + }, + }); + }); }); describe('parseAlertConfig', () => { From 9e0bc61180fd029a12b9d3dfc2c1a9c395546aed Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Fri, 20 Sep 2024 17:17:41 +0200 Subject: [PATCH 4/5] rename labels to fields --- __tests__/push/monitor.test.ts | 8 +++++--- __tests__/utils/test-config.ts | 1 + src/common_types.ts | 2 +- src/dsl/monitor.ts | 2 +- src/push/monitor.ts | 20 ++++++++++---------- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/__tests__/push/monitor.test.ts b/__tests__/push/monitor.test.ts index f28ebfea..4fed4d2b 100644 --- a/__tests__/push/monitor.test.ts +++ b/__tests__/push/monitor.test.ts @@ -83,6 +83,7 @@ describe('Monitors', () => { hash: expect.any(String), locations: ['europe-west2-a', 'australia-southeast1-a'], privateLocations: ['germany'], + fields: { area: 'website' }, }); }); @@ -102,6 +103,7 @@ describe('Monitors', () => { filter: { match: 'test', }, + fields: { area: 'website' }, }); monitor.update({ locations: ['brazil'] }); const schema1 = await buildMonitorSchema([monitor], true); @@ -485,8 +487,8 @@ heartbeat.monitors: tags: - ltag1 - ltag2 - labels.foo: bar - labels.baz: qux + fields.foo: bar + fields.baz: qux `); const [mon] = await createLightweightMonitors(PROJECT_DIR, { @@ -510,7 +512,7 @@ heartbeat.monitors: schedule: 5, tags: ['ltag1', 'ltag2'], retestOnFailure: false, - labels: { + fields: { baz: 'qux', foo: 'bar', }, diff --git a/__tests__/utils/test-config.ts b/__tests__/utils/test-config.ts index 079be903..8eef4d6e 100644 --- a/__tests__/utils/test-config.ts +++ b/__tests__/utils/test-config.ts @@ -39,6 +39,7 @@ export function createTestMonitor(filename: string, type = 'browser') { enabled: true, locations: ['united_kingdom', 'australia_east'], privateLocations: ['germany'], + fields: { area: 'website' }, }); monitor.setSource({ file: join(FIXTURES_DIR, filename), diff --git a/src/common_types.ts b/src/common_types.ts index 7d1f5e7c..a369bf73 100644 --- a/src/common_types.ts +++ b/src/common_types.ts @@ -255,7 +255,7 @@ export type PushOptions = Partial & kibanaVersion?: string; yes?: boolean; tags?: Array; - labels?: Record; + fields?: Record; alert?: AlertConfig; retestOnFailure?: MonitorConfig['retestOnFailure']; enabled?: boolean; diff --git a/src/dsl/monitor.ts b/src/dsl/monitor.ts index c093e897..0e2b943c 100644 --- a/src/dsl/monitor.ts +++ b/src/dsl/monitor.ts @@ -58,7 +58,7 @@ export type MonitorConfig = { name?: string; type?: string; tags?: string[]; - labels?: Record; + fields?: Record; schedule?: typeof ALLOWED_SCHEDULES[number]; enabled?: boolean; locations?: SyntheticsLocationsType[]; diff --git a/src/push/monitor.ts b/src/push/monitor.ts index 24685ddf..217a87d6 100644 --- a/src/push/monitor.ts +++ b/src/push/monitor.ts @@ -268,7 +268,7 @@ export function buildMonitorFromYaml( enabled: config.enabled ?? options.enabled, locations: options.locations, tags: options.tags, - labels: parseLabels(config, options.labels), + fields: parseFields(config, options.fields), ...normalizeConfig(config), retestOnFailure, privateLocations, @@ -312,23 +312,23 @@ export const parseAlertConfig = ( return Object.keys(result).length > 0 ? result : undefined; }; -export const parseLabels = ( +export const parseFields = ( config: MonitorConfig, - gLabels?: Record + gFields?: Record ) => { // get all keys starting with `label.` - const keys = Object.keys(config).filter(key => key.startsWith('labels.')); - const labels = {}; + const keys = Object.keys(config).filter(key => key.startsWith('fields.')); + const fields = {}; for (const key of keys) { - labels[key.replace('labels.', '')] = config[key]; + fields[key.replace('fields.', '')] = config[key]; delete config[key]; } - if (gLabels) { - for (const key of Object.keys(gLabels)) { - labels[key] = gLabels[key]; + if (gFields) { + for (const key of Object.keys(gFields)) { + fields[key] = gFields[key]; } } - return Object.keys(labels).length > 0 ? labels : undefined; + return Object.keys(fields).length > 0 ? fields : undefined; }; export function getAlertKeyValue( From fe2dcb89b0b917199d8d33ac0fe5c636ddf469f3 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Mon, 23 Sep 2024 11:20:30 +0200 Subject: [PATCH 5/5] add more tests --- __tests__/push/monitor.test.ts | 8 ++++++-- src/common_types.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/__tests__/push/monitor.test.ts b/__tests__/push/monitor.test.ts index 4fed4d2b..0d890c96 100644 --- a/__tests__/push/monitor.test.ts +++ b/__tests__/push/monitor.test.ts @@ -105,9 +105,13 @@ describe('Monitors', () => { }, fields: { area: 'website' }, }); - monitor.update({ locations: ['brazil'] }); + monitor.update({ locations: ['brazil'], fields: { env: 'dev' } }); const schema1 = await buildMonitorSchema([monitor], true); expect(schema1[0].hash).not.toEqual(schema[0].hash); + expect(schema1[0].fields).toEqual({ + area: 'website', + env: 'dev', + }); }); it('parse @every schedule format', async () => { @@ -475,7 +479,7 @@ heartbeat.monitors: }); }); - it('supports labels in config', async () => { + it('supports fields in config', async () => { await writeHBFile(` heartbeat.monitors: - type: icmp diff --git a/src/common_types.ts b/src/common_types.ts index a369bf73..cc12fafe 100644 --- a/src/common_types.ts +++ b/src/common_types.ts @@ -255,7 +255,7 @@ export type PushOptions = Partial & kibanaVersion?: string; yes?: boolean; tags?: Array; - fields?: Record; + fields?: MonitorConfig['fields']; alert?: AlertConfig; retestOnFailure?: MonitorConfig['retestOnFailure']; enabled?: boolean;