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

Commit

Permalink
Enforce/strictNullChecks: Stackdriver (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Mar 13, 2019
1 parent a499b36 commit 1c0d0b5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function createAttributes(
droppedAttributesCount: number): types.Attributes {
const attributesBuilder =
createAttributesBuilder(attributes, droppedAttributesCount);
attributesBuilder.attributeMap[AGENT_LABEL_KEY] = AGENT_LABEL_VALUE;
if (attributesBuilder.attributeMap) {
attributesBuilder.attributeMap[AGENT_LABEL_KEY] = AGENT_LABEL_VALUE;
}
attributesBuilder.attributeMap =
Object.assign({}, attributesBuilder.attributeMap, resourceLabels);
return attributesBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ export class StackdriverTraceExporter implements Exporter {
status: {code: span.status.code},
sameProcessAsParentSpan: !span.remoteParent,
childSpanCount: numberOfChildren,
stackTrace: null, // Unsupported by nodejs
stackTrace: undefined, // Unsupported by nodejs
};
if (span.parentSpanId) {
spanBuilder.parentSpanId = span.parentSpanId;
}
if (span.status.message) {
if (span.status.message && spanBuilder.status) {
spanBuilder.status.message = span.status.message;
}

Expand All @@ -131,7 +131,7 @@ export class StackdriverTraceExporter implements Exporter {
// data to backend :
// https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.
// cloudtrace.v2#google.devtools.cloudtrace.v2.TraceService
cloudTrace.projects.traces.batchWrite(spans, (err: Error) => {
cloudTrace.projects.traces.batchWrite(spans, (err: Error|null) => {
if (err) {
err.message = `batchWriteSpans error: ${err.message}`;
this.logger.error(err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function createTimeSeriesList(
metricKind,
valueType,
points: timeSeries.points.map(point => {
return createPoint(point, timeSeries.startTimestamp, valueType);
return createPoint(point, valueType, timeSeries.startTimestamp);
})
});
}
Expand Down Expand Up @@ -153,8 +153,8 @@ function createMetric(
* Converts timeseries's point, so that metric can be uploaded to StackDriver.
*/
function createPoint(
point: TimeSeriesPoint, startTimeStamp: Timestamp,
valueType: ValueType): Point {
point: TimeSeriesPoint, valueType: ValueType,
startTimeStamp?: Timestamp): Point {
const value = createValue(valueType, point);
const endTime = toISOString(point.timestamp);
if (startTimeStamp) {
Expand Down Expand Up @@ -224,8 +224,8 @@ function generateDefaultTaskValue(): string {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {logger, Logger, Measurement, Metric, MetricDescriptor as OCMetricDescrip
import {auth, JWT} from 'google-auth-library';
import {google} from 'googleapis';
import {getDefaultResource} from './common-utils';
import {createMetricDescriptorData, createTimeSeriesList} from './stackdriver-stats-utils';
import {createMetricDescriptorData, createTimeSeriesList} from './stackdriver-monitoring-utils';
import {MonitoredResource, StackdriverExporterOptions, TimeSeries} from './types';

const OC_USER_AGENT = {
Expand All @@ -39,8 +39,8 @@ export class StackdriverStatsExporter implements StatsEventListener {
private projectId: string;
private metricPrefix: string;
private displayNamePrefix: string;
private onMetricUploadError: (err: Error) => void;
private timer: NodeJS.Timer;
private onMetricUploadError?: (err: Error) => void;
private timer!: NodeJS.Timer;
static readonly DEFAULT_DISPLAY_NAME_PREFIX: string = 'OpenCensus';
static readonly CUSTOM_OPENCENSUS_DOMAIN: string =
'custom.googleapis.com/opencensus';
Expand All @@ -60,7 +60,9 @@ export class StackdriverStatsExporter implements StatsEventListener {
this.displayNamePrefix =
options.prefix || StackdriverStatsExporter.DEFAULT_DISPLAY_NAME_PREFIX;
this.logger = options.logger || logger.logger();
this.onMetricUploadError = options.onMetricUploadError;
if (options.onMetricUploadError) {
this.onMetricUploadError = options.onMetricUploadError;
}
this.DEFAULT_RESOURCE = getDefaultResource(this.projectId);
}

Expand Down Expand Up @@ -162,7 +164,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
return new Promise((resolve, reject) => {
monitoring.projects.timeSeries.create(
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]},
(err?: Error) => {
(err: Error|null) => {
this.logger.debug(
'sent time series', request.resource.timeSeries);
err ? reject(err) : resolve();
Expand All @@ -187,7 +189,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
return new Promise((resolve, reject) => {
monitoring.projects.metricDescriptors.create(
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]},
(err?: Error) => {
(err: Error|null) => {
this.logger.debug('sent metric descriptor', request.resource);
err ? reject(err) : resolve();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
const stackdriverLinks = createLinks(links, 2);

assert.equal(stackdriverLinks.droppedLinksCount, 2);
assert.equal(stackdriverLinks.link.length, 3);
assert.equal(stackdriverLinks.link!.length, 3);
assert.deepEqual(stackdriverLinks.link, expectedLink);
});
});
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {

assert.equal(stackdriverTimeEvents.droppedAnnotationsCount, 2);
assert.equal(stackdriverTimeEvents.droppedMessageEventsCount, 3);
assert.equal(stackdriverTimeEvents.timeEvent.length, 6);
assert.equal(stackdriverTimeEvents.timeEvent!.length, 6);
assert.deepEqual(stackdriverTimeEvents.timeEvent, expectedTimeEvent);
});

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

assert.equal(stackdriverTimeEvents.droppedAnnotationsCount, 0);
assert.equal(stackdriverTimeEvents.droppedMessageEventsCount, 0);
assert.equal(stackdriverTimeEvents.timeEvent.length, 0);
assert.equal(stackdriverTimeEvents.timeEvent!.length, 0);
});
});

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

Expand All @@ -215,7 +215,7 @@ describe('Stackdriver CloudTrace Exporter Utils', () => {
'g.co/r/project_id': {'stringValue': {'value': 'project1'}}
});
assert.equal(stackdriverAttribute.droppedAttributesCount, 2);
assert.equal(Object.keys(stackdriverAttribute.attributeMap).length, 5);
assert.equal(Object.keys(stackdriverAttribute.attributeMap!).length, 5);
assert.deepEqual(stackdriverAttribute.attributeMap, expectedAttributeMap);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('Stackdriver Trace Exporter', function() {
assert.strictEqual(spans[0].spanId, rootSpan.id);
assert.strictEqual(spans[1].spanId, span.id);
return true;
}, null, false);
});
span.end();
rootSpan.end();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

import {CoreResource, DistributionValue, LabelKey, LabelValue, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core';
import * as assert from 'assert';

import {getDefaultResource} from '../src/common-utils';
import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
import {createMetricDescriptorData, createTimeSeriesList, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-stats-utils';
import {createMetricDescriptorData, createTimeSeriesList, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-monitoring-utils';
import {Distribution, MetricDescriptor, MetricKind, ValueType} from '../src/types';

import * as nocks from './nocks';

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

it('should return a Stackdriver Point', () => {
const pt = TEST_ONLY.createPoint(doublePoint, null, ValueType.DOUBLE);
const pt = TEST_ONLY.createPoint(doublePoint, ValueType.DOUBLE);

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

it('should return a Stackdriver Cumulative Point', () => {
const pt =
TEST_ONLY.createPoint(intPoint, startTimestamp, ValueType.INT64);
TEST_ONLY.createPoint(intPoint, ValueType.INT64, startTimestamp);

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

it('should return a Stackdriver Distribution Point', () => {
const pt = TEST_ONLY.createPoint(
distributionPoint, startTimestamp, ValueType.DISTRIBUTION);
distributionPoint, ValueType.DISTRIBUTION, startTimestamp);

assert.deepStrictEqual(pt, {
value: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

import {globalStats, LabelKey, Logger, MeasureUnit, Metrics} from '@opencensus/core';
import * as assert from 'assert';
import * as nock from 'nock';
import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
import {MetricKind, StackdriverExporterOptions, ValueType} from '../src/types';

import * as nocks from './nocks';

const PROJECT_ID = 'fake-project-id';

class MockLogger implements Logger {
level: string;
level?: string;
// tslint:disable-next-line:no-any
debugBuffer: any[] = [];

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

nocks.metricDescriptors(PROJECT_ID, null, null, false);
nocks.timeSeries(PROJECT_ID, null, null, false);
nocks.metricDescriptors(PROJECT_ID);
nocks.timeSeries(PROJECT_ID);

await exporter.export();
assert.equal(mockLogger.debugBuffer.length, 1);
Expand Down Expand Up @@ -125,7 +123,7 @@ describe('Stackdriver Stats Exporter', () => {
timeSeries.metric.type,
`${StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN}/${
METRIC_NAME}`);
assert.strictEqual(timeSeries.resource.type, 'global');
assert.ok(timeSeries.resource.type);
assert.ok(timeSeries.resource.labels.project_id);
assert.strictEqual(timeSeries.resource.labels.project_id, PROJECT_ID);
assert.strictEqual(timeSeries.metricKind, MetricKind.GAUGE);
Expand Down
5 changes: 3 additions & 2 deletions packages/opencensus-exporter-stackdriver/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
},
"exclude": [
"node_modules"
Expand All @@ -16,4 +17,4 @@
"test/**/*.ts"
]

}
}

0 comments on commit 1c0d0b5

Please sign in to comment.