@@ -3,6 +3,7 @@ package utils
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
89 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
@@ -149,3 +150,61 @@ func CompareResourcesInLogsAndKubeAPI(K8sClient *kubernetes.Clientset, logsClien
149150
150151 return CompareResourcesHelper (logsClient , resourceID , query , resources )
151152}
153+
154+
155+ func GetComputerFromContainerLog (logsClient * azquery.LogsClient , resourceID string , window string ) (map [string ]int64 , error ) {
156+ counts , v2Err := queryCountsByComputer (logsClient , resourceID , "ContainerLogV2" , window )
157+ if v2Err == nil {
158+ return counts , nil
159+ }
160+
161+ fallback , fbErr := queryCountsByComputer (logsClient , resourceID , "ContainerLog" , window )
162+ if fbErr != nil {
163+ return nil , fmt .Errorf ("ContainerLogV2 query failed: %v; ContainerLog fallback failed: %v" , v2Err , fbErr )
164+ }
165+ return fallback , nil
166+ }
167+
168+ func queryCountsByComputer (logsClient * azquery.LogsClient , resourceID string , table string , window string ) (map [string ]int64 , error ) {
169+ query := fmt .Sprintf ("%s | where TimeGenerated > ago(%s) | summarize count() by Computer" , table , window )
170+ tables , err := QueryLogs (logsClient , resourceID , query )
171+ if err != nil {
172+ return nil , err
173+ }
174+
175+ counts := map [string ]int64 {}
176+ for _ , t := range tables {
177+ for _ , row := range t .Rows {
178+ if len (row ) < 2 {
179+ continue
180+ }
181+ computer , ok := row [0 ].(string )
182+ if ! ok || computer == "" {
183+ continue
184+ }
185+ count , _ := row [1 ].(float64 )
186+ counts [strings .ToLower (computer )] += int64 (count )
187+ }
188+ }
189+ return counts , nil
190+ }
191+
192+ // AssertContainerLogNodeCoverage returns nil if every expected node appears
193+ // in the per-Computer count map with a positive row count (compared
194+ // case-insensitively), or an error listing the missing nodes otherwise.
195+ func AssertContainerLogNodeCoverage (expectedNodes []string , observedCountsByComputer map [string ]int64 ) error {
196+ if len (expectedNodes ) == 0 {
197+ return fmt .Errorf ("no expected nodes provided; cannot verify ContainerLogV2 coverage" )
198+ }
199+
200+ var missing []string
201+ for _ , n := range expectedNodes {
202+ if observedCountsByComputer [strings .ToLower (n )] <= 0 {
203+ missing = append (missing , n )
204+ }
205+ }
206+ if len (missing ) > 0 {
207+ return fmt .Errorf ("ContainerLogV2 ingestion is missing for %d/%d expected node(s): %s" , len (missing ), len (expectedNodes ), strings .Join (missing , ", " ))
208+ }
209+ return nil
210+ }
0 commit comments