Skip to content

Commit e63b4df

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

File tree

5 files changed

+196
-102
lines changed

5 files changed

+196
-102
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"description": "Example of mysql 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 -p 3306:3306 --name example-mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql:5.7",
911
"docker:stop": "docker stop example-mysql && docker rm example-mysql",
1012
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
@@ -35,6 +37,8 @@
3537
"@opentelemetry/instrumentation": "^0.48.0",
3638
"@opentelemetry/instrumentation-http": "^0.48.0",
3739
"@opentelemetry/instrumentation-mysql": "^0.31.0",
40+
"@opentelemetry/resources": "^1.0.0",
41+
"@opentelemetry/sdk-metrics": "^1.0.0",
3842
"@opentelemetry/sdk-trace-base": "^1.0.0",
3943
"@opentelemetry/sdk-trace-node": "^1.0.0",
4044
"@opentelemetry/semantic-conventions": "^1.27.0",

examples/mysql/src/client.ts

Lines changed: 105 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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

317
import * as api from '@opentelemetry/api';
4-
import { setupTracing } from "./tracer";
5-
const tracer = setupTracing('example-mysql-client');
618
import * as http from 'http';
19+
// eslint-disable-next-line import/extensions
20+
import { setupTracing } from './tracer';
21+
22+
const tracer = setupTracing('example-mysql-client');
723

824
/** A function which makes requests and handles response. */
925
function makeRequest() {
@@ -17,90 +33,109 @@ function makeRequest() {
1733

1834
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
1935
queries += 1;
20-
http.get({
21-
host: 'localhost',
22-
port: 8080,
23-
path: '/connection/query',
24-
}, (response) => {
25-
const body: any[] = [];
26-
response.on('data', (chunk) => body.push(chunk));
27-
response.on('end', () => {
28-
responses += 1;
29-
console.log(body.toString());
30-
if (responses === queries) span.end();
31-
});
32-
});
36+
http.get(
37+
{
38+
host: 'localhost',
39+
port: 8080,
40+
path: '/connection/query',
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+
);
3352
});
3453
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
3554
queries += 1;
36-
http.get({
37-
host: 'localhost',
38-
port: 8080,
39-
path: '/pool/query',
40-
}, (response) => {
41-
const body: any[] = [];
42-
response.on('data', (chunk) => body.push(chunk));
43-
response.on('end', () => {
44-
responses += 1;
45-
console.log(body.toString());
46-
if (responses === queries) span.end();
47-
});
48-
});
55+
http.get(
56+
{
57+
host: 'localhost',
58+
port: 8080,
59+
path: '/pool/query',
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+
);
4971
});
5072
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
5173
queries += 1;
52-
http.get({
53-
host: 'localhost',
54-
port: 8080,
55-
path: '/pool/query-with-2-connections',
56-
}, (response) => {
57-
const body: any[] = [];
58-
response.on('data', (chunk) => body.push(chunk));
59-
response.on('end', () => {
60-
responses += 1;
61-
console.log(body.toString());
62-
if (responses === queries) span.end();
63-
});
64-
});
74+
http.get(
75+
{
76+
host: 'localhost',
77+
port: 8080,
78+
path: '/pool/query-with-2-connections',
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+
);
6590
});
6691
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
6792
queries += 1;
68-
http.get({
69-
host: 'localhost',
70-
port: 8080,
71-
path: '/pool/query-2-pools',
72-
}, (response) => {
73-
const body: any[] = [];
74-
response.on('data', (chunk) => body.push(chunk));
75-
response.on('end', () => {
76-
responses += 1;
77-
console.log(body.toString());
78-
if (responses === queries) span.end();
79-
});
80-
});
93+
http.get(
94+
{
95+
host: 'localhost',
96+
port: 8080,
97+
path: '/pool/query-2-pools',
98+
},
99+
response => {
100+
const body: any[] = [];
101+
response.on('data', chunk => body.push(chunk));
102+
response.on('end', () => {
103+
responses += 1;
104+
console.log(body.toString());
105+
if (responses === queries) span.end();
106+
});
107+
}
108+
);
81109
});
82110
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
83111
queries += 1;
84-
http.get({
85-
host: 'localhost',
86-
port: 8080,
87-
path: '/cluster/query',
88-
}, (response) => {
89-
const body: any[] = [];
90-
response.on('data', (chunk) => body.push(chunk));
91-
response.on('end', () => {
92-
responses += 1;
93-
console.log(body.toString());
94-
if (responses === queries) span.end();
95-
});
96-
});
112+
http.get(
113+
{
114+
host: 'localhost',
115+
port: 8080,
116+
path: '/cluster/query',
117+
},
118+
response => {
119+
const body: any[] = [];
120+
response.on('data', chunk => body.push(chunk));
121+
response.on('end', () => {
122+
responses += 1;
123+
console.log(body.toString());
124+
if (responses === queries) span.end();
125+
});
126+
}
127+
);
97128
});
98129

99130
// The process must live for at least the interval past any traces that
100131
// must be exported, or some risk being lost if they are recorded after the
101132
// last export.
102-
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
103-
setTimeout(() => { console.log('Completed.'); }, 5000);
133+
console.log(
134+
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
135+
);
136+
setTimeout(() => {
137+
console.log('Completed.');
138+
}, 5000);
104139
}
105140

106141
makeRequest();

examples/mysql/src/server.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
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-
// eslint-disable-next-line import/order
4-
import { setupTracing } from "./tracer";
5-
setupTracing('example-mysql-server');
617
import * as api from '@opentelemetry/api';
7-
import * as mysql from 'mysql'
18+
import * as mysql from 'mysql';
819
import * as http from 'http';
9-
import { MysqlError } from "mysql";
10-
import { PoolConnection } from "mysql";
20+
import { MysqlError, PoolConnection } from 'mysql';
21+
// eslint-disable-next-line import/extensions
22+
import { setupTracing } from './tracer';
23+
24+
setupTracing('example-mysql-server');
1125

1226
const pool = mysql.createPool({
1327
host: 'localhost',
@@ -19,7 +33,7 @@ const pool2 = mysql.createPool({
1933
host: 'localhost',
2034
user: 'root',
2135
password: 'secret',
22-
database: 'db_test' //this db is created by init.sql
36+
database: 'db_test', // this db is created by init.sql
2337
});
2438

2539
const connection = mysql.createConnection({
@@ -48,27 +62,23 @@ function startServer(port: number | undefined) {
4862

4963
/** A function which handles requests and send response. */
5064
function handleRequest(request: any, response: any) {
51-
const currentSpan = api.trace.getSpan(api.context.active())
65+
const currentSpan = api.trace.getSpan(api.context.active());
5266
// display traceid in the terminal
5367
const traceId = currentSpan?.spanContext().traceId;
5468
console.log(`traceid: ${traceId}`);
5569
console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`);
5670
try {
5771
const body = [];
58-
request.on('error',
59-
(err: any) => console.log(err)
60-
);
61-
request.on('data',
62-
(chunk: any) => body.push(chunk)
63-
);
72+
request.on('error', (err: any) => console.log(err));
73+
request.on('data', (chunk: any) => body.push(chunk));
6474
request.on('end', () => {
6575
if (request.url === '/connection/query') {
6676
handleConnectionQuery(response);
6777
} else if (request.url === '/pool/query') {
6878
handlePoolQuery(response);
69-
} else if(request.url === '/pool/query-with-2-connections') {
79+
} else if (request.url === '/pool/query-with-2-connections') {
7080
handlePoolwith2ConnectionsQuery(response);
71-
} else if(request.url === '/pool/query-2-pools') {
81+
} else if (request.url === '/pool/query-2-pools') {
7282
handle2PoolsQuery(response);
7383
} else if (request.url === '/cluster/query') {
7484
handleClusterQuery(response);
@@ -143,7 +153,7 @@ function handle2PoolsQuery(response: any) {
143153
});
144154
}
145155

146-
function handlePoolwith2ConnectionsQuery(response: any){
156+
function handlePoolwith2ConnectionsQuery(response: any) {
147157
const query = 'SELECT 1 + 1 as pool_2_connections_solution';
148158
pool.getConnection((connErr: MysqlError, conn: PoolConnection) => {
149159
if (connErr) {
@@ -159,7 +169,7 @@ function handlePoolwith2ConnectionsQuery(response: any){
159169
const res = results[0].pool_2_connections_solution;
160170
response.write(`${query} 1: ${res}`);
161171

162-
//finish with the 1st connection. create another one, then end() both.
172+
// finish with the 1st connection. create another one, then end() both.
163173
const query2 = `SELECT ${res} + ${res} as pool_2_connections_solution`;
164174
pool.getConnection((connErr: MysqlError, conn2: PoolConnection) => {
165175
if (connErr) {
@@ -172,7 +182,9 @@ function handlePoolwith2ConnectionsQuery(response: any){
172182
console.log('Error code 2:', err.code);
173183
response.end(err.message);
174184
} else {
175-
response.end(`${query2} 2: ${results[0].pool_2_connections_solution}`);
185+
response.end(
186+
`${query2} 2: ${results[0].pool_2_connections_solution}`
187+
);
176188
pool.end();
177189
}
178190
});

0 commit comments

Comments
 (0)