Skip to content

Commit

Permalink
Merge pull request #25 from qclaogui/lgtmp-command-line
Browse files Browse the repository at this point in the history
Take Grafana LGTMP Stack to the command line
  • Loading branch information
qclaogui committed Jan 17, 2024
2 parents b30bf57 + 9faec40 commit e27eb41
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ toolchain go1.21.5

require (
dagger.io/dagger v0.9.6
github.com/MakeNowJust/heredoc v1.0.0
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/common v0.46.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
)

Expand All @@ -17,13 +19,15 @@ require (
github.com/adrg/xdg v0.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sosodev/duration v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vektah/gqlparser/v2 v2.5.10 // indirect
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/99designs/gqlgen v0.17.41 h1:C1/zYMhGVP5TWNCNpmZ9Mb6CqT1Vr5SHEWoTOEJ3
github.com/99designs/gqlgen v0.17.41/go.mod h1:GQ6SyMhwFbgHR0a8r2Wn8fYgEwPxxmndLFPhU63+cJE=
github.com/Khan/genqlient v0.6.0 h1:Bwb1170ekuNIVIwTJEqvO8y7RxBxXu639VJOkKSrwAk=
github.com/Khan/genqlient v0.6.0/go.mod h1:rvChwWVTqXhiapdhLDV4bp9tz/Xvtewwkon4DpWWCRM=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
Expand All @@ -12,6 +14,7 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -21,6 +24,8 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down Expand Up @@ -50,10 +55,15 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/sosodev/duration v1.2.0 h1:pqK/FLSjsAADWY74SyWDCjOcd5l7H8GSnnOGEB9A1Us=
github.com/sosodev/duration v1.2.0/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
48 changes: 48 additions & 0 deletions internal/cmd/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <qclaogui@gmail.com>
//
// Licensed under the Apache License 2.0.

package all

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode"}
var mode string

func NewCmdAll() *cobra.Command {
var allCmd = &cobra.Command{
Short: "Run Grafana LGTMP Stack All-in-one.",
Use: "all",
Example: heredoc.Doc(`
# Start up all in monolithic-mode
$ lgtmp up all
# Start up all in microservices-mode
$ lgtmp up all --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-all-in-one Run monolithic-mode all-in-one
// deploy-monolithic-mode-all-in-one Deploy monolithic-mode all-in-one
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-all-in-one", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

allCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for all-in-one. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return allCmd
}
48 changes: 48 additions & 0 deletions internal/cmd/logs/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <qclaogui@gmail.com>
//
// Licensed under the Apache License 2.0.

package logs

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "read-write-mode", "microservices-mode"}
var mode string

func NewCmdLogs() *cobra.Command {
var logsCmd = &cobra.Command{
Short: "Run Loki for Logs.",
Use: "logs",
Example: heredoc.Doc(`
# Start up logs in monolithic-mode
$ lgtmp up logs
# Start up logs in microservices-mode
$ lgtmp up logs --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-logs Run monolithic-mode logs
// deploy-monolithic-mode-logs Deploy monolithic-mode logs
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-logs", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

logsCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for logs. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return logsCmd
}
48 changes: 48 additions & 0 deletions internal/cmd/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <qclaogui@gmail.com>
//
// Licensed under the Apache License 2.0.

package metrics

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "read-write-mode", "microservices-mode"}
var mode string

func NewCmdMetrics() *cobra.Command {
var metricsCmd = &cobra.Command{
Short: "Run Mimir for Metrics.",
Use: "metrics",
Example: heredoc.Doc(`
# Start up metrics in monolithic-mode
$ lgtmp up metrics
# Start up metrics in microservices-mode
$ lgtmp up metrics --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-metrics Run monolithic-mode metrics
// deploy-monolithic-mode-metrics Deploy monolithic-mode metrics
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-metrics", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

metricsCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for metrics. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return metricsCmd
}
48 changes: 48 additions & 0 deletions internal/cmd/profiles/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <qclaogui@gmail.com>
//
// Licensed under the Apache License 2.0.

package profiles

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "microservices-mode"}
var mode string

func NewCmdProfiles() *cobra.Command {
var profilesCmd = &cobra.Command{
Short: "Run Pyroscope for Profiles.",
Use: "profiles",
Example: heredoc.Doc(`
# Start up profiles in monolithic-mode
$ lgtmp up profiles
# Start up profiles in microservices-mode
$ lgtmp up profiles --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-profiles Run monolithic-mode profiles
// deploy-monolithic-mode-profiles Deploy monolithic-mode profiles
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-profiles", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

profilesCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for profiles. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return profilesCmd
}
48 changes: 48 additions & 0 deletions internal/cmd/traces/traces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © Weifeng Wang <qclaogui@gmail.com>
//
// Licensed under the Apache License 2.0.

package traces

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/qclaogui/codelab-monitoring/internal"

"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "microservices-mode"}
var mode string

func NewCmdTraces() *cobra.Command {
var tracesCmd = &cobra.Command{
Short: "Run Tempo for Traces.",
Use: "traces",
Example: heredoc.Doc(`
# Start up traces in monolithic-mode
$ lgtmp up traces
# Start up traces in microservices-mode
$ lgtmp up traces --mode microservices-mode
`),

RunE: func(cmd *cobra.Command, _ []string) error {
// up-monolithic-mode-traces Run monolithic-mode traces
// deploy-monolithic-mode-traces Deploy monolithic-mode traces
action := cmd.Parent().Use
target := fmt.Sprintf("%s-%s-traces", action, mode)
if err := internal.ExecuteCommand("make", "-C", ".", target); err != nil {
return err
}
return nil
},
}

tracesCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for traces. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))

return tracesCmd
}
61 changes: 61 additions & 0 deletions internal/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © Weifeng Wang <qclaogui@gmail.com>
//
// Licensed under the Apache License 2.0.

package internal

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)

func ExecuteCommand(command string, args ...string) error {
cmd := exec.Command(command, args...)
var stderr bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
log.Fatalf("Error: %s\n", stderr.String())
}

// make target is the last argment
target := args[len(args)-1]
if strings.HasPrefix(target, "down-") || strings.HasPrefix(target, "delete-") {
err := cmd.Wait()
if err != nil {
return err
}
} else {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

done := make(chan error, 1)
go func() { done <- cmd.Wait() }()

select {
case <-signalChan:
fmt.Printf("Received interrupt signal, stopping %s...\n", target)
_ = cmd.Process.Signal(os.Interrupt)
select {
case <-signalChan:
fmt.Printf("Force stopping %s...\n", target)
_ = cmd.Process.Kill()
os.Exit(1) // Exit with a status code of 1
case <-done:
os.Exit(0) // Exit with a status code of 0
}
case err := <-done:
if err != nil {
os.Exit(1) // Exit with a status code of 1 upon failure
}
}
}

return nil
}
Loading

0 comments on commit e27eb41

Please sign in to comment.