-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathazure.go
141 lines (127 loc) · 4.19 KB
/
azure.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2024 RapidLoop, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package collector
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
"github.com/rapidloop/pgmetrics"
)
const (
flexibleServerMetrics = `backup_storage_used,cpu_percent,memory_percent,iops,disk_queue_depth,read_throughput,write_throughput,read_iops,write_iops,storage_percent,storage_used,storage_free,txlogs_storage_used,active_connections,network_bytes_egress,network_bytes_ingress,connections_failed,connections_succeeded,maximum_used_transactionIDs`
singleServerMetrics = `cpu_percent,memory_percent,io_consumption_percent,storage_percent,storage_used,storage_limit,serverlog_storage_percent,serverlog_storage_usage,serverlog_storage_limit,active_connections,connections_failed,backup_storage_used,network_bytes_egress,network_bytes_ingress,pg_replica_log_delay_in_seconds,pg_replica_log_delay_in_bytes`
citusMetrics = `cpu_percent,memory_percent,apps_reserved_memory_percent,iops,storage_percent,storage_used,active_connections,network_bytes_egress,network_bytes_ingress`
)
var rxResource = regexp.MustCompile(`(?i)^/subscriptions/([^/]{36})/resourceGroups/([^/]+)/providers/Microsoft.DBforPostgreSQL/(flexibleServers|servers|serverGroupsv2)/([^/]+)$`)
func collectAzure(ctx context.Context, resourceID string, out *pgmetrics.Azure) error {
// parse resource URI
m := rxResource.FindStringSubmatch(resourceID)
if len(m) != 5 {
return errors.New("invalid resource ID")
}
out.ResourceType = "Microsoft.DBforPostgreSQL/" + m[3]
out.ResourceName = m[4]
out.Metrics = make(map[string]float64)
// get credentials
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return fmt.Errorf("failed to get credentials: %v", err)
}
// create a client
client, err := armmonitor.NewMetricsClient(m[1], cred, nil)
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}
// make parameters for query
to := time.Now().In(time.UTC)
from := to.Add(-5 * time.Minute)
timeRange := from.Format(time.RFC3339) + "/" + to.Format(time.RFC3339)
var interval string
var top int32 = 1
var metricNames string
switch m[3] {
case "flexibleServers":
interval = "PT1M"
metricNames = flexibleServerMetrics
case "servers":
interval = "PT15M"
metricNames = singleServerMetrics
case "serverGroupsv2":
interval = "PT1M"
metricNames = citusMetrics
}
// actually query
resp, err := client.List(
ctx,
resourceID,
&armmonitor.MetricsClientListOptions{
Interval: &interval,
Metricnames: &metricNames,
Timespan: &timeRange,
Top: &top,
},
)
if err != nil {
return fmt.Errorf("failed to query Azure API: %v", err)
}
// parse response
out.ResourceRegion = *resp.Resourceregion
for _, m := range resp.Value {
if m.ID == nil || *m.ID == "" {
// log.Printf("warning: ignoring metric with no id: %v", m)
continue
}
if len(m.Timeseries) == 0 {
continue // no timeseries data, ignore quietly
}
ts := m.Timeseries[len(m.Timeseries)-1]
if len(ts.Data) == 0 {
continue // no timeseries data, ignore quietly
}
name := azGetMetricName(*m.ID)
for i := len(ts.Data) - 1; i >= 0; i-- {
t := ts.Data[i]
if t.TimeStamp == nil {
continue
}
if t.Average != nil {
out.Metrics[name] = *t.Average
break
}
if t.Total != nil {
out.Metrics[name] = *t.Total
break
}
if t.Maximum != nil {
out.Metrics[name] = *t.Maximum
break
}
}
}
return nil
}
func azGetMetricName(id string) string {
i := strings.LastIndexByte(id, '/')
if i != -1 {
return id[int(i)+1:]
}
return id
}