Repository Layout
Backend Architecture (pkg/)
The backend is written in Go and organized by domain and function:
| Directory | Purpose |
|---|---|
pkg/api/ | HTTP API handlers and routes |
pkg/services/ | Business logic by domain (alerting, dashboards, auth, etc.) |
pkg/server/ | Server initialization and Wire dependency injection setup |
pkg/tsdb/ | Time series database query backends |
pkg/plugins/ | Plugin system and loader |
pkg/infra/ | Logging, metrics, database access |
pkg/middleware/ | HTTP middleware |
pkg/setting/ | Configuration management |
Backend Patterns
- Dependency Injection: Uses Wire (regenerate with
make gen-goafter service changes) - Service Architecture: Services implement interfaces in the same package; business logic lives in
pkg/services/<domain>/, not in API handlers - Database Access: Via
sqlstore - Plugin Communication: gRPC/protobuf
- Separate PRs: Frontend and backend changes should be in separate PRs (different deployment cadences)
Key Backend Directories
pkg/api/ - HTTP API
pkg/api/ - HTTP API
Contains HTTP API handlers and routing logic. API handlers should be thin and delegate business logic to services.
pkg/services/ - Business Logic
pkg/services/ - Business Logic
Organized by domain:
alerting/- Alerting systemdashboards/- Dashboard managementauth/- Authenticationsqlstore/- Database access layerfeaturemgmt/- Feature toggles (auto-generates code)- And 70+ other services
pkg/tsdb/ - Data Source Backends
pkg/tsdb/ - Data Source Backends
Query backends for time series databases. Each data source has its own subdirectory.
pkg/plugins/ - Plugin System
pkg/plugins/ - Plugin System
Plugin discovery, loading, and management. Plugins communicate via gRPC.
Frontend Architecture (public/app/)
The frontend is built with TypeScript and React:
| Directory | Purpose |
|---|---|
public/app/core/ | Shared services, components, utilities |
public/app/features/ | Feature code by domain (dashboard, alerting, explore) |
public/app/plugins/ | Built-in plugins (many are Yarn workspaces) |
public/app/types/ | TypeScript type definitions |
public/app/store/ | Redux store configuration |
Frontend Patterns
- State Management: Redux Toolkit with slices (not old Redux patterns)
- Components: Function components with hooks (not class components)
- Styling: Emotion CSS-in-JS via
useStyles2 - Data Fetching: RTK Query
- Testing: React Testing Library
Key Frontend Directories
public/app/core/ - Core Framework
public/app/core/ - Core Framework
Shared utilities, services, and components used across features:
services/- Core services (backend API, context, etc.)components/- Reusable UI componentsutils/- Utility functions
public/app/features/ - Feature Code
public/app/features/ - Feature Code
Each feature has its own directory with components, state, and logic:
dashboard/- Dashboard functionalityalerting/- Alerting UIexplore/- Explore interfacepanel/- Panel framework- 50+ other features organized by domain
public/app/plugins/ - Built-in Plugins
public/app/plugins/ - Built-in Plugins
Core plugins that ship with Grafana. Many are separate Yarn workspaces requiring independent builds.See Plugin Workspaces below.
Shared Packages (packages/)
Reusable packages used across the Grafana ecosystem:
| Package | Purpose |
|---|---|
@grafana/data | Core data structures, types, and utilities |
@grafana/ui | Reusable UI component library |
@grafana/runtime | Runtime services and plugin SDK |
@grafana/schema | CUE-generated TypeScript types |
@grafana/scenes | Dashboard framework |
These packages are managed as Yarn workspaces and can be published independently.
Backend Apps (apps/)
Standalone Go applications built using the Grafana App SDK:
apps/dashboard/- Dashboard serviceapps/folder/- Folder managementapps/alerting/- Alerting service
Plugin Workspaces
These built-in plugins require separate build steps:azuremonitorcloud-monitoringgrafana-postgresql-datasourcelokitempojaegermysqlparcazipkingrafana-pyroscope-datasourcegrafana-testdata-datasource
Configuration
- Defaults:
conf/defaults.ini - Overrides:
conf/custom.ini - Build Tags:
oss(default),enterprise,pro
Schema and Code Generation
CUE Schemas (kinds/)
Dashboard and panel schemas are defined in CUE and generate both Go and TypeScript code:
kinds/.
Wire Dependency Injection
Backend service initialization uses Wire for compile-time dependency injection:Feature Toggles
Feature flags are defined inpkg/services/featuremgmt/ and auto-generate code:
Go Workspace
Defined ingo.work. Update after adding Go modules:
Database Migrations
Database schema migrations live inpkg/services/sqlstore/migrations/.
Test with:
Testing Structure
- Frontend tests:
*.test.ts,*.test.tsxfiles alongside source code - Backend tests:
*_test.gofiles in Go packages - E2E tests:
e2e-playwright/directory
Key Files
| File | Purpose |
|---|---|
Makefile | Build commands and targets |
package.json | Frontend dependencies and scripts |
go.mod | Go dependencies |
go.work | Go workspace configuration |
tsconfig.json | TypeScript configuration |
.air.toml | Hot-reload configuration for make run |
AGENTS.md | AI agent guidance for development |
CONTRIBUTING.md | Contribution guidelines |
Next Steps
Building
Build and run Grafana locally
Testing
Run tests for your changes