Instrument your Node.js application for Splunk Observability Cloud π
The Splunk Distribution of OpenTelemetry JS can automatically instrument your Node.js application and many of the popular node.js libraries your application uses.
To get started, use the guided setup or follow the instructions manually.
Generate customized instructions using the guided setup π
To generate all the basic installation commands for your environment and application, use the Node.js guided setup. To access the Node.js guided setup, follow these steps:
Log in to Splunk Observability Cloud.
Open the Node.js guided setup . Optionally, you can navigate to the guided setup on your own:
In the navigation menu, select
.Go to the Available integrations tab, or select Add Integration in the Deployed integrations tab.
In the integration filter menu, select By Product.
Select the APM product.
Select the Node.js tile to open the Node.js guided setup.
Install the Splunk Distribution of OpenTelemetry JS manually π
If you donβt use the guided setup, follow these instructions to manually install the Splunk Distribution of OpenTelemetry JS:
Install and activate the Node.js instrumentation π
To instrument your Node.js application with the Splunk Distribution of OpenTelemetry JS, follow these steps:
Install the
@splunk/otel
package:npm install @splunk/otel
To add custom instrumentations, see Add custom instrumentation.
Set the
OTEL_SERVICE_NAME
environment variable:export OTEL_SERVICE_NAME=<yourServiceName>
$env:OTEL_SERVICE_NAME=<yourServiceName>
(Optional) Set the endpoint URL if the Splunk Distribution of OpenTelemetry Collector is running on a different host:
export OTEL_EXPORTER_OTLP_ENDPOINT=<yourCollectorEndpoint>:<yourCollectorPort>
$env:OTEL_EXPORTER_OTLP_ENDPOINT=<yourCollectorEndpoint>:<yourCollectorPort>
(Optional) Set the deployment environment and service version:
export OTEL_RESOURCE_ATTRIBUTES='deployment.environment=<envtype>,service.version=<version>'
$env:OTEL_RESOURCE_ATTRIBUTES='deployment.environment=<envtype>,service.version=<version>'
(Optional) Activate metric collection. See Activate metrics collection.
To run your Node.js application, enter the following command:
node -r @splunk/otel/instrument <your-app.js>
If no data appears in APM, see Troubleshoot Node.js instrumentation for Splunk Observability Cloud.
Note
To instrument applications that use Webpack, see Webpack compatibility issues.
Activate AlwaysOn Profiling π
To activate AlwaysOn Profiling, set the SPLUNK_PROFILER_ENABLED
environment variable to true
.
To activate memory profiling, set the SPLUNK_PROFILER_MEMORY_ENABLED
environment variable to true
after activating AlwaysOn Profiling.
The following example shows how to activate the profiler from your application code:
start({
serviceName: '<service-name>',
endpoint: 'collectorhost:port',
profiling: { // Activates CPU profiling
memoryProfilingEnabled: true, // Activates Memory profiling
}
});
See Get data into Splunk APM AlwaysOn Profiling for more information. For more settings, see Node.js settings for AlwaysOn Profiling.
Activate metrics collection π
To activate automatic runtime metric collection, activate the metrics feature using the SPLUNK_METRICS_ENABLED
environment variable. See Metrics configuration for more information.
export SPLUNK_METRICS_ENABLED='true'
$env:SPLUNK_METRICS_ENABLED='true'
Configure the Node.js distribution π
In most cases, the only configuration setting you need to enter is the service name. For advanced configuration, like changing trace propagation formats or configuring server trace data, see Configure the Splunk Distribution of OTel JS for Splunk Observability Cloud.
Instrument your application programmatically π
To have even finer control over the tracing pipeline, instrument your Node.js application programmatically.
To instrument your application programmatically, add the following lines at the beginning of your entry point script before calling any instrumentation function:
const { start } = require('@splunk/otel');
start({
serviceName: 'my-node-service',
endpoint: 'http://localhost:4317'
});
// Rest of your main module
The start()
function accepts configuration settings as arguments. For example, you can use it to activate runtime metrics and memory profiling:
start({
serviceName: 'my-node-service',
metrics: { runtimeMetricsEnabled: true },
profiling: { memoryProfilingEnabled: true }
});
After you add the start()
function to your entry point script, run your application by passing the instrumented entry point script using the -r
flag:
node -r <entry-point.js> <your-app.js>
Add custom instrumentation π
To add custom or third-party instrumentations that implement the OpenTelemetry JS Instrumentation interface, pass them to start()
using the following code:
const { start } = require('@splunk/otel');
const { getInstrumentations } = require('@splunk/otel/lib/instrumentations');
start({
tracing: {
instrumentations: [
...getInstrumentations(), // Adds default instrumentations
new MyCustomInstrumentation(),
new AnotherInstrumentation(),
],
},
});
For a list of supported instrumentations, see https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/metapackages/auto-instrumentations-node#supported-instrumentations on GitHub.
Note
For an example of entry point script, see the sample tracer.js file on GitHub.
Deploy the Node.js distribution in Kubernetes π
To deploy the Collector for Node.js in a Kubernetes environment, follow these steps:
Edit the Dockerfile for your application image to add the following commands:
# Install the @splunk/otel package RUN npm install @splunk/otel # Set appropriate permissions RUN chmod -R go+r /node_modules/@splunk/otel
Configure the Kubernetes Downward API to expose environment variables to Kubernetes resources.
The following example shows how to update a deployment to expose environment variables by adding the OpenTelemetry configuration under the
.spec.template.spec.containers.env
section:apiVersion: apps/v1 kind: Deployment spec: selector: matchLabels: app: your-application template: spec: containers: - name: myapp image: your-app-image env: - name: SPLUNK_OTEL_AGENT valueFrom: fieldRef: fieldPath: status.hostIP - name: OTEL_EXPORTER_OTLP_ENDPOINT value: "http://$(SPLUNK_OTEL_AGENT):4317" - name: OTEL_SERVICE_NAME value: "<serviceName>" - name: OTEL_RESOURCE_ATTRIBUTES value: "deployment.environment=<environmentName>" command: - node - -r @splunk/otel/instrument - <your-app>.js
Send data directly to Splunk Observability Cloud π
By default, all telemetry is sent to the local instance of the Splunk Distribution of OpenTelemetry Collector.
If you need to send data directly to Splunk Observability Cloud, set the following environment variables:
export SPLUNK_ACCESS_TOKEN=<access_token>
export SPLUNK_REALM=<realm>
$env:SPLUNK_ACCESS_TOKEN=<access_token>
$env:SPLUNK_REALM=<realm>
To obtain an access token, see Retrieve and manage user API access tokens using Splunk Observability Cloud.
To find your Splunk realm, see Note about realms.
For more information on the ingest API endpoints, see Send APM traces .
Caution
This procedure applies to spans and traces. To send AlwaysOn Profiling data, you must use the OTel Collector.
Specify the source host π
To override the host used by the agent, use the environment variable OTEL_RESOURCE_ATTRIBUTES
to set your hostβs name to the desired source:
$env:OTEL_RESOURCE_ATTRIBUTES=host.name=<host_name>
export OTEL_RESOURCE_ATTRIBUTES=host.name=<host_name>
Instrument Lambda functions π
You can instrument AWS Lambda functions using the Splunk OpenTelemetry Lambda Layer. See Instrument your AWS Lambda function for Splunk Observability Cloud for more information.