Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
feat: Add StatsD and Telegraf/StatsD support
Browse files Browse the repository at this point in the history
  • Loading branch information
hassy committed Jan 8, 2019
1 parent 70eef77 commit 332a3ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 1 addition & 2 deletions index.js
Expand Up @@ -17,10 +17,9 @@ function Plugin(script, events) {
this.reporters = [];

script.config.plugins['publish-metrics'].forEach((config) => {
if (config.type === 'datadog') {
if (config.type === 'datadog' || config.type === 'statsd' || config.type === 'influxdb-statsd') {
const { createDatadogReporter } = require('./lib/datadog');
this.reporters.push(createDatadogReporter(config, events, script));
// TODO: Emit user warnings if no API key is present etc.
} else {
events.emit(
'userWarning',
Expand Down
19 changes: 13 additions & 6 deletions lib/datadog.js
Expand Up @@ -5,17 +5,17 @@
const datadogMetrics = require('datadog-metrics');
const dogapi = require('dogapi');
const Hotshots = require('hot-shots');
const debug = require('debug')('plugin:publish-metrics');
const debug = require('debug')('plugin:publish-metrics:datadog-statsd');

function DatadogReporter(config, events, script) {
this.metrics = null;
this.dogapi = dogapi;
this.events = events;
this.reportingType = '';

config = Object.assign({
host: '127.0.0.1',
port: 8125,
prefix: 'artillery.',
event: { send: false },
tags: []
}, config);
Expand Down Expand Up @@ -47,13 +47,19 @@ function DatadogReporter(config, events, script) {
this.reportingType = 'api';
} else if (config.host && config.port) {
debug('Initializing datadog via agent');
this.metrics = new Hotshots({
const options = {
host: config.host,
port: config.port,
prefix: config.prefix,
globalTags: config.tags, // list of strings:likethis?
globalTags: config.tags,
bufferFlushInterval: 1000
});
};

if (config.type === 'influxdb-statsd') {
options.telegraf = true;
}

this.metrics = new Hotshots(options);
this.reportingType = 'agent';
} else {
events.emit('userWarning', `datadog reporting requires apiKey or both host and port to be set`);
Expand Down Expand Up @@ -131,7 +137,8 @@ function DatadogReporter(config, events, script) {
}

DatadogReporter.prototype.event = function(opts) {
const send = this.reportingType === 'api' ? this.dogapi.event.create : this.metrics.event;
const send = this.reportingType === 'api' ? this.dogapi.event.create.bind(this.dogapi) : this.metrics.event.bind(this.metrics);

send(opts.title,
opts.text || opts.title,
{
Expand Down

0 comments on commit 332a3ef

Please sign in to comment.