New Relic Node.js Agent

repository·main·Indexed 21 days ago

https://github.com/newrelic/node-newrelic

An 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.

Tokens
68.2K
Snippets
177
Records
295
Agent score
76%

What's inside newrelic-node-newrelic

  1. Filter transaction attributes

    main

    Attributes 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 connect event.
  2. Automatic instrumentation of supported Node.js modules

    main
    Once 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.
  3. Usage restrictions and licensing for the New Relic Node.js Agent

    main

    The 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.

  4. How subscriber-based instrumentation works

    main

    Starting 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.js diagnostic_channel TracingChannel objects.

    This mechanism uses @apm-js-collab/tracing-hooks and orchestrion-js to inject tracing channels into instrumented packages. A Subscriber listens to these channels for specific events such as asyncEnd, asyncStart, start, end, and error. Context is preserved across these asynchronous boundaries using AsyncLocalStorage.

  5. Open Source software in the New Relic Node.js Agent

    main

    The 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.

  6. What are Span Links and when to use them

    main

    Span 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

    1. Producer: A system generates a message and posts it to a broker (e.g., RabbitMQ) within a unique transaction.
    2. Consumer: A separate part of the system monitors the queue and retrieves the message in a different, unique transaction.
    3. Linking: Span links associate the retrieved message back to the original transaction that posted it, allowing for trace continuity across asynchronous boundaries.
  7. Ship OpenTelemetry Metrics to New Relic

    main

    The 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.

  8. How module instrumentations work

    main

    An 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:

    1. Core Instrumentations: Instrument modules provided by the Node.js core system (e.g., http).
    2. First Party Internal Instrumentations: Instrument npm packages (e.g., express), where the instrumentation code lives inside the agent.
    3. First Party External Instrumentations: Instrument npm packages where the instrumentation lives as a standalone npm package (e.g., @newrelic/koa).
  9. How Spans are synthesized from TraceSegments

    main

    Span 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 StreamingSpanEvent and adds it to the StreamingSpanEventAggregator stream.

    2. Full Granularity Tracing

    Creates a SpanEvent and adds it to the SpanEventAggregator.

    3. Partial Granularity Tracing

    Runs logic based on the specific partial granularity type and enqueues the span back to the trace on a trace.spans Array. If a span is dropped, its ID and parent are tracked in the trace.droppedSpans map to allow for reparenting later.

    Partial Granularity Types:

    • reduced: Creates a SpanEvent. It keeps entry point spans, LLM spans, and exit spans (e.g., span.kind of client) if they have at least one attribute used for entity relationships.
    • essential: Uses reduced logic, but also keeps all intrinsic attributes, only keeps agent attributes used for entity relationships, and drops all user attributes.
    • compact: Uses reduced and essential logic, 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 reduced or essential. If the type is compact, 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.
  10. Understand transaction attributes

    main

    In 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:

    1. Agent attributes: Metadata added by the agent for observability; these may be culled under certain conditions.
    2. User attributes: Metadata added by the developer using the agent's public API.
    3. 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.

  11. Understand the Node Agent transaction naming philosophy

    main

    Unlike 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.