Skip to content

Commit

Permalink
fix: re-org service definition
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Apr 19, 2024
1 parent 329a845 commit 4e7162b
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 33 deletions.
8 changes: 7 additions & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "2"
version: "3"

tasks:
portf:
Expand All @@ -10,3 +10,9 @@ tasks:
remote: '{{ .remote | default "9090" }}'
cmds:
- kubectl -n {{ .ns }} port-forward svc/{{ .svc }} {{ .local }}:{{ .remote }}

query:
desc: Run the query sub-command
silent: true
cmds:
- go run main.go query
28 changes: 13 additions & 15 deletions cmd/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ package query
import (
"fmt"

"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/querier"
"github.com/henrywhitaker3/prompage/internal/app"
"github.com/spf13/cobra"
)

func NewQueryCommand(conf *config.Config) *cobra.Command {
func NewQueryCommand(app *app.App) *cobra.Command {
return &cobra.Command{
Use: "query",
Short: "Run the configured queries and output the results",
RunE: func(cmd *cobra.Command, args []string) error {
q, err := querier.NewQuerier(conf)
if err != nil {
return err
}
results := app.Collector.Collect(cmd.Context())

for _, service := range conf.Services {
fmt.Printf("Querying for service '%s'\n", service.Name)
for _, query := range service.Queries {
res, err := q.Status(cmd.Context(), query)
if err != nil {
fmt.Println(fmt.Errorf("query service error: %v", err))
}
fmt.Printf(" - Query: '%s': %v\n", query.Name, res)
for _, result := range results {
fmt.Printf("Service '%s'\n", result.Service.Name)
status := "down"
if result.Status {
status = "up"
}
if !result.Success {
status = "unknown"
}
fmt.Printf(" Status: %s\n", status)
fmt.Printf(" Scrape successful: %t\n", result.Success)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"github.com/henrywhitaker3/prompage/cmd/query"
"github.com/henrywhitaker3/prompage/cmd/serve"
"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/app"
"github.com/spf13/cobra"
)

Expand All @@ -14,7 +14,7 @@ func NewRootCmd() *cobra.Command {
}
}

func LoadSubCommands(cmd *cobra.Command, conf *config.Config) {
cmd.AddCommand(serve.NewServeCommand(conf))
cmd.AddCommand(query.NewQueryCommand(conf))
func LoadSubCommands(cmd *cobra.Command, app *app.App) {
cmd.AddCommand(serve.NewServeCommand(app))
cmd.AddCommand(query.NewQueryCommand(app))
}
6 changes: 3 additions & 3 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"os/signal"
"syscall"

"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/app"
"github.com/henrywhitaker3/prompage/internal/http"
"github.com/spf13/cobra"
)

func NewServeCommand(conf *config.Config) *cobra.Command {
func NewServeCommand(app *app.App) *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "Run the status page http server",
Expand All @@ -29,7 +29,7 @@ func NewServeCommand(conf *config.Config) *cobra.Command {
cancel()
}()

http := http.NewHttp(conf)
http := http.NewHttp(app)

go func() {
if err := http.Serve(); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app

import (
"github.com/henrywhitaker3/prompage/internal/collector"
"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/querier"
)

type App struct {
Config *config.Config
Querier *querier.Querier
Collector *collector.Collector
}

func NewApp(conf *config.Config, q *querier.Querier) *App {
app := &App{
Config: conf,
Querier: q,
}

app.Collector = collector.NewCollector(app.Querier, conf.Services)

return app
}
56 changes: 56 additions & 0 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package collector

import (
"context"
"log"

"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/querier"
)

type Result struct {
// The service the result corresponds to
Service config.Service
// Whether the collection was successful or not
Success bool
// The boolean result of the main service query
Status bool
}

type Collector struct {
q *querier.Querier
svcs []config.Service
}

func NewCollector(q *querier.Querier, svcs []config.Service) *Collector {
return &Collector{
q: q,
svcs: svcs,
}
}

func (c *Collector) Collect(ctx context.Context) []Result {
results := []Result{}

for _, svc := range c.svcs {
res := Result{
Service: svc,
}
log.Printf("collecting metrics for %s\n", svc.Name)

status, err := c.q.Status(ctx, svc.Query)
if err != nil {
log.Printf("ERROR - Failed to scrape status metric for %s query %s: %s", svc.Name, svc.Query.Name, err)
res.Success = false
res.Status = false
results = append(results, res)
continue
}

res.Success = true
res.Status = status
results = append(results, res)
}

return results
}
10 changes: 8 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type Query struct {
}

type Service struct {
Name string `yaml:"name"`
Queries []Query `yaml:"queries"`
Name string `yaml:"name"`
Query Query `yaml:"query"`
// Extras []Query `yaml:"extras"`
}

type Config struct {
Expand Down Expand Up @@ -55,6 +56,11 @@ func setDefaults(conf *Config) {
if conf.Port == 0 {
conf.Port = 3000
}

for i, svc := range conf.Services {
svc.Query.Name = "main"
conf.Services[i] = svc
}
}

func (c *Config) Validate() error {
Expand Down
14 changes: 7 additions & 7 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import (
"context"
"fmt"

"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/app"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
)

type Http struct {
e *echo.Echo
conf *config.Config
e *echo.Echo
app *app.App
}

func NewHttp(conf *config.Config) *Http {
func NewHttp(app *app.App) *Http {
e := echo.New()

e.Use(echoprometheus.NewMiddleware("prompage"))

return &Http{
e: e,
conf: conf,
e: e,
app: app,
}
}

func (h *Http) Serve() error {
return h.e.Start(fmt.Sprintf(":%d", h.conf.Port))
return h.e.Start(fmt.Sprintf(":%d", h.app.Config.Port))
}

func (h *Http) Stop(ctx context.Context) error {
Expand Down
28 changes: 28 additions & 0 deletions internal/http/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package http

import (
"sync"

"github.com/henrywhitaker3/prompage/internal/app"
"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/querier"
)

type Result struct {
Query config.Query
Status bool
}

type ResultCache struct {
mu *sync.Mutex
querier *querier.Querier
results map[string][]Result
}

func NewResultCache(app *app.App) *ResultCache {
return &ResultCache{
mu: &sync.Mutex{},
querier: app.Querier,
results: map[string][]Result{},
}
}
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"os"

"github.com/henrywhitaker3/prompage/cmd"
"github.com/henrywhitaker3/prompage/internal/app"
"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/querier"
"github.com/spf13/pflag"
)

Expand All @@ -25,7 +27,15 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
cmd.LoadSubCommands(root, conf)
q, err := querier.NewQuerier(conf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

app := app.NewApp(conf, q)

cmd.LoadSubCommands(root, app)

if err := root.Execute(); err != nil {
os.Exit(2)
Expand Down

0 comments on commit 4e7162b

Please sign in to comment.