Skip to content

Commit

Permalink
Merge branch 'master' into http-option-require-parent
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored May 6, 2020
2 parents cb1e933 + e2205ef commit 6eed95e
Show file tree
Hide file tree
Showing 30 changed files with 258 additions and 208 deletions.
24 changes: 12 additions & 12 deletions packages/opentelemetry-core/src/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

Expand All @@ -25,7 +25,7 @@ const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);
* Converts a number to HrTime
* @param epochMillis
*/
function numberToHrtime(epochMillis: number): types.HrTime {
function numberToHrtime(epochMillis: number): api.HrTime {
const epochSeconds = epochMillis / 1000;
// Decimals only.
const seconds = Math.trunc(epochSeconds);
Expand All @@ -49,7 +49,7 @@ function getTimeOrigin(): number {
* Returns an hrtime calculated via performance component.
* @param performanceNow
*/
export function hrTime(performanceNow?: number): types.HrTime {
export function hrTime(performanceNow?: number): api.HrTime {
const timeOrigin = numberToHrtime(getTimeOrigin());
const now = numberToHrtime(
typeof performanceNow === 'number' ? performanceNow : performance.now()
Expand All @@ -72,10 +72,10 @@ export function hrTime(performanceNow?: number): types.HrTime {
* Converts a TimeInput to an HrTime, defaults to _hrtime().
* @param time
*/
export function timeInputToHrTime(time: types.TimeInput): types.HrTime {
export function timeInputToHrTime(time: api.TimeInput): api.HrTime {
// process.hrtime
if (isTimeInputHrTime(time)) {
return time as types.HrTime;
return time as api.HrTime;
} else if (typeof time === 'number') {
// Must be a performance.now() if it's smaller than process start time.
if (time < getTimeOrigin()) {
Expand All @@ -97,9 +97,9 @@ export function timeInputToHrTime(time: types.TimeInput): types.HrTime {
* @param endTime
*/
export function hrTimeDuration(
startTime: types.HrTime,
endTime: types.HrTime
): types.HrTime {
startTime: api.HrTime,
endTime: api.HrTime
): api.HrTime {
let seconds = endTime[0] - startTime[0];
let nanos = endTime[1] - startTime[1];

Expand All @@ -117,7 +117,7 @@ export function hrTimeDuration(
* Convert hrTime to timestamp, for example "2019-05-14T17:00:00.000123456Z"
* @param hrTime
*/
export function hrTimeToTimeStamp(hrTime: types.HrTime): string {
export function hrTimeToTimeStamp(hrTime: api.HrTime): string {
const precision = NANOSECOND_DIGITS;
const tmp = `${'0'.repeat(precision)}${hrTime[1]}Z`;
const nanoString = tmp.substr(tmp.length - precision - 1);
Expand All @@ -129,23 +129,23 @@ export function hrTimeToTimeStamp(hrTime: types.HrTime): string {
* Convert hrTime to nanoseconds.
* @param hrTime
*/
export function hrTimeToNanoseconds(hrTime: types.HrTime): number {
export function hrTimeToNanoseconds(hrTime: api.HrTime): number {
return hrTime[0] * SECOND_TO_NANOSECONDS + hrTime[1];
}

/**
* Convert hrTime to milliseconds.
* @param hrTime
*/
export function hrTimeToMilliseconds(hrTime: types.HrTime): number {
export function hrTimeToMilliseconds(hrTime: api.HrTime): number {
return Math.round(hrTime[0] * 1e3 + hrTime[1] / 1e6);
}

/**
* Convert hrTime to microseconds.
* @param hrTime
*/
export function hrTimeToMicroseconds(hrTime: types.HrTime): number {
export function hrTimeToMicroseconds(hrTime: api.HrTime): number {
return Math.round(hrTime[0] * 1e6 + hrTime[1] / 1e3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { getParentSpanContext, setExtractedSpanContext } from '../context';
export const X_B3_TRACE_ID = 'x-b3-traceid';
export const X_B3_SPAN_ID = 'x-b3-spanid';
export const X_B3_SAMPLED = 'x-b3-sampled';
const VALID_TRACEID_REGEX = /^[0-9a-f]{32}$/i;
const VALID_TRACEID_REGEX = /^([0-9a-f]{16}){1,2}$/i;
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
const INVALID_ID_REGEX = /^0+$/i;

Expand Down Expand Up @@ -72,16 +72,21 @@ export class B3Propagator implements HttpTextPropagator {
const traceIdHeader = getter(carrier, X_B3_TRACE_ID);
const spanIdHeader = getter(carrier, X_B3_SPAN_ID);
const sampledHeader = getter(carrier, X_B3_SAMPLED);
const traceId = Array.isArray(traceIdHeader)

const traceIdHeaderValue = Array.isArray(traceIdHeader)
? traceIdHeader[0]
: traceIdHeader;
const spanId = Array.isArray(spanIdHeader) ? spanIdHeader[0] : spanIdHeader;

const options = Array.isArray(sampledHeader)
? sampledHeader[0]
: sampledHeader;

if (typeof traceId !== 'string' || typeof spanId !== 'string')
if (typeof traceIdHeaderValue !== 'string' || typeof spanId !== 'string') {
return context;
}

const traceId = traceIdHeaderValue.padStart(32, '0');

if (isValidTraceId(traceId) && isValidSpanId(spanId)) {
return setExtractedSpanContext(context, {
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/src/trace/TraceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';
import { validateKey, validateValue } from '../internal/validators';

const MAX_TRACE_STATE_ITEMS = 32;
Expand All @@ -31,7 +31,7 @@ const LIST_MEMBER_KEY_VALUE_SPLITTER = '=';
* - The value of any key can be updated. Modified keys MUST be moved to the
* beginning of the list.
*/
export class TraceState implements types.TraceState {
export class TraceState implements api.TraceState {
private _internalState: Map<string, string> = new Map();

constructor(rawTraceState?: string) {
Expand Down
12 changes: 6 additions & 6 deletions packages/opentelemetry-core/test/common/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as assert from 'assert';
import { otperformance as performance } from '../../src/platform';
import * as sinon from 'sinon';
import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';
import {
hrTime,
timeInputToHrTime,
Expand Down Expand Up @@ -141,16 +141,16 @@ describe('time', () => {

describe('#hrTimeDuration', () => {
it('should return duration', () => {
const startTime: types.HrTime = [22, 400000000];
const endTime: types.HrTime = [32, 800000000];
const startTime: api.HrTime = [22, 400000000];
const endTime: api.HrTime = [32, 800000000];

const output = hrTimeDuration(startTime, endTime);
assert.deepStrictEqual(output, [10, 400000000]);
});

it('should handle nanosecond overflow', () => {
const startTime: types.HrTime = [22, 400000000];
const endTime: types.HrTime = [32, 200000000];
const startTime: api.HrTime = [22, 400000000];
const endTime: api.HrTime = [32, 200000000];

const output = hrTimeDuration(startTime, endTime);
assert.deepStrictEqual(output, [9, 800000000]);
Expand All @@ -159,7 +159,7 @@ describe('time', () => {

describe('#hrTimeToTimeStamp', () => {
it('should return timestamp', () => {
const time: types.HrTime = [1573513121, 123456];
const time: api.HrTime = [1573513121, 123456];

const output = hrTimeToTimeStamp(time);
assert.deepStrictEqual(output, '2019-11-11T22:58:41.000123456Z');
Expand Down
16 changes: 16 additions & 0 deletions packages/opentelemetry-core/test/context/B3Propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,21 @@ describe('B3Propagator', () => {
assert.ok(ctx2 === Context.ROOT_CONTEXT);
assert.ok(ctx3 === Context.ROOT_CONTEXT);
});

it('should left-pad 64 bit trace ids with 0', () => {
carrier[X_B3_TRACE_ID] = '8448eb211c80319c';
carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331';
carrier[X_B3_SAMPLED] = '1';
const extractedSpanContext = getExtractedSpanContext(
b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '00000000000000008448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
});
});
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-jaeger/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';

/**
* Options for Jaeger configuration
*/
export interface ExporterConfig {
logger?: types.Logger;
logger?: api.Logger;
serviceName: string;
tags?: Tag[];
host?: string; // default: 'localhost'
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as assert from 'assert';
import { JaegerExporter } from '../src';
import { ExportResult, NoopLogger } from '@opentelemetry/core';
import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';
import { ThriftProcess } from '../src/types';
import { ReadableSpan } from '@opentelemetry/tracing';
import { TraceFlags } from '@opentelemetry/api';
Expand Down Expand Up @@ -130,13 +130,13 @@ describe('JaegerExporter', () => {
};
const readableSpan: ReadableSpan = {
name: 'my-span1',
kind: types.SpanKind.CLIENT,
kind: api.SpanKind.CLIENT,
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.DATA_LOSS,
code: api.CanonicalCode.DATA_LOSS,
},
attributes: {},
links: [],
Expand Down
18 changes: 9 additions & 9 deletions packages/opentelemetry-exporter-jaeger/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as assert from 'assert';
import { spanToThrift } from '../src/transform';
import { ReadableSpan } from '@opentelemetry/tracing';
import { Resource } from '@opentelemetry/resources';
import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';
import { ThriftUtils, Utils, ThriftReferenceType } from '../src/types';
import { hrTimeToMicroseconds } from '@opentelemetry/core';
import { TraceFlags } from '@opentelemetry/api';
Expand All @@ -34,13 +34,13 @@ describe('transform', () => {
it('should convert an OpenTelemetry span to a Thrift', () => {
const readableSpan: ReadableSpan = {
name: 'my-span',
kind: types.SpanKind.INTERNAL,
kind: api.SpanKind.INTERNAL,
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.OK,
code: api.CanonicalCode.OK,
},
attributes: {
testBool: true,
Expand Down Expand Up @@ -155,13 +155,13 @@ describe('transform', () => {
it('should convert an OpenTelemetry span to a Thrift when links, events and attributes are empty', () => {
const readableSpan: ReadableSpan = {
name: 'my-span1',
kind: types.SpanKind.CLIENT,
kind: api.SpanKind.CLIENT,
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.DATA_LOSS,
code: api.CanonicalCode.DATA_LOSS,
message: 'data loss',
},
attributes: {},
Expand Down Expand Up @@ -213,13 +213,13 @@ describe('transform', () => {
it('should convert an OpenTelemetry span to a Thrift with ThriftReference', () => {
const readableSpan: ReadableSpan = {
name: 'my-span',
kind: types.SpanKind.INTERNAL,
kind: api.SpanKind.INTERNAL,
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.OK,
code: api.CanonicalCode.OK,
},
attributes: {},
parentSpanId: '3e0c63257de34c92',
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('transform', () => {
it('should left pad trace ids', () => {
const readableSpan: ReadableSpan = {
name: 'my-span1',
kind: types.SpanKind.CLIENT,
kind: api.SpanKind.CLIENT,
spanContext: {
traceId: '92b449d5929fda1b',
spanId: '6e0c63257de34c92',
Expand All @@ -265,7 +265,7 @@ describe('transform', () => {
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.DATA_LOSS,
code: api.CanonicalCode.DATA_LOSS,
message: 'data loss',
},
attributes: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';

/**
* Configuration interface for prometheus exporter
Expand Down Expand Up @@ -49,5 +49,5 @@ export interface ExporterConfig {
startServer?: boolean;

/** Standard logging interface */
logger?: types.Logger;
logger?: api.Logger;
}
6 changes: 3 additions & 3 deletions packages/opentelemetry-exporter-prometheus/src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
ObserverAggregator,
Sum,
} from '@opentelemetry/metrics';
import * as types from '@opentelemetry/api';
import * as api from '@opentelemetry/api';
import { createServer, IncomingMessage, Server, ServerResponse } from 'http';
import { Counter, Gauge, labelValues, Metric, Registry } from 'prom-client';
import * as url from 'url';
Expand All @@ -44,7 +44,7 @@ export class PrometheusExporter implements MetricExporter {
};

private readonly _registry = new Registry();
private readonly _logger: types.Logger;
private readonly _logger: api.Logger;
private readonly _port: number;
private readonly _endpoint: string;
private readonly _server: Server;
Expand Down Expand Up @@ -159,7 +159,7 @@ export class PrometheusExporter implements MetricExporter {
// TODO: only counter and gauge are implemented in metrics so far
}

private _getLabelValues(keys: string[], labels: types.Labels) {
private _getLabelValues(keys: string[], labels: api.Labels) {
const labelValues: labelValues = {};
for (let i = 0; i < keys.length; i++) {
if (labels[keys[i]] !== null) {
Expand Down
Loading

0 comments on commit 6eed95e

Please sign in to comment.