Skip to content

Commit cc4d578

Browse files
committed
Use 'nil' instead of empty map in 'catcher' calls system. Improve code readability and reduce memory allocation overhead.
1 parent a0ca4c5 commit cc4d578

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

log-auth-proxy/handlers/http_handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ func HttpLog(logOutputService *logservice.LogOutputService) gin.HandlerFunc {
1616
var body map[string]interface{}
1717

1818
if err := c.ShouldBindJSON(&body); err != nil {
19-
catcher.Error("Error binding http JSON", err, map[string]any{})
19+
catcher.Error("Error binding http JSON", err, nil)
2020
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
2121
return
2222
}
2323

2424
logType, source, err := getHeaderAndSource(c)
2525
if err != nil {
26-
catcher.Error("Error getting header and source:", err, map[string]any{})
26+
catcher.Error("Error getting header and source:", err, nil)
2727
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
2828
return
2929
}
@@ -36,7 +36,7 @@ func HttpLog(logOutputService *logservice.LogOutputService) gin.HandlerFunc {
3636

3737
jsonBytes, err := json.Marshal(body)
3838
if err != nil {
39-
catcher.Error("Error marshalling http JSON", err, map[string]any{})
39+
catcher.Error("Error marshalling http JSON", err, nil)
4040
c.JSON(http.StatusInternalServerError, gin.H{"error": "unable to convert JSON to string"})
4141
return
4242
}
@@ -54,13 +54,13 @@ func HttpBulkLog(logOutputService *logservice.LogOutputService) gin.HandlerFunc
5454
var body []interface{}
5555

5656
if err := c.ShouldBindJSON(&body); err != nil {
57-
catcher.Error("Error binding bulk JSON", err, map[string]any{})
57+
catcher.Error("Error binding bulk JSON", err, nil)
5858
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
5959
return
6060
}
6161
logType, source, err := getHeaderAndSource(c)
6262
if err != nil {
63-
catcher.Error("Error getting header and source:", err, map[string]any{})
63+
catcher.Error("Error getting header and source:", err, nil)
6464
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
6565
return
6666
}
@@ -79,7 +79,7 @@ func HttpBulkLog(logOutputService *logservice.LogOutputService) gin.HandlerFunc
7979

8080
str, err := json.Marshal(v)
8181
if err != nil {
82-
catcher.Error("Error marshalling bulk JSON", err, map[string]any{})
82+
catcher.Error("Error marshalling bulk JSON", err, nil)
8383
continue
8484
}
8585
log := string(str)
@@ -100,14 +100,14 @@ func HttpGitHubHandler(logOutputService *logservice.LogOutputService) gin.Handle
100100
var body interface{}
101101

102102
if err := c.ShouldBindJSON(&body); err != nil {
103-
catcher.Error("Error binding github JSON", err, map[string]any{})
103+
catcher.Error("Error binding github JSON", err, nil)
104104
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
105105
return
106106
}
107107

108108
jsonBytes, err := json.Marshal(body)
109109
if err != nil {
110-
catcher.Error("Error marshalling github JSON", err, map[string]any{})
110+
catcher.Error("Error marshalling github JSON", err, nil)
111111
c.JSON(http.StatusInternalServerError, gin.H{"error": "unable to convert JSON to string"})
112112
return
113113
}

log-auth-proxy/handlers/tcp_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func HandleRequest(conn net.Conn, interceptor *middleware.LogAuthInterceptor, lo
2020

2121
parts := strings.Split(message, ",LOG:")
2222
if len(parts) != 2 {
23-
catcher.Error("INVALID FORMAT expecting AUTH:<token>,LOG:<log>", nil, map[string]any{})
23+
catcher.Error("INVALID FORMAT expecting AUTH:<token>,LOG:<log>", nil, nil)
2424
conn.Write([]byte("INVALID FORMAT expecting AUTH:<token>,LOG:<log>\n"))
2525
continue
2626
}
@@ -39,6 +39,6 @@ func HandleRequest(conn net.Conn, interceptor *middleware.LogAuthInterceptor, lo
3939
}
4040

4141
if err := scanner.Err(); err != nil {
42-
catcher.Error("Error reading from connection:", err, map[string]any{})
42+
catcher.Error("Error reading from connection:", err, nil)
4343
}
4444
}

log-auth-proxy/logservice/auth_service.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (auth *LogAuthService) SyncAuth() {
5454
func (auth *LogAuthService) syncKeys(typ agent.ConnectorType) {
5555
serverAddress := os.Getenv(config.UTMAgentManagerHostEnv)
5656
if serverAddress == "" {
57-
catcher.Error("Failed to get the SERVER_ADDRESS", nil, map[string]any{})
57+
catcher.Error("Failed to get the SERVER_ADDRESS", nil, nil)
5858
os.Exit(1)
5959
}
6060

@@ -64,7 +64,7 @@ func (auth *LogAuthService) syncKeys(typ agent.ConnectorType) {
6464

6565
conn, err := grpc.NewClient(serverAddress, opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMessageSize)))
6666
if err != nil {
67-
catcher.Error("Failed to connect to gRPC server", err, map[string]any{})
67+
catcher.Error("Failed to connect to gRPC server", err, nil)
6868
return
6969
}
7070
defer conn.Close()
@@ -83,7 +83,7 @@ func (auth *LogAuthService) syncKeys(typ agent.ConnectorType) {
8383
SortBy: "",
8484
})
8585
if err != nil {
86-
catcher.Error("Error sync collector keys", err, map[string]any{})
86+
catcher.Error("Error sync collector keys", err, nil)
8787
return
8888
}
8989

@@ -105,7 +105,7 @@ func (auth *LogAuthService) syncKeys(typ agent.ConnectorType) {
105105
SortBy: "",
106106
})
107107
if err != nil {
108-
catcher.Error("Error sync agent keys", err, map[string]any{})
108+
catcher.Error("Error sync agent keys", err, nil)
109109
return
110110
}
111111

@@ -122,7 +122,7 @@ func (auth *LogAuthService) syncKeys(typ agent.ConnectorType) {
122122
func (auth *LogAuthService) syncConnectionKey() {
123123
panelKey, err := panelservice.GetConnectionKey()
124124
if err != nil {
125-
catcher.Error("Failed to get connection key", err, map[string]any{})
125+
catcher.Error("Failed to get connection key", err, nil)
126126
return
127127
}
128128
auth.Mutex.Lock()

log-auth-proxy/logservice/output_service.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (out *LogOutputService) SendLog(logType config.LogType, logData string) {
4545
defer out.Mutex.Unlock()
4646
port, err := out.getConnectionPort(logType)
4747
if err != nil {
48-
catcher.Error("error getting connection port", err, map[string]any{})
48+
catcher.Error("error getting connection port", err, nil)
4949
return
5050
}
5151
singleLog := logData + config.UTMLogSeparator
@@ -63,7 +63,7 @@ func (out *LogOutputService) SendBulkLog(logType config.LogType, logDataArray []
6363

6464
port, err := out.getConnectionPort(logType)
6565
if err != nil {
66-
catcher.Error("error getting connection port", err, map[string]any{})
66+
catcher.Error("error getting connection port", err, nil)
6767
return
6868
}
6969

@@ -90,13 +90,13 @@ func (out *LogOutputService) sendLogsToLogstash(port string, logs string) {
9090
url := fmt.Sprintf(config.LogstashPipelinesEndpoint, config.LogstashHost(), port)
9191
req, err := http.NewRequest("POST", url, bytes.NewBufferString(logs))
9292
if err != nil {
93-
catcher.Error("error creating request", err, map[string]any{})
93+
catcher.Error("error creating request", err, nil)
9494
}
9595

9696
resp, err := out.Client.Do(req)
9797
if err != nil {
9898
if !strings.Contains(err.Error(), "Client.Timeout exceeded while awaiting headers") {
99-
catcher.Error("error sending logs", err, map[string]any{})
99+
catcher.Error("error sending logs", err, nil)
100100
}
101101
return
102102
}
@@ -124,7 +124,7 @@ func (out *LogOutputService) SyncOutputs() {
124124
for range out.Ticker.C {
125125
serviceMap, err := getServiceMap()
126126
if err != nil {
127-
catcher.Error("error getting service map", err, map[string]any{})
127+
catcher.Error("error getting service map", err, nil)
128128
continue
129129
}
130130
out.Mutex.Lock()

log-auth-proxy/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func main() {
21-
catcher.Info("Starting Log Auth Proxy...", map[string]any{})
21+
catcher.Info("Starting Log Auth Proxy...", nil)
2222
autService := logservice.NewLogAuthService()
2323
go autService.SyncAuth()
2424
authInterceptor := middleware.NewLogAuthInterceptor(autService)
@@ -42,7 +42,7 @@ func startHTTPServer(interceptor *middleware.LogAuthInterceptor, logOutputServic
4242

4343
cert, err := tls.LoadX509KeyPair("/cert/utm.crt", "/cert/utm.key")
4444
if err != nil {
45-
catcher.Error("failed to load server certificates", err, map[string]any{})
45+
catcher.Error("failed to load server certificates", err, nil)
4646
os.Exit(1)
4747
}
4848

@@ -57,18 +57,18 @@ func startHTTPServer(interceptor *middleware.LogAuthInterceptor, logOutputServic
5757
TLSConfig: tlsConfig,
5858
}
5959

60-
catcher.Info("Starting HTTP server on 0.0.0.0:8080", map[string]any{})
60+
catcher.Info("Starting HTTP server on 0.0.0.0:8080", nil)
6161
err = server.ListenAndServeTLS("", "")
6262
if err != nil {
63-
catcher.Error("Failed to start HTTP server", err, map[string]any{})
63+
catcher.Error("Failed to start HTTP server", err, nil)
6464
os.Exit(1)
6565
}
6666
}
6767

6868
func startGRPCServer(interceptor *middleware.LogAuthInterceptor, logOutputService *logservice.LogOutputService) {
6969
cert, err := tls.LoadX509KeyPair("/cert/utm.crt", "/cert/utm.key")
7070
if err != nil {
71-
catcher.Error("failed to load server certificates", err, map[string]any{})
71+
catcher.Error("failed to load server certificates", err, nil)
7272
os.Exit(1)
7373
}
7474

@@ -97,13 +97,13 @@ func startGRPCServer(interceptor *middleware.LogAuthInterceptor, logOutputServic
9797

9898
lis, err := net.Listen("tcp", "0.0.0.0:50051")
9999
if err != nil {
100-
catcher.Error("failed to listen grpc server", err, map[string]any{})
100+
catcher.Error("failed to listen grpc server", err, nil)
101101
os.Exit(1)
102102
}
103103

104-
catcher.Info("Starting gRPC server on 0.0.0.0:50051", map[string]any{})
104+
catcher.Info("Starting gRPC server on 0.0.0.0:50051", nil)
105105
if err := grpcServer.Serve(lis); err != nil {
106-
catcher.Error("Failed to serve grpc", err, map[string]any{})
106+
catcher.Error("Failed to serve grpc", err, nil)
107107
os.Exit(1)
108108
}
109109
}

log-auth-proxy/middleware/log_auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (interceptor *LogAuthInterceptor) HTTPGitHubAuthInterceptor() gin.HandlerFu
9393
return func(c *gin.Context) {
9494
body, err := io.ReadAll(c.Request.Body)
9595
if err != nil {
96-
catcher.Error("error reading request body", err, map[string]any{})
96+
catcher.Error("error reading request body", err, nil)
9797
c.AbortWithStatusJSON(http.StatusInternalServerError, "error reading request body")
9898
return
9999
}

0 commit comments

Comments
 (0)