Skip to content

Repository files navigation

devops-tpl

A Go-based metrics monitoring template: an agent that collects runtime/system metrics and a server that receives, stores and exposes them over HTTP. Storage can be in-memory, file-based, or PostgreSQL (with SQL migrations). Includes Docker Compose setups for local development and integration testing.

Originally scaffolded from the Yandex Praktikum "Go in DevOps" course template, it can be used as a starting point for building your own metrics collection / monitoring pipeline in Go.

What it does

  • Agent (cmd/agent) — periodically collects runtime (gopsutil) and Go runtime metrics (gauges & counters) and pushes them to the server over HTTP.
  • Server (cmd/server) — accepts metrics via HTTP API (built on chi router), persists them, and exposes endpoints to read stored values.
  • Storage backends — in-memory, local file (STORE_FILE), or PostgreSQL via DATABASE_DSN, with schema migrations (migrations/, pressly/goose).
  • Config — via environment variables / flags (ilyakaznacheev/cleanenv).
  • Tests — unit tests per component and an integration test suite (integration-test/) run against a real Postgres instance via Docker Compose.

Tech stack

  • Language: Go 1.18
  • HTTP router: go-chi/chi
  • HTTP client: go-resty/resty
  • Database: PostgreSQL, jackc/pgx, lib/pq, Masterminds/squirrel (query builder), georgysavva/scany (scanning)
  • Migrations: pressly/goose
  • Config: ilyakaznacheev/cleanenv
  • System metrics: shirou/gopsutil
  • Logging: rs/zerolog
  • Testing: stretchr/testify, golang/mock, Eun/go-hit (integration HTTP testing)
  • Containerization: Docker, Docker Compose

Quick start

Prerequisites

  • Go 1.18+
  • Docker & Docker Compose (for the containerized workflow / PostgreSQL storage)

Clone & init

git clone https://github.com/costynus/devops-tpl.git
cd devops-tpl
go mod tidy

Run locally with Docker Compose (recommended)

make build   # docker-compose build
make run     # docker-compose up (starts postgres + app)

The server listens on :8080, PostgreSQL is exposed on :5432.

Run agent/server directly with Go

make run-server   # go run ./cmd/server/main.go
make run-agent    # go run ./cmd/agent/main.go

Run tests

make unit-test                    # unit tests for ./internal/...
make test                         # unit tests (integration TODO)
make compose-up-integration-test  # integration tests via docker-compose.test.yaml

Architecture

cmd/
  agent/        # entrypoint: metrics collector/reporter
  server/       # entrypoint: HTTP server (metrics receiver/storage)
internal/
  app/            # application bootstrap (agent & server wiring)
  entity/         # domain models (metric types, etc.)
  usecase/        # business logic (collecting, storing, reading metrics)
  infrastructure/ # HTTP handlers/clients, storage adapters (memory/file/postgres)
migrations/     # SQL migrations (goose)
config/         # configuration structs/loading
pkg/            # shared reusable packages
docker/         # Dockerfiles (local, etc.)
integration-test/ # end-to-end tests against a running stack
docker-compose.yaml       # local dev stack (app + postgres)
docker-compose.test.yaml  # integration-test stack
Makefile        # common dev/test/build/run tasks

Data flow: agent collects metrics → sends them via HTTP to serverserver's infrastructure layer parses requests → usecase layer applies business logic → metrics persisted through the configured storage adapter (memory, file, or Postgres via entity/migrations).

Configuration

Key environment variables:

Variable Used by Description
ADDRESS server HTTP listen address (e.g. :8080)
KEY both Signing key for metric hash validation
STORE_FILE server Path to file-based storage (if not using DB)
DATABASE_DSN server PostgreSQL DSN, enables DB-backed storage

Updating from the upstream template

To pull in updates to autotests and other template parts:

git remote add -m main template https://github.com/yandex-praktikum/go-musthave-devops-tpl.git
git fetch template && git checkout template/main .github

Then commit the resulting changes to your repository.

About

Go metrics monitoring template: agent + server with in-memory/file/Postgres storage

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages