-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_provider.go
85 lines (76 loc) · 2.12 KB
/
service_provider.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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"regexp"
"strings"
"sync"
"time"
)
func parseServiceProviderCounter(line string, id string) usageCounter {
/*
"name current min max lMin lMax lAvg total",
"BT_ACTIVE_CALLS 0 0 0 0 0 0 0",
"CENTREX_ACTIVE_CALLS 0 0 0 0 0 0 0",
*/
parts := strings.Fields(line)
if len(parts) < 8 {
return usageCounter{}
}
return usageCounter{
ID: "0",
Name: normalizeMetricName(parts[0]),
Current: parseUint64(parts[1]),
Min: parseUint64(parts[2]),
Max: parseUint64(parts[3]),
LastMin: parseUint64(parts[4]),
LastMax: parseUint64(parts[5]),
LastAvg: parseUint64(parts[6]),
Total: parseUint64(parts[7]),
}
}
func fetchServiceProviderCounters(prefix, url string, wg *sync.WaitGroup) {
defer wg.Done()
client := http.Client{Timeout: 2 * time.Second}
resp, err := client.Get(url)
if err != nil {
logError("Failed to connect", err)
return
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
var counters map[string]interface{}
error := json.Unmarshal([]byte(bodyString), &counters)
if error != nil {
log.Fatal(error)
}
re := regexp.MustCompile(`serviceProviderName: ([^"]+)`)
var clusterInfo = counters["clusterInfo"]
if clusterInfo == nil {
logError("Failed to get cluster info")
return
}
dc, cmpGrp := parseClusterInfo(clusterInfo.(string))
attrs := []MetricAttribute{{"dc", dc}, {"cmpGrp", cmpGrp}}
for key, value := range counters {
if (strings.HasPrefix(key, "spCounterTable")) {
matches := re.FindStringSubmatch(key)
if len(matches) > 1 {
serviceProvider := matches[1]
for i := range value.([]interface {}) {
line := value.([]interface {})[i].(string)
if (!strings.HasPrefix(line, "name")) {
ctr := parseServiceProviderCounter(line, serviceProvider)
setUsageMetric(prefix, ctr, append(attrs, MetricAttribute{"sp", serviceProvider}))
}
}
}
}
}
}