Skip to content

Commit 5f1b79e

Browse files
authored
chore(examples): lint examples/koa using shared top-level eslint config (#2903)
Refs: #2891
1 parent 196b7a1 commit 5f1b79e

File tree

5 files changed

+129
-45
lines changed

5 files changed

+129
-45
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"version": "0.1.0",
55
"description": "Example of Koa and @koa/router integration with OpenTelemetry",
66
"scripts": {
7+
"lint": "eslint . --ext=ts,js,mjs",
8+
"lint:fix": "eslint . --ext=ts,js,mjs --fix",
79
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
810
"zipkin:client": "cross-env EXPORTER=zipkin ts-node src/client.ts",
911
"jaeger:server": "cross-env EXPORTER=jaeger 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-koa": "^0.31.0",
42+
"@opentelemetry/resources": "^1.0.0",
4043
"@opentelemetry/sdk-trace-node": "^1.0.0",
4144
"@opentelemetry/sdk-trace-base": "^1.0.0",
4245
"@opentelemetry/semantic-conventions": "^1.27.0",

examples/koa/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-koa-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-koa-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:8081/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:8081/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/koa/src/server.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
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-
setupTracing('example-koa-server');
618

719
// Adding Koa router (if desired)
8-
import * as Router from "@koa/router";
9-
import * as Koa from "koa"
20+
import * as Router from '@koa/router';
21+
import * as Koa from 'koa';
22+
// eslint-disable-next-line import/extensions
23+
import { setupTracing } from './tracer';
1024

25+
setupTracing('example-koa-server');
1126

1227
// Setup koa
1328
const app = new Koa();
1429
const PORT = 8081;
1530
const router = new Router();
1631

1732
// route definitions
18-
router.get('/run_test', runTest)
33+
router
34+
.get('/run_test', runTest)
1935
.get('/post/new', addPost)
2036
.get('/post/:id', showNewPost);
2137

@@ -26,15 +42,15 @@ async function setUp() {
2642

2743
/**
2844
* Router functions: list, add, or show posts
29-
*/
45+
*/
3046
const posts = ['post 0', 'post 1', 'post 2'];
3147

3248
function addPost(ctx: Koa.Context) {
3349
const newPostId = posts.length;
3450
posts.push(`post ${newPostId}`);
3551
const currentSpan = api.trace.getSpan(api.context.active());
3652
currentSpan?.addEvent('Added post');
37-
currentSpan?.setAttribute('post.id', newPostId)
53+
currentSpan?.setAttribute('post.id', newPostId);
3854
ctx.body = `Added post: ${posts[posts.length - 1]}`;
3955
ctx.redirect('/post/3');
4056
}
@@ -45,14 +61,16 @@ async function showNewPost(ctx: Koa.Context) {
4561
const post = posts[id];
4662
if (!post) ctx.throw(404, 'Invalid post id');
4763
const syntheticDelay = 500;
48-
await new Promise((r) => setTimeout(r, syntheticDelay));
64+
await new Promise(r => {
65+
setTimeout(r, syntheticDelay);
66+
});
4967
ctx.body = post;
5068
}
5169

5270
function runTest(ctx: Koa.Context) {
5371
console.log('runTest');
5472
const currentSpan = api.trace.getSpan(api.context.active());
55-
if (currentSpan){
73+
if (currentSpan) {
5674
const { traceId } = currentSpan.spanContext();
5775
console.log(`traceid: ${traceId}`);
5876
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
@@ -62,14 +80,15 @@ function runTest(ctx: Koa.Context) {
6280
}
6381
}
6482

65-
async function noOp(ctx: Koa.Context, next: Koa.Next) {
83+
function noOp(ctx: Koa.Context, next: Koa.Next) {
6684
console.log('Sample basic koa middleware');
6785
const syntheticDelay = 100;
68-
await new Promise((r) => setTimeout(r, syntheticDelay));
69-
next();
86+
setTimeout(next, syntheticDelay);
7087
}
7188

72-
setUp().then(() => {
73-
app.listen(PORT);
74-
console.log(`Listening on http://localhost:${PORT}`);
75-
});
89+
setUp()
90+
.then(() => {
91+
app.listen(PORT);
92+
console.log(`Listening on http://localhost:${PORT}`);
93+
})
94+
.catch(err => console.log(err));

examples/koa/src/tracer.ts

Lines changed: 20 additions & 10 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 { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
418
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
@@ -10,10 +24,11 @@ import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
1024
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
1125
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
1226
import { Resource } from '@opentelemetry/resources';
13-
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
27+
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
1428

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

31+
// eslint-disable-next-line import/prefer-default-export
1732
export const setupTracing = (serviceName: string) => {
1833
let exporter;
1934
if (EXPORTER === 'jaeger') {
@@ -24,18 +39,13 @@ export const setupTracing = (serviceName: string) => {
2439

2540
const provider = new NodeTracerProvider({
2641
resource: new Resource({
27-
[ATTR_SERVICE_NAME]: serviceName
42+
[ATTR_SERVICE_NAME]: serviceName,
2843
}),
29-
spanProcessors: [
30-
new SimpleSpanProcessor(exporter),
31-
],
44+
spanProcessors: [new SimpleSpanProcessor(exporter)],
3245
});
3346

3447
registerInstrumentations({
35-
instrumentations: [
36-
new KoaInstrumentation(),
37-
new HttpInstrumentation(),
38-
],
48+
instrumentations: [new KoaInstrumentation(), new HttpInstrumentation()],
3949
tracerProvider: provider,
4050
});
4151

0 commit comments

Comments
 (0)