//go:build mage // +build mage package main import ( "fmt" "os" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" ) const ( binaryName = "epimetheus" mainPath = "./cmd/epimetheus" ) // Default target to run when none is specified var Default = Build // Build compiles the epimetheus binary func Build() error { fmt.Println("Building epimetheus...") return sh.RunV("go", "build", "-o", binaryName, mainPath) } // Install installs the binary to $GOPATH/bin func Install() error { fmt.Println("Installing epimetheus...") return sh.RunV("go", "install", mainPath) } // Run executes the epimetheus binary in realtime mode func Run() error { mg.Deps(Build) fmt.Println("Running epimetheus in realtime mode...") return sh.RunV("./"+binaryName, "-mode=realtime", "-continuous") } // RunHistoric runs epimetheus in historic mode func RunHistoric() error { mg.Deps(Build) fmt.Println("Running epimetheus in historic mode (24 hours ago)...") return sh.RunV("./"+binaryName, "-mode=historic", "-hours-ago=24") } // RunAuto runs epimetheus in auto mode with a file func RunAuto(file string) error { mg.Deps(Build) if file == "" { return fmt.Errorf("file parameter required: mage RunAuto ") } fmt.Printf("Running epimetheus in auto mode with file: %s\n", file) return sh.RunV("./"+binaryName, "-mode=auto", "-file="+file) } // RunWatchClickHouse runs epimetheus in watch mode with ClickHouse ingestion func RunWatchClickHouse(file string) error { mg.Deps(Build) if file == "" { file = "test-data/watch-clickhouse-test.csv" } fmt.Printf("Running epimetheus in watch mode with ClickHouse (file: %s)\n", file) return sh.RunV("./"+binaryName, "-mode=watch", "-file="+file, "-metric-name=watch_test", "-clickhouse=http://localhost:8123", "-prometheus=") } // Test runs all tests func Test() error { fmt.Println("Running tests...") return sh.RunV("go", "test", "./...", "-v") } // TestCoverage runs tests with coverage report func TestCoverage() error { fmt.Println("Running tests with coverage...") if err := sh.RunV("go", "test", "./...", "-cover", "-coverprofile=coverage.out"); err != nil { return err } return sh.RunV("go", "tool", "cover", "-html=coverage.out", "-o", "coverage.html") } // TestRace runs tests with race detector func TestRace() error { fmt.Println("Running tests with race detector...") return sh.RunV("go", "test", "./...", "-race", "-v") } // Benchmark runs all benchmarks func Benchmark() error { fmt.Println("Running benchmarks...") return sh.RunV("go", "test", "./...", "-bench=.", "-benchmem") } // Lint runs golangci-lint func Lint() error { fmt.Println("Running linter...") return sh.RunV("golangci-lint", "run", "./...") } // Fmt formats all Go code func Fmt() error { fmt.Println("Formatting code...") return sh.RunV("go", "fmt", "./...") } // Vet runs go vet func Vet() error { fmt.Println("Running go vet...") return sh.RunV("go", "vet", "./...") } // Tidy runs go mod tidy func Tidy() error { fmt.Println("Tidying dependencies...") return sh.RunV("go", "mod", "tidy") } // Clean removes build artifacts func Clean() error { fmt.Println("Cleaning build artifacts...") files := []string{ binaryName, "coverage.out", "coverage.html", } for _, f := range files { if err := sh.Rm(f); err != nil && !os.IsNotExist(err) { return err } } return nil } // Generate runs go generate func Generate() error { fmt.Println("Running go generate...") return sh.RunV("go", "generate", "./...") } // Version prints the version func Version() error { fmt.Println("Printing version...") mg.Deps(Build) return sh.RunV("./"+binaryName, "-version") } // All runs format, vet, test, and build func All() { mg.Deps(Fmt, Vet, Test, Build) } // CI runs the full CI pipeline (format check, vet, test, build) func CI() error { fmt.Println("Running CI pipeline...") mg.Deps(Tidy, Vet, Test) return Build() } // Dev starts development mode with port-forwarding func Dev() error { mg.Deps(Build) fmt.Println("Starting development mode...") fmt.Println("Setting up port-forward to Pushgateway...") // Start port-forward in background portForwardCmd := sh.RunCmd("kubectl", "port-forward", "-n", "monitoring", "svc/pushgateway", "9091:9091") go func() { if err := portForwardCmd(); err != nil { fmt.Printf("Port-forward error: %v\n", err) } }() fmt.Println("Running epimetheus in realtime mode...") return sh.RunV("./"+binaryName, "-mode=realtime", "-continuous") } // GenerateTestData creates test data files func GenerateTestData() error { fmt.Println("Generating test data...") return sh.RunV("./generate-test-data.sh") } // Backfill runs backfill for the last 48 hours func Backfill() error { mg.Deps(Build) fmt.Println("Running backfill (last 48 hours)...") return sh.RunV("./"+binaryName, "-mode=backfill", "-start-hours=48", "-end-hours=0", "-interval=1") } // Benchmark100MB runs the 100MB benchmark func Benchmark100MB() error { fmt.Println("Running 100MB benchmark...") return sh.RunV("./benchmark-100mb.sh") } // Benchmark1GB runs the 1GB benchmark func Benchmark1GB() error { fmt.Println("Running 1GB benchmark...") return sh.RunV("./benchmark-1gb.sh") } // CleanupBenchmarkData removes benchmark data from Prometheus func CleanupBenchmarkData() error { fmt.Println("Cleaning up benchmark data...") return sh.RunV("./cleanup-benchmark-data.sh") } // CleanupBenchmarkMetrics removes benchmark metric files func CleanupBenchmarkMetrics() error { fmt.Println("Cleaning up benchmark metric files...") return sh.RunV("./cleanup-benchmark-metrics.sh") } // DeployDashboard deploys the Grafana dashboard func DeployDashboard() error { fmt.Println("Deploying Grafana dashboard...") return sh.RunV("./deploy-dashboard.sh") } // Help prints available targets func Help() { fmt.Println("Available targets:") fmt.Println(" build - Build the epimetheus binary (default)") fmt.Println(" install - Install the binary to $GOPATH/bin") fmt.Println(" run - Build and run in realtime mode") fmt.Println(" runHistoric - Build and run in historic mode") fmt.Println(" runAuto - Build and run in auto mode with file") fmt.Println(" runWatchClickHouse [file] - Build and run watch mode with ClickHouse (default: test-data/watch-clickhouse-test.csv)") fmt.Println(" test - Run all tests") fmt.Println(" testCoverage - Run tests with coverage report") fmt.Println(" testRace - Run tests with race detector") fmt.Println(" benchmark - Run Go benchmarks") fmt.Println(" lint - Run golangci-lint") fmt.Println(" fmt - Format all Go code") fmt.Println(" vet - Run go vet") fmt.Println(" tidy - Run go mod tidy") fmt.Println(" clean - Remove build artifacts") fmt.Println(" generate - Run go generate") fmt.Println(" version - Print version") fmt.Println(" all - Run fmt, vet, test, and build") fmt.Println(" ci - Run full CI pipeline") fmt.Println(" dev - Start development mode with port-forwarding") fmt.Println(" generateTestData - Generate test data files") fmt.Println(" backfill - Run backfill for last 48 hours") fmt.Println(" benchmark100MB - Run 100MB benchmark") fmt.Println(" benchmark1GB - Run 1GB benchmark") fmt.Println(" cleanupBenchmarkData - Clean up benchmark data from Prometheus") fmt.Println(" cleanupBenchmarkMetrics - Clean up benchmark metric files") fmt.Println(" deployDashboard - Deploy Grafana dashboard") fmt.Println(" help - Print this help message") }