Skip to content

Commit

Permalink
Merge pull request #2498 from artilleryio/sleep-utility-for-publish-m…
Browse files Browse the repository at this point in the history
…etrics

refactor(publish-metrics): create sleep utility for publish-metrics
  • Loading branch information
InesNi authored Feb 19, 2024
2 parents fde8a88 + b345e7a commit db4ca95
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/artillery-plugin-publish-metrics/lib/cloudwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const {
} = require('@aws-sdk/client-cloudwatch');
const debug = require('debug')('plugin:publish-metrics:cloudwatch');

const { sleep } = require('./util');

const COUNTERS_STATS = 'counters'; // counters stats
const RATES_STATS = 'rates'; // rates stats
const SUMMARIES_STATS = 'summaries'; // summaries stats
Expand Down Expand Up @@ -177,7 +179,7 @@ class CloudWatchReporter {
async waitingForRequest() {
do {
debug('Waiting for pending request ...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
} while (this.pendingRequests > 0);

debug('Pending requests done');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const got = require('got');
const debug = require('debug')('plugin:publish-metrics:dynatrace');
const path = require('path');
const { sleep } = require('../util');

class DynatraceReporter {
constructor(config, events, script) {
Expand Down Expand Up @@ -294,7 +295,7 @@ class DynatraceReporter {
async waitingForRequest() {
while (this.pendingRequests > 0) {
debug('Waiting for pending request ...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
}

debug('Pending requests done');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const got = require('got');
const { sleep } = require('../util');
const debug = require('debug')('plugin:publish-metrics:newrelic');

class NewRelicReporter {
Expand Down Expand Up @@ -270,7 +271,7 @@ class NewRelicReporter {
async waitingForRequest() {
while (this.pendingRequests > 0) {
debug('Waiting for pending request...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
}

debug('Pending requests done');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
const grpc = require('@grpc/grpc-js');
const { metricExporters, validateExporter } = require('./exporters');
const { metrics } = require('@opentelemetry/api');
const { sleep } = require('../util');

class OTelMetricsReporter {
constructor(config, events, resource) {
Expand Down Expand Up @@ -160,7 +161,7 @@ class OTelMetricsReporter {
async cleanup() {
while (this.pendingRequests > 0) {
debug('Waiting for pending metric request ...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
}
debug('Pending metric requests done');
debug('Shutting the Reader down');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
BatchSpanProcessor
} = require('@opentelemetry/sdk-trace-base');
const { SpanKind, trace } = require('@opentelemetry/api');
const { sleep } = require('../../util');

class OTelTraceConfig {
constructor(config, resource) {
Expand Down Expand Up @@ -152,7 +153,7 @@ class OTelTraceBase {
waitedTime < maxWaitTime
) {
debug('Waiting for pending traces ...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
waitedTime += 500;
}
return true;
Expand All @@ -176,7 +177,7 @@ class OTelTraceBase {

debug('Pending traces done');
debug('Waiting for flush period to complete');
await new Promise((resolve) => setTimeout(resolve, 5000));
await sleep(5000);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/artillery-plugin-publish-metrics/lib/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const PromClient = require('prom-client');
const uuid = require('uuid');
const debug = require('debug')('plugin:publish-metrics:prometheus');
const { sleep } = require('./util');

const COUNTERS_STATS = 'counters', // counters stats
RATES_STATS = 'rates', // rates stats
Expand Down Expand Up @@ -163,7 +164,7 @@ class PrometheusReporter {
async waitingForRequest() {
do {
debug('Waiting for pending request ...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
} while (this.hasPendingRequest);

debug('Pending requests done');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const got = require('got');
const { sleep } = require('../util');
const debug = require('debug')('plugin:publish-metrics:splunk');

class SplunkReporter {
Expand Down Expand Up @@ -212,7 +213,7 @@ class SplunkReporter {
async waitingForRequest() {
while (this.pendingRequests > 0) {
debug('Waiting for pending request ...');
await new Promise((resolve) => setTimeout(resolve, 500));
await sleep(500);
}

debug('Pending requests done');
Expand Down
11 changes: 10 additions & 1 deletion packages/artillery-plugin-publish-metrics/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
module.exports = {
attachScenarioHooks,
versionCheck
versionCheck,
sleep
};

const semver = require('semver');

const sleep = async function (n) {
return new Promise((resolve, _reject) => {
setTimeout(function () {
resolve();
}, n);
});
};

// TODO: Extract into a utility function in Artillery itself
function versionCheck(range) {
if (!global.artillery || !global.artillery.version) {
Expand Down

0 comments on commit db4ca95

Please sign in to comment.