From b373c9a3705b1db8ccb706fde94e343f8a374aa0 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Sun, 3 Jan 2021 20:13:08 -0500 Subject: [PATCH 1/3] feat: add no-floating-promises lint rule Only warn in tests, disable rule for existing occurences in non-test modules. Signed-off-by: naseemkullah --- eslint.config.js | 24 ++++++++++--------- .../src/util.ts | 1 + .../src/export/Controller.ts | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index af60d20dfb..938de96c05 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -4,13 +4,14 @@ module.exports = { "header" ], extends: [ - "./node_modules/gts", + "./node_modules/gts", ], parser: "@typescript-eslint/parser", parserOptions: { - "project": "./tsconfig.json" + "project": "./tsconfig.json" }, rules: { + "@typescript-eslint/no-floating-promises": 2, "@typescript-eslint/no-this-alias": "off", "eqeqeq": [ "error", @@ -18,13 +19,13 @@ module.exports = { ], "prefer-rest-params": "off", "@typescript-eslint/naming-convention": [ - "error", - { - "selector": "memberLike", - "modifiers": ["private", "protected"], - "format": ["camelCase"], - "leadingUnderscore": "require" - } + "error", + { + "selector": "memberLike", + "modifiers": ["private", "protected"], + "format": ["camelCase"], + "leadingUnderscore": "require" + } ], "no-console": "error", "no-shadow": "off", @@ -35,8 +36,8 @@ module.exports = { "prettier/prettier": ["error", { "singleQuote": true, "arrowParens": "avoid" }], "node/no-deprecated-api": ["warn"], "header/header": [2, "block", [{ - pattern: / \* Copyright The OpenTelemetry Authors[\r\n]+ \*[\r\n]+ \* Licensed under the Apache License, Version 2\.0 \(the \"License\"\);[\r\n]+ \* you may not use this file except in compliance with the License\.[\r\n]+ \* You may obtain a copy of the License at[\r\n]+ \*[\r\n]+ \* https:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0[\r\n]+ \*[\r\n]+ \* Unless required by applicable law or agreed to in writing, software[\r\n]+ \* distributed under the License is distributed on an \"AS IS\" BASIS,[\r\n]+ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.[\r\n]+ \* See the License for the specific language governing permissions and[\r\n]+ \* limitations under the License\./gm, - template: + pattern: / \* Copyright The OpenTelemetry Authors[\r\n]+ \*[\r\n]+ \* Licensed under the Apache License, Version 2\.0 \(the \"License\"\);[\r\n]+ \* you may not use this file except in compliance with the License\.[\r\n]+ \* You may obtain a copy of the License at[\r\n]+ \*[\r\n]+ \* https:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0[\r\n]+ \*[\r\n]+ \* Unless required by applicable law or agreed to in writing, software[\r\n]+ \* distributed under the License is distributed on an \"AS IS\" BASIS,[\r\n]+ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.[\r\n]+ \* See the License for the specific language governing permissions and[\r\n]+ \* limitations under the License\./gm, + template: `\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n ` }]] }, @@ -48,6 +49,7 @@ module.exports = { "@typescript-eslint/ban-ts-ignore": "off", "@typescript-eslint/no-empty-function": "off", "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-floating-promises": 1, "@typescript-eslint/no-unused-vars": "off", "@typescript-eslint/no-var-requires": "off", "@typescript-eslint/no-shadow": ["off"], diff --git a/packages/opentelemetry-exporter-collector-grpc/src/util.ts b/packages/opentelemetry-exporter-collector-grpc/src/util.ts index 72ea1c7369..642c930848 100644 --- a/packages/opentelemetry-exporter-collector-grpc/src/util.ts +++ b/packages/opentelemetry-exporter-collector-grpc/src/util.ts @@ -38,6 +38,7 @@ export function onInit( const includeDirs = [path.resolve(__dirname, '..', 'protos')]; + // eslint-disable-next-line @typescript-eslint/no-floating-promises protoLoader .load(collector.getServiceProtoPath(), { keepCase: false, diff --git a/packages/opentelemetry-metrics/src/export/Controller.ts b/packages/opentelemetry-metrics/src/export/Controller.ts index 239080a4e2..7e84684bbd 100644 --- a/packages/opentelemetry-metrics/src/export/Controller.ts +++ b/packages/opentelemetry-metrics/src/export/Controller.ts @@ -37,6 +37,7 @@ export class PushController extends Controller { ) { super(); this._timer = setInterval(() => { + // eslint-disable-next-line @typescript-eslint/no-floating-promises this._collect(); }, interval); unrefTimer(this._timer); From 71781eca9d03540419442522961e271e44a218ba Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Mon, 12 Apr 2021 17:19:48 -0400 Subject: [PATCH 2/3] fix: then and catch order Signed-off-by: naseemkullah --- .../opentelemetry-tracing/src/export/BatchSpanProcessor.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts b/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts index 8aaa15b13b..29a396bac3 100644 --- a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts +++ b/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts @@ -170,14 +170,14 @@ export class BatchSpanProcessor implements SpanProcessor { if (this._timer !== undefined) return; this._timer = setTimeout(() => { this._flushOneBatch() - .catch(e => { - globalErrorHandler(e); - }) .then(() => { if (this._finishedSpans.length > 0) { this._clearTimer(); this._maybeStartTimer(); } + }) + .catch(e => { + globalErrorHandler(e); }); }, this._scheduledDelayMillis); unrefTimer(this._timer); From a9eb7ca9b265fb2df8da3e5a2c0247489ed92835 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Mon, 12 Apr 2021 21:45:21 -0400 Subject: [PATCH 3/3] fix: use globalErrorHandler Signed-off-by: naseemkullah --- .../src/util.ts | 12 +++++++----- .../opentelemetry-metrics/src/export/Controller.ts | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry-exporter-collector-grpc/src/util.ts b/packages/opentelemetry-exporter-collector-grpc/src/util.ts index 642c930848..45ded6e4f5 100644 --- a/packages/opentelemetry-exporter-collector-grpc/src/util.ts +++ b/packages/opentelemetry-exporter-collector-grpc/src/util.ts @@ -14,18 +14,18 @@ * limitations under the License. */ -import { diag } from '@opentelemetry/api'; +import * as grpc from '@grpc/grpc-js'; import * as protoLoader from '@grpc/proto-loader'; +import { diag } from '@opentelemetry/api'; +import { globalErrorHandler } from '@opentelemetry/core'; import { collectorTypes } from '@opentelemetry/exporter-collector'; -import * as grpc from '@grpc/grpc-js'; import * as path from 'path'; - +import { CollectorExporterNodeBase } from './CollectorExporterNodeBase'; import { CollectorExporterConfigNode, GRPCQueueItem, ServiceClientType, } from './types'; -import { CollectorExporterNodeBase } from './CollectorExporterNodeBase'; export function onInit( collector: CollectorExporterNodeBase, @@ -38,7 +38,6 @@ export function onInit( const includeDirs = [path.resolve(__dirname, '..', 'protos')]; - // eslint-disable-next-line @typescript-eslint/no-floating-promises protoLoader .load(collector.getServiceProtoPath(), { keepCase: false, @@ -69,6 +68,9 @@ export function onInit( collector.send(item.objects, item.onSuccess, item.onError); }); } + }) + .catch(err => { + globalErrorHandler(err); }); } diff --git a/packages/opentelemetry-metrics/src/export/Controller.ts b/packages/opentelemetry-metrics/src/export/Controller.ts index 7e84684bbd..7c0c82700c 100644 --- a/packages/opentelemetry-metrics/src/export/Controller.ts +++ b/packages/opentelemetry-metrics/src/export/Controller.ts @@ -37,8 +37,9 @@ export class PushController extends Controller { ) { super(); this._timer = setInterval(() => { - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this._collect(); + this._collect().catch(err => { + globalErrorHandler(err); + }); }, interval); unrefTimer(this._timer); }