Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 90f1905

Browse files
authored
Set startTimestamp to creation time of cumulative view (#273)
* Set startTimestamp to creation time of cumulative view * fix review comments
1 parent 7af93bb commit 90f1905

5 files changed

Lines changed: 38 additions & 18 deletions

File tree

packages/opencensus-core/src/common/time-util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export function getTimestampWithProcessHRTime(): Timestamp {
5959
return {seconds, nanos};
6060
}
6161

62+
/**
63+
* Creates a new timestamp from the given milliseconds.
64+
*
65+
* @param {number} epochMilli the timestamp represented in milliseconds since
66+
* epoch.
67+
* @returns {Timestamp} new timestamp with specified fields.
68+
*/
69+
export function timestampFromMillis(epochMilli: number): Timestamp {
70+
return {
71+
seconds: Math.floor(epochMilli / MILLIS_PER_SECOND),
72+
nanos: (epochMilli % MILLIS_PER_SECOND) * NANOS_PER_MILLI
73+
};
74+
}
75+
6276
setHrtimeReference();
6377

6478
export const TEST_ONLY = {

packages/opencensus-core/src/stats/stats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class Stats {
145145

146146
for (const measureName of Object.keys(this.registeredViews)) {
147147
for (const view of this.registeredViews[measureName]) {
148-
metrics.push(view.getMetric());
148+
metrics.push(view.getMetric(view.startTime));
149149
}
150150
}
151151

packages/opencensus-core/src/stats/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export interface View {
118118
/** Gets the view's tag keys */
119119
getColumns(): string[];
120120
/** Gets view`s metric */
121-
getMetric(): Metric;
121+
getMetric(start: number): Metric;
122122
}
123123

124124
/**

packages/opencensus-core/src/stats/view.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as defaultLogger from '../common/console-logger';
18-
import {getTimestampWithProcessHRTime} from '../common/time-util';
18+
import {getTimestampWithProcessHRTime, timestampFromMillis} from '../common/time-util';
1919
import * as loggerTypes from '../common/types';
2020
import {DistributionValue, LabelValue, Metric, MetricDescriptor, MetricDescriptorType, Point, TimeSeries, Timestamp} from '../metrics/export/types';
2121

@@ -200,9 +200,10 @@ export class BaseView implements View {
200200

201201
/**
202202
* Gets view`s metric
203+
* @param start The start timestamp in epoch milliseconds
203204
* @returns {Metric}
204205
*/
205-
getMetric(): Metric {
206+
getMetric(start: number): Metric {
206207
const {type} = this.metricDescriptor;
207208
let startTimestamp: Timestamp;
208209

@@ -215,8 +216,7 @@ export class BaseView implements View {
215216
startTimestamp = null;
216217
break;
217218
default:
218-
// TODO (mayurkale): This should be set when create Cumulative view.
219-
startTimestamp = getTimestampWithProcessHRTime();
219+
startTimestamp = timestampFromMillis(start);
220220
}
221221

222222
const timeseries: TimeSeries[] = [];

packages/opencensus-core/test/test-view.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ describe('BaseView', () => {
199199
const realHrtimeFn = process.hrtime;
200200
const realNowFn = Date.now;
201201
const mockedTime: Timestamp = {seconds: 1450000100, nanos: 1e7};
202+
const mockStartTime = 1546540757282;
203+
const mockStartTimestamp: Timestamp = {seconds: 1546540757, nanos: 282e6};
202204
const measurementValues = [1.1, 2.3, 3.2, 4.3, 5.2];
203205
const buckets = [2, 4, 6];
204206
const tags: Tags = {testKey1: 'testValue', testKey2: 'testValue'};
@@ -227,7 +229,7 @@ describe('BaseView', () => {
227229
TEST_ONLY.resetHrtimeFunctionCache();
228230
});
229231

230-
const {descriptor, timeseries} = view.getMetric();
232+
const {descriptor, timeseries} = view.getMetric(mockStartTime);
231233

232234
describe(
233235
`Aggregation type: ${aggregationTestCase.aggregationType}`, () => {
@@ -263,8 +265,7 @@ describe('BaseView', () => {
263265
assert.ok(startTimestamp);
264266
assert.equal(typeof startTimestamp.nanos, 'number');
265267
assert.equal(typeof startTimestamp.seconds, 'number');
266-
assert.ok(startTimestamp.seconds > 0);
267-
assert.ok(startTimestamp.nanos > 0);
268+
assert.deepStrictEqual(startTimestamp, mockStartTimestamp);
268269
});
269270
}
270271

@@ -288,8 +289,8 @@ describe('BaseView', () => {
288289
}
289290

290291
it('should have point', () => {
291-
const {timeseries} = view.getMetric();
292-
const [{points}] = timeseries;
292+
const {timeseries} = view.getMetric(mockStartTime);
293+
const [{points, startTimestamp}] = timeseries;
293294
assert.ok(points);
294295
const [point] = points;
295296
const {timestamp, value} = point;
@@ -306,6 +307,8 @@ describe('BaseView', () => {
306307
sum: total,
307308
sumOfSquaredDeviation: 10.427999999999997
308309
});
310+
311+
assert.deepStrictEqual(startTimestamp, mockStartTimestamp);
309312
});
310313
});
311314

@@ -325,7 +328,7 @@ describe('BaseView', () => {
325328
}
326329

327330
it('should have points', () => {
328-
const {timeseries} = view.getMetric();
331+
const {timeseries} = view.getMetric(mockStartTime);
329332
assert.equal(timeseries.length, 2);
330333
const [{labelValues: labelValues1, points: points1}, {
331334
labelValues: labelValues2,
@@ -383,8 +386,8 @@ describe('BaseView', () => {
383386
}
384387

385388
it('should have point', () => {
386-
const {timeseries} = view.getMetric();
387-
const [{points}] = timeseries;
389+
const {timeseries} = view.getMetric(mockStartTime);
390+
const [{points, startTimestamp}] = timeseries;
388391
assert.ok(points);
389392
const [point] = points;
390393
const {timestamp, value} = point;
@@ -395,6 +398,7 @@ describe('BaseView', () => {
395398
assert.equal(timestamp.nanos, mockedTime.nanos);
396399
assert.equal(typeof value, 'number');
397400
assert.strictEqual(value, 5);
401+
assert.deepStrictEqual(startTimestamp, mockStartTimestamp);
398402
});
399403
});
400404

@@ -410,8 +414,8 @@ describe('BaseView', () => {
410414
}
411415

412416
it('should have point', () => {
413-
const {timeseries} = view.getMetric();
414-
const [{points}] = timeseries;
417+
const {timeseries} = view.getMetric(mockStartTime);
418+
const [{points, startTimestamp}] = timeseries;
415419
assert.ok(points);
416420
const [point] = points;
417421
const {timestamp, value} = point;
@@ -422,6 +426,7 @@ describe('BaseView', () => {
422426
assert.equal(timestamp.nanos, mockedTime.nanos);
423427
assert.equal(typeof value, 'number');
424428
assert.strictEqual(value, total);
429+
assert.deepStrictEqual(startTimestamp, mockStartTimestamp);
425430
});
426431
});
427432

@@ -435,8 +440,8 @@ describe('BaseView', () => {
435440
}
436441

437442
it('should have point', () => {
438-
const {timeseries} = view.getMetric();
439-
const [{points}] = timeseries;
443+
const {timeseries} = view.getMetric(mockStartTime);
444+
const [{points, startTimestamp}] = timeseries;
440445
assert.ok(points);
441446
const [point] = points;
442447
const {timestamp, value} = point;
@@ -445,6 +450,7 @@ describe('BaseView', () => {
445450
assert.equal(typeof value, 'number');
446451
assert.strictEqual(
447452
value, measurementValues[measurementValues.length - 1]);
453+
assert.strictEqual(startTimestamp, undefined);
448454
});
449455
});
450456
});

0 commit comments

Comments
 (0)