This Node.js application demonstrates how to set up default HTTP and HTTPS OpenTelemetry tracing.
Before you begin, make sure you have the following installed on your system:
- Node.js: Ensure you have Node.js installed (v16 or higher).
- npm (Node Package Manager)
- AWS credentials (Access Key ID, Secret Access Key, and region).
- S3 bucket to store user profile JSON files.
- DynamoDB table to store user data.
-
Clone this repository to your local machine.
git clone https://github.com/thisissneha/nodejs-enhanced-default-http-tracing.git cd nodejs-enhanced-default-http-tracing -
Install the necessary dependencies by running:
npm install
-
Create a .env file in the root directory to store your AWS credentials and other environment variables. The contents of the .env file should look like this:
AWS_ACCESS_KEY_ID=your-access-key-id AWS_SECRET_ACCESS_KEY=your-secret-access-key AWS_REGION=your-region
-
Set up AWS resources
-
S3 Bucket: Create a bucket in S3 where user profiles will be stored.
-
DynamoDB Table: Create a DynamoDB table to store user records.
- Table name: Users
- Partition key: userId (String)
-
The project consists of two main files:
-
tracing.js: This file configures deafult OpenTelemetry tracing. -
index.js: This file sets up an Express.js server and defines two API routes (GET /users and POST /users).node-tracing-app/ │ ├── tracing.js # Default OpenTelemetry tracing setup ├── index.js # Express server with two API routes └── package.json # Project dependencies and scripts
Run the application:
node index.js-
POST /user-
Description: This API stores user data by uploading a profile to S3 and storing additional data in DynamoDB.
-
Request Body:
{ "userId": "123", "profile": { "name": "John Doe", "email": "john.doe@example.com", "preferences": { "theme": "dark", "notifications": true } }, "data": { "age": 30, "loyaltyPoints": 150 } } -
Sample Request:
curl -X POST http://localhost:3000/user \ -H 'Content-Type: application/json' \ -d '{ "userId": "123", "profile": { "name": "John Doe", "email": "john.doe@example.com", "preferences": { "theme": "dark", "notifications": true } }, "data": { "age": 30, "loyaltyPoints": 150 } }'
-
Response:
{ "message": "User data stored successfully" }
-
-
GET /user/:userId-
Description: Fetches user profile from S3 and additional data from DynamoDB.
-
URL Parameters:
userId: The ID of the user whose data needs to be fetched.
-
Sample Request:
curl -X GET http://localhost:3000/user/123
-
Response:
{ "message": "User data retrieved successfully", "profile": { "name": "John Doe", "email": "john.doe@example.com", "preferences": { "theme": "dark", "notifications": true } }, "record": { "age": 30, "loyaltyPoints": 150 } }
-
-
Trace Exporter URL: If you have set up your New Relic (NR) account in the US region, the trace endpoint will be:
https://otlp.nr-data.net:4318/v1/traces.For more information, refer to the New Relic OTLP Endpoints documentation.
-
New Relic Keys: Use the New Relic API keys.
-
Concurrency Limit: It is the number of trace batches that can be sent concurrently to the New Relic. The default concurrency limit is 30. You can adjust this value according to your use case.
// Initialize OTEL trace exporter
const exporter = new OTLPTraceExporter({
url: <TRACE_EXPORTER_ENDPOINT>, // Exporter endpoint
headers: {
"api-key": <NEW_RELIC_KEY>, // NR license key for authentication
},
concurrencyLimit: <CONCURRENCY_LIMIT>, // the number of trace batches that can be sent concurrently to the New Relic
});-
Entity Name: Entity Name is the service name of the application.
-
Max Export Batch Size: It is the maximum batch size of every export. It must be smaller or equal to max queue size. The default max export batch size is 512. You can adjust this value according to your use case.
-
Scheduled Delay Millis: It is the delay interval in milliseconds between two consecutive exports. The default scheduled delay in millisecond is 5000. You can adjust this value according to your use case.
-
Export Timeout Millis: It indicates how long the export can run before it is canceled. The default export timeout in millisecond is 30000. You can adjust this value according to your use case.
-
Max Queue Size: It is the maximum queue size. After the size is reached spans are dropped. The default max queue size is 2048. You can adjust this value according to your use case.
// BatchSpanProcessor with OTLP exporter (send to New Relic)
provider.addSpanProcessor(
new BatchSpanProcessor(otlpExporter, {
maxExportBatchSize: <MAX_EXPORT_BATCH_SIZE>, // The maximum batch size of every export. It must be smaller or equal to maxQueueSize.
scheduledDelayMillis: <SCHEDULED_DELAY_MILLIS>, // The delay interval in milliseconds between two consecutive exports.
exportTimeoutMillis: <EXPORT_TIMEOUT_MILLIS>, // How long the export can run before it is cancelled.
maxQueueSize: <MAX_QUEUE_SIZE>, // The maximum queue size. After the size is reached spans are dropped.
})
);Note: We are using the default configuration values for demonstration purposes.
-
ignoreIncomingRequestHook: Http instrumentation will not trace all incoming requests that matched with custom function
-
ignoreOutgoingRequestHook: Http instrumentation will not trace all outgoing requests that matched with custom function
-
applyCustomAttributesOnSpan: Function for adding custom attributes
-
requestHook: Function for adding custom attributes before request is handled
-
responseHook: Function for adding custom attributes before response is handled
// Register default HTTP instrumentations
registerInstrumentations({
instrumentations: [
new HttpInstrumentation({
ignoreIncomingRequestHook(request) {
// logic implementation
},
requestHook(span, request) {
// logic implementation
},
}),
],
});For more information, refer to the OTel http/https instrumentation document.
Feel free to submit issues or pull requests to improve the application.

