Skip to content

Commit 6eed95e

Browse files
authored
Merge branch 'master' into http-option-require-parent
2 parents cb1e933 + e2205ef commit 6eed95e

File tree

30 files changed

+258
-208
lines changed

30 files changed

+258
-208
lines changed

packages/opentelemetry-core/src/common/time.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as types from '@opentelemetry/api';
17+
import * as api from '@opentelemetry/api';
1818
import { otperformance as performance } from '../platform';
1919
import { TimeOriginLegacy } from './types';
2020

@@ -25,7 +25,7 @@ const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);
2525
* Converts a number to HrTime
2626
* @param epochMillis
2727
*/
28-
function numberToHrtime(epochMillis: number): types.HrTime {
28+
function numberToHrtime(epochMillis: number): api.HrTime {
2929
const epochSeconds = epochMillis / 1000;
3030
// Decimals only.
3131
const seconds = Math.trunc(epochSeconds);
@@ -49,7 +49,7 @@ function getTimeOrigin(): number {
4949
* Returns an hrtime calculated via performance component.
5050
* @param performanceNow
5151
*/
52-
export function hrTime(performanceNow?: number): types.HrTime {
52+
export function hrTime(performanceNow?: number): api.HrTime {
5353
const timeOrigin = numberToHrtime(getTimeOrigin());
5454
const now = numberToHrtime(
5555
typeof performanceNow === 'number' ? performanceNow : performance.now()
@@ -72,10 +72,10 @@ export function hrTime(performanceNow?: number): types.HrTime {
7272
* Converts a TimeInput to an HrTime, defaults to _hrtime().
7373
* @param time
7474
*/
75-
export function timeInputToHrTime(time: types.TimeInput): types.HrTime {
75+
export function timeInputToHrTime(time: api.TimeInput): api.HrTime {
7676
// process.hrtime
7777
if (isTimeInputHrTime(time)) {
78-
return time as types.HrTime;
78+
return time as api.HrTime;
7979
} else if (typeof time === 'number') {
8080
// Must be a performance.now() if it's smaller than process start time.
8181
if (time < getTimeOrigin()) {
@@ -97,9 +97,9 @@ export function timeInputToHrTime(time: types.TimeInput): types.HrTime {
9797
* @param endTime
9898
*/
9999
export function hrTimeDuration(
100-
startTime: types.HrTime,
101-
endTime: types.HrTime
102-
): types.HrTime {
100+
startTime: api.HrTime,
101+
endTime: api.HrTime
102+
): api.HrTime {
103103
let seconds = endTime[0] - startTime[0];
104104
let nanos = endTime[1] - startTime[1];
105105

@@ -117,7 +117,7 @@ export function hrTimeDuration(
117117
* Convert hrTime to timestamp, for example "2019-05-14T17:00:00.000123456Z"
118118
* @param hrTime
119119
*/
120-
export function hrTimeToTimeStamp(hrTime: types.HrTime): string {
120+
export function hrTimeToTimeStamp(hrTime: api.HrTime): string {
121121
const precision = NANOSECOND_DIGITS;
122122
const tmp = `${'0'.repeat(precision)}${hrTime[1]}Z`;
123123
const nanoString = tmp.substr(tmp.length - precision - 1);
@@ -129,23 +129,23 @@ export function hrTimeToTimeStamp(hrTime: types.HrTime): string {
129129
* Convert hrTime to nanoseconds.
130130
* @param hrTime
131131
*/
132-
export function hrTimeToNanoseconds(hrTime: types.HrTime): number {
132+
export function hrTimeToNanoseconds(hrTime: api.HrTime): number {
133133
return hrTime[0] * SECOND_TO_NANOSECONDS + hrTime[1];
134134
}
135135

136136
/**
137137
* Convert hrTime to milliseconds.
138138
* @param hrTime
139139
*/
140-
export function hrTimeToMilliseconds(hrTime: types.HrTime): number {
140+
export function hrTimeToMilliseconds(hrTime: api.HrTime): number {
141141
return Math.round(hrTime[0] * 1e3 + hrTime[1] / 1e6);
142142
}
143143

144144
/**
145145
* Convert hrTime to microseconds.
146146
* @param hrTime
147147
*/
148-
export function hrTimeToMicroseconds(hrTime: types.HrTime): number {
148+
export function hrTimeToMicroseconds(hrTime: api.HrTime): number {
149149
return Math.round(hrTime[0] * 1e6 + hrTime[1] / 1e3);
150150
}
151151

packages/opentelemetry-core/src/context/propagation/B3Propagator.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { getParentSpanContext, setExtractedSpanContext } from '../context';
2626
export const X_B3_TRACE_ID = 'x-b3-traceid';
2727
export const X_B3_SPAN_ID = 'x-b3-spanid';
2828
export const X_B3_SAMPLED = 'x-b3-sampled';
29-
const VALID_TRACEID_REGEX = /^[0-9a-f]{32}$/i;
29+
const VALID_TRACEID_REGEX = /^([0-9a-f]{16}){1,2}$/i;
3030
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
3131
const INVALID_ID_REGEX = /^0+$/i;
3232

@@ -72,16 +72,21 @@ export class B3Propagator implements HttpTextPropagator {
7272
const traceIdHeader = getter(carrier, X_B3_TRACE_ID);
7373
const spanIdHeader = getter(carrier, X_B3_SPAN_ID);
7474
const sampledHeader = getter(carrier, X_B3_SAMPLED);
75-
const traceId = Array.isArray(traceIdHeader)
75+
76+
const traceIdHeaderValue = Array.isArray(traceIdHeader)
7677
? traceIdHeader[0]
7778
: traceIdHeader;
7879
const spanId = Array.isArray(spanIdHeader) ? spanIdHeader[0] : spanIdHeader;
80+
7981
const options = Array.isArray(sampledHeader)
8082
? sampledHeader[0]
8183
: sampledHeader;
8284

83-
if (typeof traceId !== 'string' || typeof spanId !== 'string')
85+
if (typeof traceIdHeaderValue !== 'string' || typeof spanId !== 'string') {
8486
return context;
87+
}
88+
89+
const traceId = traceIdHeaderValue.padStart(32, '0');
8590

8691
if (isValidTraceId(traceId) && isValidSpanId(spanId)) {
8792
return setExtractedSpanContext(context, {

packages/opentelemetry-core/src/trace/TraceState.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as types from '@opentelemetry/api';
17+
import * as api from '@opentelemetry/api';
1818
import { validateKey, validateValue } from '../internal/validators';
1919

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

3737
constructor(rawTraceState?: string) {

packages/opentelemetry-core/test/common/time.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import * as assert from 'assert';
1818
import { otperformance as performance } from '../../src/platform';
1919
import * as sinon from 'sinon';
20-
import * as types from '@opentelemetry/api';
20+
import * as api from '@opentelemetry/api';
2121
import {
2222
hrTime,
2323
timeInputToHrTime,
@@ -141,16 +141,16 @@ describe('time', () => {
141141

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

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

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

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

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

164164
const output = hrTimeToTimeStamp(time);
165165
assert.deepStrictEqual(output, '2019-11-11T22:58:41.000123456Z');

packages/opentelemetry-core/test/context/B3Propagator.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,21 @@ describe('B3Propagator', () => {
289289
assert.ok(ctx2 === Context.ROOT_CONTEXT);
290290
assert.ok(ctx3 === Context.ROOT_CONTEXT);
291291
});
292+
293+
it('should left-pad 64 bit trace ids with 0', () => {
294+
carrier[X_B3_TRACE_ID] = '8448eb211c80319c';
295+
carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331';
296+
carrier[X_B3_SAMPLED] = '1';
297+
const extractedSpanContext = getExtractedSpanContext(
298+
b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
299+
);
300+
301+
assert.deepStrictEqual(extractedSpanContext, {
302+
spanId: 'b7ad6b7169203331',
303+
traceId: '00000000000000008448eb211c80319c',
304+
isRemote: true,
305+
traceFlags: TraceFlags.SAMPLED,
306+
});
307+
});
292308
});
293309
});

packages/opentelemetry-exporter-jaeger/src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as types from '@opentelemetry/api';
17+
import * as api from '@opentelemetry/api';
1818

1919
/**
2020
* Options for Jaeger configuration
2121
*/
2222
export interface ExporterConfig {
23-
logger?: types.Logger;
23+
logger?: api.Logger;
2424
serviceName: string;
2525
tags?: Tag[];
2626
host?: string; // default: 'localhost'

packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import * as assert from 'assert';
1818
import { JaegerExporter } from '../src';
1919
import { ExportResult, NoopLogger } from '@opentelemetry/core';
20-
import * as types from '@opentelemetry/api';
20+
import * as api from '@opentelemetry/api';
2121
import { ThriftProcess } from '../src/types';
2222
import { ReadableSpan } from '@opentelemetry/tracing';
2323
import { TraceFlags } from '@opentelemetry/api';
@@ -130,13 +130,13 @@ describe('JaegerExporter', () => {
130130
};
131131
const readableSpan: ReadableSpan = {
132132
name: 'my-span1',
133-
kind: types.SpanKind.CLIENT,
133+
kind: api.SpanKind.CLIENT,
134134
spanContext,
135135
startTime: [1566156729, 709],
136136
endTime: [1566156731, 709],
137137
ended: true,
138138
status: {
139-
code: types.CanonicalCode.DATA_LOSS,
139+
code: api.CanonicalCode.DATA_LOSS,
140140
},
141141
attributes: {},
142142
links: [],

packages/opentelemetry-exporter-jaeger/test/transform.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as assert from 'assert';
1818
import { spanToThrift } from '../src/transform';
1919
import { ReadableSpan } from '@opentelemetry/tracing';
2020
import { Resource } from '@opentelemetry/resources';
21-
import * as types from '@opentelemetry/api';
21+
import * as api from '@opentelemetry/api';
2222
import { ThriftUtils, Utils, ThriftReferenceType } from '../src/types';
2323
import { hrTimeToMicroseconds } from '@opentelemetry/core';
2424
import { TraceFlags } from '@opentelemetry/api';
@@ -34,13 +34,13 @@ describe('transform', () => {
3434
it('should convert an OpenTelemetry span to a Thrift', () => {
3535
const readableSpan: ReadableSpan = {
3636
name: 'my-span',
37-
kind: types.SpanKind.INTERNAL,
37+
kind: api.SpanKind.INTERNAL,
3838
spanContext,
3939
startTime: [1566156729, 709],
4040
endTime: [1566156731, 709],
4141
ended: true,
4242
status: {
43-
code: types.CanonicalCode.OK,
43+
code: api.CanonicalCode.OK,
4444
},
4545
attributes: {
4646
testBool: true,
@@ -155,13 +155,13 @@ describe('transform', () => {
155155
it('should convert an OpenTelemetry span to a Thrift when links, events and attributes are empty', () => {
156156
const readableSpan: ReadableSpan = {
157157
name: 'my-span1',
158-
kind: types.SpanKind.CLIENT,
158+
kind: api.SpanKind.CLIENT,
159159
spanContext,
160160
startTime: [1566156729, 709],
161161
endTime: [1566156731, 709],
162162
ended: true,
163163
status: {
164-
code: types.CanonicalCode.DATA_LOSS,
164+
code: api.CanonicalCode.DATA_LOSS,
165165
message: 'data loss',
166166
},
167167
attributes: {},
@@ -213,13 +213,13 @@ describe('transform', () => {
213213
it('should convert an OpenTelemetry span to a Thrift with ThriftReference', () => {
214214
const readableSpan: ReadableSpan = {
215215
name: 'my-span',
216-
kind: types.SpanKind.INTERNAL,
216+
kind: api.SpanKind.INTERNAL,
217217
spanContext,
218218
startTime: [1566156729, 709],
219219
endTime: [1566156731, 709],
220220
ended: true,
221221
status: {
222-
code: types.CanonicalCode.OK,
222+
code: api.CanonicalCode.OK,
223223
},
224224
attributes: {},
225225
parentSpanId: '3e0c63257de34c92',
@@ -255,7 +255,7 @@ describe('transform', () => {
255255
it('should left pad trace ids', () => {
256256
const readableSpan: ReadableSpan = {
257257
name: 'my-span1',
258-
kind: types.SpanKind.CLIENT,
258+
kind: api.SpanKind.CLIENT,
259259
spanContext: {
260260
traceId: '92b449d5929fda1b',
261261
spanId: '6e0c63257de34c92',
@@ -265,7 +265,7 @@ describe('transform', () => {
265265
endTime: [1566156731, 709],
266266
ended: true,
267267
status: {
268-
code: types.CanonicalCode.DATA_LOSS,
268+
code: api.CanonicalCode.DATA_LOSS,
269269
message: 'data loss',
270270
},
271271
attributes: {},

packages/opentelemetry-exporter-prometheus/src/export/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as types from '@opentelemetry/api';
17+
import * as api from '@opentelemetry/api';
1818

1919
/**
2020
* Configuration interface for prometheus exporter
@@ -49,5 +49,5 @@ export interface ExporterConfig {
4949
startServer?: boolean;
5050

5151
/** Standard logging interface */
52-
logger?: types.Logger;
52+
logger?: api.Logger;
5353
}

packages/opentelemetry-exporter-prometheus/src/prometheus.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
ObserverAggregator,
3030
Sum,
3131
} from '@opentelemetry/metrics';
32-
import * as types from '@opentelemetry/api';
32+
import * as api from '@opentelemetry/api';
3333
import { createServer, IncomingMessage, Server, ServerResponse } from 'http';
3434
import { Counter, Gauge, labelValues, Metric, Registry } from 'prom-client';
3535
import * as url from 'url';
@@ -44,7 +44,7 @@ export class PrometheusExporter implements MetricExporter {
4444
};
4545

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

162-
private _getLabelValues(keys: string[], labels: types.Labels) {
162+
private _getLabelValues(keys: string[], labels: api.Labels) {
163163
const labelValues: labelValues = {};
164164
for (let i = 0; i < keys.length; i++) {
165165
if (labels[keys[i]] !== null) {

0 commit comments

Comments
 (0)