InfluxDB 2.x - Send annotations (#4175)

* InfluxDB 2.x - send annotations

* InfluxDB 2.x - send annotations. Add organisation query param
This commit is contained in:
pavel bairov 2024-06-14 12:08:18 +03:00 committed by GitHub
parent ebecb2eea2
commit a1968f2c1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 131 additions and 16 deletions

View File

@ -71,9 +71,9 @@ jobs:
- name: Run test without a CLI
run: xvfb-run node test/runWithoutCli.js
- name: Run test with Influx 1.8
run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --xvfb --logToFile
run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --xvfb --logToFile --resultBaseUrl https://result.sitespeed.io --influxdb.annotationScreenshot=true
- name: Run test with Influx 2.6.1
run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --influxdb.port 8087 --influxdb.version 2 --influxdb.organisation sitespeed --influxdb.token sitespeed --xvfb
run: bin/sitespeed.js http://127.0.0.1:3001/simple/ -n 1 --influxdb.host 127.0.0.1 --influxdb.port 8087 --influxdb.version 2 --influxdb.organisation sitespeed --influxdb.token sitespeed --xvfb --resultBaseUrl https://result.sitespeed.io --influxdb.annotationScreenshot=true
- name: Run Chrome test with config
run: node bin/sitespeed.js --config test/exampleConfig.json http://127.0.0.1:3001/simple/ --xvfb
- name: Run Chrome test using compare plugin

View File

@ -5,7 +5,8 @@ import dayjs from 'dayjs';
import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import { InfluxDBSender as Sender } from './sender.js';
import { InfluxDB2Sender as SenderV2 } from './senderV2.js';
import { send } from './send-annotation.js';
import { sendV1 } from './send-annotation.js';
import { sendV2 } from './send-annotationV2.js';
import { InfluxDBDataGenerator as DataGenerator } from './data-generator.js';
import { throwIfMissing } from '../../support/util.js';
@ -137,18 +138,31 @@ export default class InfluxDBPlugin extends SitespeedioPlugin {
);
this.receivedTypesThatFireAnnotations[message.url] = 0;
return send(
message.url,
message.group,
absolutePagePath,
this.useScreenshots,
this.screenshotType,
// Browsertime pass on when the first run was done for that URL
message.runTime,
this.alias,
this.usingBrowsertime,
this.options
);
return this.options.influxdb.version == 2
? sendV2(
message.url,
message.group,
absolutePagePath,
this.useScreenshots,
this.screenshotType,
// Browsertime pass on when the first run was done for that URL
message.runTime,
this.alias,
this.usingBrowsertime,
this.options
)
: sendV1(
message.url,
message.group,
absolutePagePath,
this.useScreenshots,
this.screenshotType,
// Browsertime pass on when the first run was done for that URL
message.runTime,
this.alias,
this.usingBrowsertime,
this.options
);
}
});
} else {

View File

@ -13,7 +13,7 @@ import {
const log = intel.getLogger('sitespeedio.plugin.influxdb');
export function send(
export function sendV1(
url,
group,
absolutePagePath,

View File

@ -0,0 +1,101 @@
import http from 'node:http';
import https from 'node:https';
import intel from 'intel';
import dayjs from 'dayjs';
import { getConnectivity, getURLAndGroup } from '../../support/tsdbUtil.js';
import {
getAnnotationMessage,
getTagsAsString
} from '../../support/annotationsHelper.js';
const log = intel.getLogger('sitespeedio.plugin.influxdb');
export function sendV2(
url,
group,
absolutePagePath,
screenShotsEnabledInBrowsertime,
screenshotType,
runTime,
alias,
usingBrowsertime,
options
) {
// The tags make it possible for the dashboard to use the
// templates to choose which annotations that will be showed.
// That's why we need to send tags that matches the template
// variables in Grafana.
const connectivity = getConnectivity(options);
const browser = options.browser;
const urlAndGroup = getURLAndGroup(
options,
group,
url,
options.influxdb.includeQueryParams,
alias
).split('.');
let tags = [connectivity, browser, urlAndGroup[0], urlAndGroup[1]];
if (options.slug) {
tags.push(options.slug);
}
const message = getAnnotationMessage(
absolutePagePath,
screenShotsEnabledInBrowsertime,
screenshotType,
undefined,
usingBrowsertime,
options
);
const timestamp = runTime
? Math.round(dayjs(runTime) / 1000)
: Math.round(dayjs() / 1000);
// if we have a category, let us send that category too
if (options.influxdb.tags) {
for (let row of options.influxdb.tags.split(',')) {
const keyAndValue = row.split('=');
tags.push(keyAndValue[1]);
}
}
const influxDBTags = getTagsAsString(tags);
const postData = `events title="Sitespeed.io",text="${message}",tags=${influxDBTags} ${timestamp}`;
const postOptions = {
hostname: options.influxdb.host,
port: options.influxdb.port,
path: `/api/v2/write?org=${options.influxdb.organisation}&bucket=${options.influxdb.database}&precision=s`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
Authorization: `Token ${options.influxdb.token}`
}
};
return new Promise((resolve, reject) => {
log.debug('Send annotation to Influx: %j', postData);
// not perfect but maybe work for us
const library = options.influxdb.protocol === 'https' ? https : http;
const request = library.request(postOptions, res => {
if (res.statusCode === 204) {
res.setEncoding('utf8');
log.debug('Sent annotation to InfluxDB');
resolve();
} else {
const e = new Error(
`Got ${res.statusCode} from InfluxDB when sending annotation ${res.statusMessage}`
);
log.warn(e.message);
reject(e);
}
});
request.on('error', error => {
log.error('Got error from InfluxDB when sending annotation', error);
reject(error);
});
request.write(postData);
request.end();
});
}