Skip to content

Commit

Permalink
fix(publish-metrics): route debug correctly for publish metrics repor…
Browse files Browse the repository at this point in the history
…ters (artilleryio#2514)

* refactor(publish-metrics): run diag logger for any reporter

* refactor(publish-metrics): call debug for appropriate reporters

* refactor(publish-metrics): implement dynamic debug for tracing

* refactor(publish-metrics): implement dynamic debug for metrics
  • Loading branch information
InesNi authored Feb 21, 2024
1 parent 2da5209 commit 6e56af4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const debug = require('debug')('plugin:publish-metrics:open-telemetry');
const vendorTranslators = require('./vendor-translators');
const {
diag,
Expand All @@ -24,7 +23,7 @@ context.setGlobalContextManager(contextManager);
// DEBUGGING SETUP - setting the OpenTelemetry's internal diagnostic handler here to run when debug is enabled
if (
process.env.DEBUG &&
process.env.DEBUG === 'plugin:publish-metrics:open-telemetry'
process.env.DEBUG.includes('plugin:publish-metrics:')
) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ERROR);
}
Expand All @@ -47,11 +46,21 @@ class OTelReporter {
// Setting traces to first traces configured
if (translatedConfig.traces && !this.tracesConfig) {
this.tracesConfig = translatedConfig.traces;

// Setting debug for traces
this.traceDebug = require('debug')(
`plugin:publish-metrics:${this.tracesConfig.type}`
);
}

// Setting metrics to first metrics configured
if (translatedConfig.metrics && !this.metricsConfig) {
this.metricsConfig = translatedConfig.metrics;

// Setting debug for metrics
this.metricDebug = require('debug')(
`plugin:publish-metrics:${this.metricsConfig.type}`
);
}
return translatedConfig;
});
Expand Down Expand Up @@ -112,6 +121,14 @@ class OTelReporter {
}
}
}
debug(msg) {
if (this.traceDebug) {
this.traceDebug(msg);
}
if (this.metricDebug) {
this.metricDebug(msg);
}
}
warnIfDuplicateTracesConfigured(configList) {
const tracesConfigs = configList.filter((config) => config.traces);
if (tracesConfigs.length > 1) {
Expand All @@ -133,7 +150,7 @@ class OTelReporter {
}

async cleanup(done) {
debug('Cleaning up');
this.debug('Cleaning up');
if (!this.metricsConfig && !this.tracesConfig) {
return done();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const debug = require('debug')('plugin:publish-metrics:open-telemetry');
const {
AggregationTemporality,
MeterProvider,
Expand Down Expand Up @@ -45,6 +44,7 @@ class OTelMetricsReporter {
}

configure(config) {
this.debug = require('debug')(`plugin:publish-metrics:${this.config.type}`);
this.config = {
exporter: config.exporter || 'otlp-http',
meterName: config.meterName || 'Artillery.io_metrics',
Expand All @@ -60,7 +60,7 @@ class OTelMetricsReporter {
resource: this.resource
});

debug('Configuring Metric Exporter');
this.debug('Configuring Metric Exporter');

// Setting configuration options for exporter
this.exporterOpts = {
Expand Down Expand Up @@ -163,13 +163,13 @@ class OTelMetricsReporter {

async cleanup() {
while (this.pendingRequests > 0) {
debug('Waiting for pending metric request ...');
this.debug('Waiting for pending metric request ...');
await sleep(500);
}
debug('Pending metric requests done');
debug('Shutting the Reader down');
this.debug('Pending metric requests done');
this.debug('Shutting the Reader down');
await this.theReader.shutdown();
debug('Shut down sucessfull');
this.debug('Shut down sucessfull');
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const debug = require('debug')('plugin:publish-metrics:open-telemetry');
const grpc = require('@grpc/grpc-js');
const { traceExporters, validateExporter } = require('../exporters');
const {
Expand All @@ -21,13 +20,14 @@ class OTelTraceConfig {
constructor(config, resource) {
this.config = config;
this.resource = resource;
this.debug = require('debug')(`plugin:publish-metrics:${this.config.type}`);

// Validate exporter provided by user
validateExporter(traceExporters, this.config.exporter, 'trace');
}

configure() {
debug('Configuring Tracer Provider');
this.debug('Configuring Tracer Provider');
this.tracerOpts = {
resource: this.resource
};
Expand All @@ -39,7 +39,7 @@ class OTelTraceConfig {

this.tracerProvider = new BasicTracerProvider(this.tracerOpts);

debug('Configuring Exporter');
this.debug('Configuring Exporter');
this.exporterOpts = {};
if (this.config.endpoint) {
this.exporterOpts.url = this.config.endpoint;
Expand Down Expand Up @@ -75,20 +75,21 @@ class OTelTraceConfig {
}

async shutDown() {
debug('Initiating TracerProvider shutdown');
this.debug('Initiating TracerProvider shutdown');
try {
await this.tracerProvider.shutdown();
} catch (err) {
debug(err);
this.debug(err);
}
debug('TracerProvider shutdown completed');
this.debug('TracerProvider shutdown completed');
}
}

class OTelTraceBase {
constructor(config, script) {
this.config = config;
this.script = script;
this.debug = require('debug')(`plugin:publish-metrics:${this.config.type}`);
this.pendingRequestSpans = 0;
this.pendingScenarioSpans = 0;
this.pendingPageSpans = 0;
Expand Down Expand Up @@ -153,7 +154,7 @@ class OTelTraceBase {
(pendingRequests > 0 || pendingScenarios > 0) &&
waitedTime < maxWaitTime
) {
debug('Waiting for pending traces ...');
this.debug('Waiting for pending traces ...');
await sleep(500);
waitedTime += 500;
}
Expand All @@ -176,8 +177,8 @@ class OTelTraceBase {
);
}

debug('Pending traces done');
debug('Waiting for flush period to complete');
this.debug('Pending traces done');
this.debug('Waiting for flush period to complete');
await sleep(5000);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const debug = require('debug')('plugin:publish-metrics:open-telemetry');
const { attachScenarioHooks } = require('../../util');
const { OTelTraceBase } = require('./base');

Expand Down Expand Up @@ -155,7 +154,7 @@ class OTelHTTPTraceReporter extends OTelTraceBase {
this.pendingRequestSpans--;
}
} catch (err) {
debug(err);
this.debug(err);
}
return done();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const debug = require('debug')('plugin:publish-metrics:open-telemetry');
const { attachScenarioHooks } = require('../../util');
const { OTelTraceBase } = require('./base');

Expand Down Expand Up @@ -183,7 +182,7 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
code: SpanStatusCode.ERROR,
message: err.message
});
debug('There has been an error during step execution:');
this.debug('There has been an error during step execution:');
throw err;
} finally {
const difference = Date.now() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ const vendorTranslators = {
return otelTemplate(config, dynatraceTraceSettings);
},
'open-telemetry': (config) => {
let tracesConfig = config;
let newConfig = config;
if (config.traces) {
tracesConfig.traces.type = 'otel';
newConfig.traces.type = 'open-telemetry';
}
return tracesConfig;
if (config.metrics) {
newConfig.metrics.type = 'open-telemetry';
}
return newConfig;
}
};

Expand Down

0 comments on commit 6e56af4

Please sign in to comment.