Skip to content

Commit 3705ed7

Browse files
committed
npm run format:fix
1 parent 4a1c408 commit 3705ed7

File tree

18 files changed

+281
-221
lines changed

18 files changed

+281
-221
lines changed

examples/express/src/client.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
// eslint-disable-next-line import/order
4-
import { setupTracing } from "./tracer";
4+
import { setupTracing } from './tracer';
55
const tracer = setupTracing('example-express-client');
66

77
import * as api from '@opentelemetry/api';
@@ -24,8 +24,12 @@ function makeRequest() {
2424
}
2525
}
2626
span.end();
27-
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
28-
setTimeout(() => { console.log('Completed.'); }, 5000);
27+
console.log(
28+
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
29+
);
30+
setTimeout(() => {
31+
console.log('Completed.');
32+
}, 5000);
2933
});
3034
}
3135

examples/express/src/server.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { setupTracing } from './tracer'
1+
import { setupTracing } from './tracer';
22

33
setupTracing('example-express-server');
44

55
// Require in rest of modules
66
import * as express from 'express';
77
import { default as axios } from 'axios';
8-
import { RequestHandler } from "express";
8+
import { RequestHandler } from 'express';
99

1010
// Setup express
1111
const app = express();
@@ -32,19 +32,21 @@ const authMiddleware: RequestHandler = (req, res, next) => {
3232
};
3333

3434
app.use(express.json());
35-
app.get('/health', (req, res) => res.status(200).send("HEALTHY")); // endpoint that is called by framework/cluster
35+
app.get('/health', (req, res) => res.status(200).send('HEALTHY')); // endpoint that is called by framework/cluster
3636
app.get('/run_test', async (req, res) => {
3737
// 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',
38+
const createdCat = await axios.post(
39+
`http://localhost:${PORT}/cats`,
40+
{
41+
name: 'Tom',
42+
friends: ['Jerry'],
4643
},
47-
});
44+
{
45+
headers: {
46+
Authorization: 'secret_token',
47+
},
48+
}
49+
);
4850

4951
return res.status(201).send(createdCat.data);
5052
});

examples/express/src/tracer.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { SpanKind, Attributes } from "@opentelemetry/api";
3+
import { SpanKind, Attributes } from '@opentelemetry/api';
44

55
const opentelemetry = require('@opentelemetry/api');
66

@@ -10,13 +10,22 @@ diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
1010

1111
import { registerInstrumentations } from '@opentelemetry/instrumentation';
1212
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
13-
import { Sampler, AlwaysOnSampler, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
13+
import {
14+
Sampler,
15+
AlwaysOnSampler,
16+
SimpleSpanProcessor,
17+
} from '@opentelemetry/sdk-trace-base';
1418
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
1519
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
1620
import { Resource } from '@opentelemetry/resources';
17-
import { SEMRESATTRS_SERVICE_NAME, SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
21+
import {
22+
SEMRESATTRS_SERVICE_NAME,
23+
SEMATTRS_HTTP_ROUTE,
24+
} from '@opentelemetry/semantic-conventions';
1825

19-
const Exporter = (process.env.EXPORTER || '').toLowerCase().startsWith('z') ? ZipkinExporter : OTLPTraceExporter;
26+
const Exporter = (process.env.EXPORTER || '').toLowerCase().startsWith('z')
27+
? ZipkinExporter
28+
: OTLPTraceExporter;
2029
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
2130
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
2231

@@ -48,7 +57,11 @@ export const setupTracing = (serviceName: string) => {
4857
return opentelemetry.trace.getTracer(serviceName);
4958
};
5059

51-
type FilterFunction = (spanName: string, spanKind: SpanKind, attributes: Attributes) => boolean;
60+
type FilterFunction = (
61+
spanName: string,
62+
spanKind: SpanKind,
63+
attributes: Attributes
64+
) => boolean;
5265

5366
function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
5467
return {
@@ -60,10 +73,17 @@ function filterSampler(filterFn: FilterFunction, parent: Sampler): Sampler {
6073
},
6174
toString() {
6275
return `FilterSampler(${parent.toString()})`;
63-
}
64-
}
76+
},
77+
};
6578
}
6679

67-
function ignoreHealthCheck(spanName: string, spanKind: SpanKind, attributes: Attributes) {
68-
return spanKind !== opentelemetry.SpanKind.SERVER || attributes[SEMATTRS_HTTP_ROUTE] !== "/health";
80+
function ignoreHealthCheck(
81+
spanName: string,
82+
spanKind: SpanKind,
83+
attributes: Attributes
84+
) {
85+
return (
86+
spanKind !== opentelemetry.SpanKind.SERVER ||
87+
attributes[SEMATTRS_HTTP_ROUTE] !== '/health'
88+
);
6989
}

examples/koa/src/client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { setupTracing } from "./tracer";
3+
import { setupTracing } from './tracer';
44
const tracer = setupTracing('example-koa-client');
55
import * as api from '@opentelemetry/api';
66
import { default as axios } from 'axios';
@@ -16,13 +16,17 @@ function makeRequest() {
1616
span.setStatus({ code: api.SpanStatusCode.OK });
1717
console.log(res.statusText);
1818
} catch (e) {
19-
if(e instanceof Error) {
19+
if (e instanceof Error) {
2020
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message });
2121
}
2222
}
2323
span.end();
24-
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
25-
setTimeout(() => { console.log('Completed.'); }, 5000);
24+
console.log(
25+
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
26+
);
27+
setTimeout(() => {
28+
console.log('Completed.');
29+
}, 5000);
2630
});
2731
}
2832

examples/koa/src/server.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
22

33
import * as api from '@opentelemetry/api';
4-
import { setupTracing } from './tracer'
4+
import { setupTracing } from './tracer';
55
setupTracing('example-koa-server');
66

77
// Adding Koa router (if desired)
8-
import * as Router from "@koa/router";
9-
import * as Koa from "koa"
10-
8+
import * as Router from '@koa/router';
9+
import * as Koa from 'koa';
1110

1211
// Setup koa
1312
const app = new Koa();
1413
const PORT = 8081;
1514
const router = new Router();
1615

1716
// route definitions
18-
router.get('/run_test', runTest)
17+
router
18+
.get('/run_test', runTest)
1919
.get('/post/new', addPost)
2020
.get('/post/:id', showNewPost);
2121

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

2727
/**
2828
* Router functions: list, add, or show posts
29-
*/
29+
*/
3030
const posts = ['post 0', 'post 1', 'post 2'];
3131

3232
function addPost(ctx: Koa.Context) {
3333
const newPostId = posts.length;
3434
posts.push(`post ${newPostId}`);
3535
const currentSpan = api.trace.getSpan(api.context.active());
3636
currentSpan?.addEvent('Added post');
37-
currentSpan?.setAttribute('post.id', newPostId)
37+
currentSpan?.setAttribute('post.id', newPostId);
3838
ctx.body = `Added post: ${posts[posts.length - 1]}`;
3939
ctx.redirect('/post/3');
4040
}
@@ -45,14 +45,14 @@ async function showNewPost(ctx: Koa.Context) {
4545
const post = posts[id];
4646
if (!post) ctx.throw(404, 'Invalid post id');
4747
const syntheticDelay = 500;
48-
await new Promise((r) => setTimeout(r, syntheticDelay));
48+
await new Promise(r => setTimeout(r, syntheticDelay));
4949
ctx.body = post;
5050
}
5151

5252
function runTest(ctx: Koa.Context) {
5353
console.log('runTest');
5454
const currentSpan = api.trace.getSpan(api.context.active());
55-
if (currentSpan){
55+
if (currentSpan) {
5656
const { traceId } = currentSpan.spanContext();
5757
console.log(`traceid: ${traceId}`);
5858
console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`);
@@ -65,7 +65,7 @@ function runTest(ctx: Koa.Context) {
6565
async function noOp(ctx: Koa.Context, next: Koa.Next) {
6666
console.log('Sample basic koa middleware');
6767
const syntheticDelay = 100;
68-
await new Promise((r) => setTimeout(r, syntheticDelay));
68+
await new Promise(r => setTimeout(r, syntheticDelay));
6969
next();
7070
}
7171

examples/koa/src/tracer.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
1010
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
1111
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
1212
import { Resource } from '@opentelemetry/resources';
13-
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
13+
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
1414

1515
const EXPORTER = process.env.EXPORTER || '';
1616

1717
export const setupTracing = (serviceName: string) => {
1818
const provider = new NodeTracerProvider({
1919
resource: new Resource({
20-
[SEMRESATTRS_SERVICE_NAME]: serviceName
21-
})
20+
[SEMRESATTRS_SERVICE_NAME]: serviceName,
21+
}),
2222
});
2323

2424
let exporter;
@@ -30,10 +30,7 @@ export const setupTracing = (serviceName: string) => {
3030
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
3131

3232
registerInstrumentations({
33-
instrumentations: [
34-
new KoaInstrumentation(),
35-
new HttpInstrumentation(),
36-
],
33+
instrumentations: [new KoaInstrumentation(), new HttpInstrumentation()],
3734
tracerProvider: provider,
3835
});
3936

examples/mongodb/src/client.ts

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

3-
import * as api from '@opentelemetry/api';
3+
import * as api from '@opentelemetry/api';
44
import { setupTracing } from './tracer';
55

6-
const tracer = setupTracing('example-mongodb-http-client')
6+
const tracer = setupTracing('example-mongodb-http-client');
77
import * as http from 'http';
88

9-
109
/** A function which makes requests and handles response. */
1110
function makeRequest() {
1211
// span corresponds to outgoing requests. Here, we have manually created
@@ -19,58 +18,71 @@ function makeRequest() {
1918

2019
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
2120
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-
});
21+
http.get(
22+
{
23+
host: 'localhost',
24+
port: 8080,
25+
path: '/collection/',
26+
},
27+
response => {
28+
const body: any = [];
29+
response.on('data', chunk => body.push(chunk));
30+
response.on('end', () => {
31+
responses += 1;
32+
console.log(body.toString());
33+
if (responses === queries) span.end();
34+
});
35+
}
36+
);
3537
});
3638
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
3739
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-
});
40+
http.get(
41+
{
42+
host: 'localhost',
43+
port: 8080,
44+
path: '/insert/',
45+
},
46+
response => {
47+
const body: any = [];
48+
response.on('data', chunk => body.push(chunk));
49+
response.on('end', () => {
50+
responses += 1;
51+
console.log(body.toString());
52+
if (responses === queries) span.end();
53+
});
54+
}
55+
);
5156
});
5257
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), () => {
5358
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-
});
59+
http.get(
60+
{
61+
host: 'localhost',
62+
port: 8080,
63+
path: '/get/',
64+
},
65+
response => {
66+
const body: any = [];
67+
response.on('data', chunk => body.push(chunk));
68+
response.on('end', () => {
69+
responses += 1;
70+
console.log(body.toString());
71+
if (responses === queries) span.end();
72+
});
73+
}
74+
);
6775
});
6876

6977
// The process must live for at least the interval past any traces that
7078
// must be exported, or some risk being lost if they are recorded after the
7179
// last export.
72-
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
73-
setTimeout(() => { console.log('Completed.'); }, 5000);
80+
console.log(
81+
'Sleeping 5 seconds before shutdown to ensure all records are flushed.'
82+
);
83+
setTimeout(() => {
84+
console.log('Completed.');
85+
}, 5000);
7486
}
7587

7688
makeRequest();

0 commit comments

Comments
 (0)