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

Commit

Permalink
Cleanup type assertion (#525)
Browse files Browse the repository at this point in the history
* Cleanup type assertion

* Fix build
  • Loading branch information
mayurkale22 committed May 15, 2019
1 parent 940ef79 commit 0fcb0cb
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 51 deletions.
4 changes: 2 additions & 2 deletions packages/opencensus-core/src/metrics/gauges/derived-gauge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ export class DerivedGauge implements types.Meter {
descriptor: this.metricDescriptor,
timeseries: Array.from(
this.registeredPoints,
([_, gaugeEntry]) => ({
([_, gaugeEntry]): TimeSeries => ({
labelValues:
[...gaugeEntry.labelValues, ...this.constantLabelValues],
points: [{value: gaugeEntry.extractor(), timestamp}]
} as TimeSeries))
}))
};
}
}
10 changes: 5 additions & 5 deletions packages/opencensus-core/src/stats/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export class BaseView implements View {
* @returns The Point.
*/
private toPoint(timestamp: Timestamp, data: AggregationData): Point {
let value;
if (data.type === AggregationType.DISTRIBUTION) {
const {count, sum, sumOfSquaredDeviation, exemplars} = data;
const buckets = [];
Expand All @@ -241,17 +240,18 @@ export class BaseView implements View {
buckets.push(this.getMetricBucket(bucketCount, statsExemplar));
}
}
value = {
const value: DistributionValue = {
count,
sum,
sumOfSquaredDeviation,
buckets,
bucketOptions: {explicit: {bounds: data.buckets}},
} as DistributionValue;
};
return {timestamp, value};
} else {
value = data.value as number;
const value: number = data.value;
return {timestamp, value};
}
return {timestamp, value};
}

/**
Expand Down
24 changes: 8 additions & 16 deletions packages/opencensus-core/src/trace/model/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,14 @@ export class Span implements types.Span {
* @param timestamp A time, in milliseconds. Defaults to Date.now()
*/
addAnnotation(
description: string, attributes?: types.Attributes, timestamp = 0) {
description: string, attributes: types.Attributes = {},
timestamp = Date.now()) {
if (this.annotations.length >=
this.activeTraceParams.numberOfAnnontationEventsPerSpan!) {
this.annotations.shift();
this.droppedAnnotationsCount++;
}
this.annotations.push({
'description': description,
'attributes': attributes || {},
'timestamp': timestamp ? timestamp : Date.now(),
} as types.Annotation);
this.annotations.push({description, attributes, timestamp});
}

/**
Expand All @@ -246,18 +243,13 @@ export class Span implements types.Span {
*/
addLink(
traceId: string, spanId: string, type: types.LinkType,
attributes?: types.Attributes) {
attributes: types.Attributes = {}) {
if (this.links.length >= this.activeTraceParams.numberOfLinksPerSpan!) {
this.links.shift();
this.droppedLinksCount++;
}

this.links.push({
'traceId': traceId,
'spanId': spanId,
'type': type,
'attributes': attributes || {}
} as types.Link);
this.links.push({traceId, spanId, type, attributes});
}

/**
Expand All @@ -270,7 +262,7 @@ export class Span implements types.Span {
* zero or undefined, assumed to be the same size as uncompressed.
*/
addMessageEvent(
type: types.MessageEventType, id: number, timestamp = 0,
type: types.MessageEventType, id: number, timestamp = Date.now(),
uncompressedSize?: number, compressedSize?: number) {
if (this.messageEvents.length >=
this.activeTraceParams.numberOfMessageEventsPerSpan!) {
Expand All @@ -281,10 +273,10 @@ export class Span implements types.Span {
this.messageEvents.push({
type,
id,
timestamp: timestamp ? timestamp : Date.now(),
timestamp,
uncompressedSize,
compressedSize,
} as types.MessageEvent);
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-core/test/test-root-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {RootSpan} from '../src/trace/model/root-span';
import {Span} from '../src/trace/model/span';
import {CoreTracer} from '../src/trace/model/tracer';
import * as types from '../src/trace/model/types';
import {Annotation, Attributes, Link} from '../src/trace/model/types';
import {Annotation, Link} from '../src/trace/model/types';

const tracer = new CoreTracer();

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

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

assert.ok(rootSpan.annotations.length > 0);
assert.ok(instanceOfAnnotation(rootSpan.annotations[0]));
Expand Down
6 changes: 3 additions & 3 deletions packages/opencensus-core/test/test-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {RootSpan} from '../src/trace/model/root-span';
import {Span} from '../src/trace/model/span';
import {CoreTracer} from '../src/trace/model/tracer';
import * as types from '../src/trace/model/types';
import {Annotation, Attributes, Link} from '../src/trace/model/types';
import {Annotation, Link} from '../src/trace/model/types';

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

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

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

assert.equal(span.annotations.length, 32);
Expand Down
14 changes: 8 additions & 6 deletions packages/opencensus-core/test/test-tracer-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Tracer Base', () => {

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

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

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

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

describe('startRootSpan() with context propagation', () => {
const traceOptions = {name: 'rootName', kind: types.SpanKind.UNSPECIFIED} as
types.TraceOptions;
const traceOptions: types.TraceOptions = {
name: 'rootName',
kind: types.SpanKind.UNSPECIFIED
};

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

const spanContextPropagated = {
const spanContextPropagated: types.SpanContext = {
traceId: uuid.v4().split('-').join(''),
spanId: randomSpanId(),
options: 0x1
} as types.SpanContext;
};


it('should create the new RootSpan with propagation', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/opencensus-exporter-jaeger/test/test-jaeger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Jaeger Exporter', () => {
bufferTimeout: DEFAULT_BUFFER_TIMEOUT,
logger: testLogger,
maxPacketSize: 1000
} as JaegerTraceExporterOptions;
};
});

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Stackdriver Trace Exporter', function() {
projectId: PROJECT_ID,
bufferTimeout: 200,
logger: testLogger
} as StackdriverExporterOptions;
};
});

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
sumOfSquaredDeviation: 14,
bucketOptions: {explicit: {bounds: [1.2, 3.2, 5.2]}},
buckets: [{count: 3}, {count: 1}, {count: 2}, {count: 4}],
} as DistributionValue,
},
timestamp: pointTimestamp
};

Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-exporter-zipkin/src/zipkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class ZipkinTraceExporter implements Exporter {
* @param span Span to be translated
*/
translateSpan(span: Span): TranslatedSpan {
const spanTranslated = {
const spanTranslated: TranslatedSpan = {
traceId: span.traceId,
name: span.name,
id: span.id,
Expand All @@ -169,7 +169,7 @@ export class ZipkinTraceExporter implements Exporter {
localEndpoint: {serviceName: this.serviceName},
tags: this.createTags(span.attributes, span.status),
annotations: this.createAnnotations(span.annotations, span.messageEvents)
} as TranslatedSpan;
};

if (span.parentSpanId) {
spanTranslated.parentId = span.parentSpanId;
Expand Down
8 changes: 4 additions & 4 deletions packages/opencensus-exporter-zipkin/test/test-zipkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const postPath = '/api/v2/spans';
const OPENCENSUS_NETWORK_TESTS = process.env.OPENCENSUS_NETWORK_TESTS;

/** Default options for zipkin tests */
const zipkinOptions = {
const zipkinOptions: ZipkinExporterOptions = {
url: zipkinHost + postPath,
serviceName: 'opencensus-tests'
} as ZipkinExporterOptions;
};

/** Default config for traces tests */
const defaultConfig: TracerConfig = {
Expand Down Expand Up @@ -185,10 +185,10 @@ describe('Zipkin Exporter', function() {
it('shouldn\'t send traces to Zipkin service and return an 404 error',
() => {
/** Creating new options with a wrong url */
const options = {
const options: ZipkinExporterOptions = {
url: zipkinHost + '/wrong',
serviceName: 'opencensus-tests'
} as ZipkinExporterOptions;
};

const exporter = new ZipkinTraceExporter(options);
const tracer = new CoreTracer();
Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-exporter-zpages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import * as tracing from '@opencensus/nodejs';
import {ZpagesExporter, ZpagesExporterOptions} from '@opencensus/zpages-exporter';

// Add your zipkin url and application name to the Zipkin options
const options = {
const options: ZpagesExporterOptions = {
port: 8080, // default
startServer: true, // default
spanNames: ['predefined/span1', 'predefined/span2']
} as ZpagesExporterOptions;
};

const exporter = new ZpagesExporter(options);
```
Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-exporter-zpages/src/zpages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export class ZpagesExporter implements Exporter, StatsEventListener {
private port: number;
private traces: Map<string, Span[]> = new Map();
private logger: Logger;
private statsParams = {
private statsParams: StatsParams = {
registeredViews: [],
registeredMeasures: [],
recordedData: {}
} as StatsParams;
};

constructor(options: ZpagesExporterOptions) {
/** create express app */
Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-exporter-zpages/test/test-zpages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import {TraceConfigzData, TraceConfigzParams} from '../src/zpages-frontend/page-
import {TracezData, TracezParams} from '../src/zpages-frontend/page-handlers/tracez.page-handler';

/** Default options for zpages tests */
const options = {
const options: ZpagesExporterOptions = {
port: 8081,
spanNames: ['predefined/span1', 'predefined/span2'],
startServer: false
} as ZpagesExporterOptions;
};

/** Zpages Server URL, just to support tests, shouldn't be changed */
const zpagesServerUrl = 'http://localhost:' + options.port;
Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-instrumentation-http/test/test-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const VERSION = process.versions.node;

class DummyPropagation implements Propagation {
extract(getter: HeaderGetter): SpanContext {
return {traceId: 'dummy-trace-id', spanId: 'dummy-span-id'} as SpanContext;
return {traceId: 'dummy-trace-id', spanId: 'dummy-span-id'};
}

inject(setter: HeaderSetter, spanContext: SpanContext): void {
Expand All @@ -88,7 +88,7 @@ class DummyPropagation implements Propagation {
}

generate(): SpanContext {
return {traceId: 'dummy-trace-id', spanId: 'dummy-span-id'} as SpanContext;
return {traceId: 'dummy-trace-id', spanId: 'dummy-span-id'};
}
}

Expand Down

0 comments on commit 0fcb0cb

Please sign in to comment.