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 race condition between response end and the trace end #25

Merged
merged 4 commits into from
Feb 2, 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
5 changes: 5 additions & 0 deletions .changeset/early-coats-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/otel": patch
---

Eliminate race condition between response end and trace end
4 changes: 2 additions & 2 deletions .github/workflows/release-snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ on:

jobs:
release-snapshot:
name: Release
name: Release Snapshot
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -68,7 +68,7 @@ jobs:
run: pnpm install

- name: Build
run: pnpm build
run: pnpm --filter @vercel/otel build

- name: Add SHORT_SHA env property with commit short sha
run: echo "SHORT_SHA=`echo ${{ github.sha }} | cut -c1-8`" >> $GITHUB_ENV
Expand Down
2 changes: 2 additions & 0 deletions packages/otel/src/exporters/otlp-exporter-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export abstract class OTLPExporterEdgeBase<
.then((res) => {
diag.debug("@vercel/otel/otlp: onSuccess", res.status, res.statusText);
onSuccess();
// Drain the response body.
void res.arrayBuffer().catch(() => undefined);
})
.catch((err) => {
diag.error("@vercel/otel/otlp: onError", err);
Expand Down
37 changes: 33 additions & 4 deletions packages/otel/src/processor/composite-span-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getVercelRequestContextAttributes } from "../vercel-request-context/att
/** @internal */
export class CompositeSpanProcessor implements SpanProcessor {
private readonly rootSpanIds = new Map<string, string>();
private readonly waitSpanEnd = new Map<string, () => void>();

constructor(private processors: SpanProcessor[]) {}

Expand Down Expand Up @@ -45,7 +46,29 @@ export class CompositeSpanProcessor implements SpanProcessor {
// Flush the streams to avoid data loss.
const vrc = getVercelRequestContext();
if (vrc) {
vrc.waitUntil(() => this.forceFlush());
vrc.waitUntil(async () => {
if (this.rootSpanIds.has(traceId)) {
// Not root has not completed yet, so no point in flushing.
// Need to wait for onEnd.
const promise = new Promise<void>((resolve) => {
this.waitSpanEnd.set(traceId, resolve);
});
let timer: NodeJS.Timeout | undefined;
await Promise.race([
promise,
new Promise((resolve) => {
timer = setTimeout(() => {
this.waitSpanEnd.delete(traceId);
resolve(undefined);
}, 50);
}),
]);
if (timer) {
clearTimeout(timer);
}
}
return this.forceFlush();
});
}
}

Expand All @@ -57,13 +80,19 @@ export class CompositeSpanProcessor implements SpanProcessor {
onEnd(span: ReadableSpan): void {
const { traceId, spanId } = span.spanContext();
const isRoot = this.rootSpanIds.get(traceId) === spanId;
if (isRoot) {
this.rootSpanIds.delete(traceId);
}

for (const spanProcessor of this.processors) {
spanProcessor.onEnd(span);
}

if (isRoot) {
this.rootSpanIds.delete(traceId);
const pending = this.waitSpanEnd.get(traceId);
if (pending) {
this.waitSpanEnd.delete(traceId);
pending();
}
}
}
}

Expand Down
Loading