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

feat: support yml anchors during push command #903

Merged
merged 2 commits into from
Mar 14, 2024
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
57 changes: 57 additions & 0 deletions __tests__/push/monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,63 @@ heartbeat.monitors:
},
});
});

it('support anchors', async () => {
await writeHBFile(`
http-type: &http-type
type: http
schedule5: &schedule-5
schedule: '@every 5m'
team-tag: &team-tag
tags:
- team1
- team2

heartbeat.monitors:
- id: http1
name: "http1"
schedule: '@every 1m'
<<: *http-type
<<: *team-tag
urls: "https://blog.elastic.co"
- id: http2
name: "http2"
<<: *http-type
<<: *schedule-5
urls: "https://elastic.co"
- id: tcp1
name: "tcp1"
type: tcp
<<: *schedule-5
hosts: ["elastic.co:443"]
`);
const [mon1, mon2, mon3] = await createLightweightMonitors(
PROJECT_DIR,
opts
);
expect(mon1.config).toMatchObject({
id: 'http1',
name: 'http1',
schedule: 1,
type: 'http',
tags: ['team1', 'team2'],
urls: 'https://blog.elastic.co',
});
expect(mon2.config).toMatchObject({
id: 'http2',
name: 'http2',
schedule: 5,
type: 'http',
urls: 'https://elastic.co',
});
expect(mon3.config).toMatchObject({
id: 'tcp1',
name: 'tcp1',
schedule: 5,
type: 'tcp',
hosts: ['elastic.co:443'],
});
});
});

describe('parseAlertConfig', () => {
Expand Down
23 changes: 16 additions & 7 deletions src/push/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { mkdir, rm, readFile } from 'fs/promises';
import { extname, join } from 'path';
import { LineCounter, parseDocument, YAMLSeq, YAMLMap } from 'yaml';
import { LineCounter, parseDocument, Document, YAMLSeq, YAMLMap } from 'yaml';
import { bold, red } from 'kleur/colors';
import { Bundler } from './bundler';
import { SYNTHETICS_PATH, totalist, indent, warn } from '../helpers';
Expand Down Expand Up @@ -188,8 +188,9 @@ export async function createLightweightMonitors(
const lineCounter = new LineCounter();
const parsedDoc = parseDocument(content, {
lineCounter,
merge: true,
keepSourceTokens: true,
});
}) as Document.Parsed;
// Skip other yml files that are not relevant
const monitorSeq = parsedDoc.get('heartbeat.monitors') as YAMLSeq<YAMLMap>;
if (!monitorSeq) {
Expand All @@ -203,21 +204,29 @@ export async function createLightweightMonitors(
warnOnce = true;
}

// Store the offsets of each monitor in the sequence to construct the source
// location later for capturing the error
const offsets = [];
for (const monNode of monitorSeq.items) {
offsets.push(monNode.srcToken.offset);
}

const mergedConfig = parsedDoc.toJS()['heartbeat.monitors'];
for (let i = 0; i < mergedConfig.length; i++) {
const monitor = mergedConfig[i];
// Skip browser monitors from the YML files
if (monNode.get('type') === 'browser') {
if (monitor['type'] === 'browser') {
continue;
}
const config = monNode.toJSON();
const { line, col } = lineCounter.linePos(monNode.srcToken.offset);
const { line, col } = lineCounter.linePos(offsets[i]);
try {
const mon = buildMonitorFromYaml(config, options);
const mon = buildMonitorFromYaml(monitor, options);
mon.setSource({ file, line, column: col });
monitors.push(mon);
} catch (e) {
let outer = bold(`Aborted: ${e}\n`);
outer += indent(
`* ${config.id || config.name} - ${file}:${line}:${col}\n`
`* ${monitor.id || monitor.name} - ${file}:${line}:${col}\n`
);
throw red(outer);
}
Expand Down
Loading