Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

🚀tcp single checker (#1071)

authored by

Thibault Le Ouay and committed by
GitHub
9b25a0da 11c774cf

+18 -41
+18 -41
apps/checker/handlers/tcp.go
··· 5 5 "fmt" 6 6 "net/http" 7 7 "strconv" 8 + "time" 8 9 9 10 "github.com/cenkalti/backoff/v4" 10 11 "github.com/gin-gonic/gin" ··· 14 15 ) 15 16 16 17 type TCPResponse struct { 17 - Error string `json:"error,omitempty"` 18 - Region string `json:"region"` 19 - RequestId int64 `json:"requestId,omitempty"` 20 - WorkspaceID int64 `json:"workspaceId"` 21 - MonitorID int64 `json:"monitorId"` 22 - Timestamp int64 `json:"timestamp"` 23 - Timing checker.TCPResponseTiming `json:"timing"` 18 + Region string `json:"region"` 19 + ErrorMessage string `json:"errorMessage"` 20 + RequestId int64 `json:"requestId,omitempty"` 21 + WorkspaceID int64 `json:"workspaceId"` 22 + MonitorID int64 `json:"monitorId"` 23 + Timestamp int64 `json:"timestamp"` 24 + Latency int64 `json:"latency"` 25 + Timing checker.TCPResponseTiming `json:"timing"` 26 + Error uint8 `json:"error,omitempty"` 24 27 } 25 28 26 29 // Only used for Tinybird. ··· 233 236 return 234 237 } 235 238 236 - workspaceId, err := strconv.ParseInt(req.WorkspaceID, 10, 64) 237 - if err != nil { 238 - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"}) 239 - 240 - return 241 - } 242 - 243 - monitorId, err := strconv.ParseInt(req.MonitorID, 10, 64) 244 - if err != nil { 245 - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"}) 246 - 247 - return 248 - } 249 - 250 239 var called int 251 240 252 241 var response TCPResponse 253 242 254 243 op := func() error { 255 244 called++ 245 + timestamp := time.Now().UTC().UnixMilli() 256 246 res, err := checker.PingTcp(int(req.Timeout), req.URL) 257 247 258 248 if err != nil { ··· 260 250 } 261 251 262 252 response = TCPResponse{ 263 - WorkspaceID: workspaceId, 264 - Timestamp: req.CronTimestamp, 253 + Timestamp: timestamp, 265 254 Timing: checker.TCPResponseTiming{ 266 255 TCPStart: res.TCPStart, 267 256 TCPDone: res.TCPDone, 268 257 }, 269 - Region: h.Region, 270 - MonitorID: monitorId, 258 + Latency: res.TCPDone - res.TCPStart, 259 + Region: h.Region, 271 260 } 272 261 273 262 timingAsString, err := json.Marshal(res) ··· 278 267 latency := res.TCPDone - res.TCPStart 279 268 280 269 data := TCPData{ 281 - WorkspaceID: workspaceId, 282 270 CronTimestamp: req.CronTimestamp, 283 271 Timestamp: res.TCPStart, 284 272 Error: 0, 285 273 ErrorMessage: "", 286 274 Region: h.Region, 287 - MonitorID: monitorId, 288 275 Timing: string(timingAsString), 289 276 Latency: latency, 290 277 RequestId: req.RequestId, ··· 301 288 return nil 302 289 } 303 290 304 - if err := backoff.Retry(op, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 3)); err != nil && req.RequestId != 0 { 305 - if err := h.TbClient.SendEvent(ctx, TCPData{ 306 - WorkspaceID: workspaceId, 307 - CronTimestamp: req.CronTimestamp, 308 - ErrorMessage: err.Error(), 309 - Region: h.Region, 310 - MonitorID: monitorId, 311 - Error: 1, 312 - RequestId: req.RequestId, 313 - Trigger: "api", 314 - URI: req.URL, 315 - }, dataSourceName); err != nil { 316 - log.Ctx(ctx).Error().Err(err).Msg("failed to send event to tinybird") 317 - } 291 + if err := backoff.Retry(op, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 3)); err != nil { 292 + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"}) 293 + 294 + return 318 295 } 319 296 320 297 c.JSON(http.StatusOK, response)