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

Commit fa97a9b

Browse files
authored
Enforce strictNullChecks and noUnusedLocals on opencensus-propagation-tracecontext (#377)
1 parent 342a10e commit fa97a9b

File tree

3 files changed

+20
-52
lines changed

3 files changed

+20
-52
lines changed

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

+6-12
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@
1616

1717
import {HeaderGetter, HeaderSetter, Propagation, SpanContext} from '@opencensus/core';
1818
import * as crypto from 'crypto';
19-
2019
import {isValidOption, isValidSpanId, isValidTraceId, isValidVersion} from './validators';
2120

2221
// Header names
23-
const TRACE_PARENT = 'traceparent';
24-
const TRACE_STATE = 'tracestate';
25-
26-
// Option flags
27-
const REQUESTED_FLAG = 0x1;
28-
const RECORDED_FLAG = 0x2;
29-
30-
const DEFAULT_OPTIONS = 0x0;
22+
export const TRACE_PARENT = 'traceparent';
23+
export const TRACE_STATE = 'tracestate';
24+
export const DEFAULT_OPTIONS = 0x0;
3125

3226
/**
3327
* Propagates span context through Trace Context format propagation.
@@ -47,12 +41,12 @@ export class TraceContextFormat implements Propagation {
4741
* context is returned.
4842
* @param getter
4943
*/
50-
extract(getter: HeaderGetter): SpanContext {
44+
extract(getter: HeaderGetter): SpanContext|null {
5145
if (getter) {
5246
// Construct empty span context that we will fill
5347
const spanContext: SpanContext = {
54-
traceId: undefined,
55-
spanId: undefined,
48+
traceId: '',
49+
spanId: '',
5650
options: DEFAULT_OPTIONS,
5751
traceState: undefined
5852
};

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

+12-39
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@
1717
import {HeaderGetter, HeaderSetter, SpanContext} from '@opencensus/core';
1818
import * as assert from 'assert';
1919

20-
import {TraceContextFormat} from '../src/';
21-
22-
// Header names
23-
const TRACE_PARENT = 'traceparent';
24-
const TRACE_STATE = 'tracestate';
25-
26-
const DEFAULT_OPTIONS = 0x0;
20+
import {DEFAULT_OPTIONS, TRACE_PARENT, TRACE_STATE, TraceContextFormat} from '../src/';
2721

2822
const traceContextFormat = new TraceContextFormat();
2923

@@ -34,8 +28,8 @@ describe('TraceContextPropagation', () => {
3428

3529
beforeEach(() => {
3630
emptySpanContext = {
37-
traceId: undefined,
38-
spanId: undefined,
31+
traceId: '',
32+
spanId: '',
3933
options: DEFAULT_OPTIONS,
4034
traceState: undefined
4135
};
@@ -44,7 +38,7 @@ describe('TraceContextPropagation', () => {
4438
// Generates the appropriate `traceparent` header for the given SpanContext
4539
const traceParentHeaderFromSpanContext =
4640
(spanContext: SpanContext): string => {
47-
const {traceId, spanId, options} = spanContext;
41+
const {traceId, spanId} = spanContext;
4842
return `00-${traceId}-${spanId}-${
4943
Buffer.from([spanContext.options]).toString('hex')}`;
5044
};
@@ -57,8 +51,9 @@ describe('TraceContextPropagation', () => {
5751
// Construct headers from the generated span context
5852
const headers: Record<string, string> = {};
5953
headers[TRACE_PARENT] = traceParentHeaderFromSpanContext(spanContext);
60-
headers[TRACE_STATE] = spanContext.traceState;
61-
54+
if (spanContext.traceState) {
55+
headers[TRACE_STATE] = spanContext.traceState;
56+
}
6257
const getter: HeaderGetter = {
6358
getHeader(name: string) {
6459
return headers[name];
@@ -135,7 +130,6 @@ describe('TraceContextPropagation', () => {
135130
};
136131

137132
Object.getOwnPropertyNames(testCases).forEach(testCase => {
138-
const traceState = '';
139133
const headers: Headers = {
140134
[TRACE_PARENT]: testCases[testCase],
141135
[TRACE_STATE]: '',
@@ -179,8 +173,10 @@ describe('TraceContextPropagation', () => {
179173
};
180174

181175
const extractedSpanContext = traceContextFormat.extract(getter);
182-
assert.strictEqual(
183-
extractedSpanContext.options, DEFAULT_OPTIONS, testCase);
176+
if (extractedSpanContext !== null) {
177+
assert.strictEqual(
178+
extractedSpanContext.options, DEFAULT_OPTIONS, testCase);
179+
}
184180
});
185181
});
186182

@@ -205,18 +201,13 @@ describe('TraceContextPropagation', () => {
205201
it('should gracefully handle an unset header', () => {
206202
const getter: HeaderGetter = {
207203
getHeader(name: string) {
208-
return null;
204+
return undefined;
209205
}
210206
};
211207

212208
const extractedSpanContext = traceContextFormat.extract(getter);
213209
assert.deepEqual(extractedSpanContext, emptySpanContext);
214210
});
215-
216-
it('should gracefully handle null getter', () => {
217-
const spanContext = traceContextFormat.extract(null);
218-
assert.equal(spanContext, null);
219-
});
220211
});
221212

222213
describe('inject', () => {
@@ -251,24 +242,6 @@ describe('TraceContextPropagation', () => {
251242
traceContextFormat.inject(setter, spanContext);
252243
assert.strictEqual(headers[TRACE_STATE], 'foo=bar');
253244
});
254-
255-
it('should gracefully handle null setter or context', () => {
256-
// Null setter
257-
const spanContext = traceContextFormat.generate();
258-
try {
259-
traceContextFormat.inject(null, spanContext);
260-
} catch (err) {
261-
assert.fail(err);
262-
}
263-
264-
// Null context
265-
const setter: HeaderSetter = {setHeader(name: string, value: string) {}};
266-
try {
267-
traceContextFormat.inject(setter, null);
268-
} catch (err) {
269-
assert.fail(err);
270-
}
271-
});
272245
});
273246

274247
describe('generate', () => {

packages/opencensus-propagation-tracecontext/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"pretty": true,
77
"module": "commonjs",
88
"target": "es6",
9-
"strictNullChecks": false
9+
"strictNullChecks": true,
10+
"noUnusedLocals": true
1011
},
1112
"include": [
1213
"src/**/*.ts",

0 commit comments

Comments
 (0)