From 7871d398cfb462a5bc31d766df63762f82eb32fe Mon Sep 17 00:00:00 2001 From: Ines Fazlic Date: Wed, 16 Aug 2023 11:39:52 +0100 Subject: [PATCH] feat(publish-metrics): fail early if required config missing (#2049) --- .../lib/dynatrace/index.js | 12 ++++++------ .../lib/honeycomb.js | 5 +++++ .../lib/lightstep.js | 6 ++++++ .../artillery-plugin-publish-metrics/lib/mixpanel.js | 9 ++++++--- .../lib/newrelic/index.js | 12 ++++++------ .../lib/prometheus.js | 10 +++++----- .../lib/splunk/index.js | 10 ++++++++-- 7 files changed, 42 insertions(+), 22 deletions(-) diff --git a/packages/artillery-plugin-publish-metrics/lib/dynatrace/index.js b/packages/artillery-plugin-publish-metrics/lib/dynatrace/index.js index edde174295..de2627bac5 100644 --- a/packages/artillery-plugin-publish-metrics/lib/dynatrace/index.js +++ b/packages/artillery-plugin-publish-metrics/lib/dynatrace/index.js @@ -5,6 +5,12 @@ const debug = require('debug')('plugin:publish-metrics:dynatrace'); class DynatraceReporter { constructor(config, events, script) { + if (!config.apiToken || !config.envUrl) { + throw new Error( + 'Dynatrace reporter: both apiToken and envUrl must be set. More info in the docs (https://docs.art/reference/extensions/publish-metrics#dynatrace)' + ); + } + this.config = { apiToken: config.apiToken, envUrl: config.envUrl, @@ -14,12 +20,6 @@ class DynatraceReporter { dimensions: this.parseDimensions(config.dimensions) }; - if (!config.apiToken || !config.envUrl) { - throw new Error( - 'Dynatrace API Access Token or Environment URL not specified. In order to send metrics to Dynatrace both `apiToken` and `envUrl` must be set' - ); - } - // Configure event if set - if event key is set but its value isn't we use defaults if (config.hasOwnProperty('event')) { this.eventConfig = { diff --git a/packages/artillery-plugin-publish-metrics/lib/honeycomb.js b/packages/artillery-plugin-publish-metrics/lib/honeycomb.js index 0c88097527..488d4a61d6 100644 --- a/packages/artillery-plugin-publish-metrics/lib/honeycomb.js +++ b/packages/artillery-plugin-publish-metrics/lib/honeycomb.js @@ -10,6 +10,11 @@ const { URL } = require('url'); class HoneycombReporter { constructor(config, events, script) { + if (!config.apiKey || !config.writeKey) { + throw new Error( + 'Honeycomb reporter: apiKey or writeKey must be provided. More info in the docs (https://docs.art/reference/extensions/publish-metrics#honeycomb)' + ); + } this.hnyOpts = { writeKey: config.apiKey || config.writeKey, dataset: config.dataset, diff --git a/packages/artillery-plugin-publish-metrics/lib/lightstep.js b/packages/artillery-plugin-publish-metrics/lib/lightstep.js index 7c8a18f06c..1f80d22e48 100644 --- a/packages/artillery-plugin-publish-metrics/lib/lightstep.js +++ b/packages/artillery-plugin-publish-metrics/lib/lightstep.js @@ -13,6 +13,11 @@ const { URL } = require('url'); class LightstepReporter { constructor(config, events, script) { + if (!config.accessToken || !config.componentName) { + throw new Error( + 'Lightstep reporter: accessToken and componentName must be provided. More info in the docs (https://docs.art/reference/extensions/publish-metrics#lightstep)' + ); + } this.lightstepOpts = { accessToken: config.accessToken, componentName: config.componentName, @@ -45,6 +50,7 @@ class LightstepReporter { component_name: this.lightstepOpts.componentName, access_token: this.lightstepOpts.accessToken }); + opentracing.initGlobalTracer(this.tracer); attachScenarioHooks(script, [ diff --git a/packages/artillery-plugin-publish-metrics/lib/mixpanel.js b/packages/artillery-plugin-publish-metrics/lib/mixpanel.js index b0e91f3c37..fbb8e1ba4a 100644 --- a/packages/artillery-plugin-publish-metrics/lib/mixpanel.js +++ b/packages/artillery-plugin-publish-metrics/lib/mixpanel.js @@ -4,6 +4,11 @@ const debug = require('debug')('plugin:publish-metrics:mixpanel'); class MixPanelReporter { constructor(config, events, script) { + if (!config.projectToken) { + throw new Error( + 'Mixpanel reporter: projectToken must be provided. More info in the docs (https://docs.art/reference/extensions/publish-metrics#mixpanel)' + ); + } this.mixPanelOpts = { projectToken: config.projectToken }; @@ -15,9 +20,7 @@ class MixPanelReporter { })` ); } - if (!this.mixPanelOpts.projectToken) { - console.error('mix panel project token not specified'); - } + this.mixpanel = Mixpanel.init(this.mixPanelOpts.projectToken); this.sendToMixPanel(config, events, script); debug('init done'); diff --git a/packages/artillery-plugin-publish-metrics/lib/newrelic/index.js b/packages/artillery-plugin-publish-metrics/lib/newrelic/index.js index c83439786e..f676af7bab 100644 --- a/packages/artillery-plugin-publish-metrics/lib/newrelic/index.js +++ b/packages/artillery-plugin-publish-metrics/lib/newrelic/index.js @@ -3,6 +3,12 @@ const debug = require('debug')('plugin:publish-metrics:newrelic'); class NewRelicReporter { constructor(config, events, script) { + if (!config.licenseKey) { + throw new Error( + 'New Relic reporter: licenseKey must be provided. More info in the docs (https://docs.art/reference/extensions/publish-metrics#newrelic)' + ); + } + // Set each config value as matching user config if exists, else default values this.config = { region: config.region || 'us', @@ -13,12 +19,6 @@ class NewRelicReporter { licenseKey: config.licenseKey }; - if (!config.licenseKey) { - throw new Error( - 'New Relic License Key not specified. In order to send metrics to New Relic `licenseKey` must be provided' - ); - } - if (config.hasOwnProperty('event') && !config.event?.accountId) { throw new Error( 'New Relic account ID not specified. In order to send events to New Relic `accountId` must be provided' diff --git a/packages/artillery-plugin-publish-metrics/lib/prometheus.js b/packages/artillery-plugin-publish-metrics/lib/prometheus.js index 42319e1f57..bb03b1f416 100644 --- a/packages/artillery-plugin-publish-metrics/lib/prometheus.js +++ b/packages/artillery-plugin-publish-metrics/lib/prometheus.js @@ -11,6 +11,11 @@ const COUNTERS_STATS = 'counters', // counters stats class PrometheusReporter { constructor(config, events) { + if (!config.pushgateway) { + throw new Error( + 'Prometheus reporter: pushgateway must be provided. More info in the docs (https://docs.art/reference/extensions/publish-metrics#prometheus-pushgateway)' + ); + } this.hasPendingRequest = false; this.workerID = process.env.WORKER_ID || uuid.v4(); this.config = Object.assign( @@ -26,11 +31,6 @@ class PrometheusReporter { ca: config.ca }; - debug('ensuring pushgatewayUrl is configured'); - if (!this.prometheusOpts.pushgatewayUrl) { - console.error(`the prometheus [pushgateway] url was not specified`); - } - debug('setting default labels'); PromClient.register.setDefaultLabels(this.tagsToLabels(this.config.tags)); diff --git a/packages/artillery-plugin-publish-metrics/lib/splunk/index.js b/packages/artillery-plugin-publish-metrics/lib/splunk/index.js index 77b035e620..747df9cdaa 100644 --- a/packages/artillery-plugin-publish-metrics/lib/splunk/index.js +++ b/packages/artillery-plugin-publish-metrics/lib/splunk/index.js @@ -3,6 +3,12 @@ const debug = require('debug')('plugin:publish-metrics:splunk'); class SplunkReporter { constructor(config, events, script) { + if (!config.accessToken) { + throw new Error( + 'Splunk reporter: accessToken must be provided. More info in the docs (https://docs.art/reference/extensions/publish-metrics#splunk)' + ); + } + this.config = { realm: config.realm || 'us0', prefix: config.prefix || 'artillery.', @@ -18,7 +24,7 @@ class SplunkReporter { // Event API endpoint requires request payload to be an array of objects this.eventOpts = [ { - eventType: config.event.eventType || `Artillery_io_Test`, + eventType: config.event.eventType || 'Artillery_io_Test', dimensions: { target: script.config.target, ...this.parseDimensions(config.event.dimensions) @@ -217,7 +223,7 @@ class SplunkReporter { if (this.startedEventSent) { const timestamp = Date.now(); this.eventOpts[0].timestamp = timestamp; - this.eventOpts[0].dimensions.phase = `Test-Finished`; + this.eventOpts[0].dimensions.phase = 'Test-Finished'; this.sendRequest(this.ingestAPIEventEndpoint, this.eventOpts, 'event'); }