Skip to content

Commit

Permalink
feat: allow timestamps to be passed to endSpan (#747)
Browse files Browse the repository at this point in the history
PR-URL: #747
  • Loading branch information
kjin authored May 14, 2018
1 parent 908e431 commit 319642a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
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

0 comments on commit 319642a

Please sign in to comment.