# newrelic/newrelic-ruby-agent architecture

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

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

## Overview

##### Main Components

a. Core Agent Components:
- Agent Core (centered around lib/new_relic/agent)
- Instrumentation Layer
- Data Collection & Processing
- Communication Layer
- Configuration Management

b. External Interfaces:
- New Relic Backend Services
- Monitored Application
- Various Framework Integrations

##### Key Architectural Elements

a. Data Flow Components:
**[Monitored Application] → [Instrumentation Layer] → [Data Collectors] → [Data Processing] → [New Relic Service]**

b. Instrumentation Categories:
- Framework Instrumentation (Rails, Sinatra, etc.)
- Database Monitoring (ActiveRecord, Sequel, etc.)
- External Services (HTTP, Redis, etc.)
- Background Jobs (Sidekiq, Resque, etc.)

##### Detailed Component Breakdown

a. Core Services Layer:
**┌─ Agent Core Services ─────────────────┐**
- Transaction Tracing                 │
- Error Collection                    │
- Metric Collection                   │
- Distributed Tracing                 │
- Performance Monitoring              │
- ┘

b. Instrumentation Layer:
**┌─ Instrumentation Framework ──────────┐**
- Method Tracers                     │
- Event Listeners                    │
- Middleware Hooks                   │
- Framework-specific Adapters        │
- ┘

##### Integration Points

- Framework Integration Points
- Database Monitoring Hooks
- External Service Monitoring
- Background Job Processing

##### Data Flow Representation

**[Application Code]**
- ↓
**[Instrumentation Hooks]**
- ↓
**[Data Aggregation]**
- ↓
**[Data Processing]**
- ↓
**[New Relic Backend]**

##### Key Components to Highlight

a. Monitoring Systems:
- Transaction Monitoring
- Error Tracking
- Performance Metrics
- Distributed Tracing

b. Configuration Management:
- YAML Configuration
- Environment Settings
- Security Policies

##### Implementation Details

Classes and their relationships:
**Agent**
- ↓
**Transaction**
- ↓
**Segments (Database, External, etc.)**
- ↓
**Metrics & Events**

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Legend"
        L1[Core Components]:::core
        L2[Instrumentation]:::instrumentation
        L3[Data Processing]:::data
        L4[External Communication]:::communication
        L5[Configuration]:::config
    end

    subgraph "Core Agent Layer"
        AC["Agent Core"]:::core
        TM["Transaction Monitoring"]:::core
        ET["Error Tracking"]:::core
        DT["Distributed Tracing"]:::core
        PM["Performance Monitoring"]:::core
    end

    subgraph "Instrumentation Layer"
        IL["Instrumentation Framework"]:::instrumentation
        FI["Framework Integration"]:::instrumentation
        DM["Database Monitoring"]:::instrumentation
        ES["External Services"]:::instrumentation
        BJ["Background Jobs"]:::instrumentation
    end

    subgraph "Data Processing Layer"
        DC["Data Collection"]:::data
        DP["Data Processing"]:::data
        MA["Metrics Aggregation"]:::data
        EA["Event Aggregation"]:::data
    end

    subgraph "Communication Layer"
        CL["Communication Layer"]:::communication
        JM["JSON Marshaller"]:::communication
    end

    subgraph "Configuration Layer"
        CM["Configuration Management"]:::config
        YC["YAML Configuration"]:::config
        SP["Security Policies"]:::config
    end

    MA --> CL
    EA --> CL
    CL --> NR["New Relic Backend"]:::external

    APP["Monitored Application"]:::external --> IL
    IL --> AC
    AC --> DC
    DC --> DP
    DP --> MA
    DP --> EA

    IL --> FI
    IL --> DM
    IL --> ES
    IL --> BJ

    AC --> TM
    AC --> ET
    AC --> DT
    AC --> PM

    CM --> YC
    CM --> SP
    CM --> AC

    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef instrumentation fill:#4CAF50,stroke:#2E7D32,color:white
    classDef data fill:#FF9800,stroke:#EF6C00,color:white
    classDef communication fill:#9C27B0,stroke:#6A1B9A,color:white
    classDef config fill:#757575,stroke:#424242,color:white
    classDef external fill:#ECEFF1,stroke:#90A4AE,color:black

    click AC "https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/new_relic/agent/agent.rb"
    click IL "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/instrumentation/"
    click DC "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/samplers/"
    click CM "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/configuration/"
    click CL "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/new_relic_service/"
    click JM "https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/new_relic/agent/new_relic_service/json_marshaller.rb"
    click TM "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/transaction/"
    click ET "https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/new_relic/agent/error_collector.rb"
    click DM "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/database/"
    click FI "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/instrumentation/rails/"
    click DT "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/distributed_tracing/"
    click MA "https://github.com/newrelic/newrelic-ruby-agent/tree/dev/lib/new_relic/agent/stats_engine/"
    click SP "https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/new_relic/agent/configuration/security_policy_source.rb"
    click YC "https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/new_relic/agent/configuration/yaml_source.rb"
```
