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

Commit

Permalink
Enforce strictNullChecks and noUnusedLocals on opencensus-propagation…
Browse files Browse the repository at this point in the history
…-tracecontext (#377)
  • Loading branch information
mayurkale22 committed Mar 7, 2019
1 parent 342a10e commit fa97a9b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@

import {HeaderGetter, HeaderSetter, Propagation, SpanContext} from '@opencensus/core';
import * as crypto from 'crypto';

import {isValidOption, isValidSpanId, isValidTraceId, isValidVersion} from './validators';

// Header names
const TRACE_PARENT = 'traceparent';
const TRACE_STATE = 'tracestate';

// Option flags
const REQUESTED_FLAG = 0x1;
const RECORDED_FLAG = 0x2;

const DEFAULT_OPTIONS = 0x0;
export const TRACE_PARENT = 'traceparent';
export const TRACE_STATE = 'tracestate';
export const DEFAULT_OPTIONS = 0x0;

/**
* Propagates span context through Trace Context format propagation.
Expand All @@ -47,12 +41,12 @@ export class TraceContextFormat implements Propagation {
* context is returned.
* @param getter
*/
extract(getter: HeaderGetter): SpanContext {
extract(getter: HeaderGetter): SpanContext|null {
if (getter) {
// Construct empty span context that we will fill
const spanContext: SpanContext = {
traceId: undefined,
spanId: undefined,
traceId: '',
spanId: '',
options: DEFAULT_OPTIONS,
traceState: undefined
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@
import {HeaderGetter, HeaderSetter, SpanContext} from '@opencensus/core';
import * as assert from 'assert';

import {TraceContextFormat} from '../src/';

// Header names
const TRACE_PARENT = 'traceparent';
const TRACE_STATE = 'tracestate';

const DEFAULT_OPTIONS = 0x0;
import {DEFAULT_OPTIONS, TRACE_PARENT, TRACE_STATE, TraceContextFormat} from '../src/';

const traceContextFormat = new TraceContextFormat();

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

beforeEach(() => {
emptySpanContext = {
traceId: undefined,
spanId: undefined,
traceId: '',
spanId: '',
options: DEFAULT_OPTIONS,
traceState: undefined
};
Expand All @@ -44,7 +38,7 @@ describe('TraceContextPropagation', () => {
// Generates the appropriate `traceparent` header for the given SpanContext
const traceParentHeaderFromSpanContext =
(spanContext: SpanContext): string => {
const {traceId, spanId, options} = spanContext;
const {traceId, spanId} = spanContext;
return `00-${traceId}-${spanId}-${
Buffer.from([spanContext.options]).toString('hex')}`;
};
Expand All @@ -57,8 +51,9 @@ describe('TraceContextPropagation', () => {
// Construct headers from the generated span context
const headers: Record<string, string> = {};
headers[TRACE_PARENT] = traceParentHeaderFromSpanContext(spanContext);
headers[TRACE_STATE] = spanContext.traceState;

if (spanContext.traceState) {
headers[TRACE_STATE] = spanContext.traceState;
}
const getter: HeaderGetter = {
getHeader(name: string) {
return headers[name];
Expand Down Expand Up @@ -135,7 +130,6 @@ describe('TraceContextPropagation', () => {
};

Object.getOwnPropertyNames(testCases).forEach(testCase => {
const traceState = '';
const headers: Headers = {
[TRACE_PARENT]: testCases[testCase],
[TRACE_STATE]: '',
Expand Down Expand Up @@ -179,8 +173,10 @@ describe('TraceContextPropagation', () => {
};

const extractedSpanContext = traceContextFormat.extract(getter);
assert.strictEqual(
extractedSpanContext.options, DEFAULT_OPTIONS, testCase);
if (extractedSpanContext !== null) {
assert.strictEqual(
extractedSpanContext.options, DEFAULT_OPTIONS, testCase);
}
});
});

Expand All @@ -205,18 +201,13 @@ describe('TraceContextPropagation', () => {
it('should gracefully handle an unset header', () => {
const getter: HeaderGetter = {
getHeader(name: string) {
return null;
return undefined;
}
};

const extractedSpanContext = traceContextFormat.extract(getter);
assert.deepEqual(extractedSpanContext, emptySpanContext);
});

it('should gracefully handle null getter', () => {
const spanContext = traceContextFormat.extract(null);
assert.equal(spanContext, null);
});
});

describe('inject', () => {
Expand Down Expand Up @@ -251,24 +242,6 @@ describe('TraceContextPropagation', () => {
traceContextFormat.inject(setter, spanContext);
assert.strictEqual(headers[TRACE_STATE], 'foo=bar');
});

it('should gracefully handle null setter or context', () => {
// Null setter
const spanContext = traceContextFormat.generate();
try {
traceContextFormat.inject(null, spanContext);
} catch (err) {
assert.fail(err);
}

// Null context
const setter: HeaderSetter = {setHeader(name: string, value: string) {}};
try {
traceContextFormat.inject(setter, null);
} catch (err) {
assert.fail(err);
}
});
});

describe('generate', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/opencensus-propagation-tracecontext/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"pretty": true,
"module": "commonjs",
"target": "es6",
"strictNullChecks": false
"strictNullChecks": true,
"noUnusedLocals": true
},
"include": [
"src/**/*.ts",
Expand Down

0 comments on commit fa97a9b

Please sign in to comment.