Application Performance Monitoring (APM) agent for Node.js applications with transaction tracing, error tracking, custom metrics, and distributed tracing capabilities.
npx @tessl/cli install tessl/npm-newrelic@13.2.0The New Relic Node.js Agent is a comprehensive Application Performance Monitoring (APM) library that provides deep observability into Node.js applications. It automatically instruments popular frameworks and databases while offering extensive APIs for custom instrumentation, transaction management, error tracking, metrics collection, and distributed tracing.
npm install newrelicconst newrelic = require('newrelic');For ESM (ES Modules), the agent must be loaded with the import flag:
node --import newrelic/esm-loader.mjs -r newrelic your-app.jsOr preloaded with CommonJS:
node -r newrelic your-app.jsconst newrelic = require('newrelic');
// Custom transaction naming
newrelic.setTransactionName('my-custom-transaction');
// Add custom attributes
newrelic.addCustomAttribute('userId', '12345');
newrelic.addCustomAttribute('planType', 'premium');
// Record custom metrics
newrelic.recordMetric('Custom/MyOperation/Duration', 245);
// Handle errors
try {
riskyOperation();
} catch (error) {
newrelic.noticeError(error, { context: 'user-action' });
throw error;
}
// Custom events for analytics
newrelic.recordCustomEvent('UserAction', {
action: 'purchase',
amount: 99.99,
category: 'electronics'
});The New Relic Agent operates through several key components:
Core transaction lifecycle management for web requests and background jobs. Provides manual control over transaction naming, timing, and metadata.
function setTransactionName(name);
function setControllerName(name, action);
function getTransaction();
function startWebTransaction(url, handle);
function startBackgroundTransaction(name, group, handle);
function endTransaction();Add custom metadata to transactions, spans, and events for enhanced filtering and analysis in New Relic dashboards.
function addCustomAttribute(key, value);
function addCustomAttributes(attributes);
function addCustomSpanAttribute(key, value);
function addCustomSpanAttributes(attributes);
function setUserID(id);Record custom metrics and events for business analytics and performance tracking beyond standard APM data.
function recordMetric(name, value);
function incrementMetric(name, value);
function recordCustomEvent(eventType, attributes);
function recordLogEvent(logEvent);Comprehensive error tracking with custom attributes, error grouping, and expected error marking.
function noticeError(error, customAttributes, expected);
function setErrorGroupCallback(callback);Create custom timing segments for specific operations and code blocks within transactions.
function startSegment(name, record, handler, callback);Generate Real User Monitoring (RUM) scripts for correlating server-side performance with client-side metrics.
function getBrowserTimingHeader(options);Cross-service tracing capabilities with trace metadata extraction and injection for microservices architectures.
function getLinkingMetadata(omitSupportability);
function getTraceMetadata();Register custom instrumentation for third-party modules and application-specific code patterns.
function instrument(moduleName, onRequire, onError);
function instrumentDatastore(moduleName, onRequire, onError);
function instrumentWebframework(moduleName, onRequire, onError);
function instrumentMessages(moduleName, onRequire, onError);
function instrumentConglomerate(moduleName, onRequire, onError);
function instrumentLoadedModule(moduleName, module);Configure automatic transaction naming patterns and URL filtering for better metric organization.
function addNamingRule(pattern, name);
function addIgnoringRule(pattern);Specialized monitoring capabilities for Large Language Model applications including token counting and feedback tracking.
function recordLlmFeedbackEvent(params);
function setLlmTokenCountCallback(callback);
function withLlmCustomAttributes(context, callback);Serverless monitoring capabilities for AWS Lambda functions with automatic cold start detection.
function setLambdaHandler(handler);Miscellaneous utility functions for SQL obfuscation, performance tuning, and agent lifecycle management.
function obfuscateSql(sql, dialect);
function ignoreApdex();
function setDispatcher(name, version);
function shutdown(options, callback);interface TransactionHandle {
end(callback?: Function): void;
ignore(): void;
isSampled(): boolean;
acceptDistributedTraceHeaders(transportType: string, headers: object): void;
insertDistributedTraceHeaders(headers: object): void;
}
interface LinkingMetadata {
'trace.id': string;
'span.id': string;
'entity.name': string;
'entity.type': string;
'entity.guid': string;
hostname: string;
}
interface TraceMetadata {
traceId: string;
spanId: string;
}
interface ValidationResult {
isValid: boolean;
data: any;
errors: ValidationError[];
}
interface ValidationError {
field: string;
message: string;
value: any;
}
interface BrowserTimingOptions {
nonce?: string;
hasToRemoveScriptWrapper?: boolean;
allowTransactionlessInjection?: boolean;
}
interface ShutdownOptions {
collectPendingData?: boolean;
timeout?: number;
waitForIdle?: boolean;
}The agent automatically instruments popular Node.js frameworks and libraries including:
The agent reads configuration from multiple sources in order of precedence:
Key configuration options include high security mode, custom attributes, browser monitoring, distributed tracing, and instrumentation enable/disable flags.