...

Source file src/edge-infra.dev/pkg/edge/monitoring/billman/cmd/logs/logs.go

Documentation: edge-infra.dev/pkg/edge/monitoring/billman/cmd/logs

     1  // Package `logs` contains the billman CLI for calculating
     2  // log billing data.
     3  package logs
     4  
     5  import (
     6  	"context"
     7  	"flag"
     8  
     9  	"github.com/peterbourgon/ff/v3/ffcli"
    10  
    11  	"edge-infra.dev/pkg/edge/monitoring/billman/cmd"
    12  	"edge-infra.dev/pkg/edge/monitoring/billman/costs"
    13  	"edge-infra.dev/pkg/edge/monitoring/billman/gcp"
    14  )
    15  
    16  const (
    17  	name      = "logs"
    18  	container = "k8s_container"
    19  	node      = "k8s_node"
    20  )
    21  
    22  // config for the logs subcommand
    23  type config struct {
    24  	RootConfig  *cmd.Config
    25  	DisplayType string
    26  }
    27  
    28  // New returns a usable ffcli.Command for the logs subcommand.
    29  func New(rootConfig *cmd.Config) *ffcli.Command {
    30  	cfg := config{
    31  		RootConfig:  rootConfig,
    32  		DisplayType: name,
    33  	}
    34  
    35  	fs := flag.NewFlagSet("billman"+name, flag.ExitOnError)
    36  
    37  	return &ffcli.Command{
    38  		Name:       name,
    39  		ShortUsage: "billman " + name + " [flags]",
    40  		ShortHelp:  "Print the estimated billing costs for logs",
    41  		FlagSet:    cmd.WithGlobalFlags(fs),
    42  		Exec:       cfg.Exec,
    43  	}
    44  }
    45  
    46  // Exec runs the `billman logs` command. Only log
    47  // data will be calculated.
    48  func (c *config) Exec(_ context.Context, _ []string) error {
    49  	err := cmd.CheckRequiredFlags(cmd.GlobalFlags)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	err = cmd.CheckPeriod(c.RootConfig.Period)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	bytesContainer, err := gcp.GetLogBytes(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period, container)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	bytesNode, err := gcp.GetLogBytes(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period, node)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	lcd := costs.LogContainerData{
    66  		Bytes: bytesContainer,
    67  		Cost:  costs.LogCosts(bytesContainer, c.RootConfig.LoggingRate),
    68  	}
    69  	lnd := costs.LogNodeData{
    70  		Bytes: bytesNode,
    71  		Cost:  costs.LogCosts(bytesNode, c.RootConfig.LoggingRate),
    72  	}
    73  	ld := costs.LogData{
    74  		Bytes: lcd.Bytes + lnd.Bytes,
    75  		Cost:  lcd.Cost + lnd.Cost,
    76  	}
    77  	b := costs.Billing{
    78  		Cluster:          c.RootConfig.Cluster,
    79  		LogContainerData: lcd,
    80  		LogNodeData:      lnd,
    81  		LogData:          ld,
    82  		MetricData:       costs.MetricData{},
    83  		Options:          c.RootConfig.Options,
    84  		DisplayType:      c.DisplayType,
    85  	}
    86  	costs.PrintClusterCosts(b)
    87  	return nil
    88  }
    89  

View as plain text