Skip to content

Commit daa2261

Browse files
authored
chore(examples): lint examples/express using shared top-level eslint config (#2894)
Refs: #2891
1 parent 2819660 commit daa2261

File tree

5 files changed

+153
-46
lines changed

5 files changed

+153
-46
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"version": "0.1.0",
55
"description": "Example of Express integration with OpenTelemetry",
66
"scripts": {
7+
"lint": "eslint . --ext=ts,js,mjs",
8+
"lint:fix": "eslint . --ext=ts,js,mjs --fix",
79
"server": "ts-node src/server.ts",
810
"client": "ts-node src/client.ts",
911
"compile": "tsc -p ."

examples/express/src/client.ts

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,57 @@
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-
const tracer = setupTracing('example-express-client');
17+
// eslint-disable-next-line import/order, import/extensions
18+
import { setupTracing } from './tracer';
619

720
import * as api from '@opentelemetry/api';
8-
import { default as axios } from 'axios';
21+
import * as axios from 'axios';
22+
23+
const tracer = setupTracing('example-express-client');
924

10-
function makeRequest() {
25+
async function makeRequest() {
1126
const span = tracer.startSpan('client.makeRequest()', {
1227
kind: api.SpanKind.CLIENT,
1328
});
1429

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

32-
makeRequest();
57+
makeRequest().catch(err => console.log(err));

examples/express/src/server.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
import { setupTracing } from './tracer'
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-
setupTracing('example-express-server');
17+
// eslint-disable-next-line import/order, import/extensions
18+
import { setupTracing } from './tracer';
419

520
// Require in rest of modules
621
import * as express from 'express';
7-
import { default as axios } from 'axios';
8-
import { RequestHandler } from "express";
22+
import * as axios from 'axios';
23+
import { RequestHandler } from 'express';
24+
25+
setupTracing('example-express-server');
926

1027
// Setup express
1128
const app = express();
@@ -32,19 +49,21 @@ const authMiddleware: RequestHandler = (req, res, next) => {
3249
};
3350

3451
app.use(express.json());
35-
app.get('/health', (req, res) => res.status(200).send("HEALTHY")); // endpoint that is called by framework/cluster
52+
app.get('/health', (req, res) => res.status(200).send('HEALTHY')); // endpoint that is called by framework/cluster
3653
app.get('/run_test', async (req, res) => {
3754
// Calls another endpoint of the same API, somewhat mimicking an external API call
38-
const createdCat = await axios.post(`http://localhost:${PORT}/cats`, {
39-
name: 'Tom',
40-
friends: [
41-
'Jerry',
42-
],
43-
}, {
44-
headers: {
45-
Authorization: 'secret_token',
55+
const createdCat = await axios.post(
56+
`http://localhost:${PORT}/cats`,
57+
{
58+
name: 'Tom',
59+
friends: ['Jerry'],
4660
},
47-
});
61+
{
62+
headers: {
63+
Authorization: 'secret_token',
64+
},
65+
}
66+
);
4867

4968
return res.status(201).send(createdCat.data);
5069
});

examples/express/src/tracer.ts

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
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 { trace, SamplingDecision, SpanKind, Attributes } from '@opentelemetry/api';
17+
import {
18+
trace,
19+
SamplingDecision,
20+
SpanKind,
21+
Attributes,
22+
} from '@opentelemetry/api';
423
import { registerInstrumentations } from '@opentelemetry/instrumentation';
524
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
6-
import { Sampler, AlwaysOnSampler, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
25+
import {
26+
Sampler,
27+
AlwaysOnSampler,
28+
SimpleSpanProcessor,
29+
} from '@opentelemetry/sdk-trace-base';
730
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
831
import { Resource } from '@opentelemetry/resources';
9-
import { ATTR_SERVICE_NAME, ATTR_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
32+
import {
33+
ATTR_SERVICE_NAME,
34+
ATTR_HTTP_ROUTE,
35+
} from '@opentelemetry/semantic-conventions';
1036
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
1137
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
1238

39+
// eslint-disable-next-line import/prefer-default-export
1340
export const setupTracing = (serviceName: string) => {
1441
const exporter = new OTLPTraceExporter({});
1542
const provider = new NodeTracerProvider({
1643
resource: new Resource({
1744
[ATTR_SERVICE_NAME]: serviceName,
1845
}),
19-
spanProcessors: [
20-
new SimpleSpanProcessor(exporter),
21-
],
46+
spanProcessors: [new SimpleSpanProcessor(exporter)],
2247
sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()),
2348
});
2449
registerInstrumentations({
@@ -36,7 +61,11 @@ export const setupTracing = (serviceName: string) => {
3661
return trace.getTracer(serviceName);
3762
};
3863

39-
type FilterFunction = (spanName: string, spanKind: SpanKind, attributes: Attributes) => boolean;
64+
type FilterFunction = (
65+
spanName: string,
66+
spanKind: SpanKind,
67+
attributes: Attributes
68+
) => boolean;
4069

4170
function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
4271
return {
@@ -48,10 +77,16 @@ function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
4877
},
4978
toString() {
5079
return `FilterSampler(${parent.toString()})`;
51-
}
52-
}
80+
},
81+
};
5382
}
5483

55-
function ignoreHealthCheck(spanName: string, spanKind: SpanKind, attributes: Attributes) {
56-
return spanKind !== SpanKind.SERVER || attributes[ATTR_HTTP_ROUTE] !== "/health";
84+
function ignoreHealthCheck(
85+
spanName: string,
86+
spanKind: SpanKind,
87+
attributes: Attributes
88+
) {
89+
return (
90+
spanKind !== SpanKind.SERVER || attributes[ATTR_HTTP_ROUTE] !== '/health'
91+
);
5792
}

0 commit comments

Comments
 (0)