diff --git a/examples/router/.eslintrc.js b/examples/router/.eslintrc.js new file mode 100644 index 0000000000..53452072e2 --- /dev/null +++ b/examples/router/.eslintrc.js @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const baseConfig = require('../../eslint.config'); + +module.exports = { + ...baseConfig, + env: { + node: true, + }, +}; diff --git a/examples/router/.npmrc b/examples/router/.npmrc new file mode 100644 index 0000000000..43c97e719a --- /dev/null +++ b/examples/router/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/examples/router/client.js b/examples/router/client.js index 9cd52f415a..b5fb684ee7 100644 --- a/examples/router/client.js +++ b/examples/router/client.js @@ -1,3 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + 'use strict'; // required to initialize the service name for the auto-instrumentation @@ -10,25 +26,34 @@ function makeRequest(path) { // span corresponds to outgoing requests. Here, we have manually created // the span, which is created to track work that happens outside of the // request lifecycle entirely. - http.get({ - host: 'localhost', - headers: { - accept: 'text/plain', + http.get( + { + host: 'localhost', + headers: { + accept: 'text/plain', + }, + port: 8080, + path, }, - port: 8080, - path, - }, (response) => { - response.on('data', (chunk) => console.log(path, '::', chunk.toString('utf8'))); - response.on('end', () => { - console.log(path, 'status', response.statusCode); - }); - }); + response => { + response.on('data', chunk => + console.log(path, '::', chunk.toString('utf8')) + ); + response.on('end', () => { + console.log(path, 'status', response.statusCode); + }); + } + ); // The process must live for at least the interval past any traces that // must be exported, or some risk being lost if they are recorded after the // last export. - console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); - setTimeout(() => { console.log('Completed.'); }, 5000); + console.log( + 'Sleeping 5 seconds before shutdown to ensure all records are flushed.' + ); + setTimeout(() => { + console.log('Completed.'); + }, 5000); } makeRequest('/hello/world'); diff --git a/examples/router/package.json b/examples/router/package.json index 2914f86b3f..5a2a9e22fc 100644 --- a/examples/router/package.json +++ b/examples/router/package.json @@ -5,6 +5,8 @@ "description": "Example of router integration with OpenTelemetry", "main": "index.js", "scripts": { + "lint": "eslint . --ext=ts,js,mjs", + "lint:fix": "eslint . --ext=ts,js,mjs --fix", "zipkin:server": "cross-env EXPORTER=zipkin node ./server.js", "zipkin:client": "cross-env EXPORTER=zipkin node ./client.js", "jaeger:server": "cross-env EXPORTER=jaeger node ./server.js", diff --git a/examples/router/server.js b/examples/router/server.js index 8e06630d1e..f234ce7eb5 100644 --- a/examples/router/server.js +++ b/examples/router/server.js @@ -1,3 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + 'use strict'; require('./tracer')('example-router-server'); @@ -16,7 +32,8 @@ const router = Router(); router.use(setDefaultName); router.param('name', (req, res, next, name) => { - req.params.name = typeof name === 'string' ? name.toUpperCase() : req.defaultName; + req.params.name = + typeof name === 'string' ? name.toUpperCase() : req.defaultName; next(); }); @@ -33,7 +50,7 @@ router.get('/err', function erroringRoute(req, res, next) { // eslint-disable-next-line prefer-arrow-callback, func-names const server = http.createServer(function (req, res) { - router(req, res, (error) => { + router(req, res, error => { if (error) { res.statusCode = 500; } else { diff --git a/examples/router/tracer.js b/examples/router/tracer.js index 73ee585060..cbc77e5e36 100644 --- a/examples/router/tracer.js +++ b/examples/router/tracer.js @@ -1,3 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + 'use strict'; const opentelemetry = require('@opentelemetry/api'); @@ -7,14 +23,19 @@ diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.VERBOSE); const { registerInstrumentations } = require('@opentelemetry/instrumentation'); const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); -const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base'); +const { + SimpleSpanProcessor, + ConsoleSpanExporter, +} = require('@opentelemetry/sdk-trace-base'); const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); -const { RouterInstrumentation } = require('@opentelemetry/instrumentation-router'); +const { + RouterInstrumentation, +} = require('@opentelemetry/instrumentation-router'); -const Exporter = ((exporterParam) => { +const Exporter = (exporterParam => { if (typeof exporterParam === 'string') { const exporterString = exporterParam.toLowerCase(); if (exporterString.startsWith('z')) { @@ -27,23 +48,18 @@ const Exporter = ((exporterParam) => { return ConsoleSpanExporter; })(process.env.EXPORTER); -module.exports = (serviceName) => { +module.exports = serviceName => { const exporter = new Exporter({ serviceName, }); const provider = new NodeTracerProvider({ - spanProcessors: [ - new SimpleSpanProcessor(exporter), - ], + spanProcessors: [new SimpleSpanProcessor(exporter)], }); registerInstrumentations({ tracerProvider: provider, - instrumentations: [ - HttpInstrumentation, - RouterInstrumentation, - ], + instrumentations: [HttpInstrumentation, RouterInstrumentation], }); // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings