...

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

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

     1  // Package `metrics` contains the billman CLI for calculating
     2  // metric billing data.
     3  package metrics
     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 = "metrics"
    18  )
    19  
    20  // config for the metrics subcommand
    21  type config struct {
    22  	RootConfig  *cmd.Config
    23  	DisplayType string
    24  }
    25  
    26  // New returns a usable ffcli.Command for the metrics subcommand.
    27  func New(rootConfig *cmd.Config) *ffcli.Command {
    28  	cfg := config{
    29  		RootConfig:  rootConfig,
    30  		DisplayType: name,
    31  	}
    32  
    33  	fs := flag.NewFlagSet("billman"+name, flag.ExitOnError)
    34  
    35  	return &ffcli.Command{
    36  		Name:       name,
    37  		ShortUsage: "billman " + name + " [flags]",
    38  		ShortHelp:  "Print the estimated billing costs for metrics",
    39  		FlagSet:    cmd.WithGlobalFlags(fs),
    40  		Exec:       cfg.Exec,
    41  	}
    42  }
    43  
    44  // Exec runs the `billman metrics` command. Only metric
    45  // 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  	err = cmd.CheckPeriod(c.RootConfig.Period)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	samples, err := gcp.GetMetricSamples(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	md := costs.MetricData{
    60  		Samples: samples,
    61  		Cost:    costs.MetricCosts(samples, c.RootConfig.MetricsRate),
    62  	}
    63  	b := costs.Billing{
    64  		Cluster:     c.RootConfig.Cluster,
    65  		LogData:     costs.LogData{},
    66  		MetricData:  md,
    67  		Options:     c.RootConfig.Options,
    68  		DisplayType: c.DisplayType,
    69  	}
    70  	costs.PrintClusterCosts(b)
    71  	return nil
    72  }
    73  

View as plain text