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

Commit 5d65f62

Browse files
committed
Add check for an array and falsey values
1. Remove unwanted check on getter. 2. Add check for an array or falsey value 3. Make sentence readable
1 parent d423f35 commit 5d65f62

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

packages/opencensus-propagation-b3/src/b3-format.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,13 @@ export class B3Format implements Propagation {
3232
* in the headers, null is returned.
3333
* @param getter
3434
*/
35-
extract(getter: HeaderGetter): SpanContext|null {
36-
if (!getter) return null;
37-
let opt = getter.getHeader(X_B3_SAMPLED);
38-
if (opt instanceof Array) {
39-
opt = opt[0];
40-
}
35+
extract(getter: HeaderGetter): SpanContext {
36+
const opt = this.parseHeader(getter.getHeader(X_B3_SAMPLED));
4137
return {
42-
traceId: getter.getHeader(X_B3_TRACE_ID),
43-
spanId: getter.getHeader(X_B3_SPAN_ID),
38+
traceId: this.parseHeader(getter.getHeader(X_B3_TRACE_ID)),
39+
spanId: this.parseHeader(getter.getHeader(X_B3_SPAN_ID)),
4440
options: isNaN(Number(opt)) ? NOT_SAMPLED_VALUE : Number(opt)
45-
} as SpanContext;
41+
};
4642
}
4743

4844
/**
@@ -54,7 +50,7 @@ export class B3Format implements Propagation {
5450
setter.setHeader(X_B3_TRACE_ID, spanContext.traceId || '');
5551
setter.setHeader(X_B3_SPAN_ID, spanContext.spanId || '');
5652
if (spanContext &&
57-
(spanContext.options || NOT_SAMPLED_VALUE & SAMPLED_VALUE) !== 0) {
53+
((spanContext.options || NOT_SAMPLED_VALUE) & SAMPLED_VALUE) !== 0) {
5854
setter.setHeader(X_B3_SAMPLED, `${SAMPLED_VALUE}`);
5955
} else {
6056
setter.setHeader(X_B3_SAMPLED, `${NOT_SAMPLED_VALUE}`);
@@ -71,4 +67,12 @@ export class B3Format implements Propagation {
7167
options: SAMPLED_VALUE
7268
};
7369
}
70+
71+
/** Converts a headers type to a string. */
72+
private parseHeader(str: string|string[]|undefined): string {
73+
if (Array.isArray(str)) {
74+
return str[0];
75+
}
76+
return str || '';
77+
}
7478
}

packages/opencensus-propagation-b3/test/test-b3-format.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
import {HeaderGetter, HeaderSetter} from '@opencensus/core';
1818
import * as assert from 'assert';
19+
import * as uuid from 'uuid';
1920

20-
import {B3Format, X_B3_SAMPLED, X_B3_SPAN_ID, X_B3_TRACE_ID} from '../src/';
21+
import {B3Format, NOT_SAMPLED_VALUE, SAMPLED_VALUE, X_B3_SAMPLED, X_B3_SPAN_ID, X_B3_TRACE_ID} from '../src/';
2122

2223
const b3Format = new B3Format();
2324

@@ -41,6 +42,43 @@ describe('B3Propagation', () => {
4142

4243
assert.deepEqual(b3Format.extract(getter), spanContext);
4344
});
45+
46+
it('should return valid context when options and spanId are undefined',
47+
() => {
48+
const traceId = uuid.v4().split('-').join('');
49+
// tslint:disable-next-line
50+
const headers = {} as any;
51+
headers[X_B3_TRACE_ID] = traceId;
52+
headers[X_B3_SPAN_ID] = undefined;
53+
54+
const getter: HeaderGetter = {
55+
getHeader(name: string) {
56+
return headers[name];
57+
}
58+
};
59+
60+
assert.deepEqual(
61+
b3Format.extract(getter),
62+
{traceId, spanId: '', options: NOT_SAMPLED_VALUE});
63+
});
64+
65+
it('should extract data from an array', () => {
66+
const spanContext = b3Format.generate();
67+
// tslint:disable-next-line
68+
const headers = {} as any;
69+
headers[X_B3_TRACE_ID] =
70+
[spanContext.traceId, uuid.v4().split('-').join('')];
71+
headers[X_B3_SPAN_ID] = [spanContext.spanId];
72+
headers[X_B3_SAMPLED] = [SAMPLED_VALUE];
73+
74+
const getter: HeaderGetter = {
75+
getHeader(name: string) {
76+
return headers[name];
77+
}
78+
};
79+
80+
assert.deepEqual(b3Format.extract(getter), spanContext);
81+
});
4482
});
4583

4684
describe('inject', () => {

0 commit comments

Comments
 (0)