Skip to content

Commit f1a091b

Browse files
authored
Merge branch 'main' into trentm-lint-examples-react-load
2 parents a5acf8e + 212fc05 commit f1a091b

File tree

17 files changed

+340
-77
lines changed

17 files changed

+340
-77
lines changed

examples/connect/.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/connect/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/connect/client.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
'use strict';
218

319
// eslint-disable-next-line import/order

examples/connect/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"description": "Example of Connect 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
"client": "node ./client.js",
911
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
1012
"docker:stop": "cd ./docker && docker-compose down",

examples/connect/server.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
'use strict';
218

319
// eslint-disable-next-line
@@ -21,7 +37,9 @@ app.use((req, res, next) => {
2137
});
2238

2339
app.use('/run_test', async (req, res) => {
24-
const result = await axios.get('https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json');
40+
const result = await axios.get(
41+
'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json'
42+
);
2543
tracing.log('sending response');
2644
res.end(`OK ${result.data.version}`);
2745

examples/connect/tracing.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
'use strict';
218

319
const opentelemetry = require('@opentelemetry/api');
@@ -12,7 +28,9 @@ const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
1228
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
1329
const { CollectorTraceExporter } = require('@opentelemetry/exporter-collector');
1430

15-
const { ConnectInstrumentation } = require('@opentelemetry/instrumentation-connect');
31+
const {
32+
ConnectInstrumentation,
33+
} = require('@opentelemetry/instrumentation-connect');
1634
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
1735

1836
function log() {
@@ -21,15 +39,13 @@ function log() {
2139
console.log.apply(this, args);
2240
}
2341

24-
module.exports = (serviceName) => {
42+
module.exports = serviceName => {
2543
const exporter = new CollectorTraceExporter();
2644
const provider = new NodeTracerProvider({
2745
resource: new Resource({
2846
[ATTR_SERVICE_NAME]: serviceName,
2947
}),
30-
spanProcessors: [
31-
new SimpleSpanProcessor(exporter),
32-
],
48+
spanProcessors: [new SimpleSpanProcessor(exporter)],
3349
});
3450
const connectInstrumentation = new ConnectInstrumentation();
3551
registerInstrumentations({

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
});

0 commit comments

Comments
 (0)