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

Commit 469d15a

Browse files
authored
[Core] Enforce strictNullChecks (#453)
* Enforce null check on [core] package * fix review comments 1. Assign default value to parentSpanId and name. 2. Assign default value({}) to activeTraceParams. 3. Add traceState back for no-record-span-base * Add NoopLogger class * Use map and filter instread of loop + added TODO * Remove and fix comment * Change TraceState = string|undefined -> TraceState = string * Fix build
1 parent 9f4a627 commit 469d15a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+406
-650
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ All notable changes to this project will be documented in this file.
2323
- Add support for recording Exemplars.
2424
- Add `TagMetadata` that defines the properties associated with a `Tag`.
2525
- Add HTTP text format serializer to Tag propagation component.
26+
- Enforce `--strictNullChecks` and `--noUnusedLocals` Compiler Options on [opencensus-core] package.
2627

2728
## 0.0.9 - 2019-02-12
2829
- Add Metrics API.

packages/opencensus-core/src/common/console-logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const logDriver = require('log-driver');
2525
export class ConsoleLogger implements types.Logger {
2626
private logger: typeof logDriver;
2727
static LEVELS = ['silent', 'error', 'warn', 'info', 'debug'];
28-
level: string;
28+
level?: string;
2929

3030
/**
3131
* Constructs a new ConsoleLogger instance
@@ -45,7 +45,7 @@ export class ConsoleLogger implements types.Logger {
4545
} else {
4646
opt = options || {};
4747
}
48-
this.level = opt.level;
48+
if (opt.level) this.level = opt.level;
4949
this.logger =
5050
logDriver({levels: ConsoleLogger.LEVELS, level: opt.level || 'silent'});
5151
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {Logger} from './types';
18+
19+
/** No-op implementation of Logger */
20+
class NoopLogger implements Logger {
21+
// tslint:disable-next-line:no-any
22+
debug(message: string, ...args: any[]) {}
23+
// tslint:disable-next-line:no-any
24+
error(...args: any[]) {}
25+
// tslint:disable-next-line:no-any
26+
warn(...args: any[]) {}
27+
// tslint:disable-next-line:no-any
28+
info(...args: any[]) {}
29+
}
30+
31+
export const noopLogger = new NoopLogger();

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as types from './types';
2424

2525
/** Do not send span data */
2626
export class NoopExporter implements types.Exporter {
27-
logger: loggerTypes.Logger;
27+
logger?: loggerTypes.Logger;
2828
onStartSpan(root: modelTypes.RootSpan) {}
2929
onEndSpan(root: modelTypes.RootSpan) {}
3030
publish(rootSpans: modelTypes.RootSpan[]) {
@@ -36,7 +36,7 @@ export class NoopExporter implements types.Exporter {
3636
export class ConsoleExporter implements types.Exporter {
3737
/** Buffer object to store the spans. */
3838
// @ts-ignore
39-
private logger: loggerTypes.Logger;
39+
private logger?: loggerTypes.Logger;
4040
private buffer: ExporterBuffer;
4141

4242
/**

packages/opencensus-core/src/exporters/exporter-buffer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import * as configTypes from '../trace/config/types';
2020
import * as modelTypes from '../trace/model/types';
2121
import * as types from './types';
2222

23+
const DEFAULT_BUFFER_SIZE = 100;
24+
const DEFAULT_BUFFER_TIMEOUT = 20000;
2325

2426
/** Controls the sending of traces to exporters. */
2527
export class ExporterBuffer {
@@ -46,8 +48,12 @@ export class ExporterBuffer {
4648
constructor(exporter: types.Exporter, config: configTypes.BufferConfig) {
4749
this.exporter = exporter;
4850
this.logger = config.logger || logger.logger();
49-
this.bufferSize = config.bufferSize;
50-
this.bufferTimeout = config.bufferTimeout;
51+
this.bufferSize = isNaN(Number(config.bufferSize)) ?
52+
DEFAULT_BUFFER_SIZE :
53+
Number(config.bufferSize);
54+
this.bufferTimeout = isNaN(Number(config.bufferTimeout)) ?
55+
DEFAULT_BUFFER_TIMEOUT :
56+
Number(config.bufferTimeout);
5157
return this;
5258
}
5359

packages/opencensus-core/src/internal/clock.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Clock {
2626
/** The time in high resolution in a [seconds, nanoseconds]. */
2727
private hrtimeLocal: [number, number];
2828
/** The duration between start and end of the clock. */
29-
private diff: [number, number] = null;
29+
private diff: [number, number] = [0, 0];
3030

3131
/** Constructs a new SamplerImpl instance. */
3232
constructor() {
@@ -46,7 +46,7 @@ export class Clock {
4646
/** Gets the duration of the clock. */
4747
get duration(): number {
4848
if (!this.endedLocal) {
49-
return null;
49+
return 0;
5050
}
5151
const ns = this.diff[0] * 1e9 + this.diff[1];
5252
return ns / 1e6;
@@ -63,11 +63,10 @@ export class Clock {
6363
* @returns A Date object with the current duration.
6464
*/
6565
get endTime(): Date {
66-
let result: Date = null;
6766
if (this.ended) {
68-
result = new Date(this.startTime.getTime() + this.duration);
67+
return new Date(this.startTime.getTime() + this.duration);
6968
}
70-
return result;
69+
return new Date();
7170
}
7271

7372
/** Indicates if the clock was ended. */

packages/opencensus-core/src/metrics/gauges/derived-gauge.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class DerivedGauge implements types.Meter {
7272
private metricDescriptor: MetricDescriptor;
7373
private labelKeysLength: number;
7474
private registeredPoints: Map<string, GaugeEntry> = new Map();
75-
private extractor: ValueExtractor;
75+
private extractor?: ValueExtractor;
7676

7777
private static readonly LABEL_VALUE = 'labelValue';
7878
private static readonly LABEL_VALUES = 'labelValues';
@@ -201,7 +201,7 @@ export class DerivedGauge implements types.Meter {
201201
*
202202
* @returns {Metric} The Metric.
203203
*/
204-
getMetric(): Metric {
204+
getMetric(): Metric|null {
205205
if (this.registeredPoints.size === 0) {
206206
return null;
207207
}

packages/opencensus-core/src/metrics/gauges/gauge.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class Gauge implements types.Meter {
110110
const hash = hashLabelValues(labelValues);
111111
// return if the specified labelValues is already associated with the point.
112112
if (this.registeredPoints.has(hash)) {
113-
return this.registeredPoints.get(hash);
113+
return this.registeredPoints.get(hash)!;
114114
}
115115
if (this.labelKeysLength !== labelValues.length) {
116116
throw new Error(Gauge.ERROR_MESSAGE_INVALID_SIZE);
@@ -126,7 +126,7 @@ export class Gauge implements types.Meter {
126126
*
127127
* @returns {Metric} The Metric.
128128
*/
129-
getMetric(): Metric {
129+
getMetric(): Metric|null {
130130
if (this.registeredPoints.size === 0) {
131131
return null;
132132
}

packages/opencensus-core/src/metrics/gauges/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface Meter {
2222
*
2323
* @returns {Metric} The Metric.
2424
*/
25-
getMetric(): Metric;
25+
getMetric(): Metric|null;
2626
}
2727

2828
export interface Point {

packages/opencensus-core/src/metrics/metric-registry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ class MetricProducerForRegistry extends BaseMetricProducer {
198198
* @returns {Metric[]} The list of metrics.
199199
*/
200200
getMetrics(): Metric[] {
201-
return Array.from(
202-
this.registeredMetrics, ([_, meter]) => meter.getMetric());
201+
return Array.from(this.registeredMetrics.values())
202+
.map(meter => meter.getMetric())
203+
.filter(meter => !!meter) as Metric[];
203204
}
204205
}

0 commit comments

Comments
 (0)