Skip to content

Commit f34fa0b

Browse files
committed
updated node.js linux example to include a slow function
1 parent 0953d39 commit f34fa0b

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

instrumentation/nodejs/linux/README.md

+14-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Open a command line terminal and navigate to the root of the directory.
3434
For example:
3535

3636
````
37-
cd ~/splunk-opentelemetry-examples/instrumentation/nodejs
37+
cd ~/splunk-opentelemetry-examples/instrumentation/nodejs/linux
3838
````
3939

4040
### Initialize NPM and Install Packages (Optional)
@@ -53,18 +53,18 @@ npm install express
5353
npm install pino
5454
````
5555

56-
There's no need to run these commands again as you can use the `package.json` file that
57-
was already created.
58-
59-
### Instrument with the Splunk Distribution of OpenTelemetry JS (Optional)
60-
61-
Next, we'll instrument our application using the Splunk Distribution of OpenTelemetry JS
62-
by first installing the `@splunk/otel` package:
56+
Next, we'll instrument our application using the Splunk Distribution of OpenTelemetry JS
57+
by first installing the `@splunk/otel` package, so let's install this package as well:
6358

6459
````
6560
npm install @splunk/otel
6661
````
6762

63+
There's no need to run these commands again as you can use the `package.json` file that
64+
was already created.
65+
66+
### Configure the Splunk Distribution of OpenTelemetry JS
67+
6868
To configure the instrumentation, we've set the following environment variables:
6969

7070
````
@@ -80,6 +80,12 @@ AlwaysOn Profiling capabilities.
8080

8181
### Execute the application
8282

83+
Let's install the packages in the `package.json` file with the following command:
84+
85+
````
86+
npm install
87+
````
88+
8389
Next, we'll execute the application with the `@splunk/otel` package as follows:
8490

8591
````

instrumentation/nodejs/linux/index.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
const express = require("express");
22
const app = express();
33
const logger = require('pino')()
4+
const opentelemetry = require('@opentelemetry/api');
45

56
const PORT = process.env.PORT || "8080";
7+
const tracer = opentelemetry.trace.getTracer('example-app', '0.1.0');
68

79
app.get("/hello", (_, res) => {
8-
logger.info('/hello endpoint invoked, sending response');
9-
res.status(200).send("Hello, World!");
10+
hello(res);
1011
});
1112

1213
app.listen(parseInt(PORT, 10), () => {
1314
logger.info(`Listening for requests on http://localhost:${PORT}`);
1415
});
16+
17+
function hello(res) {
18+
logger.info('/hello endpoint invoked, sending response');
19+
slow_function()
20+
res.status(200).send("Hello, World!");
21+
}
22+
23+
// include a slow function to demonstrate CPU call stacks
24+
function slow_function() {
25+
26+
let result = 0;
27+
for (let i = 0; i < 1000000000; i++) {
28+
result += i;
29+
}
30+
31+
return result;
32+
}

instrumentation/nodejs/package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)