Skip to content

Commit

Permalink
added exporting metrics to prometheus and removed custom log setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzxnia committed Feb 17, 2024
1 parent 35129f3 commit 3863a88
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 315 deletions.
10 changes: 6 additions & 4 deletions cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// tutaj nie powinno wchodzić proto
func StartAgent(context context.Context, stdin bool, port string, configFile string) (err error) {
func StartAgent(context context.Context, stdin bool, cmdPort string, configFile string) (err error) {
var requestConfig *lbot.ConfigRequest

if stdin {
Expand All @@ -24,11 +24,13 @@ func StartAgent(context context.Context, stdin bool, port string, configFile str
}
}

agent := lbot.NewAgent(context, Logger)
if cmdPort != "" {
requestConfig.AgentPort = cmdPort
}
agent := lbot.NewAgent(context)
if requestConfig != nil {
agent.ApplyConfig(requestConfig)
}

agent.Listen(port)
agent.Listen()
return nil
}
27 changes: 5 additions & 22 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package cli
import (
"errors"
"fmt"
"strings"
"time"

"github.com/kuzxnia/loadbot/lbot"
"github.com/kuzxnia/loadbot/lbot/config"
applog "github.com/kuzxnia/loadbot/lbot/log"
"github.com/kuzxnia/loadbot/lbot/proto"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -19,33 +16,21 @@ const (
AgentUri = "agent-uri"
)

var (
Logger *log.Entry
Conn *grpc.ClientConn
)

func New(rootLogger *log.Entry, version string, commit string, date string) *cobra.Command {
Logger = rootLogger
var Conn *grpc.ClientConn

func New(version string, commit string, date string) *cobra.Command {
cmd := cobra.Command{
Use: "loadbot",
Short: "A command-line database workload driver ",
Version: fmt.Sprintf("%s (commit: %s) (build date: %s)", version, commit, date),
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
f := cmd.Flags()
loglvl, _ := f.GetString(config.FlagLogLevel)
logfmt, _ := f.GetString(config.FlagLogFormat)
err = applog.Configure(Logger, loglvl, logfmt)
if err != nil {
return fmt.Errorf("failed to configure logger: %w", err)
}

// move to driver group
// move to driver group
agentUri, _ := f.GetString(AgentUri)
Conn, err = grpc.Dial(agentUri, grpc.WithInsecure())
// valiedate connection
if err != nil {
Logger.Fatal("Found errors trying to connect to loadbot-agent:", err)
log.Fatal("Found errors trying to connect to loadbot-agent:", err)
return
}

Expand All @@ -58,10 +43,8 @@ func New(rootLogger *log.Entry, version string, commit string, date string) *cob
},
}
pf := cmd.PersistentFlags()
// move to driver group
// move to driver group
pf.StringP(AgentUri, "u", "127.0.0.1:1234", "loadbot agent uri (default: 127.0.0.1:1234)")
pf.String(config.FlagLogLevel, applog.LevelInfo, fmt.Sprintf("log level, must be one of: %s", strings.Join(applog.Levels, ", ")))
pf.String(config.FlagLogFormat, applog.FormatFancy, fmt.Sprintf("log format, must be one of: %s", strings.Join(applog.Formats, ", ")))

// setup supcommands
// cmd.AddGroup(&OrchiestrationGroup)
Expand Down
31 changes: 16 additions & 15 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/kuzxnia/loadbot/lbot"
"github.com/kuzxnia/loadbot/lbot/proto"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

Expand All @@ -20,16 +21,16 @@ import (
func SetConfigDriver(conn *grpc.ClientConn, parsedConfig *lbot.ConfigRequest) (err error) {
requestConfig := BuildConfigRequest(parsedConfig)

Logger.Info("🚀 Setting new config")
log.Info("🚀 Setting new config")

client := proto.NewSetConfigProcessClient(conn)
reply, err := client.Run(context.TODO(), requestConfig)
if err != nil {
return fmt.Errorf("Setting config failed: %w", err)
}

Logger.Infof("Received: %v", reply)
Logger.Info("✅ Setting config succeeded")
log.Infof("Received: %v", reply)
log.Info("✅ Setting config succeeded")

return
}
Expand All @@ -43,18 +44,18 @@ func BuildConfigRequest(request *lbot.ConfigRequest) *proto.ConfigRequest {
}
for i, job := range request.Jobs {
cfg.Jobs[i] = &proto.JobRequest{
Name: job.Name,
Database: job.Database,
Collection: job.Collection,
Type: job.Type,
Schema: job.Schema,
Connections: job.Connections,
Pace: job.Pace,
DataSize: job.DataSize,
BatchSize: job.BatchSize,
Duration: job.Duration.String(),
Operations: job.Operations,
Timeout: job.Timeout.String(),
Name: job.Name,
Database: job.Database,
Collection: job.Collection,
Type: job.Type,
Schema: job.Schema,
Connections: job.Connections,
Pace: job.Pace,
DataSize: job.DataSize,
BatchSize: job.BatchSize,
Duration: job.Duration.String(),
Operations: job.Operations,
Timeout: job.Timeout.String(),
// todo: setup filters and schema inside
// Filter: job.Filter,
}
Expand Down
9 changes: 5 additions & 4 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/rpc"

"github.com/kuzxnia/loadbot/orchiestrator"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,19 +39,19 @@ func installationHandler(cmd *cobra.Command, args []string) error {
}
var reply int

Logger.Info("🚀 Starting installation process")
log.Info("🚀 Starting installation process")

client, err := rpc.DialHTTP("tcp", "127.0.0.1:1234")
if err != nil {
Logger.Fatal("Found errors trying to connect to lbot-agent:", err)
log.Fatal("Found errors trying to connect to lbot-agent:", err)
}

err = client.Call("InstallationProcess.Run", request, &reply)
if err != nil {
Logger.Fatal("InstallationProcess error:", err)
log.Fatal("InstallationProcess error:", err)
}

Logger.Info("✅ Installation process succeeded")
log.Info("✅ Installation process succeeded")

return nil
}
2 changes: 1 addition & 1 deletion cli/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"fmt"
"io"
"log"
"os"

"github.com/cheggaaa/pb/v3"
"github.com/kuzxnia/loadbot/lbot/proto"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

Expand Down
7 changes: 4 additions & 3 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/kuzxnia/loadbot/lbot/proto"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

Expand All @@ -13,7 +14,7 @@ import (
// tutaj nie powinno wchodzić proto
func StartDriver(conn grpc.ClientConnInterface, request *proto.StartRequest) (err error) {
// todo: mapowanie to proto
Logger.Info("🚀 Starting stress test")
log.Info("🚀 Starting stress test")

client := proto.NewStartProcessClient(conn)

Expand All @@ -22,8 +23,8 @@ func StartDriver(conn grpc.ClientConnInterface, request *proto.StartRequest) (er
return fmt.Errorf("starting stress test failed: %w", err)
}

Logger.Infof("Received: %v", reply)
Logger.Info("✅ Starting stress test succeeded")
log.Infof("Received: %v", reply)
log.Info("✅ Starting stress test succeeded")

return
}
9 changes: 5 additions & 4 deletions cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import (
"context"

"github.com/kuzxnia/loadbot/lbot/proto"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

func StopDriver(conn grpc.ClientConnInterface, request *proto.StopRequest) (err error) {
Logger.Info("🚀 Stopping stress test")
log.Info("🚀 Stopping stress test")

client := proto.NewStopProcessClient(conn)

reply, err := client.Run(context.TODO(), request)
if err != nil {
Logger.Fatal("arith error:", err)
log.Fatal("arith error:", err)
return
}

Logger.Infof("Received: %v", reply)
Logger.Info("✅ Stopping stress test succeeded")
log.Infof("Received: %v", reply)
log.Info("✅ Stopping stress test succeeded")

return nil
}
9 changes: 5 additions & 4 deletions cli/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import (
"net/rpc"

"github.com/kuzxnia/loadbot/orchiestrator"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func unInstallationHandler(cmd *cobra.Command, args []string) error {
request := orchiestrator.UnInstallationRequest{}

Logger.Info("🚀 Starting installation process")
log.Info("🚀 Starting installation process")

var reply int
client, err := rpc.DialHTTP("tcp", "127.0.0.1:1234")
if err != nil {
Logger.Fatal("Found errors trying to connect to lbot-agent:", err)
log.Fatal("Found errors trying to connect to lbot-agent:", err)
}

err = client.Call("UnInstallationProcess.Run", request, &reply)
if err != nil {
Logger.Fatal("UnInstallationProcess error:", err)
log.Fatal("UnInstallationProcess error:", err)
// return fmt.Errorf("installation failed: %w", err)
}

Logger.Info("✅ Installation process succeeded")
log.Info("✅ Installation process succeeded")

return nil
}
6 changes: 3 additions & 3 deletions cli/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"
"io"
"log"

"github.com/kuzxnia/loadbot/lbot/proto"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

func WatchDriver(conn grpc.ClientConnInterface, request *proto.WatchRequest) (err error) {
Logger.Info("🚀 Starting stress test")
log.Info("🚀 Starting stress test")

client := proto.NewWatchProcessClient(conn)

Expand All @@ -38,7 +38,7 @@ func WatchDriver(conn grpc.ClientConnInterface, request *proto.WatchRequest) (er

<-done // we will wait until all response is received

Logger.Info("✅ Starting stress test succeeded")
log.Info("✅ Starting stress test succeeded")

return
}
21 changes: 8 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package main

import (
"context"
"fmt"
"os"

"github.com/kuzxnia/loadbot/cli"
"github.com/kuzxnia/loadbot/lbot/log"
log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -26,19 +25,15 @@ func run() int {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

logger, err := log.NewLogger(ctx)
if err != nil {
fmt.Printf("❌ Error: %s\n", err.Error()) //nolint:forbidigo

return 1
}

rootCmd := cli.New(logger, version, commit, date)

err = rootCmd.ExecuteContext(ctx)
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
rootCmd := cli.New(version, commit, date)

err := rootCmd.ExecuteContext(ctx)
if err != nil {
logger.Errorf("❌ Error: %s", err.Error())
log.Errorf("❌ Error: %s", err.Error())
return 1
}

Expand Down
Loading

0 comments on commit 3863a88

Please sign in to comment.