github.com/Azure/aad-pod-identity@v1.8.17/pkg/stats/stats.go (about) 1 package stats 2 3 import ( 4 "sync" 5 "time" 6 7 "k8s.io/klog/v2" 8 ) 9 10 type minMaxTime struct { 11 min time.Time 12 max time.Time 13 } 14 15 var ( 16 // globalStats is a map that stores the duration of each statistic. 17 globalStats map[Type]time.Duration 18 19 // minMaxTimeStats is a map that stores the earliest start time and 20 // latest end time of statistics that are being collected concurrently. 21 minMaxTimeStats map[Type]minMaxTime 22 23 // countStats is a map that stores the count of each statistic. 24 countStats map[Type]int 25 26 mutex *sync.RWMutex 27 ) 28 29 // Type represents differnet statistics that are being collected. 30 type Type string 31 32 const ( 33 // Total represents the total duration of a specific operation. 34 Total Type = "Total" 35 36 // System represents the duration it takes to list all aad-pod-identity CRDs. 37 System Type = "System" 38 39 // CacheSync represents the duration it takes to sync CRD client's cache. 40 CacheSync Type = "CacheSync" 41 42 // CurrentState represents the duration it takes to generate a list of desired AzureAssignedIdentities. 43 CurrentState Type = "Gather current state" 44 45 // PodList represents the duration it takes to list pods. 46 PodList Type = "Pod listing" 47 48 // AzureIdentityBindingList represents the duration it takes to list AzureIdentityBindings. 49 AzureIdentityBindingList Type = "AzureIdentityBinding listing" 50 51 // AzureIdentityList represents the duration it takes to list AzureIdentities. 52 AzureIdentityList Type = "AzureIdentity listing" 53 54 // AzurePodIdentityExceptionList represents the duration it takes to list AzurePodIdentityExceptions. 55 AzurePodIdentityExceptionList Type = "AzurePodIdentityException listing" 56 57 // AzureAssignedIdentityList represents the duration it takes to list AzureAssignedIdentities. 58 AzureAssignedIdentityList Type = "AzureAssignedIdentity listing" 59 60 // CloudGet represents the duration it takes to complete a GET request to ARM in a given sync cycle. 61 CloudGet Type = "Cloud provider GET" 62 63 // CloudPatch represents the duration it takes to complete a PATCH request to ARM in a given sync cycle. 64 CloudPatch Type = "Cloud provider PATCH" 65 66 // TotalPatchCalls represents the number of PATCH requests to ARM in a given sync cycle. 67 TotalPatchCalls Type = "Number of cloud provider PATCH" 68 69 // TotalGetCalls represents the number of GET requests to ARM in a given sync cycle. 70 TotalGetCalls Type = "Number of cloud provider GET" 71 72 // TotalAzureAssignedIdentitiesCreated represents the number of AzureAssignedIdentities created in a given sync cycle. 73 TotalAzureAssignedIdentitiesCreated Type = "Number of AzureAssignedIdentities created in this sync cycle" 74 75 // TotalAzureAssignedIdentitiesUpdated represents the number of AzureAssignedIdentities updated in a given sync cycle. 76 TotalAzureAssignedIdentitiesUpdated Type = "Number of AzureAssignedIdentities updated in this sync cycle" 77 78 // TotalAzureAssignedIdentitiesDeleted represents the number of AzureAssignedIdentities deleted in a given sync cycle. 79 TotalAzureAssignedIdentitiesDeleted Type = "Number of AzureAssignedIdentities deleted in this sync cycle" 80 81 // FindAzureAssignedIdentitiesToDelete represents the duration it takes to generate a list of AzureAssignedIdentities to be deleted. 82 FindAzureAssignedIdentitiesToDelete Type = "Find AzureAssignedIdentities to delete" 83 84 // FindAzureAssignedIdentitiesToCreate represents the duration it takes to generate a list of AzureAssignedIdentities to be created. 85 FindAzureAssignedIdentitiesToCreate Type = "Find AzureAssignedIdentities to create" 86 87 // DeleteAzureAssignedIdentity represents the duration it takes to delete an AzureAssignedIdentity. 88 DeleteAzureAssignedIdentity Type = "AzureAssignedIdentity deletion" 89 90 // CreateAzureAssignedIdentity represents the duration it takes to create an AzureAssignedIdentity. 91 CreateAzureAssignedIdentity Type = "AzureAssignedIdentity creation" 92 93 // UpdateAzureAssignedIdentity represents the duration it takes to update an AzureAssignedIdentity. 94 UpdateAzureAssignedIdentity Type = "AzureAssignedIdentity update" 95 96 // TotalAzureAssignedIdentitiesCreateOrUpdate represents the duration it takes to create or update a given list of AzureAssignedIdentities. 97 TotalAzureAssignedIdentitiesCreateOrUpdate Type = "Total time to assign or update AzureAssignedIdentities" 98 ) 99 100 // Init initializes the maps uesd to store the 101 func Init() { 102 globalStats = make(map[Type]time.Duration) 103 minMaxTimeStats = make(map[Type]minMaxTime) 104 countStats = make(map[Type]int) 105 mutex = &sync.RWMutex{} 106 } 107 108 // Put puts a value to a specific statistic. 109 func Put(key Type, val time.Duration) { 110 if globalStats != nil { 111 mutex.Lock() 112 defer mutex.Unlock() 113 globalStats[key] = val 114 } 115 } 116 117 // Aggregate aggregates the value of a specific statistic. 118 func Aggregate(key Type, val time.Duration) { 119 if globalStats != nil { 120 mutex.Lock() 121 defer mutex.Unlock() 122 123 globalStats[key] = globalStats[key] + val 124 } 125 } 126 127 // AggregateConcurrent aggregates the value of a specific statistic that is being collected concurrently. 128 func AggregateConcurrent(key Type, begin, end time.Time) { 129 if globalStats != nil && minMaxTimeStats != nil { 130 mutex.Lock() 131 defer mutex.Unlock() 132 133 // we only need the earliest begin time and the latest end 134 // time to calculate the total duration of a statistic 135 var min, max time.Time 136 if _, ok := minMaxTimeStats[key]; !ok { 137 min, max = begin, end 138 } else { 139 min, max = minMaxTimeStats[key].min, minMaxTimeStats[key].max 140 } 141 142 if begin.Before(min) { 143 min = begin 144 } 145 if end.After(max) { 146 max = end 147 } 148 149 minMaxTimeStats[key] = minMaxTime{ 150 min: min, 151 max: max, 152 } 153 globalStats[key] = minMaxTimeStats[key].max.Sub(minMaxTimeStats[key].min) 154 } 155 } 156 157 // Print prints the value of a specific statistic. 158 func Print(key Type) { 159 mutex.RLock() 160 defer mutex.RUnlock() 161 162 klog.Infof("%s: %s", key, globalStats[key]) 163 } 164 165 // PrintCount prints the count of a specific statistic. 166 func PrintCount(key Type) { 167 mutex.RLock() 168 defer mutex.RUnlock() 169 170 klog.Infof("%s: %d", key, countStats[key]) 171 } 172 173 // Increment Increments the count of a specific statistic. 174 func Increment(key Type, count int) { 175 mutex.Lock() 176 defer mutex.Unlock() 177 178 countStats[key] = countStats[key] + count 179 } 180 181 // PrintSync prints all relevant statistics in a sync cycle. 182 func PrintSync() { 183 klog.Infof("** stats collected **") 184 if globalStats != nil { 185 Print(PodList) 186 Print(AzureIdentityList) 187 Print(AzureIdentityBindingList) 188 Print(AzureAssignedIdentityList) 189 Print(System) 190 Print(CacheSync) 191 192 Print(CloudGet) 193 Print(CloudPatch) 194 Print(CreateAzureAssignedIdentity) 195 Print(UpdateAzureAssignedIdentity) 196 Print(DeleteAzureAssignedIdentity) 197 198 PrintCount(TotalPatchCalls) 199 PrintCount(TotalGetCalls) 200 201 PrintCount(TotalAzureAssignedIdentitiesCreated) 202 PrintCount(TotalAzureAssignedIdentitiesUpdated) 203 PrintCount(TotalAzureAssignedIdentitiesDeleted) 204 205 Print(FindAzureAssignedIdentitiesToCreate) 206 Print(FindAzureAssignedIdentitiesToDelete) 207 208 Print(TotalAzureAssignedIdentitiesCreateOrUpdate) 209 210 Print(Total) 211 } 212 klog.Infof("*********************") 213 }