@@ -28,6 +28,21 @@ type HEPOutputer struct {
28
28
msgPing []byte
29
29
}
30
30
31
+ func writeAndFlush (client * HEPConn , data []byte , action string ) (int , error ) {
32
+ hl , err := client .conn .Write (data )
33
+ if err != nil {
34
+ promstats .HepFileFlushesError .Inc ()
35
+ return 0 , fmt .Errorf ("error writing to socket during %s: %w" , action , err )
36
+ }
37
+
38
+ if err := client .writer .Flush (); err != nil {
39
+ promstats .HepFileFlushesError .Inc ()
40
+ return 0 , fmt .Errorf ("error flushing writer during %s: %w" , action , err )
41
+ }
42
+
43
+ return hl , nil
44
+ }
45
+
31
46
func NewHEPOutputer (serverAddr string ) (* HEPOutputer , error ) {
32
47
a := strings .Split (cutSpace (serverAddr ), "," )
33
48
l := len (a )
@@ -43,6 +58,11 @@ func NewHEPOutputer(serverAddr string) (*HEPOutputer, error) {
43
58
errCnt ++
44
59
} else {
45
60
if config .Cfg .HEPBufferEnable {
61
+ logp .Debug ("collector" , "send ping packet after disconnect" )
62
+ _ , err := writeAndFlush (& h .client [n ], h .msgPing , "ping operation establish connection" )
63
+ if err != nil {
64
+ return nil , err
65
+ }
46
66
if _ , err := os .Stat (config .Cfg .HEPBufferFile ); err == nil {
47
67
if _ , err := h .copyHEPFileOut (n ); err != nil {
48
68
logp .Err ("Sending HEP from file error: %v" , err )
@@ -161,8 +181,10 @@ func (h *HEPOutputer) Send(msg []byte) {
161
181
logp .Debug ("Connection is not up" , fmt .Sprintf ("index: %d, Len: %d, once: %v" , n , len (h .addr ), onceSent ))
162
182
err = fmt .Errorf ("connection is broken" )
163
183
} else {
164
- h .client [n ].writer .Write (msg )
165
- err = h .client [n ].writer .Flush ()
184
+ _ , err := writeAndFlush (& h .client [n ], msg , "sending message" )
185
+ if err != nil {
186
+ logp .Err ("Failed to send message: %s" , err .Error ())
187
+ }
166
188
}
167
189
168
190
if err != nil {
@@ -183,17 +205,15 @@ func (h *HEPOutputer) Send(msg []byte) {
183
205
} else {
184
206
if h .msgPing != nil {
185
207
logp .Debug ("collector" , "send ping packet after disconnect" )
186
- h .client [n ].writer .Write (h .msgPing )
187
- err = h .client [n ].writer .Flush ()
208
+ _ , err := writeAndFlush (& h .client [n ], h .msgPing , "Error during resend ping packet" )
188
209
if err != nil {
189
- logp .Err ("Bad during resend ping packet : %v " , err )
210
+ logp .Err ("Error sending ping packet: %s " , err . Error () )
190
211
}
191
212
}
192
213
193
- h .client [n ].writer .Write (msg )
194
- err = h .client [n ].writer .Flush ()
214
+ _ , err := writeAndFlush (& h .client [n ], msg , "Bad resend" )
195
215
if err != nil {
196
- logp .Err ("Bad resend: %v" , err )
216
+ logp .Err (err . Error () )
197
217
if config .Cfg .HEPBufferEnable && (! onceSent && n == (len (h .addr )- 1 )) {
198
218
h .copyHEPbufftoFile (msg )
199
219
}
@@ -241,11 +261,16 @@ func (h *HEPOutputer) copyHEPFileOut(n int) (int, error) {
241
261
return 0 , fmt .Errorf ("Connection is broken" )
242
262
}
243
263
244
- //Send Logged HEP upon reconnect out to backend
245
- hl , err := h .client [n ].conn .Write (HEPFileData )
264
+ hl , err := writeAndFlush (& h .client [n ], h .msgPing , "ping operation" )
265
+ if err != nil {
266
+ return 0 , err
267
+ }
268
+
269
+ // Send Logged HEP upon reconnect
270
+ hl , err = writeAndFlush (& h .client [n ], HEPFileData , "HEP reconnect" )
246
271
if err != nil {
247
272
promstats .HepFileFlushesError .Inc ()
248
- return 0 , fmt . Errorf ( "Bad write to socket" )
273
+ return 0 , err
249
274
}
250
275
251
276
err = h .client [n ].writer .Flush ()
0 commit comments