Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add jaeger http trace format (#696) #701

Merged
merged 30 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d4de43e
feat: add jaeger http trace format (#696)
vladislav-kiva Jan 16, 2020
ae93d80
feat: add jaeger http trace format (#696)
vladislav-kiva Jan 16, 2020
4b2a9d3
feat: add jaeger http trace format (#696)
vladislav-kiva Jan 16, 2020
1a206cb
feat: add jaeger http trace format (#696)
vladislav-kiva Jan 16, 2020
b7a345f
feat: add jaeger http trace format (#696)
vladislav-kiva Jan 16, 2020
ee56b19
feat: add jaeger http trace format (#696)
vladislav-kiva Jan 16, 2020
0fa26ef
fix: we should set sampled\unsampled via flag
vladislav-kiva Jan 16, 2020
b0958ee
fix: we should set sampled\unsampled via flag
vladislav-kiva Jan 16, 2020
aee5f41
fix: flags should be converted to hex, not decimal
vladislav-kiva Jan 16, 2020
e946b6a
Merge branch 'master' into jaeger-format
DotSpy Jan 16, 2020
c41944c
feat: create new package for propagation jaeger
DotSpy Jan 16, 2020
6d8ebda
fix: remove unused dependencies, correct readme header, moved out jae…
DotSpy Jan 16, 2020
ba8547c
fix: added jaeger keyword
DotSpy Jan 16, 2020
126f81c
fix: remove comma
DotSpy Jan 16, 2020
49428b4
docs: replace NodeTracer with NodeTracerRegistry
DotSpy Jan 16, 2020
b94f716
fix: added missing jaeger keyword to exporter-jaeger
DotSpy Jan 16, 2020
9981ad1
Merge branch 'master' into jaeger-format
DotSpy Jan 16, 2020
6ab6a87
fix: remove test for browser
DotSpy Jan 16, 2020
21c2fed
fix: remove yarn for browser
DotSpy Jan 16, 2020
d2cd262
Merge branch 'master' into jaeger-format
DotSpy Jan 16, 2020
982e008
fix: use same naming style as other packages
DotSpy Jan 16, 2020
0d41630
feat: added index.ts and version.ts, revert test for browser
DotSpy Jan 16, 2020
89be82a
fix: tests added index-webpack.ts
vladislav-kiva Jan 17, 2020
5c3db80
test: add test with span generated by jaeger client
vladislav-kiva Jan 17, 2020
7330161
fix: apply review changes
DotSpy Jan 17, 2020
8533133
fix: move out from sub dirs
DotSpy Jan 17, 2020
8b072b3
Merge branch 'master' into jaeger-format
DotSpy Jan 17, 2020
bda9831
docs: use common language for docs
DotSpy Jan 17, 2020
e0e7b85
fix: test script fix
DotSpy Jan 17, 2020
b5c900d
Merge branch 'master' into jaeger-format
DotSpy Jan 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*!
* Copyright 2020, 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 { SpanContext, HttpTextFormat, TraceFlags } from '@opentelemetry/types';

export const UBER_TRACE_ID_HEADER = 'uber-trace-id';
DotSpy marked this conversation as resolved.
Show resolved Hide resolved

/**
dyladan marked this conversation as resolved.
Show resolved Hide resolved
* Propagates {@link SpanContext} through Trace Context format propagation.
* {trace-id}:{span-id}:{parent-span-id}:{flags}
* {trace-id}
* 64-bit or 128-bit random number in base16 format
* Can be variable length, shorter values are 0-padded on the left
* Value of 0 is invalid
* {span-id}
* 64-bit random number in base16 format
* {parent-span-id} set to 0 because this field is deprecated
* {flags}
* One byte bitmap, as two hex digits
* Inspired by jaeger-client-node project
*/
export class JaegerHttpTraceFormat implements HttpTextFormat {
inject(
spanContext: SpanContext,
format: string,
carrier: { [key: string]: unknown }
) {
const flags = TraceFlags.SAMPLED;
DotSpy marked this conversation as resolved.
Show resolved Hide resolved

carrier[
UBER_TRACE_ID_HEADER
] = `${spanContext.traceId}:${spanContext.spanId}:0:${flags}`;
}

extract(
format: string,
carrier: { [key: string]: unknown }
): SpanContext | null {
const uberTraceIdHeader = carrier[UBER_TRACE_ID_HEADER];
if (!uberTraceIdHeader) return null;
const uberTraceId = Array.isArray(uberTraceIdHeader)
? uberTraceIdHeader[0]
: uberTraceIdHeader;

return deserializeSpanContext(uberTraceId);
}
}

/**
* @param {string} serializedString - a serialized span context.
* @return {SpanContext} - returns a span context represented by the serializedString.
**/
function deserializeSpanContext(serializedString: string): SpanContext | null {
let headers = serializedString.split(':');
if (headers.length !== 4) {
return null;
}
const [traceId, spanId, , flags] = headers;

const traceFlags = flags.match(/^[0-9a-f]{2}$/i) ? parseInt(flags) & 1 : 1;

return { traceId, spanId, isRemote: true, traceFlags };
}
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './version';
export * from './context/propagation/B3Format';
export * from './context/propagation/BinaryTraceContext';
export * from './context/propagation/HttpTraceContext';
export * from './context/propagation/JaegerHttpTraceFormat';
DotSpy marked this conversation as resolved.
Show resolved Hide resolved
export * from './platform';
export * from './trace/globaltracer-utils';
export * from './trace/instrumentation/BasePlugin';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*!
* Copyright 2020, 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 * as assert from 'assert';
import {
JaegerHttpTraceFormat,
UBER_TRACE_ID_HEADER,
} from '../../src/context/propagation/JaegerHttpTraceFormat';
import { SpanContext, TraceFlags } from '@opentelemetry/types';

describe('JaegerHttpTraceFormat', () => {
const jaegerHttpTraceFormat = new JaegerHttpTraceFormat();
let carrier: { [key: string]: unknown };

beforeEach(() => {
carrier = {};
});

describe('.inject()', () => {
it('should set uber trace id header', () => {
const spanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};

jaegerHttpTraceFormat.inject(spanContext, '', carrier);
assert.deepStrictEqual(
carrier[UBER_TRACE_ID_HEADER],
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:1'
);
});
});

describe('.extract()', () => {
it('should extract context of a sampled span from carrier', () => {
carrier[UBER_TRACE_ID_HEADER] =
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:1';
const extractedSpanContext = jaegerHttpTraceFormat.extract('', carrier);

assert.deepStrictEqual(extractedSpanContext, {
spanId: '6e0c63257de34c92',
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});

it('returns null if UBER_TRACE_ID_HEADER header is missing', () => {
assert.deepStrictEqual(jaegerHttpTraceFormat.extract('', carrier), null);
});

it('returns null if UBER_TRACE_ID_HEADER header is invalid', () => {
carrier[UBER_TRACE_ID_HEADER] = 'invalid!';
assert.deepStrictEqual(
jaegerHttpTraceFormat.extract('HttpTraceContext', carrier),
null
);
});
});
});