|
| 1 | +package xcap |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/dustin/go-humanize" |
| 9 | +) |
| 10 | + |
| 11 | +// observations holds aggregated observations that can be transformed and merged. |
| 12 | +// |
| 13 | +// All transformation methods (filter, prefix, normalizeKeys) return new instances, |
| 14 | +// leaving the original unchanged. |
| 15 | +type observations struct { |
| 16 | + data map[StatisticKey]*AggregatedObservation |
| 17 | +} |
| 18 | + |
| 19 | +// newObservations creates an empty observations. |
| 20 | +func newObservations() *observations { |
| 21 | + return &observations{data: make(map[StatisticKey]*AggregatedObservation)} |
| 22 | +} |
| 23 | + |
| 24 | +// filter returns a new observations containing only entries with matching stat names. |
| 25 | +func (o *observations) filter(names ...string) *observations { |
| 26 | + if len(names) == 0 || o == nil { |
| 27 | + return o |
| 28 | + } |
| 29 | + |
| 30 | + nameSet := make(map[string]struct{}, len(names)) |
| 31 | + for _, n := range names { |
| 32 | + nameSet[n] = struct{}{} |
| 33 | + } |
| 34 | + |
| 35 | + result := newObservations() |
| 36 | + for k, obs := range o.data { |
| 37 | + if _, ok := nameSet[k.Name]; ok { |
| 38 | + result.data[k] = obs |
| 39 | + } |
| 40 | + } |
| 41 | + return result |
| 42 | +} |
| 43 | + |
| 44 | +// prefix returns a new observations with all stat names prefixed. |
| 45 | +func (o *observations) prefix(p string) *observations { |
| 46 | + if p == "" || o == nil { |
| 47 | + return o |
| 48 | + } |
| 49 | + |
| 50 | + result := newObservations() |
| 51 | + for k, obs := range o.data { |
| 52 | + newKey := StatisticKey{ |
| 53 | + Name: p + k.Name, |
| 54 | + DataType: k.DataType, |
| 55 | + Aggregation: k.Aggregation, |
| 56 | + } |
| 57 | + result.data[newKey] = obs |
| 58 | + } |
| 59 | + return result |
| 60 | +} |
| 61 | + |
| 62 | +// normalizeKeys returns a new observations with dots replaced by underscores in stat names. |
| 63 | +func (o *observations) normalizeKeys() *observations { |
| 64 | + if o == nil { |
| 65 | + return o |
| 66 | + } |
| 67 | + |
| 68 | + result := newObservations() |
| 69 | + for k, obs := range o.data { |
| 70 | + newKey := StatisticKey{ |
| 71 | + Name: strings.ReplaceAll(k.Name, ".", "_"), |
| 72 | + DataType: k.DataType, |
| 73 | + Aggregation: k.Aggregation, |
| 74 | + } |
| 75 | + result.data[newKey] = obs |
| 76 | + } |
| 77 | + return result |
| 78 | +} |
| 79 | + |
| 80 | +// merge merges another observations into this one. |
| 81 | +func (o *observations) merge(other *observations) { |
| 82 | + if other == nil { |
| 83 | + return |
| 84 | + } |
| 85 | + for k, obs := range other.data { |
| 86 | + if existing, ok := o.data[k]; ok { |
| 87 | + existing.Merge(obs) |
| 88 | + } else { |
| 89 | + o.data[k] = &AggregatedObservation{ |
| 90 | + Statistic: obs.Statistic, |
| 91 | + Value: obs.Value, |
| 92 | + Count: obs.Count, |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// ToLogValues converts observations to a slice suitable for go-kit/log. |
| 99 | +// Keys are sorted for deterministic output. |
| 100 | +func (o *observations) toLogValues() []any { |
| 101 | + if o == nil { |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + // Collect key-value pairs for sorting by name. |
| 106 | + type kv struct { |
| 107 | + name string |
| 108 | + value any |
| 109 | + } |
| 110 | + pairs := make([]kv, 0, len(o.data)) |
| 111 | + for k, obs := range o.data { |
| 112 | + pairs = append(pairs, kv{name: k.Name, value: obs.Value}) |
| 113 | + } |
| 114 | + sort.Slice(pairs, func(i, j int) bool { |
| 115 | + return strings.Compare(pairs[i].name, pairs[j].name) < 0 |
| 116 | + }) |
| 117 | + |
| 118 | + result := make([]any, 0, len(pairs)*2) |
| 119 | + for _, p := range pairs { |
| 120 | + value := p.value |
| 121 | + |
| 122 | + // Format bytes values (keys ending with "_bytes") |
| 123 | + if strings.HasSuffix(p.name, "_bytes") { |
| 124 | + switch val := value.(type) { |
| 125 | + case uint64: |
| 126 | + value = humanize.Bytes(val) |
| 127 | + case int64: |
| 128 | + value = humanize.Bytes(uint64(val)) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Format duration values (keys ending with "duration_ns") |
| 133 | + if strings.HasSuffix(p.name, "duration_ns") { |
| 134 | + switch val := value.(type) { |
| 135 | + case int64: |
| 136 | + value = time.Duration(val).String() |
| 137 | + case uint64: |
| 138 | + value = time.Duration(val).String() |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + result = append(result, p.name, value) |
| 143 | + } |
| 144 | + return result |
| 145 | +} |
| 146 | + |
| 147 | +// observationCollector provides methods to collect observations from a Capture. |
| 148 | +type observationCollector struct { |
| 149 | + capture *Capture |
| 150 | + childrenMap map[identifier][]*Region |
| 151 | +} |
| 152 | + |
| 153 | +// newObservationCollector creates a new collector for gathering observations from the given capture. |
| 154 | +func newObservationCollector(capture *Capture) *observationCollector { |
| 155 | + if capture == nil { |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + // Build parent -> children map |
| 160 | + childrenMap := make(map[identifier][]*Region) |
| 161 | + for _, r := range capture.Regions() { |
| 162 | + childrenMap[r.parentID] = append(childrenMap[r.parentID], r) |
| 163 | + } |
| 164 | + |
| 165 | + return &observationCollector{ |
| 166 | + capture: capture, |
| 167 | + childrenMap: childrenMap, |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// fromRegions collects observations from regions with the given name. |
| 172 | +// If rollUp is true, each region's stats include all its descendant stats |
| 173 | +// aggregated according to each stat's aggregation type. |
| 174 | +func (c *observationCollector) fromRegions(name string, rollUp bool, excluded ...string) *observations { |
| 175 | + if c == nil { |
| 176 | + return newObservations() |
| 177 | + } |
| 178 | + |
| 179 | + excludedSet := make(map[string]struct{}, len(excluded)) |
| 180 | + for _, name := range excluded { |
| 181 | + excludedSet[name] = struct{}{} |
| 182 | + } |
| 183 | + |
| 184 | + result := newObservations() |
| 185 | + for _, region := range c.capture.Regions() { |
| 186 | + if region.name != name { |
| 187 | + continue |
| 188 | + } |
| 189 | + |
| 190 | + var obs *observations |
| 191 | + if rollUp { |
| 192 | + obs = c.rollUpObservations(region, excludedSet) |
| 193 | + } else { |
| 194 | + obs = c.getRegionObservations(region) |
| 195 | + } |
| 196 | + |
| 197 | + result.merge(obs) |
| 198 | + } |
| 199 | + |
| 200 | + return result |
| 201 | +} |
| 202 | + |
| 203 | +// getRegionObservations returns a copy of a region's observations. |
| 204 | +func (c *observationCollector) getRegionObservations(region *Region) *observations { |
| 205 | + result := newObservations() |
| 206 | + for k, obs := range region.observations { |
| 207 | + result.data[k] = &AggregatedObservation{ |
| 208 | + Statistic: obs.Statistic, |
| 209 | + Value: obs.Value, |
| 210 | + Count: obs.Count, |
| 211 | + } |
| 212 | + } |
| 213 | + return result |
| 214 | +} |
| 215 | + |
| 216 | +// rollUpObservations computes observations for a region including all its descendants. |
| 217 | +// Stats are aggregated according to their aggregation type. |
| 218 | +func (c *observationCollector) rollUpObservations(region *Region, excludedSet map[string]struct{}) *observations { |
| 219 | + result := c.getRegionObservations(region) |
| 220 | + |
| 221 | + // Recursively aggregate from children. |
| 222 | + for _, child := range c.childrenMap[region.id] { |
| 223 | + // Skip children with excluded names. |
| 224 | + if _, excluded := excludedSet[child.name]; excluded { |
| 225 | + continue |
| 226 | + } |
| 227 | + result.merge(c.rollUpObservations(child, excludedSet)) |
| 228 | + } |
| 229 | + |
| 230 | + return result |
| 231 | +} |
0 commit comments