New Relic Node.js Agent
repository·main·Indexed 21 days ago
https://github.com/newrelic/node-newrelicAn instrumentation tool for monitoring Node.js application performance, providing visibility into transactions, errors, and system metrics. Includes documentation on Azure Site Extension installation, distributed tracing sampling configuration, and the subscriber-based instrumentation model using diagnostic_channel TracingChannel objects.
What's inside newrelic-node-newrelic
- This repository provides cloud-based tools for the New Relic Node Agent, such as Azure Site Extensions. To use these tools, you must refer to the specific documentation for the tool you are interested in, as setup and usage vary by cloud provider.
Filter transaction attributes
mainAttributes can be filtered out based on rules defined by New Relic or the user. For example, a rule can be set to drop any attribute with a specific key (e.g.,
foo.bar).Filtering configuration can be provided via:
- The local agent's configuration file.
- Environment variables.
- The server, via the configuration returned during the
connectevent.
Automatic instrumentation of supported Node.js modules
mainOnce the New Relic agent is installed, it automatically instruments a wide catalog of supported Node.js libraries and frameworks. This provides immediate, granular monitoring for your web applications and servers without manual configuration. If you are using a framework or library not included in the supported list, you must perform manual instrumentation using the Node.js agent API.Usage restrictions and licensing for the New Relic Node.js Agent
mainThe New Relic Node.js agent is proprietary software intended solely for use with New Relic's proprietary SaaS service. To use the software, you must have a valid and active subscription to the New Relic Service.
Key Restrictions:
- Scope of Use: Limited to you and your organization. You may not expose the software to third parties via a network or use it on behalf of others.
- Modifications: You cannot modify the software or create derivative works, though you may compile or link it into your programs as per New Relic documentation.
- Reverse Engineering: Reverse engineering or decompiling the software is prohibited.
- Competitive Use: Use for competitive analysis, benchmarking, or any purpose intended to compete with New Relic is prohibited.
- Distribution: You may only copy the software as needed for use within your organization; distribution to third parties is prohibited.
- Proprietary Notices: You must not remove or obscure any proprietary notices within the software.
Intellectual Property & Feedback: Any feedback provided to New Relic (including source code or comments) is provided under the terms of the Apache Software License, version 2. This grant is irrevocable.
Termination: This license terminates when the software is no longer provided by New Relic or as described in your subscription agreement. Upon termination, you must remove the software from your systems within a commercially reasonable timeframe.
How subscriber-based instrumentation works
mainStarting with v13.2.0, the New Relic Node.js agent is refactoring traditional
Shim-based monkey-patching to a subscriber-based model. Instead of patching code directly, the agent subscribes to events emitted by Node.jsdiagnostic_channel TracingChannelobjects.This mechanism uses
@apm-js-collab/tracing-hooksandorchestrion-jsto inject tracing channels into instrumented packages. ASubscriberlistens to these channels for specific events such asasyncEnd,asyncStart,start,end, anderror. Context is preserved across these asynchronous boundaries usingAsyncLocalStorage.Open Source software in the New Relic Node.js Agent
mainThe New Relic Node.js agent may contain third-party and open source software (OSS). Each third-party component is governed by its own specific license and terms, which are disclosed in the provided licensing information.
If you require source code disclosure as required by the terms of an OSS component used within the agent, you can request it from New Relic by emailing
opensource@newrelic.com.What are Span Links and when to use them
mainSpan links are a mechanism used to describe how an entity (like a span) relates to the transaction that produced it. They are primarily used to support the propagation of metadata from OpenTelemetry instrumentations, particularly in distributed messaging systems where a single transaction might be decoupled from another via a queue.
Common Use Case: Distributed Messaging
- Producer: A system generates a message and posts it to a broker (e.g., RabbitMQ) within a unique transaction.
- Consumer: A separate part of the system monitors the queue and retrieves the message in a different, unique transaction.
- Linking: Span links associate the retrieved message back to the original transaction that posted it, allowing for trace continuity across asynchronous boundaries.
Ship OpenTelemetry Metrics to New Relic
mainThe New Relic Node.js agent automatically configures the OpenTelemetry Metrics API to ship metrics to New Relic.
To ensure metrics are not lost during the agent's initialization, the agent collects metrics in memory during its bootup sequence and flushes them once the sequence is complete. While the agent handles this automatically, for maximum robustness, you should wait for the agent's bootup to complete before initializing your metrics objects.
How module instrumentations work
mainAn instrumentation is a mechanism that allows the New Relic agent to run code immediately after a Node.js program loads a specific module via
require. This code can "wrap" (monkey-patch) module methods to create metrics, events, and segments.Instrumentations use helper objects called shims to perform shared tasks like naming a transaction or creating a segment.
There are three categories of instrumentations:
- Core Instrumentations: Instrument modules provided by the Node.js core system (e.g.,
http). - First Party Internal Instrumentations: Instrument npm packages (e.g.,
express), where the instrumentation code lives inside the agent. - First Party External Instrumentations: Instrument npm packages where the instrumentation lives as a standalone npm package (e.g.,
@newrelic/koa).
- Core Instrumentations: Instrument modules provided by the Node.js core system (e.g.,
How Spans are synthesized from TraceSegments
mainSpan creation depends on your configuration for distributed tracing and span events capture. The agent iterates over every segment in the trace and processes it based on the tracing granularity type:
1. Infinite Tracing
Creates a
StreamingSpanEventand adds it to theStreamingSpanEventAggregatorstream.2. Full Granularity Tracing
Creates a
SpanEventand adds it to theSpanEventAggregator.3. Partial Granularity Tracing
Runs logic based on the specific partial granularity type and enqueues the span back to the trace on a
trace.spansArray. If a span is dropped, its ID and parent are tracked in thetrace.droppedSpansmap to allow for reparenting later.Partial Granularity Types:
reduced: Creates aSpanEvent. It keeps entry point spans, LLM spans, and exit spans (e.g.,span.kindofclient) if they have at least one attribute used for entity relationships.essential: Usesreducedlogic, but also keeps all intrinsic attributes, only keeps agent attributes used for entity relationships, and drops all user attributes.compact: Usesreducedandessentiallogic, but further restricts the trace to only one exit span per entity.
Finalization in Partial Granularity: Unlike infinite or full granularity, partial granularity traces undergo a finalization process once spans are generated:
- Reparenting: Spans are re-parented when the type is
reducedoressential. If the type iscompact, all exit spans are re-parented to the entry point span. - Exit Span Attributes: For kept exit spans, the agent adds two specific attributes:
nr.ids: An array of exit span IDs that were dropped for the same entity.nr.durations: A float representing the total unique duration of all exit spans for that entity.
Understand transaction attributes
mainIn New Relic, attributes are key-value pairs used as metadata for transactions and segments. Keys are strings (which can be dot-separated paths), and values are either strings or numbers.
Attributes are categorized into three main buckets:
- Agent attributes: Metadata added by the agent for observability; these may be culled under certain conditions.
- User attributes: Metadata added by the developer using the agent's public API.
- Intrinsics: Required metadata defined by the protocol for specific event types.
Data is typically sent to the collector as a collection of these three buckets.
Understand the Node Agent transaction naming philosophy
mainUnlike other language agents that name transactions after the class or method handling the response, the Node Agent uses a routing-based naming scheme. This is designed to align with the middleware paradigm common in Node.js web frameworks (like Express).
Transactions are named based on the mounting paths of the middleware that eventually responds to the request. This approach ensures that transaction names reflect the API structure (routes) rather than internal function names, which are often anonymous in Node.js.