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

Commit 0fcb0cb

Browse files
authored
Cleanup type assertion (#525)
* Cleanup type assertion * Fix build
1 parent 940ef79 commit 0fcb0cb

File tree

15 files changed

+45
-51
lines changed

15 files changed

+45
-51
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ export class DerivedGauge implements types.Meter {
151151
descriptor: this.metricDescriptor,
152152
timeseries: Array.from(
153153
this.registeredPoints,
154-
([_, gaugeEntry]) => ({
154+
([_, gaugeEntry]): TimeSeries => ({
155155
labelValues:
156156
[...gaugeEntry.labelValues, ...this.constantLabelValues],
157157
points: [{value: gaugeEntry.extractor(), timestamp}]
158-
} as TimeSeries))
158+
}))
159159
};
160160
}
161161
}

packages/opencensus-core/src/stats/view.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export class BaseView implements View {
230230
* @returns The Point.
231231
*/
232232
private toPoint(timestamp: Timestamp, data: AggregationData): Point {
233-
let value;
234233
if (data.type === AggregationType.DISTRIBUTION) {
235234
const {count, sum, sumOfSquaredDeviation, exemplars} = data;
236235
const buckets = [];
@@ -241,17 +240,18 @@ export class BaseView implements View {
241240
buckets.push(this.getMetricBucket(bucketCount, statsExemplar));
242241
}
243242
}
244-
value = {
243+
const value: DistributionValue = {
245244
count,
246245
sum,
247246
sumOfSquaredDeviation,
248247
buckets,
249248
bucketOptions: {explicit: {bounds: data.buckets}},
250-
} as DistributionValue;
249+
};
250+
return {timestamp, value};
251251
} else {
252-
value = data.value as number;
252+
const value: number = data.value;
253+
return {timestamp, value};
253254
}
254-
return {timestamp, value};
255255
}
256256

257257
/**

packages/opencensus-core/src/trace/model/span.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,14 @@ export class Span implements types.Span {
224224
* @param timestamp A time, in milliseconds. Defaults to Date.now()
225225
*/
226226
addAnnotation(
227-
description: string, attributes?: types.Attributes, timestamp = 0) {
227+
description: string, attributes: types.Attributes = {},
228+
timestamp = Date.now()) {
228229
if (this.annotations.length >=
229230
this.activeTraceParams.numberOfAnnontationEventsPerSpan!) {
230231
this.annotations.shift();
231232
this.droppedAnnotationsCount++;
232233
}
233-
this.annotations.push({
234-
'description': description,
235-
'attributes': attributes || {},
236-
'timestamp': timestamp ? timestamp : Date.now(),
237-
} as types.Annotation);
234+
this.annotations.push({description, attributes, timestamp});
238235
}
239236

240237
/**
@@ -246,18 +243,13 @@ export class Span implements types.Span {
246243
*/
247244
addLink(
248245
traceId: string, spanId: string, type: types.LinkType,
249-
attributes?: types.Attributes) {
246+
attributes: types.Attributes = {}) {
250247
if (this.links.length >= this.activeTraceParams.numberOfLinksPerSpan!) {
251248
this.links.shift();
252249
this.droppedLinksCount++;
253250
}
254251

255-
this.links.push({
256-
'traceId': traceId,
257-
'spanId': spanId,
258-
'type': type,
259-
'attributes': attributes || {}
260-
} as types.Link);
252+
this.links.push({traceId, spanId, type, attributes});
261253
}
262254

263255
/**
@@ -270,7 +262,7 @@ export class Span implements types.Span {
270262
* zero or undefined, assumed to be the same size as uncompressed.
271263
*/
272264
addMessageEvent(
273-
type: types.MessageEventType, id: number, timestamp = 0,
265+
type: types.MessageEventType, id: number, timestamp = Date.now(),
274266
uncompressedSize?: number, compressedSize?: number) {
275267
if (this.messageEvents.length >=
276268
this.activeTraceParams.numberOfMessageEventsPerSpan!) {
@@ -281,10 +273,10 @@ export class Span implements types.Span {
281273
this.messageEvents.push({
282274
type,
283275
id,
284-
timestamp: timestamp ? timestamp : Date.now(),
276+
timestamp,
285277
uncompressedSize,
286278
compressedSize,
287-
} as types.MessageEvent);
279+
});
288280
}
289281

290282
/**

packages/opencensus-core/test/test-root-span.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {RootSpan} from '../src/trace/model/root-span';
2020
import {Span} from '../src/trace/model/span';
2121
import {CoreTracer} from '../src/trace/model/tracer';
2222
import * as types from '../src/trace/model/types';
23-
import {Annotation, Attributes, Link} from '../src/trace/model/types';
23+
import {Annotation, Link} from '../src/trace/model/types';
2424

2525
const tracer = new CoreTracer();
2626

@@ -260,7 +260,7 @@ describe('RootSpan', () => {
260260
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
261261
rootSpan.start();
262262

263-
rootSpan.addAnnotation('description test', {} as Attributes, Date.now());
263+
rootSpan.addAnnotation('description test', {}, Date.now());
264264

265265
assert.ok(rootSpan.annotations.length > 0);
266266
assert.ok(instanceOfAnnotation(rootSpan.annotations[0]));

packages/opencensus-core/test/test-span.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {RootSpan} from '../src/trace/model/root-span';
1919
import {Span} from '../src/trace/model/span';
2020
import {CoreTracer} from '../src/trace/model/tracer';
2121
import * as types from '../src/trace/model/types';
22-
import {Annotation, Attributes, Link} from '../src/trace/model/types';
22+
import {Annotation, Link} from '../src/trace/model/types';
2323

2424
// TODO: we should evaluate a way to merge similar test cases between span and
2525
// rootspan
@@ -233,7 +233,7 @@ describe('Span', () => {
233233
const span = new Span(tracer, rootSpan);
234234
span.start();
235235

236-
span.addAnnotation('description test', {} as Attributes, Date.now());
236+
span.addAnnotation('description test', {}, Date.now());
237237

238238
assert.ok(span.annotations.length > 0);
239239
assert.equal(span.droppedAnnotationsCount, 0);
@@ -247,7 +247,7 @@ describe('Span', () => {
247247
const span = new Span(tracer, rootSpan);
248248
span.start();
249249
for (let i = 0; i < 40; i++) {
250-
span.addAnnotation('description test', {} as Attributes, Date.now());
250+
span.addAnnotation('description test', {}, Date.now());
251251
}
252252

253253
assert.equal(span.annotations.length, 32);

packages/opencensus-core/test/test-tracer-base.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Tracer Base', () => {
141141

142142
describe('startRootSpan() with sampler never', () => {
143143
const tracer = new CoreTracerBase();
144-
const config = {samplingRate: 0} as TracerConfig;
144+
const config: TracerConfig = {samplingRate: 0};
145145

146146
it('should start the new NoRecordRootSpan instance', () => {
147147
tracer.start(config);
@@ -161,7 +161,7 @@ describe('Tracer Base', () => {
161161

162162
describe('startRootSpan() with sampler always', () => {
163163
const tracer = new CoreTracerBase();
164-
const config = {samplingRate: 1} as TracerConfig;
164+
const config: TracerConfig = {samplingRate: 1};
165165

166166
it('should start the new RootSpan instance', () => {
167167
tracer.start(config);
@@ -191,8 +191,10 @@ describe('Tracer Base', () => {
191191
});
192192

193193
describe('startRootSpan() with context propagation', () => {
194-
const traceOptions = {name: 'rootName', kind: types.SpanKind.UNSPECIFIED} as
195-
types.TraceOptions;
194+
const traceOptions: types.TraceOptions = {
195+
name: 'rootName',
196+
kind: types.SpanKind.UNSPECIFIED
197+
};
196198

197199
it('should create new RootSpan instance, no propagation', () => {
198200
const tracer = new CoreTracerBase();
@@ -204,11 +206,11 @@ describe('Tracer Base', () => {
204206
});
205207
});
206208

207-
const spanContextPropagated = {
209+
const spanContextPropagated: types.SpanContext = {
208210
traceId: uuid.v4().split('-').join(''),
209211
spanId: randomSpanId(),
210212
options: 0x1
211-
} as types.SpanContext;
213+
};
212214

213215

214216
it('should create the new RootSpan with propagation', () => {

packages/opencensus-exporter-jaeger/test/test-jaeger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Jaeger Exporter', () => {
4949
bufferTimeout: DEFAULT_BUFFER_TIMEOUT,
5050
logger: testLogger,
5151
maxPacketSize: 1000
52-
} as JaegerTraceExporterOptions;
52+
};
5353
});
5454

5555
beforeEach(() => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Stackdriver Trace Exporter', function() {
3939
projectId: PROJECT_ID,
4040
bufferTimeout: 200,
4141
logger: testLogger
42-
} as StackdriverExporterOptions;
42+
};
4343
});
4444

4545
beforeEach(() => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
253253
sumOfSquaredDeviation: 14,
254254
bucketOptions: {explicit: {bounds: [1.2, 3.2, 5.2]}},
255255
buckets: [{count: 3}, {count: 1}, {count: 2}, {count: 4}],
256-
} as DistributionValue,
256+
},
257257
timestamp: pointTimestamp
258258
};
259259

packages/opencensus-exporter-zipkin/src/zipkin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class ZipkinTraceExporter implements Exporter {
155155
* @param span Span to be translated
156156
*/
157157
translateSpan(span: Span): TranslatedSpan {
158-
const spanTranslated = {
158+
const spanTranslated: TranslatedSpan = {
159159
traceId: span.traceId,
160160
name: span.name,
161161
id: span.id,
@@ -169,7 +169,7 @@ export class ZipkinTraceExporter implements Exporter {
169169
localEndpoint: {serviceName: this.serviceName},
170170
tags: this.createTags(span.attributes, span.status),
171171
annotations: this.createAnnotations(span.annotations, span.messageEvents)
172-
} as TranslatedSpan;
172+
};
173173

174174
if (span.parentSpanId) {
175175
spanTranslated.parentId = span.parentSpanId;

0 commit comments

Comments
 (0)