Custom configuration for Node.js applications

Updated at:

You can configure agent parameters for Node.js applications in the Cloud Monitor 2.0 console, including sampling strategies, agent switches, and span attributes.

Overview

The Node.js agent supports dynamic configuration from the Cloud Monitor 2.0 console. Configurations take effect without restarting your application (the agent polls for changes every 60 seconds by default). You can:

  • Adjust the sampling strategy and rate to balance data completeness with system performance.

  • Enable or disable specific plugins to collect data on demand.

  • Configure span attribute limits to optimize data storage.

Sampling settings

Sampling strategy

The following sampling strategies are supported:

Strategy

Description

Use cases

Always-on sampling (always-on)

Collects 100% of all requests.

Development/testing environments, troubleshooting, and critical business services.

Always-off sampling (always-off)

Does not collect any requests.

Temporarily disabling monitoring or performance-sensitive scenarios.

Fixed-rate sampling (trace-id-ratio)

Samples a specified percentage of requests (0% to 100%).

Recommended for production environments.

Sampling rate percentage

  • Value range: 0 to 100

  • Default value: 100

  • Description: A higher sampling rate consumes more system resources. We recommend that you keep the default value. In production, adjust the rate based on your traffic and storage costs.

Procedure

  1. Log in to the CMS 2.0 console.

  2. Go to Application Monitoring > Application List.

  3. Select an application and click Settings. Expand the Sampling Settings panel.

  4. Select a sampling strategy and set the sampling rate. Click Save.

Configuration examples

// Always-on sampling (100%)
{
  "apsara": {
    "apm": {
      "sampler": {
        "strategy": 1  // 1 = always-on
      }
    }
  }
}

// Fixed-rate sampling (90%)
{
  "apsara": {
    "apm": {
      "sampler": {
        "strategy": 2,  // 2 = trace-id-ratio
        "rate": 90      // 90% sampling rate
      }
    }
  }
}

// Always-off sampling
{
  "apsara": {
    "apm": {
      "sampler": {
        "strategy": 0  // 0 = always-off
      }
    }
  }
}

Agent switch settings

Pause application monitoring

  • Function: Pauses the agent completely, including data collection and billing.

Important

Changes take effect dynamically without an application restart. Enabling this option stops the agent completely, causing gaps in metrics and trace data. Proceed with caution.

Plugin categories

The Node.js agent supports the following plugin categories:

Database plugins

Plugin name

Monitored object

Default status

mongodb-plugin

MongoDB database

Enabled

mongoose-plugin

Mongoose ODM

Enabled

mysql-plugin

MySQL database

Enabled

mysql2-plugin

MySQL2 database

Enabled

pg-plugin

PostgreSQL database

Enabled

redis-plugin

Redis database

Enabled

ioredis-plugin

ioredis client

Enabled

HTTP/RPC plugins

Plugin name

Monitored object

Default status

http-plugin

Node.js HTTP client/server

Enabled

undici-plugin

Undici HTTP client

Enabled

grpc-plugin

gRPC framework

Enabled

dns-plugin

DNS queries

Disabled

Web framework plugins

Plugin name

Monitored object

Default status

express-plugin

Express framework

Enabled

koa-plugin

Koa framework

Enabled

socketio-plugin

Socket.IO

Enabled

net-plugin

Net module (TCP/Unix)

Enabled

Messaging plugins

Plugin name

Monitored object

Default status

kafka-plugin

Kafka message queue

Enabled

AI plugins

Plugin name

Monitored object

Default status

openai-plugin

OpenAI API

Enabled

Configure plugin switches

Configure in the console

  1. On the application settings page, expand the Agent Switch Settings panel.

  2. Select or clear the checkboxes for the plugins as needed.

  3. Click Save.

Remote configuration example

// Enable or disable specific plugins
{
  "otel": {
    "instrumentation": {
      "mongodb": { "enabled": true },
      "mysql": { "enabled": false },
      "http": { "enabled": true },
      "redis": { "enabled": true }
    }
  }
}

Configuration in initialization code

const { NodeSDK } = require('@loongsuite/cms_node_sdk');

const sdk = new NodeSDK({
  licenseKey: 'your_license_key',
  serviceName: 'my-app',
  regionId: 'cn-hangzhou',
  // Custom plugin configuration
  instrumentations: {
    mongodb: { enabled: true, enhancedDatabaseReporting: true },
    mysql: { enabled: false },  // Disable the MySQL plugin
    http: { enabled: true },
    redis: { enabled: true }
  }
});

sdk.start();

GenAI content collection control

When you initialize NodeSDK, set content: { genai: { enabled: false } } to disable the recording of GenAI content recognized by the agent.

const { NodeSDK } = require('@loongsuite/cms_node_sdk');

const sdk = new NodeSDK({
  licenseKey: 'your_license_key',
  serviceName: 'my-app',
  regionId: 'cn-hangzhou',
  content: { genai: { enabled: false } }
});

sdk.start();

This option only disables the recording of GenAI content recognized by the agent. Traces and metrics for call counts, token usage, latency, and errors are retained. The option applies only to traces managed by NodeSDK and does not turn off sampling, disable plugins, or pause application monitoring. Independent third-party exporters are controlled by their own configurations.

Recommendations

  • Enable on demand: Enable only the plugins your application uses to minimize performance overhead.

  • Troubleshooting: If a plugin causes issues, temporarily disable it without restarting the application.

  • Database monitoring: Enable all database plugins in use to trace data access paths end-to-end.

  • Web framework: Enable the corresponding framework plugin (such as Express or Koa) based on your technology stack.

Span attribute configuration

Span attributes store metadata for each span. Properly configuring attribute limits helps optimize storage usage and query performance.

Configuration items

Parameter

Default

Description

Attribute value length limit

4096

Maximum character length for each attribute value.

Attribute count limit

128

Maximum number of attributes per span.

Link count limit

128

Maximum number of links per span.

Event count limit

128

Maximum number of events per span.

Link attribute count limit

128

Maximum number of attributes per link.

Event attribute count limit

128

Maximum number of attributes per event.

Procedure

  1. On the application settings page, expand the Span Attribute Configuration panel.

  2. Adjust the limits for each item based on your requirements.

  3. Click Save.

Remote configuration example

{
  "apsara": {
    "apm": {
      "trace": {
        "span_limits": {
          "attribute_value_length_limit": 4096,
          "attribute_count_limit": 128,
          "link_count_limit": 128,
          "event_count_limit": 128,
          "attribute_per_link_count_limit": 128,
          "attribute_per_event_count_limit": 128
        }
      }
    }
  }
}

Code configuration example

const { NodeSDK } = require('@loongsuite/cms_node_sdk');

const sdk = new NodeSDK({
  licenseKey: 'your_license_key',
  serviceName: 'my-app',
  regionId: 'cn-hangzhou',
  // Span limit configuration
  spanLimits: {
    attributeValueLengthLimit: 4096,
    attributeCountLimit: 128,
    linkCountLimit: 128,
    eventCountLimit: 128,
    attributePerLinkCountLimit: 128,
    attributePerEventCountLimit: 128
  }
});

sdk.start();

Configuration recommendations

  • Attribute value length: Increase this limit if your span attributes contain long URLs or SQL statements.

  • Attribute count: The default of 128 attributes is sufficient for most cases. Adjust only for special scenarios.

  • Links and events: Adjust these limits based on actual usage.

  • Performance impact: Higher limits increase storage and transmission overhead. Set values based on your actual needs.

Batch operations

Batch copy to other applications

To apply the same configuration to multiple applications, use the batch copy feature.

  1. Configure the parameters for one application.

  2. Click the Batch copy to other applications button.

  3. Select the other applications to which you want to apply the same configuration.

  4. Confirm the batch copy. This applies all configurations of the current application (including sampling settings, agent switches, and span attributes) to the selected applications.

Save as a custom template

If you frequently use the same combination of settings, save it as a template for reuse.

  1. Configure all the items.

  2. Click the Save current application settings as custom configuration template button.

  3. Name the template and save it.

  4. You can then quickly apply this template to new applications.

How configurations take effect

Dynamic update mechanism

  • Polling interval: The agent polls the Cloud Monitor 2.0 server for configuration changes every 60 seconds by default.

  • Effective time: Configurations take effect within 60 seconds after delivery.

  • Version detection: The system detects configuration version changes and skips unchanged settings.

Manually refresh configurations

To apply the latest configuration immediately, call the refresh method from your code:

// Immediately fetch the latest configuration
await sdk.refreshRemoteConfig();

Start and stop polling

const { NodeSDK } = require('@loongsuite/cms_node_sdk');

const sdk = new NodeSDK({
  licenseKey: 'your_license_key',
  serviceName: 'my-app',
  regionId: 'cn-hangzhou'
});

// Start the SDK (automatically starts remote configuration polling)
sdk.start();

// Optional: Customize the polling interval
sdk.startRemoteConfigPolling(30000); // Poll every 30 seconds

// Stop polling (when the application exits)
process.on('SIGTERM', () => {
  sdk.stopRemoteConfigPolling();
  sdk.shutdown().then(() => process.exit(0));
});

Best practices

Recommended production configuration

{
  "apsara": {
    "apm": {
      "sampler": {
        "strategy": 2,
        "rate": 10  // 10% sampling rate to balance cost and monitoring effectiveness
      },
      "trace": {
        "span_limits": {
          "attribute_value_length_limit": 4096,
          "attribute_count_limit": 128
        }
      }
    }
  },
  "otel": {
    "instrumentation": {
      // Enable only the plugins in use
      "http": { "enabled": true },
      "express": { "enabled": true },
      "mysql": { "enabled": true },
      "redis": { "enabled": true }
      // Disable unused plugins to save resources
    }
  }
}

Temporary configuration for troubleshooting

When you need detailed troubleshooting:

  1. Temporarily increase the sampling rate to 100%.

  2. Enable all relevant plugins.

  3. Increase the span attribute limits as needed.

  4. After the issue is resolved, restore the original configuration.

Optimization for high-traffic scenarios

For high-traffic applications, consider the following optimizations:

  • Lower the sampling rate to between 1% and 5%.

  • Disable unnecessary plugins.

  • Reduce the span attribute limit values.

  • Monitor the agent's performance overhead.

Configuration management recommendations

  • Differentiate environments: Use different sampling rates for development, testing, and production environments.

  • Adjust gradually: Adjust the sampling rate incrementally, observing the impact before making further changes.

  • Document changes: Record the reason for and time of configuration changes for traceability.

  • Monitor and alert: Set up alerts for configuration changes to prevent accidental misconfigurations.

FAQ

How long does it take for configuration changes to take effect?

By default, changes take effect within 60 seconds. To apply changes immediately, call sdk.refreshRemoteConfig() to refresh the configuration manually.

Does disabling a plugin affect existing data?

No. Disabling a plugin only stops future data collection. Previously reported data remains intact.

How can I verify that my configuration has taken effect?

You can verify the configuration in the following ways.

  • Check the application logs for entries related to [RemoteConfig].

  • In the Cloud Monitor 2.0 console, check if the sampling rate has changed.

  • Observe whether the collected trace data meets your expectations.

What happens if I set the sampling rate to 0?

Setting the sampling rate to 0 is equivalent to always-off sampling. No request data is collected, but the agent continues running and consumes minimal resources.

Can I dynamically modify configurations in code?

Yes. The NodeSDK provides the following APIs:

  • updateArmsConfig(): Updates basic Cloud Monitor 2.0 configurations.

  • refreshRemoteConfig(): Manually refreshes the remote configuration.

  • Directly modify properties such as spanLimits (requires restarting some components).

Does batch copying configurations overwrite the existing configurations of target applications?

Yes. Batch copying overwrites all configurations of the target applications. Verify their current configurations before proceeding.