|
1 | 1 | package metricsink |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "math" |
6 | 4 | "runtime" |
7 | 5 | "runtime/pprof" |
8 | | - "strconv" |
9 | 6 | "strings" |
10 | | - "time" |
11 | | - |
12 | | - "github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix" |
13 | | - "github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/metric_collector" |
14 | | - "github.com/go-mesh/openlogging" |
15 | 7 | ) |
16 | 8 |
|
17 | 9 | var threadCreateProfile = pprof.Lookup("threadcreate") |
18 | 10 |
|
19 | | -// MonitorData is an object which stores the monitoring information for an application |
20 | | -type MonitorData struct { |
21 | | - AppID string `json:"appId"` |
22 | | - Version string `json:"version"` |
23 | | - Name string `json:"name"` |
24 | | - Environment string `json:"environment"` |
25 | | - Instance string `json:"instance"` |
26 | | - Thread int `json:"thread"` |
27 | | - Customs map[string]interface{} `json:"customs"` // ? |
28 | | - Interfaces []*InterfaceInfo `json:"interfaces"` |
29 | | - CPU float64 `json:"cpu"` |
30 | | - Memory map[string]interface{} `json:"memory"` |
31 | | - ServiceID string `json:"serviceId"` |
32 | | - InstanceID string `json:"instanceId"` |
33 | | -} |
34 | | - |
35 | | -// InterfaceInfo is an object which store the monitoring information of a particular interface |
36 | | -type InterfaceInfo struct { |
37 | | - Name string `json:"name"` |
38 | | - Desc string `json:"desc"` |
39 | | - QPS float64 `json:"qps"` |
40 | | - Latency int `json:"latency"` |
41 | | - L995 int `json:"l995"` |
42 | | - L99 int `json:"l99"` |
43 | | - L90 int `json:"l90"` |
44 | | - L75 int `json:"l75"` |
45 | | - L50 int `json:"l50"` |
46 | | - L25 int `json:"l25"` |
47 | | - L5 int `json:"l5"` |
48 | | - Rate float64 `json:"rate"` |
49 | | - Total int64 `json:"total"` |
50 | | - Failure int64 `json:"failure"` |
51 | | - ShortCircuited int64 `json:"shortCircuited"` |
52 | | - IsCircuitBreakerOpen bool `json:"circuitBreakerOpen"` |
53 | | - SemaphoreRejected int64 `json:"semaphoreRejected"` |
54 | | - ThreadPoolRejected int64 `json:"threadPoolRejected"` |
55 | | - CountTimeout int64 `json:"countTimeout"` |
56 | | - FailureRate float64 `json:"failureRate"` |
57 | | - successCount int64 |
58 | | -} |
59 | | - |
60 | | -// NewMonitorData creates a new monitoring object |
61 | | -func NewMonitorData() *MonitorData { |
62 | | - monitorData := new(MonitorData) |
63 | | - monitorData.Interfaces = make([]*InterfaceInfo, 0) |
64 | | - return monitorData |
65 | | -} |
66 | | - |
67 | | -func (monitorData *MonitorData) getOrCreateInterfaceInfo(name string) *InterfaceInfo { |
68 | | - for _, interfaceInfo := range monitorData.Interfaces { |
69 | | - if interfaceInfo.Name == name { |
70 | | - return interfaceInfo |
71 | | - } |
72 | | - } |
73 | | - interfaceInfo := new(InterfaceInfo) |
74 | | - interfaceInfo.Name = name |
75 | | - interfaceInfo.Desc = name |
76 | | - monitorData.Interfaces = append(monitorData.Interfaces, interfaceInfo) |
77 | | - return interfaceInfo |
78 | | -} |
79 | | - |
80 | | -func (monitorData *MonitorData) appendInterfaceInfo(name string, c *metricCollector.DefaultMetricCollector) { |
81 | | - var interfaceInfo = monitorData.getOrCreateInterfaceInfo(name) |
82 | | - now := time.Now() |
83 | | - //attempts: |
84 | | - interfaceInfo.Total = int64(c.NumRequests().Sum(now)) |
85 | | - //errors |
86 | | - interfaceInfo.Failure = int64(c.Failures().Sum(now)) |
87 | | - //shortCircuits |
88 | | - interfaceInfo.ShortCircuited = int64(c.ShortCircuits().Sum(now)) |
89 | | - //successes |
90 | | - interfaceInfo.successCount = int64(c.Successes().Sum(now)) |
91 | | - |
92 | | - if isCBOpen, err := hystrix.IsCircuitBreakerOpen(name); err != nil { |
93 | | - interfaceInfo.IsCircuitBreakerOpen = false |
94 | | - openlogging.Error("can't get circuit status", openlogging.WithTags(openlogging.Tags{ |
95 | | - "err": err.Error(), |
96 | | - "name": name, |
97 | | - })) |
98 | | - } else { |
99 | | - interfaceInfo.IsCircuitBreakerOpen = isCBOpen |
100 | | - } |
101 | | - |
102 | | - qps := (float64(interfaceInfo.Total) * (1 - math.Exp(-5.0/60.0/1))) |
103 | | - movingAverageFor3Precision, err := strconv.ParseFloat(fmt.Sprintf("%.3f", qps), 64) |
104 | | - if err == nil { |
105 | | - interfaceInfo.QPS = movingAverageFor3Precision |
106 | | - } else { |
107 | | - interfaceInfo.QPS = 0 |
108 | | - } |
109 | | - runDuration := c.RunDuration() |
110 | | - interfaceInfo.L5 = int(runDuration.Percentile(0.05)) |
111 | | - interfaceInfo.L25 = int(runDuration.Percentile(0.25)) |
112 | | - interfaceInfo.L50 = int(float64(runDuration.Percentile(0.5))) |
113 | | - interfaceInfo.L75 = int(runDuration.Percentile(0.75)) |
114 | | - interfaceInfo.L90 = int(runDuration.Percentile(0.90)) |
115 | | - interfaceInfo.L99 = int(runDuration.Percentile(0.99)) |
116 | | - interfaceInfo.L995 = int(runDuration.Percentile(0.995)) |
117 | | - interfaceInfo.Latency = int(runDuration.Mean()) |
118 | | - interfaceInfo.Rate = 1 //rate is no use any more and must be set to 1 |
119 | | - if interfaceInfo.Total == 0 { |
120 | | - interfaceInfo.FailureRate = 0 |
121 | | - } else { |
122 | | - totalErrorCount := interfaceInfo.Failure + interfaceInfo.SemaphoreRejected + interfaceInfo.ThreadPoolRejected + interfaceInfo.CountTimeout |
123 | | - if totalErrorCount == 0 { |
124 | | - interfaceInfo.FailureRate = 0 |
125 | | - } else { |
126 | | - failureRate, err := strconv.ParseFloat(fmt.Sprintf("%.3f", float64(totalErrorCount)/float64(interfaceInfo.Total)), 64) |
127 | | - if err == nil && failureRate > 0 { |
128 | | - interfaceInfo.FailureRate = failureRate |
129 | | - } else { |
130 | | - openlogging.GetLogger().Warnf("Error in calculating the failureRate %v, default value(0) is assigned to failureRate", failureRate) |
131 | | - interfaceInfo.FailureRate = 0 |
132 | | - } |
133 | | - } |
134 | | - } |
135 | | -} |
136 | | - |
137 | | -func GetInterfaceName(metricName string) string { |
138 | | - command := strings.Split(metricName, ".") |
139 | | - return strings.Join(command[:len(command)-1], ".") |
140 | | - |
141 | | -} |
142 | | - |
143 | 11 | func getProcessInfo() map[string]interface{} { |
144 | 12 | var memoryInfo = make(map[string]interface{}) |
145 | 13 | var memStats = runtime.MemStats{} |
|
0 commit comments