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

feat: allow timestamps to be passed to endSpan #747

Merged
merged 3 commits into from
May 14, 2018
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
4 changes: 3 additions & 1 deletion src/plugin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ export interface SpanData {

/**
* Ends the span. This method should only be called once.
* @param timestamp A custom span end time; defaults to the time when endSpan
* was called if not provided.
*/
endSpan(): void;
endSpan(timestamp?: Date): void;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/span-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ export abstract class BaseSpanData implements SpanData {
this.span.labels[k] = v;
}

endSpan() {
this.span.endTime = (new Date()).toISOString();
endSpan(timestamp?: Date) {
timestamp = timestamp || new Date();
this.span.endTime = timestamp.toISOString();
}
}

Expand All @@ -131,8 +132,8 @@ export class RootSpanData extends BaseSpanData implements types.RootSpanData {
skipFrames); /* # of frames to skip in stack trace */
}

endSpan() {
super.endSpan();
endSpan(timestamp?: Date) {
super.endSpan(timestamp);
traceWriter.get().writeSpan(this.trace);
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/test-span-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ describe('SpanData', () => {
});

it('accurately records timestamps', async () => {
// Create another span, to determine start time correctness
const startLowerBound = Date.now();
const spanData = new CommonSpanData(trace, 'name', '0', 0);
const startUpperBound = Date.now();
Expand All @@ -106,6 +105,16 @@ describe('SpanData', () => {
expectedTimes.slice().sort(ascending), expectedTimes);
});

it('accepts a custom span end time', () => {
const spanData = new CommonSpanData(trace, 'name', '0', 0);
const startTime = new Date(spanData.span.startTime).getTime();
// This input Date is far enough in the future that it's unlikely that the
// time this function was called could be close to it.
spanData.endSpan(new Date(startTime + 1000000));
const endTime = new Date(spanData.span.endTime).getTime();
assert.strictEqual(endTime - startTime, 1000000);
});

it('truncates large span names to limit', () => {
const name = 'a'.repeat(200);
const spanData = new CommonSpanData(trace, name, '0', 0);
Expand Down