Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(eslint): no-console error rule #2018

Merged
merged 6 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
"leadingUnderscore": "require"
}
],
"no-console": "error",
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["warn"],
"@typescript-eslint/no-unused-vars": ["error", {"argsIgnorePattern": "^_", "args": "after-used"}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { MetricExporter, MetricRecord, Histogram } from './types';
import { ExportResult, ExportResultCode } from '@opentelemetry/core';

Expand All @@ -27,18 +28,18 @@ export class ConsoleMetricExporter implements MetricExporter {
resultCallback: (result: ExportResult) => void
): void {
for (const metric of metrics) {
console.log(metric.descriptor);
console.log(metric.labels);
diag.info('Descriptor', metric.descriptor);
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
diag.info('Labels', metric.labels);
const point = metric.aggregator.toPoint();
if (typeof point.value === 'number') {
console.log('value: ' + point.value);
diag.info('value: ', point.value);
} else if (typeof (point.value as Histogram).buckets === 'object') {
const histogram = point.value as Histogram;
console.log(
diag.info(
`count: ${histogram.count}, sum: ${histogram.sum}, buckets: ${histogram.buckets}`
);
} else {
console.log(point.value);
diag.info('value: ', point.value);
}
}
return resultCallback({ code: ExportResultCode.SUCCESS });
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ describe('Meter', () => {
let counter = 0;

function getValue() {
console.log('getting value, counter:', counter);
diag.info('getting value, counter:', counter);
if (++counter % 2 === 0) {
return 3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,29 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { ConsoleMetricExporter, MeterProvider, MetricKind } from '../../src';
import { ValueType } from '@opentelemetry/api-metrics';

describe('ConsoleMetricExporter', () => {
let consoleExporter: ConsoleMetricExporter;
let previousConsoleLog: any;
let previousInfo: any;

beforeEach(() => {
previousConsoleLog = console.log;
console.log = () => {};
previousInfo = diag.info;
diag.info = () => {};
consoleExporter = new ConsoleMetricExporter();
});

afterEach(() => {
console.log = previousConsoleLog;
diag.info = previousInfo;
});

describe('.export()', () => {
it('should export information about metrics', async () => {
const spyConsole = sinon.spy(console, 'log');
const spyInfo = sinon.spy(diag, 'info');

const meter = new MeterProvider().getMeter(
'test-console-metric-exporter'
Expand All @@ -51,24 +52,20 @@ describe('ConsoleMetricExporter', () => {

await meter.collect();
consoleExporter.export(meter.getProcessor().checkPointSet(), () => {});
assert.strictEqual(spyConsole.args.length, 3);
const [descriptor, labels, value] = spyConsole.args;
assert.deepStrictEqual(descriptor, [
{
description: 'a test description',
metricKind: MetricKind.COUNTER,
name: 'counter',
unit: '1',
valueType: ValueType.DOUBLE,
},
]);
assert.deepStrictEqual(labels, [
{
key1: 'labelValue1',
key2: 'labelValue2',
},
]);
assert.deepStrictEqual(value[0], 'value: 10');
assert.strictEqual(spyInfo.args.length, 3);
const [descriptor, labels, value] = spyInfo.args;
assert.deepStrictEqual(descriptor[1], {
description: 'a test description',
metricKind: MetricKind.COUNTER,
name: 'counter',
unit: '1',
valueType: ValueType.DOUBLE,
});
assert.deepStrictEqual(labels[1], {
key1: 'labelValue1',
key2: 'labelValue2',
});
assert.deepStrictEqual(value[1], 10);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* limitations under the License.
*/

import {
diag,
SpanAttributes,
SpanKind,
SpanStatus,
TimedEvent,
} from '@opentelemetry/api';
import { SpanExporter } from './SpanExporter';
import { ReadableSpan } from './ReadableSpan';
import {
Expand Down Expand Up @@ -51,7 +58,7 @@ export class ConsoleSpanExporter implements SpanExporter {
* converts span info into more readable format
* @param span
*/
private _exportInfo(span: ReadableSpan) {
private _exportInfo(span: ReadableSpan): LoggedSpan {
return {
traceId: span.spanContext.traceId,
parentId: span.parentSpanId,
Expand All @@ -76,10 +83,23 @@ export class ConsoleSpanExporter implements SpanExporter {
done?: (result: ExportResult) => void
): void {
for (const span of spans) {
console.log(this._exportInfo(span));
diag.info('span', this._exportInfo(span));
}
if (done) {
return done({ code: ExportResultCode.SUCCESS });
}
}
}

export interface LoggedSpan {
traceId: string;
parentId: string | undefined;
name: string;
id: string;
kind: SpanKind;
timestamp: number;
duration: number;
attributes: SpanAttributes;
status: SpanStatus;
events: TimedEvent[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import {
AlwaysOnSampler,
ExportResultCode,
Expand Down Expand Up @@ -237,7 +238,10 @@ describe('BatchSpanProcessor', () => {
clock.tick(defaultBufferConfig.scheduledDelayMillis + 10);
clock.restore();

console.log(exporter.getFinishedSpans().length);
diag.info(
'finished spans count',
exporter.getFinishedSpans().length
);
assert.strictEqual(
exporter.getFinishedSpans().length,
totalSpans + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
BasicTracerProvider,
ConsoleSpanExporter,
LoggedSpan,
SimpleSpanProcessor,
} from '../../src';

describe('ConsoleSpanExporter', () => {
let consoleExporter: ConsoleSpanExporter;
let previousConsoleLog: any;
let previousInfo: any;

beforeEach(() => {
previousConsoleLog = console.log;
console.log = () => {};
previousInfo = diag.info;
diag.info = () => {};
consoleExporter = new ConsoleSpanExporter();
});

afterEach(() => {
console.log = previousConsoleLog;
diag.info = previousInfo;
});

describe('.export()', () => {
Expand All @@ -42,7 +44,7 @@ describe('ConsoleSpanExporter', () => {
const basicTracerProvider = new BasicTracerProvider();
consoleExporter = new ConsoleSpanExporter();

const spyConsole = sinon.spy(console, 'log');
const spyInfo = sinon.spy(diag, 'info');
const spyExport = sinon.spy(consoleExporter, 'export');

basicTracerProvider.addSpanProcessor(
Expand All @@ -56,9 +58,9 @@ describe('ConsoleSpanExporter', () => {
const spans = spyExport.args[0];
const firstSpan = spans[0][0];
const firstEvent = firstSpan.events[0];
const consoleArgs = spyConsole.args[0];
const consoleSpan = consoleArgs[0];
const keys = Object.keys(consoleSpan).sort().join(',');
const infoArgs = spyInfo.args[0];
const loggedSpan = infoArgs[1] as LoggedSpan;
const keys = Object.keys(loggedSpan).sort().join(',');

const expectedKeys = [
'attributes',
Expand All @@ -75,7 +77,7 @@ describe('ConsoleSpanExporter', () => {

assert.ok(firstSpan.name === 'foo');
assert.ok(firstEvent.name === 'foobar');
assert.ok(consoleSpan.id === firstSpan.spanContext.spanId);
assert.ok(loggedSpan.id === firstSpan.spanContext.spanId);
assert.ok(keys === expectedKeys);

assert.ok(spyExport.calledOnce);
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-web/src/WebTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import {
BasicTracerProvider,
SDKRegistrationConfig,
Expand Down Expand Up @@ -41,7 +42,7 @@ export class WebTracerProvider extends BasicTracerProvider {
*/
constructor(config: WebTracerConfig = {}) {
if (typeof config.plugins !== 'undefined') {
console.warn(
diag.warn(
'plugins option was removed, please use' +
' "registerInstrumentations" to load plugins'
);
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-web/test/WebTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { context, getSpan, setSpan, ContextManager } from '@opentelemetry/api';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { B3Propagator } from '@opentelemetry/propagator-b3';
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('WebTracerProvider', () => {

it('should show warning when plugins are defined', () => {
const dummyPlugin1 = {};
const spyWarn = sinon.spy(window.console, 'warn');
const spyWarn = sinon.spy(diag, 'warn');

const plugins = [dummyPlugin1];

Expand Down