From aca803c076dbd3fc79a1364a2803a8003be53cda Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 26 Jun 2025 11:39:37 -0700 Subject: [PATCH] chore(examples): lint examples/redis using shared top-level eslint config Refs: https://github.com/open-telemetry/opentelemetry-js-contrib/issues/2891 --- examples/redis/.eslintrc.js | 26 ++++++++ examples/redis/package.json | 3 + examples/redis/src/client.ts | 62 +++++++++++++------ examples/redis/src/express-tracer-handlers.ts | 28 +++++++-- examples/redis/src/server.ts | 45 ++++++++++---- examples/redis/src/setup-redis.ts | 27 +++++--- examples/redis/src/tracer.ts | 26 +++++--- 7 files changed, 165 insertions(+), 52 deletions(-) create mode 100644 examples/redis/.eslintrc.js diff --git a/examples/redis/.eslintrc.js b/examples/redis/.eslintrc.js new file mode 100644 index 0000000000..53452072e2 --- /dev/null +++ b/examples/redis/.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/redis/package.json b/examples/redis/package.json index 70957a4cf8..a76b6b8dc2 100644 --- a/examples/redis/package.json +++ b/examples/redis/package.json @@ -5,6 +5,8 @@ "description": "Example of Redis integration with OpenTelemetry", "main": "index.js", "scripts": { + "lint": "eslint . --ext=ts,js,mjs", + "lint:fix": "eslint . --ext=ts,js,mjs --fix", "docker:start": "docker run -d -p 6379:6379 --name otel-redis redis:alpine", "docker:stop": "docker stop otel-redis && docker rm otel-redis", "zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts", @@ -37,6 +39,7 @@ "@opentelemetry/instrumentation": "^0.48.0", "@opentelemetry/instrumentation-http": "^0.48.0", "@opentelemetry/instrumentation-redis": "^0.32.0", + "@opentelemetry/resources": "^1.0.0", "@opentelemetry/sdk-trace-base": "^1.0.0", "@opentelemetry/sdk-trace-node": "^1.0.0", "@opentelemetry/semantic-conventions": "^1.27.0", diff --git a/examples/redis/src/client.ts b/examples/redis/src/client.ts index 4f83690393..0063f6a460 100644 --- a/examples/redis/src/client.ts +++ b/examples/redis/src/client.ts @@ -1,29 +1,55 @@ -'use strict'; +/* + * 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. + */ -import { setupTracing } from "./tracer"; -const tracer = setupTracing('example-redis-client'); import * as api from '@opentelemetry/api'; -import { default as axios } from 'axios'; +import * as axios from 'axios'; +// eslint-disable-next-line import/extensions +import { setupTracing } from './tracer'; + +const tracer = setupTracing('example-redis-client'); -function makeRequest() { +async function makeRequest() { const span = tracer.startSpan('client.makeRequest()', { kind: api.SpanKind.CLIENT, }); - api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), async () => { - try { - const res = await axios.get('http://localhost:8080/run_test'); - span.setStatus({ code: api.SpanStatusCode.OK }); - console.log(res.statusText); - } catch (e) { - if(e instanceof Error) { - span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message }); + await api.context.with( + api.trace.setSpan(api.ROOT_CONTEXT, span), + async () => { + try { + const res = await axios.get('http://localhost:8080/run_test'); + span.setStatus({ code: api.SpanStatusCode.OK }); + console.log(res.statusText); + } catch (e) { + if (e instanceof Error) { + span.setStatus({ + code: api.SpanStatusCode.ERROR, + message: e.message, + }); + } } + span.end(); + console.log( + 'Sleeping 5 seconds before shutdown to ensure all records are flushed.' + ); + setTimeout(() => { + console.log('Completed.'); + }, 5000); } - span.end(); - console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); - setTimeout(() => { console.log('Completed.'); }, 5000); - }); + ); } -makeRequest(); +makeRequest().catch(err => console.log(err)); diff --git a/examples/redis/src/express-tracer-handlers.ts b/examples/redis/src/express-tracer-handlers.ts index 8caed1357b..ad05d8314c 100644 --- a/examples/redis/src/express-tracer-handlers.ts +++ b/examples/redis/src/express-tracer-handlers.ts @@ -1,12 +1,29 @@ -'use strict'; +/* + * 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. + */ import * as api from '@opentelemetry/api'; export function getMiddlewareTracer(tracer: api.Tracer) { return (req: any, res: any, next: any) => { - const span = tracer.startSpan(`express.middleware.tracer(${req.method} ${req.path})`, { - kind: api.SpanKind.SERVER, - }); + const span = tracer.startSpan( + `express.middleware.tracer(${req.method} ${req.path})`, + { + kind: api.SpanKind.SERVER, + } + ); // End this span before sending out the response const originalSend = res.send; @@ -22,7 +39,7 @@ export function getMiddlewareTracer(tracer: api.Tracer) { export function getErrorTracer(tracer: api.Tracer) { return (err: any, _req: any, res: any, _next: any) => { console.error('Caught error', err.message); - const span = api.trace.getSpan(api.context.active()) + const span = api.trace.getSpan(api.context.active()); if (span) { span.setStatus({ code: api.SpanStatusCode.ERROR, message: err.message }); @@ -30,4 +47,3 @@ export function getErrorTracer(tracer: api.Tracer) { res.status(500).send(err.message); }; } - diff --git a/examples/redis/src/server.ts b/examples/redis/src/server.ts index b3a5c1c936..b9d1085cd7 100644 --- a/examples/redis/src/server.ts +++ b/examples/redis/src/server.ts @@ -1,14 +1,31 @@ -'use strict'; - -import { setupTracing } from './tracer' -const tracer = setupTracing('example-redis-server'); +/* + * 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. + */ // Require in rest of modules import * as express from 'express'; -import axios from 'axios'; -import * as tracerHandlers from './express-tracer-handlers'; +import * as axios from 'axios'; import { randomBytes } from 'crypto'; -const redisPromise = require('./setup-redis').redis; +// eslint-disable-next-line import/extensions +import { setupTracing } from './tracer'; +// eslint-disable-next-line import/extensions +import * as tracerHandlers from './express-tracer-handlers'; + +const tracer = setupTracing('example-redis-server'); +// eslint-disable-next-line import/extensions +const { redisPromise } = require('./setup-redis'); // Setup express const app = express(); @@ -32,7 +49,7 @@ async function setupRoutes() { } }); - app.get('/:cmd', (req: any , res: any) => { + app.get('/:cmd', (req: any, res: any) => { if (!req.query.args) { res.status(400).send('No args provided'); return; @@ -54,8 +71,10 @@ async function setupRoutes() { // Setup express routes & middleware app.use(tracerHandlers.getMiddlewareTracer(tracer)); -setupRoutes().then(() => { - app.use(tracerHandlers.getErrorTracer(tracer)); - app.listen(PORT); - console.log(`Listening on http://localhost:${PORT}`); -}); +setupRoutes() + .then(() => { + app.use(tracerHandlers.getErrorTracer(tracer)); + app.listen(PORT); + console.log(`Listening on http://localhost:${PORT}`); + }) + .catch(err => console.log(err)); diff --git a/examples/redis/src/setup-redis.ts b/examples/redis/src/setup-redis.ts index 7c8984aaed..b7568f9c84 100644 --- a/examples/redis/src/setup-redis.ts +++ b/examples/redis/src/setup-redis.ts @@ -1,15 +1,28 @@ -'use strict'; +/* + * 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. + */ -import {createClient} from 'redis'; +import { createClient } from 'redis'; const client = createClient('redis://localhost:6379'); -const redisPromise = new Promise(((resolve, reject) => { +// eslint-disable-next-line import/prefer-default-export +export const redisPromise = new Promise((resolve, reject) => { client.once('ready', () => { resolve(client); }); - client.once('error', (error) => { + client.once('error', error => { reject(error); }); -})); - -exports.redis = redisPromise; +}); diff --git a/examples/redis/src/tracer.ts b/examples/redis/src/tracer.ts index 93d09a788f..e5f3c42c4d 100644 --- a/examples/redis/src/tracer.ts +++ b/examples/redis/src/tracer.ts @@ -1,4 +1,18 @@ -'use strict'; +/* + * 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. + */ import * as api from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; @@ -13,6 +27,7 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; const EXPORTER = process.env.EXPORTER || ''; +// eslint-disable-next-line import/prefer-default-export export const setupTracing = (serviceName: string) => { let exporter; if (EXPORTER.toLowerCase().startsWith('z')) { @@ -25,19 +40,14 @@ export const setupTracing = (serviceName: string) => { resource: new Resource({ [ATTR_SERVICE_NAME]: serviceName, }), - spanProcessors: [ - new SimpleSpanProcessor(exporter), - ] + spanProcessors: [new SimpleSpanProcessor(exporter)], }); // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); registerInstrumentations({ - instrumentations: [ - new HttpInstrumentation(), - new RedisInstrumentation(), - ], + instrumentations: [new HttpInstrumentation(), new RedisInstrumentation()], tracerProvider: provider, });