Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(publish-metrics): otel tracing bug #2439

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class OTelReporter {
return done();
}
if (this.httpReporter) {
await this.httpReporter.cleanup();
await this.httpReporter.cleanup('http');
}
if (this.playwrightReporter) {
await this.playwrightReporter.cleanup();
await this.playwrightReporter.cleanup('playwright');
}
await this.traceConfig.shutDown();
return done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ class OTelTraceConfig {
this.exporterOpts
);

this.processorOpts = {
scheduledDelayMillis: this.config.scheduledDelayMillis || 5000,
maxExportBatchSize: this.config.maxExportBatchSize || 1000,
maxQueueSize: this.config.maxQueueSize || 2000
};
const Processor = this.config.smartSampling
? OutlierDetectionBatchSpanProcessor
: BatchSpanProcessor;

this.tracerProvider.addSpanProcessor(
new Processor(this.exporter, {
scheduledDelayMillis: 1000
})
);
this.tracerProvider.addSpanProcessor(new Processor(this.exporter));
this.tracerProvider.register();
}

Expand All @@ -89,6 +90,9 @@ class OTelTraceBase {
this.script = script;
this.pendingRequestSpans = 0;
this.pendingScenarioSpans = 0;
this.pendingPageSpans = 0;
this.pendingStepSpans = 0;
this.pendingPlaywrightScenarioSpans = 0;
}
setTracer(engine) {
// Get and set the tracer by engine
Expand Down Expand Up @@ -141,13 +145,38 @@ class OTelTraceBase {
otelTraceOnError(scenarioErr, req, userContext, ee, done) {
done();
}

async cleanup() {
while (this.pendingRequestSpans > 0 || this.pendingScenarioSpans > 0) {
async waitOnPendingSpans(pendingRequests, pendingScenarios, maxWaitTime) {
let waitedTime = 0;
while (
(pendingRequests > 0 || pendingScenarios > 0) &&
waitedTime < maxWaitTime
) {
debug('Waiting for pending traces ...');
await new Promise((resolve) => setTimeout(resolve, 500));
waitedTime += 500;
}
return true;
}

async cleanup(engines) {
if (engines.includes('http')) {
await this.waitOnPendingSpans(
this.pendingRequestSpans,
this.pendingScenarioSpans,
5000
);
}
if (engines.includes('playwright')) {
await this.waitOnPendingSpans(
this.pendingPlaywrightScenarioSpans,
this.pendingPlaywrightScenarioSpans,
5000
);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
'vu.uuid': vuContext.vars.$uuid,
...(this.config.attributes || {})
});
this.pendingPlaywrightScenarioSpans++;
// Set variables to track state and context
const ctx = context.active();
let lastPageUrl;
Expand Down Expand Up @@ -104,6 +105,7 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
scenarioSpan.addEvent(`navigated to ${page.url()}`);
if (pageSpan) {
pageSpan.end();
this.pendingPlaywrightSpans--;
}

pageSpan = this.playwrightTracer.startSpan(
Expand All @@ -116,6 +118,7 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
...(this.config.attributes || {})
});
lastPageUrl = pageUrl;
this.pendingPlaywrightSpans++;
}
});

Expand Down Expand Up @@ -143,8 +146,10 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
} finally {
if (pageSpan && !pageSpan.endTime[0]) {
pageSpan.end();
this.pendingPlaywrightSpans--;
}
scenarioSpan.end();
this.pendingPlaywrightScenarioSpans--;
}
}
);
Expand All @@ -159,6 +164,7 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
{ kind: SpanKind.CLIENT },
context.active()
);
this.pendingPlaywrightSpans++;
const startTime = Date.now();

try {
Expand All @@ -180,6 +186,7 @@ class OTelPlaywrightTraceReporter extends OTelTraceBase {
const difference = Date.now() - startTime;
events.emit('histogram', `browser.step.${stepName}`, difference);
span.end();
this.pendingPlaywrightSpans--;
}
});
};
Expand Down
Loading