...

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

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

     1  // Package `all` contains the billman CLI for calculating
     2  // log and metric billing data.
     3  package all
     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      = "all"
    18  	container = "k8s_container"
    19  	node      = "k8s_node"
    20  )
    21  
    22  // config for the "all" subcommand
    23  type config struct {
    24  	RootConfig  *cmd.Config
    25  	DisplayType string
    26  }
    27  
    28  func New(rootConfig *cmd.Config) *ffcli.Command {
    29  	cfg := config{
    30  		RootConfig:  rootConfig,
    31  		DisplayType: name,
    32  	}
    33  
    34  	fs := flag.NewFlagSet("billman "+name, flag.ExitOnError)
    35  
    36  	return &ffcli.Command{
    37  		Name:       name,
    38  		ShortUsage: "billman " + name + " [flags]",
    39  		FlagSet:    cmd.WithGlobalFlags(fs),
    40  		Exec:       cfg.Exec,
    41  	}
    42  }
    43  
    44  // Exec runs the `billman all` command. Both log and
    45  // metric data will be calculated.
    46  func (c *config) Exec(context.Context, []string) error {
    47  	err := cmd.CheckRequiredFlags(cmd.GlobalFlags)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	err = cmd.CheckPeriod(c.RootConfig.Period)
    53  	if err != nil {
    54  		return err
    55  	}
    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  
    74  	ld := costs.LogData{
    75  		Bytes: lcd.Bytes + lnd.Bytes,
    76  		Cost:  lcd.Cost + lnd.Cost,
    77  	}
    78  	samples, err := gcp.GetMetricSamples(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	md := costs.MetricData{
    83  		Samples: samples,
    84  		Cost:    costs.MetricCosts(samples, c.RootConfig.MetricsRate),
    85  	}
    86  	b := costs.Billing{
    87  		Cluster:          c.RootConfig.Cluster,
    88  		LogContainerData: lcd,
    89  		LogNodeData:      lnd,
    90  		LogData:          ld,
    91  		MetricData:       md,
    92  		Options:          c.RootConfig.Options,
    93  		DisplayType:      c.DisplayType,
    94  	}
    95  	costs.PrintClusterCosts(b)
    96  	return nil
    97  }
    98  

View as plain text