UNPKG

artillery-plugin-datadog

Version:

Datadog output plugin for Artillery

109 lines (96 loc) 3.59 kB
// Generated by CoffeeScript 2.0.1 (function() { // Report Artillery results to Datadog // License: Apache-2.0 "use strict"; var DatadogPlugin, datadog, debug; datadog = require('datadog-metrics'); debug = require('debug')('plugin:datadog'); DatadogPlugin = class DatadogPlugin { constructor(config, ee) { // This runs on artillery 'stats' event, when load test results are available. // It can be run 1...n times. Upon running, extract metrics, format them and // add to Datadog queue (but do not send them yet). this.addStats = this.addStats.bind(this); this.config = config; this.ee = ee; debug('Initializing Datadog...'); datadog.init(this.getDatadogConfig()); // Set event handlers debug('Binding event handlers...'); this.ee.on('stats', this.addStats); this.ee.on('done', this.flushStats); } getDatadogConfig() { return { host: this.config.plugins.datadog.host || '', prefix: this.config.plugins.datadog.prefix || 'artillery.' }; } // Return a list of Datadog tags for all metrics // Example: ['target: google.com', 'team:sre'] getTags() { var tags; tags = [`target:${this.config.target}`]; return tags.concat(this.config.plugins.datadog.tags); } // Calculate the % value of successful vs failed responses // The lower the return value, the more requests failed (HTTP 5xx) // Treat redirects as OK getOkPercentage(metrics) { var percentage; percentage = (metrics['response.2xx'][0] + metrics['response.3xx'][0]) * 100 / metrics['requests.completed'][0]; if (isNaN(percentage)) { return 0; } return Math.round(percentage * 100) / 100; } addStats(statsObject) { var code, count, metrics, name, ref, ref1, ref2, results, stats, tags, type, value; stats = statsObject.report(); metrics = { 'scenarios.created': [stats.scenariosCreated, datadog.increment], 'scenarios.completed': [stats.scenariosCompleted, datadog.increment], 'requests.completed': [stats.requestsCompleted, datadog.increment], 'requests.pending': [stats.pendingRequests, datadog.increment], 'response.2xx': [0, datadog.increment], 'response.3xx': [0, datadog.increment], 'response.4xx': [0, datadog.increment], 'response.5xx': [0, datadog.increment], 'rps.mean': [stats.rps.mean, datadog.gauge] }; ref = stats.codes; for (code in ref) { count = ref[code]; metrics[`response.${code[0]}xx`][0] += count; metrics[`response.${code}`] = [count, datadog.increment]; } ref1 = stats.latency; for (type in ref1) { value = ref1[type]; metrics[`latency.${type}`] = [value, datadog.gauge]; } ref2 = stats.scenarioDuration; for (type in ref2) { value = ref2[type]; metrics[`scenarioDuration.${type}`] = [value, datadog.gauge]; } metrics['response.ok_pct'] = [this.getOkPercentage(metrics), datadog.gauge]; tags = this.getTags(); results = []; for (name in metrics) { value = metrics[name]; results.push(value[1](name, value[0], tags)); } return results; } flushStats(statsObject) { return datadog.flush(function() { return debug('Flushed metrics to Datadog'); }, function() { return debug('Unable to send metrics to Datadog!'); }); } }; module.exports = DatadogPlugin; }).call(this);