# newrelic/newrelic-browser-agent architecture

> Architecture diagram of the GitHub repository newrelic/newrelic-browser-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/newrelic/newrelic-browser-agent
- Repository: https://github.com/newrelic/newrelic-browser-agent

## Overview

##### Main Components

a. Core Agent Components:
- Browser Agent (main entry point)
- Micro Agent (lightweight version)
- Base Agent (customizable version)
- Feature Modules System

b. Feature Modules Layer:
- Ajax Monitoring
- JavaScript Error Tracking
- Metrics Collection
- Page View Events
- Session Replay
- SPA Monitoring
- Session Trace
- Generic Events

c. Common Infrastructure Layer:
- Configuration Management
- Event Aggregation
- Data Collection
- Storage Handling
- Timer Management
- URL Management

d. Integration Points:
- New Relic Backend Services
- Browser APIs
- DOM Interface
- Network Monitoring

**┌─ Browser Environment ──────────────────────────────────────────┐**

- ┌─ Browser Agent Instance ─────────────────────────┐         │

- ┌─ Core Services ─────┐  ┌─ Feature Modules ─┐  │         │
- Configuration      │  │- Ajax             │  │         │
- Event Emission     │  │- JSErrors         │  │         │
- Harvesting        │  │- Metrics          │  │         │
- Session Management │  │- Page View Event  │  │    DOM  │
- ┬─┘  │- Session Replay   │  │         │
- SPA             │  │         │
- ┌─ Common Layer ─┐ │     └────────┬─────────┘  │         │
- Aggregation   │ │              │            │         │
- Storage       │ │              │            │         │
- Timing        │ │              │            │         │
- URL Handling  │ │              │            │         │
- ┬┘ │              │            │         │

- ┘         │

- ┌─ Browser APIs ───┼───┼──────────────┼──────────────┐      │
- Performance    │   │              │              │      │
- Network        │   │              │              │      │
- Storage        │   │              │              │      │
- ┘      │

- ┘

**┌─ New Relic Backend ─│───│──────────────│──────────────────────┐**
- ▼   ▼              ▼                      │
- ┌─ Data Collection and Processing ─────────────────┐         │
- ┘         │
- ┘

##### Important Relationships
- Data flow from feature modules to core services
- Event propagation paths
- Integration points with browser APIs
- Communication channels with New Relic backend

##### Implementation Details
- Version/compatibility information
- Security boundaries

##### Performance Aspects
- Harvesting paths
- Data aggregation points
- Monitoring checkpoints
- Performance-critical paths

- Nested components
- Different line styles
- Clear text labels
- Export to common formats

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Browser["Browser Environment"]
        subgraph Agent["Browser Agent Instance"]
            subgraph Core["Core Services"]
                Config["Configuration"]:::core
                EventEmit["Event Emission"]:::core
                Harvest["Harvesting"]:::core
                Session["Session Management"]:::core
            end

            subgraph Features["Feature Modules"]
                Ajax["Ajax Monitoring"]:::feature
                JSErrors["JS Error Tracking"]:::feature
                Metrics["Metrics Collection"]:::feature
                PageView["Page View Events"]:::feature
                SessionReplay["Session Replay"]:::feature
                SPA["SPA Monitoring"]:::feature
                SessionTrace["Session Trace"]:::feature
                GenEvents["Generic Events"]:::feature
            end

            subgraph Common["Common Infrastructure"]
                Aggregation["Event Aggregation"]:::common
                Storage["Storage Handling"]:::common
                Timer["Timer Management"]:::common
                URLHandler["URL Management"]:::common
            end
        end

        subgraph BrowserAPIs["Browser APIs"]
            Performance["Performance API"]:::api
            Network["Network API"]:::api
            StorageAPI["Storage API"]:::api
            DOM["DOM Interface"]:::api
        end
    end

    subgraph Backend["New Relic Backend"]
        Collection["Data Collection & Processing"]:::backend
    end

    %% Relationships
    Core --> Common
    Features --> Core
    Common --> BrowserAPIs
    Core --> BrowserAPIs
    Features --> BrowserAPIs
    Agent --> Collection
    BrowserAPIs --> Collection

    %% Click Events
    click Ajax "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/ajax/"
    click JSErrors "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/jserrors/"
    click Metrics "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/metrics/"
    click PageView "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/page_view_event/"
    click SessionReplay "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/session_replay/"
    click SPA "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/spa/"
    click SessionTrace "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/session_trace/"
    click GenEvents "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/features/generic_events/"
    click Config "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/config/"
    click EventEmit "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/event-emitter/"
    click Harvest "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/harvest/"
    click Session "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/session/"
    click Aggregation "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/aggregate/"
    click Storage "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/storage/"
    click Timer "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/timer/"
    click URLHandler "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/url/"
    click DOM "https://github.com/newrelic/newrelic-browser-agent/tree/main/src/common/dom/"
    click Network "https://github.com/newrelic/newrelic-browser-agent/blob/main/src/common/wrap/wrap-xhr.js"
    click Agent "https://github.com/newrelic/newrelic-browser-agent/blob/main/src/loaders/browser-agent.js"

    %% Styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef feature fill:#4CAF50,stroke:#2E7D32,color:white
    classDef common fill:#FFC107,stroke:#FFA000,color:black
    classDef api fill:#9E9E9E,stroke:#616161,color:white
    classDef backend fill:#F44336,stroke:#C62828,color:white

    %% Legend
    subgraph Legend
        CoreLegend["Core Components"]:::core
        FeatureLegend["Feature Modules"]:::feature
        CommonLegend["Common Infrastructure"]:::common
        APILegend["APIs & Interfaces"]:::api
        BackendLegend["Backend Services"]:::backend
    end
```
