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

Commit 1c0d0b5

Browse files
authored
Enforce/strictNullChecks: Stackdriver (#408)
1 parent a499b36 commit 1c0d0b5

9 files changed

Lines changed: 36 additions & 35 deletions

packages/opencensus-exporter-stackdriver/src/stackdriver-cloudtrace-utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export function createAttributes(
4545
droppedAttributesCount: number): types.Attributes {
4646
const attributesBuilder =
4747
createAttributesBuilder(attributes, droppedAttributesCount);
48-
attributesBuilder.attributeMap[AGENT_LABEL_KEY] = AGENT_LABEL_VALUE;
48+
if (attributesBuilder.attributeMap) {
49+
attributesBuilder.attributeMap[AGENT_LABEL_KEY] = AGENT_LABEL_VALUE;
50+
}
4951
attributesBuilder.attributeMap =
5052
Object.assign({}, attributesBuilder.attributeMap, resourceLabels);
5153
return attributesBuilder;

packages/opencensus-exporter-stackdriver/src/stackdriver-cloudtrace.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ export class StackdriverTraceExporter implements Exporter {
108108
status: {code: span.status.code},
109109
sameProcessAsParentSpan: !span.remoteParent,
110110
childSpanCount: numberOfChildren,
111-
stackTrace: null, // Unsupported by nodejs
111+
stackTrace: undefined, // Unsupported by nodejs
112112
};
113113
if (span.parentSpanId) {
114114
spanBuilder.parentSpanId = span.parentSpanId;
115115
}
116-
if (span.status.message) {
116+
if (span.status.message && spanBuilder.status) {
117117
spanBuilder.status.message = span.status.message;
118118
}
119119

@@ -131,7 +131,7 @@ export class StackdriverTraceExporter implements Exporter {
131131
// data to backend :
132132
// https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.
133133
// cloudtrace.v2#google.devtools.cloudtrace.v2.TraceService
134-
cloudTrace.projects.traces.batchWrite(spans, (err: Error) => {
134+
cloudTrace.projects.traces.batchWrite(spans, (err: Error|null) => {
135135
if (err) {
136136
err.message = `batchWriteSpans error: ${err.message}`;
137137
this.logger.error(err.message);

packages/opencensus-exporter-stackdriver/src/stackdriver-stats-utils.ts renamed to packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring-utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function createTimeSeriesList(
6060
metricKind,
6161
valueType,
6262
points: timeSeries.points.map(point => {
63-
return createPoint(point, timeSeries.startTimestamp, valueType);
63+
return createPoint(point, valueType, timeSeries.startTimestamp);
6464
})
6565
});
6666
}
@@ -153,8 +153,8 @@ function createMetric(
153153
* Converts timeseries's point, so that metric can be uploaded to StackDriver.
154154
*/
155155
function createPoint(
156-
point: TimeSeriesPoint, startTimeStamp: Timestamp,
157-
valueType: ValueType): Point {
156+
point: TimeSeriesPoint, valueType: ValueType,
157+
startTimeStamp?: Timestamp): Point {
158158
const value = createValue(valueType, point);
159159
const endTime = toISOString(point.timestamp);
160160
if (startTimeStamp) {
@@ -224,8 +224,8 @@ function generateDefaultTaskValue(): string {
224224
}
225225

226226
function toISOString(timestamp: Timestamp) {
227-
const str = new Date(timestamp.seconds * 1000).toISOString();
228-
const nsStr = `${leftZeroPad(timestamp.nanos)}`.replace(/0+$/, '');
227+
const str = new Date(timestamp.seconds! * 1000).toISOString();
228+
const nsStr = `${leftZeroPad(timestamp.nanos!)}`.replace(/0+$/, '');
229229
return str.replace('000Z', `${nsStr}Z`);
230230
}
231231

packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {logger, Logger, Measurement, Metric, MetricDescriptor as OCMetricDescrip
1818
import {auth, JWT} from 'google-auth-library';
1919
import {google} from 'googleapis';
2020
import {getDefaultResource} from './common-utils';
21-
import {createMetricDescriptorData, createTimeSeriesList} from './stackdriver-stats-utils';
21+
import {createMetricDescriptorData, createTimeSeriesList} from './stackdriver-monitoring-utils';
2222
import {MonitoredResource, StackdriverExporterOptions, TimeSeries} from './types';
2323

2424
const OC_USER_AGENT = {
@@ -39,8 +39,8 @@ export class StackdriverStatsExporter implements StatsEventListener {
3939
private projectId: string;
4040
private metricPrefix: string;
4141
private displayNamePrefix: string;
42-
private onMetricUploadError: (err: Error) => void;
43-
private timer: NodeJS.Timer;
42+
private onMetricUploadError?: (err: Error) => void;
43+
private timer!: NodeJS.Timer;
4444
static readonly DEFAULT_DISPLAY_NAME_PREFIX: string = 'OpenCensus';
4545
static readonly CUSTOM_OPENCENSUS_DOMAIN: string =
4646
'custom.googleapis.com/opencensus';
@@ -60,7 +60,9 @@ export class StackdriverStatsExporter implements StatsEventListener {
6060
this.displayNamePrefix =
6161
options.prefix || StackdriverStatsExporter.DEFAULT_DISPLAY_NAME_PREFIX;
6262
this.logger = options.logger || logger.logger();
63-
this.onMetricUploadError = options.onMetricUploadError;
63+
if (options.onMetricUploadError) {
64+
this.onMetricUploadError = options.onMetricUploadError;
65+
}
6466
this.DEFAULT_RESOURCE = getDefaultResource(this.projectId);
6567
}
6668

@@ -162,7 +164,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
162164
return new Promise((resolve, reject) => {
163165
monitoring.projects.timeSeries.create(
164166
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]},
165-
(err?: Error) => {
167+
(err: Error|null) => {
166168
this.logger.debug(
167169
'sent time series', request.resource.timeSeries);
168170
err ? reject(err) : resolve();
@@ -187,7 +189,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
187189
return new Promise((resolve, reject) => {
188190
monitoring.projects.metricDescriptors.create(
189191
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]},
190-
(err?: Error) => {
192+
(err: Error|null) => {
191193
this.logger.debug('sent metric descriptor', request.resource);
192194
err ? reject(err) : resolve();
193195
});

packages/opencensus-exporter-stackdriver/test/test-stackdriver-cloudtrace-utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
7676
const stackdriverLinks = createLinks(links, 2);
7777

7878
assert.equal(stackdriverLinks.droppedLinksCount, 2);
79-
assert.equal(stackdriverLinks.link.length, 3);
79+
assert.equal(stackdriverLinks.link!.length, 3);
8080
assert.deepEqual(stackdriverLinks.link, expectedLink);
8181
});
8282
});
@@ -173,7 +173,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
173173

174174
assert.equal(stackdriverTimeEvents.droppedAnnotationsCount, 2);
175175
assert.equal(stackdriverTimeEvents.droppedMessageEventsCount, 3);
176-
assert.equal(stackdriverTimeEvents.timeEvent.length, 6);
176+
assert.equal(stackdriverTimeEvents.timeEvent!.length, 6);
177177
assert.deepEqual(stackdriverTimeEvents.timeEvent, expectedTimeEvent);
178178
});
179179

@@ -183,7 +183,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
183183

184184
assert.equal(stackdriverTimeEvents.droppedAnnotationsCount, 0);
185185
assert.equal(stackdriverTimeEvents.droppedMessageEventsCount, 0);
186-
assert.equal(stackdriverTimeEvents.timeEvent.length, 0);
186+
assert.equal(stackdriverTimeEvents.timeEvent!.length, 0);
187187
});
188188
});
189189

@@ -199,7 +199,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
199199
it('should return stackdriver Attributes', () => {
200200
const stackdriverAttribute = createAttributes(attributes, {}, 0);
201201
assert.equal(stackdriverAttribute.droppedAttributesCount, 0);
202-
assert.equal(Object.keys(stackdriverAttribute.attributeMap).length, 3);
202+
assert.equal(Object.keys(stackdriverAttribute.attributeMap!).length, 3);
203203
assert.deepEqual(stackdriverAttribute.attributeMap, expectedAttributeMap);
204204
});
205205

@@ -215,7 +215,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
215215
'g.co/r/project_id': {'stringValue': {'value': 'project1'}}
216216
});
217217
assert.equal(stackdriverAttribute.droppedAttributesCount, 2);
218-
assert.equal(Object.keys(stackdriverAttribute.attributeMap).length, 5);
218+
assert.equal(Object.keys(stackdriverAttribute.attributeMap!).length, 5);
219219
assert.deepEqual(stackdriverAttribute.attributeMap, expectedAttributeMap);
220220
});
221221
});

packages/opencensus-exporter-stackdriver/test/test-stackdriver-cloudtrace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ describe('Stackdriver Trace Exporter', function() {
188188
assert.strictEqual(spans[0].spanId, rootSpan.id);
189189
assert.strictEqual(spans[1].spanId, span.id);
190190
return true;
191-
}, null, false);
191+
});
192192
span.end();
193193
rootSpan.end();
194194

packages/opencensus-exporter-stackdriver/test/test-stackdriver-stats-utils.ts renamed to packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
import {CoreResource, DistributionValue, LabelKey, LabelValue, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core';
1818
import * as assert from 'assert';
19-
2019
import {getDefaultResource} from '../src/common-utils';
2120
import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
22-
import {createMetricDescriptorData, createTimeSeriesList, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-stats-utils';
21+
import {createMetricDescriptorData, createTimeSeriesList, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-monitoring-utils';
2322
import {Distribution, MetricDescriptor, MetricKind, ValueType} from '../src/types';
24-
2523
import * as nocks from './nocks';
2624

2725
const METRIC_NAME = 'metric-name';
@@ -260,7 +258,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
260258
};
261259

262260
it('should return a Stackdriver Point', () => {
263-
const pt = TEST_ONLY.createPoint(doublePoint, null, ValueType.DOUBLE);
261+
const pt = TEST_ONLY.createPoint(doublePoint, ValueType.DOUBLE);
264262

265263
assert.deepStrictEqual(pt, {
266264
value: {doubleValue: 12345678.2},
@@ -270,7 +268,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
270268

271269
it('should return a Stackdriver Cumulative Point', () => {
272270
const pt =
273-
TEST_ONLY.createPoint(intPoint, startTimestamp, ValueType.INT64);
271+
TEST_ONLY.createPoint(intPoint, ValueType.INT64, startTimestamp);
274272

275273
assert.deepStrictEqual(pt, {
276274
value: {int64Value: 12345678},
@@ -283,7 +281,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
283281

284282
it('should return a Stackdriver Distribution Point', () => {
285283
const pt = TEST_ONLY.createPoint(
286-
distributionPoint, startTimestamp, ValueType.DISTRIBUTION);
284+
distributionPoint, ValueType.DISTRIBUTION, startTimestamp);
287285

288286
assert.deepStrictEqual(pt, {
289287
value: {

packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616

1717
import {globalStats, LabelKey, Logger, MeasureUnit, Metrics} from '@opencensus/core';
1818
import * as assert from 'assert';
19-
import * as nock from 'nock';
2019
import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
2120
import {MetricKind, StackdriverExporterOptions, ValueType} from '../src/types';
22-
2321
import * as nocks from './nocks';
2422

2523
const PROJECT_ID = 'fake-project-id';
2624

2725
class MockLogger implements Logger {
28-
level: string;
26+
level?: string;
2927
// tslint:disable-next-line:no-any
3028
debugBuffer: any[] = [];
3129

@@ -93,8 +91,8 @@ describe('Stackdriver Stats Exporter', () => {
9391
METRIC_NAME, METRIC_DESCRIPTION, UNIT, LABEL_KEYS);
9492
gauge.getDefaultTimeSeries().add(100);
9593

96-
nocks.metricDescriptors(PROJECT_ID, null, null, false);
97-
nocks.timeSeries(PROJECT_ID, null, null, false);
94+
nocks.metricDescriptors(PROJECT_ID);
95+
nocks.timeSeries(PROJECT_ID);
9896

9997
await exporter.export();
10098
assert.equal(mockLogger.debugBuffer.length, 1);
@@ -125,7 +123,7 @@ describe('Stackdriver Stats Exporter', () => {
125123
timeSeries.metric.type,
126124
`${StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN}/${
127125
METRIC_NAME}`);
128-
assert.strictEqual(timeSeries.resource.type, 'global');
126+
assert.ok(timeSeries.resource.type);
129127
assert.ok(timeSeries.resource.labels.project_id);
130128
assert.strictEqual(timeSeries.resource.labels.project_id, PROJECT_ID);
131129
assert.strictEqual(timeSeries.metricKind, MetricKind.GAUGE);

packages/opencensus-exporter-stackdriver/tsconfig.json

Lines changed: 3 additions & 2 deletions
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
"exclude": [
1213
"node_modules"
@@ -16,4 +17,4 @@
1617
"test/**/*.ts"
1718
]
1819

19-
}
20+
}

0 commit comments

Comments
 (0)