Skip to content

Commit c8d0991

Browse files
authored
chore(examples): lint examples/redis using shared top-level eslint config (#2909)
Refs: #2891
1 parent ec05de0 commit c8d0991

File tree

7 files changed

+165
-52
lines changed

7 files changed

+165
-52
lines changed

examples/redis/.eslintrc.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const baseConfig = require('../../eslint.config');
20+
21+
module.exports = {
22+
...baseConfig,
23+
env: {
24+
node: true,
25+
},
26+
};

examples/redis/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"description": "Example of Redis integration with OpenTelemetry",
66
"main": "index.js",
77
"scripts": {
8+
"lint": "eslint . --ext=ts,js,mjs",
9+
"lint:fix": "eslint . --ext=ts,js,mjs --fix",
810
"docker:start": "docker run -d -p 6379:6379 --name otel-redis redis:alpine",
911
"docker:stop": "docker stop otel-redis && docker rm otel-redis",
1012
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
@@ -37,6 +39,7 @@
3739
"@opentelemetry/instrumentation": "^0.48.0",
3840
"@opentelemetry/instrumentation-http": "^0.48.0",
3941
"@opentelemetry/instrumentation-redis": "^0.32.0",
42+
"@opentelemetry/resources": "^1.0.0",
4043
"@opentelemetry/sdk-trace-base": "^1.0.0",
4144
"@opentelemetry/sdk-trace-node": "^1.0.0",
4245
"@opentelemetry/semantic-conventions": "^1.27.0",

examples/redis/src/client.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
1-
'use strict';
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

3-
import { setupTracing } from "./tracer";
4-
const tracer = setupTracing('example-redis-client');
517
import * as api from '@opentelemetry/api';
6-
import { default as axios } from 'axios';
18+
import * as axios from 'axios';
19+
// eslint-disable-next-line import/extensions
20+
import { setupTracing } from './tracer';
21+
22+
const tracer = setupTracing('example-redis-client');
723

8-
function makeRequest() {
24+
async function makeRequest() {
925
const span = tracer.startSpan('client.makeRequest()', {
1026
kind: api.SpanKind.CLIENT,
1127
});
1228

13-
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), async () => {
14-
try {
15-
const res = await axios.get('http://localhost:8080/run_test');
16-
span.setStatus({ code: api.SpanStatusCode.OK });
17-
console.log(res.statusText);
18-
} catch (e) {
19-
if(e instanceof Error) {
20-
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message });
29+
await api.context.with(
30+
api.trace.setSpan(api.ROOT_CONTEXT, span),
31+
async () => {
32+
try {
33+
const res = await axios.get('http://localhost:8080/run_test');
34+
span.setStatus({ code: api.SpanStatusCode.OK });
35+
console.log(res.statusText);
36+
} catch (e) {
37+
if (e instanceof Error) {
38+
span.setStatus({
39+
code: api.SpanStatusCode.ERROR,
40+
message: e.message,
41+
});
42+
}
2143
}
44+
span.end();
45+
console.log(
46+
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
47+
);
48+
setTimeout(() => {
49+
console.log('Completed.');
50+
}, 5000);
2251
}
23-
span.end();
24-
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
25-
setTimeout(() => { console.log('Completed.'); }, 5000);
26-
});
52+
);
2753
}
2854

29-
makeRequest();
55+
makeRequest().catch(err => console.log(err));

examples/redis/src/express-tracer-handlers.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
'use strict';
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

317
import * as api from '@opentelemetry/api';
418

519
export function getMiddlewareTracer(tracer: api.Tracer) {
620
return (req: any, res: any, next: any) => {
7-
const span = tracer.startSpan(`express.middleware.tracer(${req.method} ${req.path})`, {
8-
kind: api.SpanKind.SERVER,
9-
});
21+
const span = tracer.startSpan(
22+
`express.middleware.tracer(${req.method} ${req.path})`,
23+
{
24+
kind: api.SpanKind.SERVER,
25+
}
26+
);
1027

1128
// End this span before sending out the response
1229
const originalSend = res.send;
@@ -22,12 +39,11 @@ export function getMiddlewareTracer(tracer: api.Tracer) {
2239
export function getErrorTracer(tracer: api.Tracer) {
2340
return (err: any, _req: any, res: any, _next: any) => {
2441
console.error('Caught error', err.message);
25-
const span = api.trace.getSpan(api.context.active())
42+
const span = api.trace.getSpan(api.context.active());
2643

2744
if (span) {
2845
span.setStatus({ code: api.SpanStatusCode.ERROR, message: err.message });
2946
}
3047
res.status(500).send(err.message);
3148
};
3249
}
33-

examples/redis/src/server.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
'use strict';
2-
3-
import { setupTracing } from './tracer'
4-
const tracer = setupTracing('example-redis-server');
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
516

617
// Require in rest of modules
718
import * as express from 'express';
8-
import axios from 'axios';
9-
import * as tracerHandlers from './express-tracer-handlers';
19+
import * as axios from 'axios';
1020
import { randomBytes } from 'crypto';
11-
const redisPromise = require('./setup-redis').redis;
21+
// eslint-disable-next-line import/extensions
22+
import { setupTracing } from './tracer';
23+
// eslint-disable-next-line import/extensions
24+
import * as tracerHandlers from './express-tracer-handlers';
25+
26+
const tracer = setupTracing('example-redis-server');
27+
// eslint-disable-next-line import/extensions
28+
const { redisPromise } = require('./setup-redis');
1229

1330
// Setup express
1431
const app = express();
@@ -32,7 +49,7 @@ async function setupRoutes() {
3249
}
3350
});
3451

35-
app.get('/:cmd', (req: any , res: any) => {
52+
app.get('/:cmd', (req: any, res: any) => {
3653
if (!req.query.args) {
3754
res.status(400).send('No args provided');
3855
return;
@@ -54,8 +71,10 @@ async function setupRoutes() {
5471

5572
// Setup express routes & middleware
5673
app.use(tracerHandlers.getMiddlewareTracer(tracer));
57-
setupRoutes().then(() => {
58-
app.use(tracerHandlers.getErrorTracer(tracer));
59-
app.listen(PORT);
60-
console.log(`Listening on http://localhost:${PORT}`);
61-
});
74+
setupRoutes()
75+
.then(() => {
76+
app.use(tracerHandlers.getErrorTracer(tracer));
77+
app.listen(PORT);
78+
console.log(`Listening on http://localhost:${PORT}`);
79+
})
80+
.catch(err => console.log(err));

examples/redis/src/setup-redis.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
'use strict';
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

3-
import {createClient} from 'redis';
17+
import { createClient } from 'redis';
418

519
const client = createClient('redis://localhost:6379');
6-
const redisPromise = new Promise(((resolve, reject) => {
20+
// eslint-disable-next-line import/prefer-default-export
21+
export const redisPromise = new Promise((resolve, reject) => {
722
client.once('ready', () => {
823
resolve(client);
924
});
10-
client.once('error', (error) => {
25+
client.once('error', error => {
1126
reject(error);
1227
});
13-
}));
14-
15-
exports.redis = redisPromise;
28+
});

examples/redis/src/tracer.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
'use strict';
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

317
import * as api from '@opentelemetry/api';
418
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
@@ -13,6 +27,7 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
1327

1428
const EXPORTER = process.env.EXPORTER || '';
1529

30+
// eslint-disable-next-line import/prefer-default-export
1631
export const setupTracing = (serviceName: string) => {
1732
let exporter;
1833
if (EXPORTER.toLowerCase().startsWith('z')) {
@@ -25,19 +40,14 @@ export const setupTracing = (serviceName: string) => {
2540
resource: new Resource({
2641
[ATTR_SERVICE_NAME]: serviceName,
2742
}),
28-
spanProcessors: [
29-
new SimpleSpanProcessor(exporter),
30-
]
43+
spanProcessors: [new SimpleSpanProcessor(exporter)],
3144
});
3245

3346
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
3447
provider.register();
3548

3649
registerInstrumentations({
37-
instrumentations: [
38-
new HttpInstrumentation(),
39-
new RedisInstrumentation(),
40-
],
50+
instrumentations: [new HttpInstrumentation(), new RedisInstrumentation()],
4151
tracerProvider: provider,
4252
});
4353

0 commit comments

Comments
 (0)