...
1
2
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
21 type config struct {
22 RootConfig *cmd.Config
23 DisplayType string
24 }
25
26
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
45
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