-
Notifications
You must be signed in to change notification settings - Fork 7
/
metrics.go
95 lines (76 loc) · 2.51 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-License-Identifier: GPL-3.0
package main
import (
"fmt"
"io"
"os"
"github.com/comrade-coop/apocryph/pkg/prometheus"
pb "github.com/comrade-coop/apocryph/pkg/proto"
"github.com/comrade-coop/apocryph/pkg/resource"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
)
var metricsCmd = &cobra.Command{
Use: "metrics",
Short: "Operations related to monitoring and pricing pod execution",
}
var prometheusUrl string
var pricingFile string
var pricingFileFormat string
var pricingFileContents string
var getMetricsCmd = &cobra.Command{
Use: "get",
Short: "Get the metrics stored in prometheus",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
namespaceConsumptions := resource.NamespaceConsumptions{}
err := prometheus.GetPrometheusClient(prometheusUrl).FetchResourceMetrics(namespaceConsumptions)
if err != nil {
return err
}
pricingTables, err := openPricingTables()
if err != nil {
return err
}
if len(pricingTables) > 0 {
for _, ptm := range pricingTables {
namespaceConsumptions.Display(cmd.OutOrStdout(), ptm)
fmt.Fprintf(cmd.OutOrStdout(), "totals: %v\n", namespaceConsumptions.Price(ptm))
}
} else {
namespaceConsumptions.Display(cmd.OutOrStdout(), nil)
}
return err
},
}
func openPricingTables() (map[common.Address]resource.PricingTableMap, error) {
if pricingFileContents != "" {
pricingTables := &pb.PricingTables{}
err := pb.Unmarshal(pricingFileFormat, []byte(pricingFileContents), pricingTables)
if err != nil {
return nil, err
}
return resource.ConvertPricingTables(pricingTables.Tables), nil
}
file, err := os.Open(pricingFile)
if err != nil {
return nil, err
}
pricingTableContents, err := io.ReadAll(file)
if err != nil {
return nil, err
}
pricingTables := &pb.PricingTables{}
err = pb.Unmarshal(pricingFileFormat, pricingTableContents, pricingTables)
if err != nil {
return nil, err
}
return resource.ConvertPricingTables(pricingTables.Tables), nil
}
func init() {
metricsCmd.AddCommand(getMetricsCmd)
getMetricsCmd.Flags().StringVar(&prometheusUrl, "prometheus", "", "address at which the prometheus API can be accessed")
AddConfig("pricing.table.filename", &pricingFile, "", "absolute path to file containing pricing information")
AddConfig("pricing.table.contents", &pricingFileContents, "", "absolute path to file containing pricing information")
AddConfig("pricing.table.format", &pricingFileFormat, "", fmt.Sprintf("pricing file format. one of %v", pb.FormatNames))
}