-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathquery_logs_api_utils.go
More file actions
210 lines (182 loc) · 5.89 KB
/
query_logs_api_utils.go
File metadata and controls
210 lines (182 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package utils
import (
"context"
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
"k8s.io/client-go/kubernetes"
)
func SetupLogsClient() (*azquery.LogsClient, error) {
// Create a new LogsClient
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, fmt.Errorf("failed to create a new LogsClient: %v", err)
}
client, err := azquery.NewLogsClient(cred, nil)
if err != nil {
return nil, fmt.Errorf("failed to create a new LogsClient: %v", err)
}
return client, nil
}
func QueryLogs(logsClient *azquery.LogsClient, resourceID string, query string) ([]*azquery.Table, error) {
res, err := logsClient.QueryResource(
context.TODO(),
resourceID,
azquery.Body{Query: to.Ptr(query)},
nil)
if err != nil {
return nil, fmt.Errorf("Failed to query logs: %v", err)
}
if res.Error != nil {
return nil, fmt.Errorf("The query returned the error: %v", *&res.Error)
}
return res.Tables, nil
}
func QueryLogsForCount(logsClient *azquery.LogsClient, resourceID string, query string, expectZeroCount bool) error {
tables, err := QueryLogs(logsClient, resourceID, query)
if err != nil {
return err
}
if tables == nil || len(tables) == 0 {
return fmt.Errorf("The query returned 0 tables")
}
fmt.Println("Query result of query: ", query)
for _, table := range tables {
fmt.Println("Number of rows: ", len(table.Rows))
if table.Rows == nil || len(table.Rows) == 0 {
return fmt.Errorf("The query returned 0 rows")
}
if len(table.Rows) > 1 {
return fmt.Errorf("The query returned more than 1 row, this test is only used for summarize count queries")
}
fmt.Println("Count: ", table.Rows[0][0])
if table.Rows[0][0].(float64) == 0 {
if expectZeroCount {
return nil
}
return fmt.Errorf("The query returned 0 count")
}
if table.Rows[0][0].(float64) > 0 {
if !expectZeroCount {
return nil
}
return fmt.Errorf("The query returned count greater than 0")
}
}
return fmt.Errorf("The query returned unexpected result")
}
func CompareResourcesHelper(logsClient *azquery.LogsClient, resourceID string, query string, resources []string) error {
tables, err := QueryLogs(logsClient, resourceID, query)
if err != nil {
return err
}
if tables == nil || len(tables) == 0 {
return fmt.Errorf("The query returned 0 tables")
}
fmt.Println("Compare resources result:")
for _, table := range tables {
// check if the resource exists in the logs
for _, resource := range resources {
fmt.Println("Checking resource: ", resource)
found := false
for _, row := range table.Rows {
for _, cell := range row {
if cell == resource {
found = true
break
}
}
if found {
break
}
}
if !found {
return fmt.Errorf("Resource %s not found in logs", resource)
}
}
// if all resources found, return nil
return nil
}
return fmt.Errorf("The query returned unexpected result")
}
func CompareResourcesInLogsAndKubeAPI(K8sClient *kubernetes.Clientset, logsClient *azquery.LogsClient, resourceID string, logsTable string) error {
var resources []string
var query string
if logsTable == "KubeNodeInventory" {
nodes, err := GetAllNodes(K8sClient)
if err != nil {
return err
}
for _, node := range nodes {
resources = append(resources, node.Name)
}
query = logsTable + " | where TimeGenerated > ago(5m) | distinct Computer"
} else if logsTable == "KubePodInventory" {
pods, err := GetAllAgentPods(K8sClient)
if err != nil {
return err
}
for _, pod := range pods {
// skip the testkube namespace as it creates runtime pods for the triggered test which might not be present in the logs
if pod.Namespace == "testkube" {
continue
}
resources = append(resources, pod.Name)
}
query = logsTable + " | where TimeGenerated > ago(5m) | distinct Name"
}
return CompareResourcesHelper(logsClient, resourceID, query, resources)
}
func GetComputerFromContainerLog(logsClient *azquery.LogsClient, resourceID string, window string) (map[string]int64, error) {
counts, v2Err := queryCountsByComputer(logsClient, resourceID, "ContainerLogV2", window)
if v2Err == nil {
return counts, nil
}
fallback, fbErr := queryCountsByComputer(logsClient, resourceID, "ContainerLog", window)
if fbErr != nil {
return nil, fmt.Errorf("ContainerLogV2 query failed: %v; ContainerLog fallback failed: %v", v2Err, fbErr)
}
return fallback, nil
}
func queryCountsByComputer(logsClient *azquery.LogsClient, resourceID string, table string, window string) (map[string]int64, error) {
query := fmt.Sprintf("%s | where TimeGenerated > ago(%s) | summarize count() by Computer", table, window)
tables, err := QueryLogs(logsClient, resourceID, query)
if err != nil {
return nil, err
}
counts := map[string]int64{}
for _, t := range tables {
for _, row := range t.Rows {
if len(row) < 2 {
continue
}
computer, ok := row[0].(string)
if !ok || computer == "" {
continue
}
count, _ := row[1].(float64)
counts[strings.ToLower(computer)] += int64(count)
}
}
return counts, nil
}
// AssertContainerLogNodeCoverage returns nil if every expected node appears
// in the per-Computer count map with a positive row count (compared
// case-insensitively), or an error listing the missing nodes otherwise.
func AssertContainerLogNodeCoverage(expectedNodes []string, observedCountsByComputer map[string]int64) error {
if len(expectedNodes) == 0 {
return fmt.Errorf("no expected nodes provided; cannot verify ContainerLogV2 coverage")
}
var missing []string
for _, n := range expectedNodes {
if observedCountsByComputer[strings.ToLower(n)] <= 0 {
missing = append(missing, n)
}
}
if len(missing) > 0 {
return fmt.Errorf("ContainerLogV2 ingestion is missing for %d/%d expected node(s): %s", len(missing), len(expectedNodes), strings.Join(missing, ", "))
}
return nil
}