# eugenemsv/spring-micrometer-observation architecture

> Architecture diagram of the GitHub repository eugenemsv/spring-micrometer-observation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eugenemsv/spring-micrometer-observation
- Repository: https://github.com/eugenemsv/spring-micrometer-observation

## Overview

##### Project type and purpose
- This is a single-service Spring Boot demo application whose primary purpose is to showcase Micrometer’s new “Observation” API in action. It exposes a simple user CRUD REST API and pushes metrics to Prometheus for scraping.

##### File structure
- Top-level artifacts:
- Dockerfile, pom.xml, Maven wrappers → Java build + containerization
- compose/ → local Docker Compose setup for the service + Prometheus
- bruno/ → HTTP test scripts / Postman collection for exercising the API
- src/main/java/com/github/eugenemsv/spring/micrometer/observation → the service code
- Application entrypoint & runner
- ObservationConfiguration (Micrometer setup)
- Controller, Service, Observation handlers & context classes
- src/main/resources/application.yaml → service configuration (ports, metrics path)

##### README
- Confirms Java 21 + Docker + Docker Compose
- Instructs how to launch the microservice container + Prometheus, and to drive the APIs via the “bruno” collection

##### Main components
a. Client:
– Bruno test scripts or any HTTP client → issues REST calls to the service on port 8080
b. Spring Boot Microservice (single container):
– HTTP layer: UserController
– Business layer: UserService, UserCreation, etc.
– Observation layer (cross-cutting):
- ObservationConfiguration bean → configures Micrometer Observation registry
- LoggingObservationHandler, UserDeletionObserver → implement Observer pattern hooks
- UserObservationContext → context passed to handlers
– Metrics endpoint: /actuator/prometheus (or default Micrometer endpoint)
c. Prometheus (running in a separate container via Docker Compose):
– Scrapes the microservice’s metrics endpoint on port 8080 at regular intervals
– Serves a UI on port 9090 for querying metrics

##### Relationships and interactions
- Client → HTTP request → Microservice’s UserController → UserService → returns response
- At each observation point, the Observation API invokes registered handlers (logger, custom observers)
- Micrometer collects metrics (counters, timers) and exposes them on the /actuator/prometheus endpoint
- Prometheus container → scrapes metrics → stores time-series data → serves queries via its UI

##### Architectural patterns & design principles
- Layered architecture (Controller → Service → Data/Observation handlers)
- Observer pattern for measurement instrumentation
- Externalized configuration (application.yaml, ObservationConfiguration)
- Containerization & orchestration via Docker + Docker Compose (one network, two containers)

##### Technologies & frameworks
- Java 21, Spring Boot
- Spring Micrometer Observation API
- Micrometer registry → Prometheus
- Docker, Docker Compose
- Maven

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["HTTP Client<br>(bruno scripts)"]:::external
    click Client "https://github.com/eugenemsv/spring-micrometer-observation/tree/master/bruno/spring-micrometer-observation"

    %% Spring Boot Microservice Container
    subgraph "Spring Boot Microservice"
        direction TB

        subgraph "HTTP/API Layer"
            direction TB
            Controller["UserController"]:::internal
            click Controller "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/user/UserController.java"
        end

        subgraph "Business Layer"
            direction TB
            Service["UserService"]:::internal
            click Service "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/user/UserService.java"
            UserCreation["UserCreation"]:::internal
            click UserCreation "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/user/UserCreation.java"
        end

        subgraph "Observation Layer"
            direction TB
            ObsConfig["ObservationConfiguration"]:::internal
            click ObsConfig "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/ObservationConfiguration.java"
            LoggingHandler["LoggingObservationHandler"]:::internal
            click LoggingHandler "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/user/LoggingObservationHandler.java"
            DeletionObserver["UserDeletionObserver"]:::internal
            click DeletionObserver "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/user/UserDeletionObserver.java"
            ObsContext["UserObservationContext"]:::internal
            click ObsContext "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/user/UserObservationContext.java"
        end

        ApplicationEntry["SpringMicrometerObservationApplication"]:::internal
        click ApplicationEntry "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/SpringMicrometerObservationApplication.java"
        AppRunner["ApplicationRunner"]:::internal
        click AppRunner "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/java/com/github/eugenemsv/spring/micrometer/observation/ApplicationRunner.java"

        ConfigYAML["application.yaml"]:::config
        click ConfigYAML "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/src/main/resources/application.yaml"

        MetricsEndpoint["/actuator/prometheus"]:::internal
    end

    %% Prometheus Server Container
    subgraph "Prometheus Server"
        direction TB
        PromNode["Prometheus<br>(scrape UI)"]:::external
        PromConfig["prometheus.yml"]:::config
        click PromConfig "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/compose/prometheus.yml"
    end

    %% Dev/Test Tooling
    subgraph "Dev/Test Tooling"
        direction TB
        BRUNO["bruno/ spring-micrometer-observation"]:::external
        DockerCompose["docker-compose.local.yml"]:::config
        click DockerCompose "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/compose/docker-compose.local.yml"
        DockerfileNode["Dockerfile"]:::config
        click DockerfileNode "https://github.com/eugenemsv/spring-micrometer-observation/tree/master/Dockerfile"
        Pom["pom.xml"]:::config
        click Pom "https://github.com/eugenemsv/spring-micrometer-observation/blob/master/pom.xml"
    end

    %% Connections
    Client -->|"HTTP REST calls on :8080"| Controller
    Controller -->|calls| Service
    Service -->|observes| ObsConfig
    ObsConfig -->|invokes| LoggingHandler
    ObsConfig -->|invokes| DeletionObserver
    ObsConfig -->|passes context| ObsContext
    MetricsEndpoint -->|"scrape TCP on :8080"| PromNode

    %% Styles
    classDef internal fill:#e0ffe0,stroke:#0f0;
    classDef external fill:#e0f0ff,stroke:#00f;
    classDef config stroke-dasharray:5 5,stroke:#888,fill:#fff;
```
