Skip to content

Commit 943b6d1

Browse files
authored
chore(examples): lint examples/mongodb using shared top-level eslint config (#2906)
Refs: #2891
1 parent e96eb64 commit 943b6d1

File tree

6 files changed

+149
-57
lines changed

6 files changed

+149
-57
lines changed

examples/mongodb/.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/mongodb/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"description": "Example of mongodb 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 -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=127.0.0.1 -p 27017:27017 --rm mongo",
911
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
1012
"zipkin:client": "cross-env EXPORTER=zipkin ts-node src/client.ts",
@@ -35,6 +37,8 @@
3537
"@opentelemetry/instrumentation": "^0.54.2",
3638
"@opentelemetry/instrumentation-http": "^0.54.2",
3739
"@opentelemetry/instrumentation-mongodb": "^0.48.0",
40+
"@opentelemetry/exporter-trace-otlp-http": "^0.54.2",
41+
"@opentelemetry/resources": "^1.27.0",
3842
"@opentelemetry/sdk-trace-node": "^1.27.0",
3943
"@opentelemetry/sdk-trace-base": "^1.27.0",
4044
"@opentelemetry/semantic-conventions": "^1.27.0",

examples/mongodb/src/client.ts

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
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 * as api from '@opentelemetry/api';
4-
import { setupTracing } from './tracer';
5-
6-
const tracer = setupTracing('example-mongodb-http-client')
17+
import * as api from '@opentelemetry/api';
718
import * as http from 'http';
19+
// eslint-disable-next-line import/extensions
20+
import { setupTracing } from './tracer';
821

22+
const tracer = setupTracing('example-mongodb-http-client');
923

1024
/** A function which makes requests and handles response. */
1125
function makeRequest() {
@@ -19,58 +33,71 @@ function makeRequest() {
1933

2034
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
2135
queries += 1;
22-
http.get({
23-
host: 'localhost',
24-
port: 8080,
25-
path: '/collection/',
26-
}, (response) => {
27-
const body: any = [];
28-
response.on('data', (chunk) => body.push(chunk));
29-
response.on('end', () => {
30-
responses += 1;
31-
console.log(body.toString());
32-
if (responses === queries) span.end();
33-
});
34-
});
36+
http.get(
37+
{
38+
host: 'localhost',
39+
port: 8080,
40+
path: '/collection/',
41+
},
42+
response => {
43+
const body: any = [];
44+
response.on('data', chunk => body.push(chunk));
45+
response.on('end', () => {
46+
responses += 1;
47+
console.log(body.toString());
48+
if (responses === queries) span.end();
49+
});
50+
}
51+
);
3552
});
3653
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
3754
queries += 1;
38-
http.get({
39-
host: 'localhost',
40-
port: 8080,
41-
path: '/insert/',
42-
}, (response) => {
43-
const body: any = [];
44-
response.on('data', (chunk) => body.push(chunk));
45-
response.on('end', () => {
46-
responses += 1;
47-
console.log(body.toString());
48-
if (responses === queries) span.end();
49-
});
50-
});
55+
http.get(
56+
{
57+
host: 'localhost',
58+
port: 8080,
59+
path: '/insert/',
60+
},
61+
response => {
62+
const body: any = [];
63+
response.on('data', chunk => body.push(chunk));
64+
response.on('end', () => {
65+
responses += 1;
66+
console.log(body.toString());
67+
if (responses === queries) span.end();
68+
});
69+
}
70+
);
5171
});
5272
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
5373
queries += 1;
54-
http.get({
55-
host: 'localhost',
56-
port: 8080,
57-
path: '/get/',
58-
}, (response) => {
59-
const body: any = [];
60-
response.on('data', (chunk) => body.push(chunk));
61-
response.on('end', () => {
62-
responses += 1;
63-
console.log(body.toString());
64-
if (responses === queries) span.end();
65-
});
66-
});
74+
http.get(
75+
{
76+
host: 'localhost',
77+
port: 8080,
78+
path: '/get/',
79+
},
80+
response => {
81+
const body: any = [];
82+
response.on('data', chunk => body.push(chunk));
83+
response.on('end', () => {
84+
responses += 1;
85+
console.log(body.toString());
86+
if (responses === queries) span.end();
87+
});
88+
}
89+
);
6790
});
6891

6992
// The process must live for at least the interval past any traces that
7093
// must be exported, or some risk being lost if they are recorded after the
7194
// last export.
72-
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
73-
setTimeout(() => { console.log('Completed.'); }, 5000);
95+
console.log(
96+
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
97+
);
98+
setTimeout(() => {
99+
console.log('Completed.');
100+
}, 5000);
74101
}
75102

76103
makeRequest();

examples/mongodb/src/server.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
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+
*/
116
import * as api from '@opentelemetry/api';
217

3-
import { setupTracing } from './tracer';
4-
5-
setupTracing('example-mongodb-server');
6-
7-
import { accessDB } from './utils';
8-
918
import * as http from 'http';
1019
import { IncomingMessage, ServerResponse } from 'http';
1120
import * as mongodb from 'mongodb';
1221
import { Collection } from 'mongodb';
22+
// eslint-disable-next-line import/extensions
23+
import { setupTracing } from './tracer';
24+
// eslint-disable-next-line import/extensions
25+
import { accessDB } from './utils';
26+
27+
setupTracing('example-mongodb-server');
1328

1429
const DB_NAME = 'mydb';
1530
const COLLECTION_NAME = 'users';
@@ -41,7 +56,7 @@ function handleRequest(request: IncomingMessage, response: ServerResponse) {
4156
const currentSpan = api.trace.getSpan(api.context.active());
4257
if (currentSpan) {
4358
// display traceID in the terminal
44-
const { traceId } = currentSpan?.spanContext();
59+
const { traceId } = currentSpan.spanContext();
4560
console.log(`traceid: ${traceId}`);
4661
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
4762
console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`);
@@ -79,9 +94,12 @@ function handleInsertQuery(response: ServerResponse) {
7994
.then(() => {
8095
console.log('1 document inserted');
8196
// find document to test context
82-
usersCollection.findOne({}).then(res => {
83-
console.log(JSON.stringify(res));
84-
});
97+
usersCollection
98+
.findOne({})
99+
.then(res => {
100+
console.log(JSON.stringify(res));
101+
})
102+
.catch(err => console.log(err));
85103
})
86104
.catch(err => {
87105
console.log('Error code:', err.code);

examples/mongodb/src/tracer.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
import * as api from '@opentelemetry/api';
217

318
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
@@ -10,6 +25,7 @@ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
1025
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
1126
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
1227

28+
// eslint-disable-next-line import/prefer-default-export
1329
export const setupTracing = (serviceName: string): api.Tracer => {
1430
const provider = new NodeTracerProvider({
1531
resource: new Resource({

examples/mongodb/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import * as mongodb from 'mongodb';
2222
* @param dbName The mongodb database name.
2323
* @param options The mongodb client config options.
2424
*/
25+
// eslint-disable-next-line import/prefer-default-export
2526
export function accessDB(
2627
url: string,
2728
dbName: string,
2829
options: mongodb.MongoClientOptions = {}
2930
): Promise<mongodb.Db> {
3031
return new Promise((resolve, reject) => {
3132
mongodb.MongoClient.connect(url, {
32-
serverSelectionTimeoutMS: 1000
33+
serverSelectionTimeoutMS: 1000,
3334
})
3435
.then(client => {
3536
resolve(client.db(dbName));

0 commit comments

Comments
 (0)