# alibaba/opentelemetry-go-auto-instrumentation architecture

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

- Interactive diagram: https://gitdiagram.com/alibaba/opentelemetry-go-auto-instrumentation
- Repository: https://github.com/alibaba/opentelemetry-go-auto-instrumentation

## Overview

##### Main Components Overview

The system should be divided into these primary components:

a) Core Instrumentation Engine
- Located in `/pkg/core`
- Handles the fundamental instrumentation logic
- Manages meters and telemetry data collection

b) Rules Engine
- Located in `/pkg/rules`
- Contains all supported library instrumentations
- Manages different instrumentation strategies for various frameworks

c) Tool Chain
- Located in `/tool`
- Handles the compile-time instrumentation process
- Includes preprocessing, instrumentation, and resource management

##### Key Relationships to Highlight

**[Build Command (otel go build)] → [Tool Chain]**
- ↓
**[Source Code] → [Preprocessor] → [Instrumenter] → [Instrumented Binary]**
- ↓
**[Rules Engine] ← [Core Engine] → [OpenTelemetry SDK]**

##### Component Details

a) Tool Chain Layer:
- Preprocessing component (/tool/preprocess)
- Instrumentation component (/tool/instrument)
- Resource management (/tool/resource)
- Configuration handling (/tool/config)

b) Rules Layer:
- Framework-specific rules (HTTP, gRPC, Redis, etc.)
- Custom rule support
- Rule definition and processing

c) Core Layer:
- API implementations (/pkg/api)
- Instrumenter implementations (/pkg/inst-api)
- Metric collection (/pkg/core/meter)

##### Data Flow

Following flows:
**1. Build Flow:**
**Source Code → Preprocessor → Rule Application → Instrumented Code → Binary**

**2. Runtime Flow:**
**Application → Instrumented Code → OpenTelemetry SDK → Exporters**

##### Integration Points

Key integration points:
- OpenTelemetry SDK integration
- Framework/library hook points
- Exporter configurations
- Custom rule injection points

##### Additional Details

Important aspects:
- Configuration points
- Supported library versions (from the compatibility table)
- Extension points for custom rules
- Metric/trace collection paths

## Diagram (Mermaid)

```mermaid
flowchart TB
    classDef core fill:#b3d1ff,stroke:#333,stroke-width:2px
    classDef tool fill:#98fb98,stroke:#333,stroke-width:2px
    classDef rules fill:#fff59d,stroke:#333,stroke-width:2px
    classDef integration fill:#ffb3b3,stroke:#333,stroke-width:2px
    classDef data fill:#e6e6e6,stroke:#333,stroke-width:2px

    subgraph Build["Build Process"]
        BuildCmd["otel go build"]:::tool
        click BuildCmd "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/tool/cmd/main.go"
        
        subgraph ToolChain["Tool Chain"]
            Preproc["Preprocessor"]:::tool
            click Preproc "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/tool/preprocess/preprocess.go"
            Instr["Instrumenter"]:::tool
            click Instr "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/tool/instrument/instrument.go"
            Config["Configuration"]:::tool
            click Config "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/tool/config/config.go"
            Resource["Resource Management"]:::tool
            click Resource "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/tool/resource/resource.go"
        end
    end

    subgraph Core["Core Components"]
        CoreEngine["Core Engine"]:::core
        click CoreEngine "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/core"
        API["API Layer"]:::core
        click API "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/api"
        InstAPI["Instrumentation API"]:::core
        click InstAPI "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/inst-api"
        Metrics["Metrics Collection"]:::core
        click Metrics "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/pkg/core/meter/meter.go"
    end

    subgraph Rules["Rules Engine"]
        RulesCore["Rules Core"]:::rules
        click RulesCore "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/rules"
        HTTP["HTTP Rules"]:::rules
        click HTTP "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/rules/http"
        GRPC["gRPC Rules"]:::rules
        click GRPC "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/rules/grpc"
        DB["Database Rules"]:::rules
        click DB "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/rules/databasesql"
        Redis["Redis Rules"]:::rules
        click Redis "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/rules/goredis"
    end

    subgraph Integration["Integration Layer"]
        OTelSetup["OpenTelemetry Setup"]:::integration
        click OTelSetup "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/pkg/otel_setup.go"
        ExpConfig["Exporter Configuration"]:::integration
        click ExpConfig "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/pkg/exporter.go"
        SemConv["Semantic Conventions"]:::integration
        click SemConv "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/tree/main/pkg/inst-api-semconv"
    end

    subgraph Data["Core Data"]
        DefConfig["Default Configuration"]:::data
        click DefConfig "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/pkg/data/default.json"
        RuleDef["Rule Definitions"]:::data
        click RuleDef "https://github.com/alibaba/opentelemetry-go-auto-instrumentation/blob/main/tool/resource/ruledef.go"
    end

    BuildCmd --> Preproc
    Preproc --> Instr
    Instr --> CoreEngine
    Config --> Instr
    Resource --> Instr

    CoreEngine --> API
    CoreEngine --> InstAPI
    CoreEngine --> Metrics
    
    RulesCore --> HTTP
    RulesCore --> GRPC
    RulesCore --> DB
    RulesCore --> Redis
    
    CoreEngine --> RulesCore
    CoreEngine --> OTelSetup
    OTelSetup --> ExpConfig
    InstAPI --> SemConv
    
    DefConfig --> Config
    RuleDef --> Resource

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Tool Chain"]:::tool
        L3["Rules Engine"]:::rules
        L4["Integration Points"]:::integration
        L5["Configuration Data"]:::data
    end
```
