go.d/portcheck: update status duration calculation (#18577)
Ilya Mashchenko committed
Sep 18, 2024 at 11:57 UTC
11b3faf96e6492608b04f6a05ca0884e5e80a1a2
4 files changed
+37
-37
src/go/plugin/go.d/modules/portcheck/check_tcp_port.go
+9
-9
@@ -13,10 +13,10 @@ const (
13
)
14
15
type tcpPort struct {
16
- number int
17
- state string
18
- inState int
19
- latency int
16
+ number int
17
+ status string
18
+ statusChangeTs time.Time
19
+ latency int
20
}
21
22
func (pc *PortCheck) checkTCPPort(port *tcpPort) {
@@ -47,10 +47,10 @@ func (pc *PortCheck) checkTCPPort(port *tcpPort) {
47
}
48
49
func (pc *PortCheck) setTcpPortCheckState(port *tcpPort, state string) {
50
- if port.state == state {
51
- port.inState += pc.UpdateEvery
52
- } else {
53
- port.inState = pc.UpdateEvery
54
- port.state = state
50
+ if port.status != state {
51
+ port.status = state
52
+ port.statusChangeTs = time.Now()
53
+ } else if port.statusChangeTs.IsZero() {
54
+ port.statusChangeTs = time.Now()
55
}
56
}
src/go/plugin/go.d/modules/portcheck/check_udp_port.go
+8
-8
@@ -19,9 +19,9 @@ const (
19
)
20
21
type udpPort struct {
22
- number int
23
- state string
24
- inState int
22
+ number int
23
+ status string
24
+ statusChangeTs time.Time
25
26
err error
27
}
@@ -48,11 +48,11 @@ func (pc *PortCheck) checkUDPPort(port *udpPort) {
48
}
49
50
func (pc *PortCheck) setUDPPortCheckState(port *udpPort, state string) {
51
- if port.state == state {
52
- port.inState += pc.UpdateEvery
53
- } else {
54
- port.inState = pc.UpdateEvery
55
- port.state = state
51
+ if port.status != state {
52
+ port.status = state
53
+ port.statusChangeTs = time.Now()
54
+ } else if port.statusChangeTs.IsZero() {
55
+ port.statusChangeTs = time.Now()
56
}
57
}
58
src/go/plugin/go.d/modules/portcheck/collect.go
+6
-6
@@ -28,10 +28,10 @@ func (pc *PortCheck) collect() (map[string]int64, error) {
28
29
wg.Wait()
30
31
- // FIXME: in state time calculation
32
-
31
mx := make(map[string]int64)
32
33
+ now := time.Now()
34
+
35
for _, p := range pc.tcpPorts {
36
if !pc.seenTcpPorts[p.number] {
37
pc.seenTcpPorts[p.number] = true
@@ -40,12 +40,12 @@ func (pc *PortCheck) collect() (map[string]int64, error) {
40
41
px := fmt.Sprintf("tcp_port_%d_", p.number)
42
43
- mx[px+"current_state_duration"] = int64(p.inState)
43
+ mx[px+"current_state_duration"] = int64(now.Sub(p.statusChangeTs).Seconds())
44
mx[px+"latency"] = int64(p.latency)
45
mx[px+tcpPortCheckStateSuccess] = 0
46
mx[px+tcpPortCheckStateTimeout] = 0
47
mx[px+tcpPortCheckStateFailed] = 0
48
- mx[px+p.state] = 1
48
+ mx[px+p.status] = 1
49
}
50
51
if pc.doUdpPorts {
@@ -65,10 +65,10 @@ func (pc *PortCheck) collect() (map[string]int64, error) {
65
66
px := fmt.Sprintf("udp_port_%d_", p.number)
67
68
- mx[px+"current_status_duration"] = int64(p.inState)
68
+ mx[px+"current_status_duration"] = int64(now.Sub(p.statusChangeTs).Seconds())
69
mx[px+udpPortCheckStateOpenFiltered] = 0
70
mx[px+udpPortCheckStateClosed] = 0
71
- mx[px+p.state] = 1
71
+ mx[px+p.status] = 1
72
}
73
}
74
src/go/plugin/go.d/modules/portcheck/portcheck_test.go
+14
-14
@@ -77,9 +77,9 @@ func TestPortCheck_Collect(t *testing.T) {
77
require.NoError(t, job.Init())
78
require.NoError(t, job.Check())
79
80
- copyLatency := func(dst, src map[string]int64) {
80
+ copyLatencyDuration := func(dst, src map[string]int64) {
81
for k := range dst {
82
- if strings.HasSuffix(k, "latency") {
82
+ if strings.HasSuffix(k, "latency") || strings.HasSuffix(k, "duration") {
83
dst[k] = src[k]
84
}
85
}
@@ -97,10 +97,10 @@ func TestPortCheck_Collect(t *testing.T) {
97
"tcp_port_39002_success": 1,
98
"tcp_port_39002_timeout": 0,
99
}
100
- collected := job.Collect()
101
- copyLatency(expected, collected)
100
+ mx := job.Collect()
101
+ copyLatencyDuration(expected, mx)
102
103
- assert.Equal(t, expected, collected)
103
+ assert.Equal(t, expected, mx)
104
105
expected = map[string]int64{
106
"tcp_port_39001_current_state_duration": int64(job.UpdateEvery) * 3,
@@ -114,10 +114,10 @@ func TestPortCheck_Collect(t *testing.T) {
114
"tcp_port_39002_success": 1,
115
"tcp_port_39002_timeout": 0,
116
}
117
- collected = job.Collect()
118
- copyLatency(expected, collected)
117
+ mx = job.Collect()
118
+ copyLatencyDuration(expected, mx)
119
120
- assert.Equal(t, expected, collected)
120
+ assert.Equal(t, expected, mx)
121
122
job.dialTCP = testDial(errors.New("checkStateFailed"))
123
@@ -133,10 +133,10 @@ func TestPortCheck_Collect(t *testing.T) {
133
"tcp_port_39002_success": 0,
134
"tcp_port_39002_timeout": 0,
135
}
136
- collected = job.Collect()
137
- copyLatency(expected, collected)
136
+ mx = job.Collect()
137
+ copyLatencyDuration(expected, mx)
138
139
- assert.Equal(t, expected, collected)
139
+ assert.Equal(t, expected, mx)
140
141
job.dialTCP = testDial(timeoutError{})
142
@@ -152,10 +152,10 @@ func TestPortCheck_Collect(t *testing.T) {
152
"tcp_port_39002_timeout": 1,
153
"tcp_port_39002_failed": 0,
154
}
155
- collected = job.Collect()
156
- copyLatency(expected, collected)
155
+ mx = job.Collect()
156
+ copyLatencyDuration(expected, mx)
157
158
- assert.Equal(t, expected, collected)
158
+ assert.Equal(t, expected, mx)
159
}
160
161
func testDial(err error) dialTCPFunc {