@@ -4,27 +4,26 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
+ "go.uber.org/zap"
7
8
"io"
8
9
"net/http"
9
10
"os"
10
11
"testing"
11
12
)
12
13
13
- // LogResponse represents the structure of the logs API response
14
14
type LogResponse struct {
15
15
Hits struct {
16
16
Total int `json:"total"`
17
17
Hits []struct {
18
18
Source struct {
19
- Kubernetes struct {
20
- ContainerImageTag string `json:"container_image_tag"`
21
- ContainerName string `json:"container_name"`
22
- ContainerImage string `json:"container_image"`
23
- NamespaceName string `json:"namespace_name"`
24
- PodName string `json:"pod_name"`
25
- PodID string `json:"pod_id"`
26
- Host string `json:"host"`
27
- } `json:"kubernetes"`
19
+ ContainerImageTag string `json:"container_image_tag"`
20
+ ContainerImageName string `json:"container_image_name"`
21
+ ContainerName string `json:"k8s_container_name"`
22
+ NamespaceName string `json:"k8s_namespace_name"`
23
+ PodName string `json:"k8s_pod_name"`
24
+ PodUID string `json:"k8s_pod_uid"`
25
+ NodeName string `json:"k8s_node_name"`
26
+ LogLevel string `json:"log_level"`
28
27
} `json:"_source"`
29
28
} `json:"hits"`
30
29
} `json:"hits"`
@@ -44,21 +43,24 @@ func TestLogzioMonitoringLogs(t *testing.T) {
44
43
if logResponse .Hits .Total == 0 {
45
44
t .Errorf ("No logs found" )
46
45
}
47
- // Verify required fields
48
- requiredFields := []string {"container_image_tag" , "container_name" , "container_image" , "namespace_name" , "pod_name" , "pod_id" , "host" }
49
- missingFields := verifyLogs (logResponse , requiredFields )
50
- if len (missingFields ) > 0 {
51
- t .Errorf ("Missing log fields: %v" , missingFields )
46
+
47
+ for _ , hit := range logResponse .Hits .Hits {
48
+ kubernetes := hit .Source
49
+ if kubernetes .ContainerImageTag == "" || kubernetes .ContainerName == "" || kubernetes .NamespaceName == "" || kubernetes .PodName == "" || kubernetes .PodUID == "" || kubernetes .NodeName == "" || kubernetes .ContainerImageName == "" || kubernetes .LogLevel == "" {
50
+ logger .Error ("Missing log fields" , zap .Any ("log" , hit ))
51
+ t .Errorf ("Missing log fields" )
52
+ break
53
+ }
52
54
}
53
55
}
54
56
55
- // fetchLogs fetches the logs from the logz.io API
56
57
func fetchLogs (logsApiKey string ) (* LogResponse , error ) {
57
58
url := fmt .Sprintf ("%s/search" , BaseLogzioApiUrl )
58
59
client := & http.Client {}
59
60
envID := os .Getenv ("ENV_ID" )
60
- query := fmt .Sprintf ("env_id:%s AND type:agent-k8s" , envID )
61
+ query := fmt .Sprintf ("env_id:%s AND type:agent-k8s AND k8s_deployment_name:log-generator " , envID )
61
62
formattedQuery := formatQuery (query )
63
+ logger .Info ("sending api request" , zap .String ("url" , url ), zap .String ("query" , query ))
62
64
req , err := http .NewRequest ("POST" , url , bytes .NewBufferString (formattedQuery ))
63
65
if err != nil {
64
66
return nil , err
@@ -90,52 +92,3 @@ func fetchLogs(logsApiKey string) (*LogResponse, error) {
90
92
91
93
return & logResponse , nil
92
94
}
93
-
94
- // verifyLogs checks if the logs contain the required Kubernetes fields
95
- func verifyLogs (logResponse * LogResponse , requiredFields []string ) []string {
96
- missingFieldsMap := make (map [string ]bool , len (requiredFields ))
97
- for _ , field := range requiredFields {
98
- missingFieldsMap [field ] = false
99
- }
100
-
101
- for _ , hit := range logResponse .Hits .Hits {
102
- kubernetes := hit .Source .Kubernetes
103
- if kubernetes .ContainerImageTag == "" {
104
- missingFieldsMap ["container_image_tag" ] = true
105
- break
106
- }
107
- if kubernetes .ContainerName == "" {
108
- missingFieldsMap ["container_name" ] = true
109
- break
110
- }
111
- if kubernetes .ContainerImage == "" {
112
- missingFieldsMap ["container_image" ] = true
113
- break
114
- }
115
- if kubernetes .NamespaceName == "" {
116
- missingFieldsMap ["namespace_name" ] = true
117
- break
118
- }
119
- if kubernetes .PodName == "" {
120
- missingFieldsMap ["pod_name" ] = true
121
- break
122
- }
123
- if kubernetes .PodID == "" {
124
- missingFieldsMap ["pod_id" ] = true
125
- break
126
- }
127
- if kubernetes .Host == "" {
128
- missingFieldsMap ["host" ] = true
129
- break
130
- }
131
- }
132
-
133
- var missingFields []string
134
- for field , value := range missingFieldsMap {
135
- if value == true {
136
- missingFields = append (missingFields , field )
137
- }
138
- }
139
-
140
- return missingFields
141
- }
0 commit comments