...
1 package metrics
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7
8 "cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
9 "google.golang.org/genproto/googleapis/api/distribution"
10 "google.golang.org/genproto/googleapis/api/metric"
11 "google.golang.org/genproto/googleapis/api/monitoredres"
12 "google.golang.org/protobuf/types/known/timestamppb"
13
14 "edge-infra.dev/pkg/lib/gcp/monitoring/monutil"
15 )
16
17 const (
18 location = "us-east1-b"
19 )
20
21
22 func (c *Client) AddTimeSeriesLabels(d *Descriptor, l []string) bool {
23 var err error
24 var tsReq monitoringpb.CreateTimeSeriesRequest
25 var tsVal monitoringpb.TimeSeries
26 tsReq.Name = fmt.Sprintf("projects/%s", c.ProjectID)
27 rLabels := monutil.SliceToMap([]string{"project_id", "location", "cluster", "namespace", "job", "instance"})
28 rLabels["project_id"] = c.ProjectID
29 rLabels["location"] = location
30
31 tsVal.Resource = &monitoredres.MonitoredResource{
32 Labels: rLabels,
33 Type: "prometheus_target",
34 }
35
36 switch strings.ToUpper(d.ValueType) {
37 case "DOUBLE":
38 tsVal.ValueType = *metric.MetricDescriptor_DOUBLE.Enum()
39 case "DISTRIBUTION":
40 tsVal.ValueType = *metric.MetricDescriptor_DISTRIBUTION.Enum()
41 default:
42 fmt.Printf("%s ValueType not supported yet\n", d.ValueType)
43 return false
44 }
45
46 tsVal.Metric = &metric.Metric{
47 Type: d.Type,
48 Labels: monutil.SliceToMap(l),
49 }
50
51 tsVal.Points, err = d.tsPoint()
52 if err != nil {
53 return false
54 }
55
56 tsReq.TimeSeries = append(tsReq.TimeSeries, &tsVal)
57
58
59 err = c.client.CreateTimeSeries(c.ctx, &tsReq)
60
61 if err != nil {
62 fmt.Printf("CreateTimeSeries Error: %s", err.Error())
63 }
64 return err == nil
65 }
66
67
68 func (d *Descriptor) tsPoint() ([]*monitoringpb.Point, error) {
69 var tsP monitoringpb.Point
70 sT, err := getTime(true)
71 if err != nil {
72 return nil, err
73 }
74
75 var eT time.Time
76 if d.MetricKind == "GAUGE" {
77 eT = sT
78 } else {
79 eT, err = getTime(false)
80 if err != nil {
81 return nil, err
82 }
83 }
84
85 tsP.Interval = &monitoringpb.TimeInterval{
86 StartTime: setTimeInterval(&sT),
87 EndTime: setTimeInterval(&eT),
88 }
89
90 if strings.ToUpper(d.ValueType) == "DOUBLE" {
91 tsP.Value = &monitoringpb.TypedValue{
92 Value: &monitoringpb.TypedValue_DoubleValue{
93 DoubleValue: 0,
94 },
95 }
96 return []*monitoringpb.Point{&tsP}, nil
97 } else if d.ValueType == "DISTRIBUTION" {
98 tsP.Value = &monitoringpb.TypedValue{
99 Value: &monitoringpb.TypedValue_DistributionValue{
100 DistributionValue: &distribution.Distribution{
101 BucketOptions: &distribution.Distribution_BucketOptions{
102 Options: &distribution.Distribution_BucketOptions_ExplicitBuckets{
103 ExplicitBuckets: &distribution.Distribution_BucketOptions_Explicit{
104 Bounds: []float64{0},
105 },
106 },
107 },
108 },
109 },
110 }
111 return []*monitoringpb.Point{&tsP}, nil
112 }
113
114 return nil, fmt.Errorf("%s ValueType or %s MetricKind not currently supported by metermaid", d.ValueType, d.MetricKind)
115 }
116
117
118 func setTimeInterval(t *time.Time) *timestamppb.Timestamp {
119 return ×tamppb.Timestamp{Seconds: t.Unix(), Nanos: 0}
120 }
121
122
123 func getTime(t bool) (time.Time, error) {
124 currTime := time.Now().UTC().Format("2006-01-02")
125 if t {
126 return time.Parse("2006-01-02T15:04:05.000000Z", currTime+"T00:00:00.000000Z")
127 }
128
129 return time.Parse("2006-01-02T15:04:05.000000Z", currTime+"T00:00:01.000000Z")
130 }
131
View as plain text