// Package `metrics` contains the billman CLI for calculating // metric billing data. package metrics import ( "context" "flag" "github.com/peterbourgon/ff/v3/ffcli" "edge-infra.dev/pkg/edge/monitoring/billman/cmd" "edge-infra.dev/pkg/edge/monitoring/billman/costs" "edge-infra.dev/pkg/edge/monitoring/billman/gcp" ) const ( name = "metrics" ) // config for the metrics subcommand type config struct { RootConfig *cmd.Config DisplayType string } // New returns a usable ffcli.Command for the metrics subcommand. func New(rootConfig *cmd.Config) *ffcli.Command { cfg := config{ RootConfig: rootConfig, DisplayType: name, } fs := flag.NewFlagSet("billman"+name, flag.ExitOnError) return &ffcli.Command{ Name: name, ShortUsage: "billman " + name + " [flags]", ShortHelp: "Print the estimated billing costs for metrics", FlagSet: cmd.WithGlobalFlags(fs), Exec: cfg.Exec, } } // Exec runs the `billman metrics` command. Only metric // data will be calculated. func (c *config) Exec(_ context.Context, _ []string) error { err := cmd.CheckRequiredFlags(cmd.GlobalFlags) if err != nil { return err } err = cmd.CheckPeriod(c.RootConfig.Period) if err != nil { return err } samples, err := gcp.GetMetricSamples(c.RootConfig.TopLevelProjectID, c.RootConfig.ClusterID, c.RootConfig.Period) if err != nil { return err } md := costs.MetricData{ Samples: samples, Cost: costs.MetricCosts(samples, c.RootConfig.MetricsRate), } b := costs.Billing{ Cluster: c.RootConfig.Cluster, LogData: costs.LogData{}, MetricData: md, Options: c.RootConfig.Options, DisplayType: c.DisplayType, } costs.PrintClusterCosts(b) return nil }