Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix: performance.now() is relative from start time

* fix: grpc plugin tests

* fix: import performance
  • Loading branch information
mayurkale22 authored Sep 18, 2019
1 parent 4ff14da commit 7fff538
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/src/common/time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,7 +15,7 @@
*/

import * as types from '@opentelemetry/types';
import { performance } from 'perf_hooks';
import { otperformance as performance } from '../platform';

const NANOSECOND_DIGITS = 9;
const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/platform/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

export * from './id';
export * from './performance';
17 changes: 17 additions & 0 deletions packages/opentelemetry-core/src/platform/browser/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export const otperformance = performance;
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

export * from './id';
export * from './performance';
19 changes: 19 additions & 0 deletions packages/opentelemetry-core/src/platform/node/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { performance } from 'perf_hooks';

export const otperformance = performance;
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/test/common/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,7 +15,7 @@
*/

import * as assert from 'assert';
import { performance } from 'perf_hooks';
import { otperformance as performance } from '../../src/platform';
import * as sinon from 'sinon';
import * as types from '@opentelemetry/types';
import {
Expand Down
9 changes: 6 additions & 3 deletions packages/opentelemetry-exporter-jaeger/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { Link, CanonicalCode } from '@opentelemetry/types';
import { ReadableSpan } from '@opentelemetry/basic-tracer';
import { hrTimeToMilliseconds } from '@opentelemetry/core';
import {
ThriftSpan,
Tag,
Expand Down Expand Up @@ -67,7 +68,7 @@ export function spanToThrift(span: ReadableSpan): ThriftSpan {
fields.push({ key: attr, value: toTagValue(attrs[attr]) })
);
}
return { timestamp: event.time, fields };
return { timestamp: hrTimeToMilliseconds(event.time), fields };
}
);
const spanLogs: ThriftLog[] = ThriftUtils.getThriftLogs(logs);
Expand All @@ -80,9 +81,11 @@ export function spanToThrift(span: ReadableSpan): ThriftSpan {
operationName: span.name,
references: spanLinksToThriftRefs(span.links, span.parentSpanId),
flags: span.spanContext.traceFlags || DEFAULT_FLAGS,
startTime: Utils.encodeInt64(span.startTime * MICROS_PER_MILLI),
startTime: Utils.encodeInt64(
hrTimeToMilliseconds(span.startTime) * MICROS_PER_MILLI
),
duration: Utils.encodeInt64(
Math.round((span.endTime - span.startTime) * MICROS_PER_MILLI)
Math.round(hrTimeToMilliseconds(span.duration) * MICROS_PER_MILLI)
),
tags: spanTags,
logs: spanLogs,
Expand Down
5 changes: 3 additions & 2 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ describe('JaegerExporter', () => {
name: 'my-span1',
kind: types.SpanKind.CLIENT,
spanContext,
startTime: 1566156729709,
endTime: 1566156729709 + 2000,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
status: {
code: types.CanonicalCode.DATA_LOSS,
},
attributes: {},
links: [],
events: [],
duration: [32, 800000000],
};

exporter.export([readableSpan], (result: ExportResult) => {
Expand Down
20 changes: 12 additions & 8 deletions packages/opentelemetry-exporter-jaeger/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { spanToThrift } from '../src/transform';
import { ReadableSpan } from '@opentelemetry/basic-tracer';
import * as types from '@opentelemetry/types';
import { ThriftUtils, Utils, ThriftReferenceType } from '../src/types';
import { hrTimeToMilliseconds } from '@opentelemetry/core';

describe('transform', () => {
const spanContext = {
Expand All @@ -32,8 +33,8 @@ describe('transform', () => {
name: 'my-span',
kind: types.SpanKind.INTERNAL,
spanContext,
startTime: 1566156729709,
endTime: 1566156729709 + 2000,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
status: {
code: types.CanonicalCode.OK,
},
Expand Down Expand Up @@ -61,9 +62,10 @@ describe('transform', () => {
attributes: {
error: true,
},
time: 1566156729709 + 100,
time: [1566156729, 809],
},
],
duration: [32, 800000000],
};

const thriftSpan = spanToThrift(readableSpan);
Expand All @@ -86,7 +88,7 @@ describe('transform', () => {
assert.deepStrictEqual(thriftSpan.flags, 1);
assert.deepStrictEqual(
thriftSpan.startTime,
Utils.encodeInt64(readableSpan.startTime * 1000)
Utils.encodeInt64(hrTimeToMilliseconds(readableSpan.startTime) * 1000)
);
assert.strictEqual(thriftSpan.tags.length, 5);
const [tag1, tag2, tag3, tag4, tag5] = thriftSpan.tags;
Expand Down Expand Up @@ -124,15 +126,16 @@ describe('transform', () => {
name: 'my-span1',
kind: types.SpanKind.CLIENT,
spanContext,
startTime: 1566156729709,
endTime: 1566156729709 + 2000,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
status: {
code: types.CanonicalCode.DATA_LOSS,
message: 'data loss',
},
attributes: {},
links: [],
events: [],
duration: [32, 800000000],
};

const thriftSpan = spanToThrift(readableSpan);
Expand Down Expand Up @@ -176,8 +179,8 @@ describe('transform', () => {
name: 'my-span',
kind: types.SpanKind.INTERNAL,
spanContext,
startTime: 1566156729709,
endTime: 1566156729709 + 2000,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
status: {
code: types.CanonicalCode.OK,
},
Expand All @@ -192,6 +195,7 @@ describe('transform', () => {
},
],
events: [],
duration: [32, 800000000],
};

const thriftSpan = spanToThrift(readableSpan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AttributeNames } from '../../src/enums/AttributeNames';
import { GrpcPlugin } from '../../src/grpc';
import * as grpc from 'grpc';
import { ReadableSpan } from '@opentelemetry/basic-tracer';
import { hrTimeToMilliseconds } from '@opentelemetry/core';

export const assertSpan = (
span: ReadableSpan,
Expand All @@ -38,8 +39,10 @@ export const assertSpan = (
assert.strictEqual(span.links.length, 0);
assert.strictEqual(span.events.length, 1);

assert.ok(span.startTime < span.endTime);
assert.ok(span.endTime > 0);
assert.ok(
hrTimeToMilliseconds(span.startTime) < hrTimeToMilliseconds(span.endTime)
);
assert.ok(hrTimeToMilliseconds(span.endTime) > 0);

if (span.kind === SpanKind.SERVER) {
assert.ok(span.spanContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('HttpPlugin', () => {
const result = await httpRequest.get(
`http://${hostname}:${serverPort}${pathname}`
);
const spans = audit.processSpans();
const spans = memoryExporter.getFinishedSpans();
const [incomingSpan, outgoingSpan] = spans;
const validations = {
hostname,
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-types/src/common/Time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down

0 comments on commit 7fff538

Please sign in to comment.