From c62ea117a6148a075fe04b2778996f9a0e3ae278 Mon Sep 17 00:00:00 2001 From: Ali Sabzevari Date: Mon, 23 Aug 2021 23:46:10 +0200 Subject: [PATCH] eslint configuration for getting-started examples (#2297) * chore: add eslint config to getting-started * chore: apply lint on getting-started * chore: fix strict rule in getting-started lint config * chore: import examples eslintrc for getting-started eslint config * chore: rearrage lint scripts in package.json * chore: restore removed require from getting-started example * chore: remove duplicate require in getting-started --- examples/{.eslintrc => .eslintrc.json} | 0 getting-started/.eslintrc.js | 6 +++ getting-started/example/app.js | 24 +++++----- getting-started/monitored-example/app.js | 27 +++++------ .../monitored-example/monitoring.js | 45 +++++++++---------- getting-started/traced-example/app.js | 24 +++++----- getting-started/traced-example/tracing.js | 18 ++++---- getting-started/ts-example/.eslintrc | 16 +++++++ getting-started/ts-example/example/app.ts | 22 ++++----- .../ts-example/monitored-example/app.ts | 24 +++++----- .../monitored-example/monitoring.ts | 18 ++++---- .../ts-example/traced-example/app.ts | 22 ++++----- .../ts-example/traced-example/tracing.ts | 6 +-- package.json | 2 + 14 files changed, 137 insertions(+), 117 deletions(-) rename examples/{.eslintrc => .eslintrc.json} (100%) create mode 100644 getting-started/.eslintrc.js create mode 100644 getting-started/ts-example/.eslintrc diff --git a/examples/.eslintrc b/examples/.eslintrc.json similarity index 100% rename from examples/.eslintrc rename to examples/.eslintrc.json diff --git a/getting-started/.eslintrc.js b/getting-started/.eslintrc.js new file mode 100644 index 0000000000..8580fbfb04 --- /dev/null +++ b/getting-started/.eslintrc.js @@ -0,0 +1,6 @@ +/* eslint-disable global-require */ +/* eslint-disable strict */ + +module.exports = { + ...require('../examples/.eslintrc.json'), +}; diff --git a/getting-started/example/app.js b/getting-started/example/app.js index 86c1a6e802..287ab78cb3 100644 --- a/getting-started/example/app.js +++ b/getting-started/example/app.js @@ -1,40 +1,40 @@ -"use strict"; +'use strict'; -const PORT = process.env.PORT || "8080"; +const PORT = process.env.PORT || '8080'; -const express = require("express"); -const axios = require("axios"); +const express = require('express'); +const axios = require('axios'); const app = express(); -app.get("/", (req, res) => { +app.get('/', (req, res) => { axios .get(`http://localhost:${PORT}/middle-tier`) .then(() => axios.get(`http://localhost:${PORT}/middle-tier`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/middle-tier", (req, res) => { +app.get('/middle-tier', (req, res) => { axios .get(`http://localhost:${PORT}/backend`) .then(() => axios.get(`http://localhost:${PORT}/backend`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/backend", (req, res) => { - res.send("Hello from the backend"); +app.get('/backend', (req, res) => { + res.send('Hello from the backend'); }); app.listen(parseInt(PORT, 10), () => { diff --git a/getting-started/monitored-example/app.js b/getting-started/monitored-example/app.js index 2144423d6a..af61cef67e 100644 --- a/getting-started/monitored-example/app.js +++ b/getting-started/monitored-example/app.js @@ -1,42 +1,43 @@ -"use strict"; +'use strict'; -const PORT = process.env.PORT || "8080"; +const PORT = process.env.PORT || '8080'; -const express = require("express"); -const axios = require("axios"); +const express = require('express'); +const axios = require('axios'); + +const { countAllRequests } = require('./monitoring'); -const { countAllRequests } = require("./monitoring"); const app = express(); app.use(countAllRequests()); -app.get("/", (req, res) => { +app.get('/', (req, res) => { axios .get(`http://localhost:${PORT}/middle-tier`) .then(() => axios.get(`http://localhost:${PORT}/middle-tier`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/middle-tier", (req, res) => { +app.get('/middle-tier', (req, res) => { axios .get(`http://localhost:${PORT}/backend`) .then(() => axios.get(`http://localhost:${PORT}/backend`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/backend", (req, res) => { - res.send("Hello from the backend"); +app.get('/backend', (req, res) => { + res.send('Hello from the backend'); }); app.listen(parseInt(PORT, 10), () => { diff --git a/getting-started/monitored-example/monitoring.js b/getting-started/monitored-example/monitoring.js index 4d01ccf675..ca20e8d2fd 100644 --- a/getting-started/monitored-example/monitoring.js +++ b/getting-started/monitored-example/monitoring.js @@ -1,11 +1,11 @@ -"use strict"; - +'use strict'; + const { MeterProvider } = require('@opentelemetry/sdk-metrics-base'); const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); - -const prometheusPort = PrometheusExporter.DEFAULT_OPTIONS.port -const prometheusEndpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint - + +const prometheusPort = PrometheusExporter.DEFAULT_OPTIONS.port; +const prometheusEndpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint; + const exporter = new PrometheusExporter( { startServer: true, @@ -16,28 +16,25 @@ const exporter = new PrometheusExporter( ); }, ); - + const meter = new MeterProvider({ exporter, interval: 1000, }).getMeter('your-meter-name'); - -const requestCount = meter.createCounter("requests", { - description: "Count all incoming requests" + +const requestCount = meter.createCounter('requests', { + description: 'Count all incoming requests', }); - + const boundInstruments = new Map(); - -module.exports.countAllRequests = () => { - return (req, res, next) => { - if (!boundInstruments.has(req.path)) { - const labels = { route: req.path }; - const boundCounter = requestCount.bind(labels); - boundInstruments.set(req.path, boundCounter); - } - - boundInstruments.get(req.path).add(1); - next(); - }; -}; +module.exports.countAllRequests = () => (req, res, next) => { + if (!boundInstruments.has(req.path)) { + const labels = { route: req.path }; + const boundCounter = requestCount.bind(labels); + boundInstruments.set(req.path, boundCounter); + } + + boundInstruments.get(req.path).add(1); + next(); +}; diff --git a/getting-started/traced-example/app.js b/getting-started/traced-example/app.js index 86c1a6e802..287ab78cb3 100644 --- a/getting-started/traced-example/app.js +++ b/getting-started/traced-example/app.js @@ -1,40 +1,40 @@ -"use strict"; +'use strict'; -const PORT = process.env.PORT || "8080"; +const PORT = process.env.PORT || '8080'; -const express = require("express"); -const axios = require("axios"); +const express = require('express'); +const axios = require('axios'); const app = express(); -app.get("/", (req, res) => { +app.get('/', (req, res) => { axios .get(`http://localhost:${PORT}/middle-tier`) .then(() => axios.get(`http://localhost:${PORT}/middle-tier`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/middle-tier", (req, res) => { +app.get('/middle-tier', (req, res) => { axios .get(`http://localhost:${PORT}/backend`) .then(() => axios.get(`http://localhost:${PORT}/backend`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/backend", (req, res) => { - res.send("Hello from the backend"); +app.get('/backend', (req, res) => { + res.send('Hello from the backend'); }); app.listen(parseInt(PORT, 10), () => { diff --git a/getting-started/traced-example/tracing.js b/getting-started/traced-example/tracing.js index 3508c1a0f2..ae6b88b015 100644 --- a/getting-started/traced-example/tracing.js +++ b/getting-started/traced-example/tracing.js @@ -1,18 +1,18 @@ -"use strict"; +'use strict'; -const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node"); -const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base"); +const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); +const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); const { Resource } = require('@opentelemetry/resources'); const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); -const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin"); +const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); const { registerInstrumentations } = require('@opentelemetry/instrumentation'); const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express'); const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); const provider = new NodeTracerProvider({ resource: new Resource({ - [SemanticResourceAttributes.SERVICE_NAME]: "getting-started", - }) + [SemanticResourceAttributes.SERVICE_NAME]: 'getting-started', + }), }); provider.addSpanProcessor( @@ -21,8 +21,8 @@ provider.addSpanProcessor( // If you are running your tracing backend on another host, // you can point to it using the `url` parameter of the // exporter config. - }) - ) + }), + ), ); provider.register(); @@ -35,4 +35,4 @@ registerInstrumentations({ ], }); -console.log("tracing initialized"); +console.log('tracing initialized'); diff --git a/getting-started/ts-example/.eslintrc b/getting-started/ts-example/.eslintrc new file mode 100644 index 0000000000..6564779085 --- /dev/null +++ b/getting-started/ts-example/.eslintrc @@ -0,0 +1,16 @@ +{ + "plugins": ["@typescript-eslint", "node"], + "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + "parser": "@typescript-eslint/parser", + "rules": { + "@typescript-eslint/no-var-requires": 0, + "import/prefer-default-export": "off", + "import/extensions": [ + "error", + "ignorePackages", + { + "": "never" + } + ] + } +} diff --git a/getting-started/ts-example/example/app.ts b/getting-started/ts-example/example/app.ts index e4267e1421..a252668ebe 100644 --- a/getting-started/ts-example/example/app.ts +++ b/getting-started/ts-example/example/app.ts @@ -1,38 +1,38 @@ -import * as express from "express"; -import axios from "axios"; +import * as express from 'express'; +import axios from 'axios'; -const PORT: string = process.env.PORT || "8080"; +const PORT: string = process.env.PORT || '8080'; const app = express(); -app.get("/", (req, res) => { +app.get('/', (req, res) => { axios .get(`http://localhost:${PORT}/middle-tier`) .then(() => axios.get(`http://localhost:${PORT}/middle-tier`)) - .then(response => { + .then((response) => { res.send(response.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/middle-tier", (req, res) => { +app.get('/middle-tier', (req, res) => { axios .get(`http://localhost:${PORT}/backend`) .then(() => axios.get(`http://localhost:${PORT}/backend`)) - .then(response => { + .then((response) => { res.send(response.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/backend", (req, res) => { - res.send("Hello from the backend"); +app.get('/backend', (req, res) => { + res.send('Hello from the backend'); }); app.listen(parseInt(PORT, 10), () => { diff --git a/getting-started/ts-example/monitored-example/app.ts b/getting-started/ts-example/monitored-example/app.ts index 047c263836..9307eba1ac 100644 --- a/getting-started/ts-example/monitored-example/app.ts +++ b/getting-started/ts-example/monitored-example/app.ts @@ -1,40 +1,40 @@ -import * as express from "express"; -import axios from "axios"; - -const PORT: string = process.env.PORT || "8080"; +import * as express from 'express'; +import axios from 'axios'; import { countAllRequests } from './monitoring'; + +const PORT: string = process.env.PORT || '8080'; const app = express(); app.use(countAllRequests()); -app.get("/", (req, res) => { +app.get('/', (req, res) => { axios .get(`http://localhost:${PORT}/middle-tier`) .then(() => axios.get(`http://localhost:${PORT}/middle-tier`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/middle-tier", (req, res) => { +app.get('/middle-tier', (req, res) => { axios .get(`http://localhost:${PORT}/backend`) .then(() => axios.get(`http://localhost:${PORT}/backend`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/backend", (req, res) => { - res.send("Hello from the backend"); +app.get('/backend', (req, res) => { + res.send('Hello from the backend'); }); app.listen(parseInt(PORT, 10), () => { diff --git a/getting-started/ts-example/monitored-example/monitoring.ts b/getting-started/ts-example/monitored-example/monitoring.ts index 626cc76323..5e933e4a93 100644 --- a/getting-started/ts-example/monitored-example/monitoring.ts +++ b/getting-started/ts-example/monitored-example/monitoring.ts @@ -27,15 +27,13 @@ const requestCount = meter.createCounter('requests', { const handles = new Map(); -export const countAllRequests = () => { - return (req: Request, _res: Response, next: NextFunction) => { - if (!handles.has(req.path)) { - const labels = { route: req.path }; - const handle = requestCount.bind(labels); - handles.set(req.path, handle); - } +export const countAllRequests = () => (req: Request, _res: Response, next: NextFunction): void => { + if (!handles.has(req.path)) { + const labels = { route: req.path }; + const handle = requestCount.bind(labels); + handles.set(req.path, handle); + } - handles.get(req.path).add(1); - next(); - }; + handles.get(req.path).add(1); + next(); }; diff --git a/getting-started/ts-example/traced-example/app.ts b/getting-started/ts-example/traced-example/app.ts index 41b6a43394..58fb8ab589 100644 --- a/getting-started/ts-example/traced-example/app.ts +++ b/getting-started/ts-example/traced-example/app.ts @@ -1,38 +1,38 @@ -import * as express from "express"; -import axios from "axios"; +import * as express from 'express'; +import axios from 'axios'; -const PORT: string = process.env.PORT || "8080"; +const PORT: string = process.env.PORT || '8080'; const app = express(); -app.get("/", (req, res) => { +app.get('/', (req, res) => { axios .get(`http://localhost:${PORT}/middle-tier`) .then(() => axios.get(`http://localhost:${PORT}/middle-tier`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/middle-tier", (req, res) => { +app.get('/middle-tier', (req, res) => { axios .get(`http://localhost:${PORT}/backend`) .then(() => axios.get(`http://localhost:${PORT}/backend`)) - .then(result => { + .then((result) => { res.send(result.data); }) - .catch(err => { + .catch((err) => { console.error(err); res.status(500).send(); }); }); -app.get("/backend", (req, res) => { - res.send("Hello from the backend"); +app.get('/backend', (req, res) => { + res.send('Hello from the backend'); }); app.listen(parseInt(PORT, 10), () => { diff --git a/getting-started/ts-example/traced-example/tracing.ts b/getting-started/ts-example/traced-example/tracing.ts index fe2ce7e4b2..4426da25f9 100644 --- a/getting-started/ts-example/traced-example/tracing.ts +++ b/getting-started/ts-example/traced-example/tracing.ts @@ -1,9 +1,10 @@ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; -const { Resource } = require('@opentelemetry/resources'); -const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base'; import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; + +const { Resource } = require('@opentelemetry/resources'); +const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); // For Jaeger, use the following line instead: // import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; @@ -36,5 +37,4 @@ registerInstrumentations({ ], }); - console.log('tracing initialized'); diff --git a/package.json b/package.json index 8df33eee95..1622db1e8b 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,8 @@ "lint:fix:changed": "lerna run --concurrency 1 --stream lint:fix --since HEAD --exclude-dependents", "lint:examples": "eslint ./examples/**/*.js", "lint:examples:fix": "eslint ./examples/**/*.js --fix", + "lint:getting-started": "eslint ./getting-started/**/*.{js,ts}", + "lint:getting-started:fix": "eslint ./getting-started/**/*.{js,ts} --fix", "lint:markdown": "./node_modules/.bin/markdownlint $(git ls-files '*.md') -i ./CHANGELOG.md", "lint:markdown:fix": "./node_modules/.bin/markdownlint $(git ls-files '*.md') -i ./CHANGELOG.md --fix", "reset": "lerna clean -y && rm -rf node_modules && npm i && npm run compile && npm run lint:fix"