Skip to content

Commit

Permalink
Changed error logs from plain text to structured
Browse files Browse the repository at this point in the history
Signed-off-by: Yanjun Zhou <zhouya@vmware.com>
  • Loading branch information
yanjunz97 committed Feb 7, 2022
1 parent 4a42e73 commit ec3efce
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions plugins/flow-visibility/clickhouse-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main() {
func skipRound() bool {
logString, err := getPodLogs()
if err != nil {
klog.Errorf("Not find last monitor job: %v", err)
klog.ErrorS(err, "Not find last monitor job")
return false
}
// A sample log string looks like the following
Expand All @@ -100,7 +100,7 @@ func skipRound() bool {
lines := strings.Split(logs[1], "\n")
remainingRoundsNum, convErr := strconv.Atoi(lines[0])
if convErr != nil {
klog.Errorf("Error in finding last monitor job: %v", convErr)
klog.ErrorS(convErr, "Error in finding last monitor job")
return false
}
if remainingRoundsNum > 0 {
Expand Down Expand Up @@ -174,13 +174,13 @@ func connectLoop() (*sql.DB, error) {
dataSourceName := fmt.Sprintf("tcp://%s:%s?debug=true&username=%s&password=%s", host, port, userName, password)
connect, err := sql.Open("clickhouse", dataSourceName)
if err != nil {
klog.Errorf("Failed to connect to Clickhouse: %v", err)
klog.ErrorS(err, "Failed to connect to Clickhouse")
}
if err := connect.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
klog.Errorf("Failed to ping Clickhouse: %s", exception.Message)
klog.ErrorS(nil, "Failed to ping Clickhouse", "message", exception.Message)
} else {
klog.Errorf("Failed to ping Clickhouse: %v", err)
klog.ErrorS(err, "Failed to ping Clickhouse")
}
} else {
return connect, nil
Expand All @@ -194,7 +194,7 @@ func monitorMemory(connect *sql.DB) bool {
// Get memory usage from Clickhouse system table
rows, err := connect.Query("SELECT free_space, total_space FROM system.disks")
if err != nil {
klog.Errorf("Failed to get memory usage for Clickhouse: %v", err)
klog.ErrorS(err, "Failed to get memory usage for Clickhouse")
return false
}
defer rows.Close()
Expand All @@ -205,7 +205,7 @@ func monitorMemory(connect *sql.DB) bool {
totalSpace uint64
)
if err := rows.Scan(&freeSpace, &totalSpace); err != nil {
klog.Errorf("Failed to read memory usage for Clickhouse: %v", err)
klog.ErrorS(err, "Failed to read memory usage for Clickhouse")
return false
}
// Calculate the memory usage
Expand All @@ -217,7 +217,7 @@ func monitorMemory(connect *sql.DB) bool {
// Call getDeleteRowNum to get the number of records to be deleted
command := fmt.Sprintf("ALTER TABLE %s DELETE WHERE timeInserted < (SELECT timeInserted FROM %s LIMIT 1 OFFSET %d)", tableName, tableName, getDeleteRowNum(connect))
if _, err := connect.Exec(command); err != nil {
klog.Errorf("Failed to delete records from Clickhouse: %v", err)
klog.ErrorS(err, "Failed to delete records from Clickhouse")
return false
}
return true
Expand All @@ -232,15 +232,15 @@ func getDeleteRowNum(connect *sql.DB) uint64 {
command := fmt.Sprintf("SELECT COUNT() FROM %s", tableName)
rows, err := connect.Query(command)
if err != nil {
klog.Errorf("Failed to get the number of records from %s: %v", tableName, err)
klog.ErrorS(err, "Failed to get the number of records", "table", tableName)
return deleteRowNum
}
defer rows.Close()

for rows.Next() {
var count uint64
if err := rows.Scan(&count); err != nil {
klog.Error("Failed to read the number of records from %s: %v", tableName, err)
klog.ErrorS(err, "Failed to get the number of records", "table", tableName)
} else {
deleteRowNum = uint64(float64(count) * deletePercentage)
}
Expand Down

0 comments on commit ec3efce

Please sign in to comment.