Skip to content

Commit 914fdd7

Browse files
authored
Replace Console with bunyan logger in Sample App (#186)
*Description of changes:* There are currently no OTel supported logs being emitted from the sample-app. Replaces sample-app logging with `bunyan` in order for OpenTelemetry to auto-instrument the logging library from the app. /aws-sdk-s3: ``` {"name":"express-app","hostname":"ip-172-31-7-29.us-west-2.compute.internal","pid":2522078,"level":30,"msg":"done aws sdk s3 request","time":"2025-05-28T17:52:53.115Z","v":0} {"name":"express-app","hostname":"ip-172-31-7-29.us-west-2.compute.internal","pid":2522078,"level":50,"msg":"Exception thrown: Invalid value \"undefined\" for header \"X-Amzn-Trace-Id\"","time":"2025-05-28T17:52:54.132Z","v":0} ``` /http: ``` {"name":"express-app","hostname":"ip-172-31-7-29.us-west-2.compute.internal","pid":2522078,"level":30,"msg":"random value from http request: [94]\n","time":"2025-05-28T17:54:09.786Z","v":0} ``` /rolldice: ``` {"name":"express-app","hostname":"ip-172-31-7-29.us-west-2.compute.internal","pid":2522987,"level":30,"msg":"rolldice: 1","time":"2025-05-28T17:54:52.830Z","v":0} ``` By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent f0eba5f commit 914fdd7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

sample-applications/simple-express-server/sample-app-express-server.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
const http = require('http');
44
const express = require('express');
5+
const bunyan = require('bunyan');
56
const { S3Client, ListObjectsCommand } = require('@aws-sdk/client-s3');
67

78
const PORT = parseInt(process.env.SAMPLE_APP_PORT || '8080', 10);
89

910
const app = express();
1011

12+
// Uses bunyan logger
13+
const logger = bunyan.createLogger({name: 'express-app', level: 'info'});
14+
1115
async function getRandomNumber(min, max) {
1216
return Math.floor(Math.random() * (max - min) + min);
1317
}
1418

19+
// Generate logs in your endpoints
1520
app.get('/rolldice', (req, res) => {
21+
1622
getRandomNumber(1, 6).then((val) => {
17-
res.send(`rolldice: ${val.toString()}`);
23+
const msg = `rolldice: ${val.toString()}`
24+
logger.info(msg);
25+
res.send(msg);
1826
});
1927
});
2028

@@ -29,7 +37,9 @@ app.get('/http', (req, res) => {
2937
const httpRequest = http.request(options, (rs) => {
3038
rs.setEncoding('utf8');
3139
rs.on('data', (result) => {
32-
res.send(`random value from http request: ${result}`);
40+
const msg = `random value from http request: ${result}`
41+
logger.info(msg);
42+
res.send(msg);
3343
});
3444
rs.on('error', console.log);
3545
});
@@ -45,14 +55,16 @@ app.get('/aws-sdk-s3', async (req, res) => {
4555
Bucket: bucketName,
4656
}),
4757
).then((data) => {
48-
console.log(data);
58+
logger.info(data);
4959
});
5060
} catch (e) {
5161
if (e instanceof Error) {
52-
console.error('Exception thrown: ', e.message);
62+
logger.error(`Exception thrown: ${e.message}`);
5363
}
5464
} finally {
55-
res.send('done aws sdk s3 request');
65+
const msg = 'done aws sdk s3 request'
66+
logger.info(msg);
67+
res.send(msg);
5668
}
5769
});
5870

0 commit comments

Comments
 (0)