Repository Layout
Backend Architecture (pkg/)
The backend is written in Go and organized by domain and function:
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:
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:
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
Next Steps
Building
Build and run Grafana locally
Testing
Run tests for your changes