set stores multiple metric values for an instance This method is unexported to prevent direct usage by modules. Use the type-safe Set() methods on generated context types instead.
(ctx any, labels any, values map[string]int64)
| 88 | // This method is unexported to prevent direct usage by modules. |
| 89 | // Use the type-safe Set() methods on generated context types instead. |
| 90 | func (s *CollectorState) set(ctx any, labels any, values map[string]int64) { |
| 91 | instance := s.GetInstance(ctx, labels) |
| 92 | |
| 93 | // Get context metadata |
| 94 | contextMeta := getContextMetadata(ctx) |
| 95 | |
| 96 | // Store each metric value |
| 97 | for dimName, rawValue := range values { |
| 98 | // Find dimension metadata |
| 99 | var dim *Dimension |
| 100 | for _, d := range contextMeta.Dimensions { |
| 101 | if d.Name == dimName { |
| 102 | dim = &d |
| 103 | break |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if dim == nil { |
| 108 | // Log warning for unknown dimensions to help catch typos and configuration errors |
| 109 | s.Warningf("unknown dimension '%s' for context '%s' - dimension will be skipped", dimName, contextMeta.Name) |
| 110 | continue |
| 111 | } |
| 112 | |
| 113 | // Apply precision FIRST, then unit conversion to avoid integer division precision loss |
| 114 | // 1. First apply precision multiplication to preserve accuracy |
| 115 | // 2. Then apply unit conversion: (value * mul) / div |
| 116 | // This ensures accurate conversion to base units |
| 117 | precisionValue := rawValue * int64(dim.Precision) |
| 118 | finalValue := precisionValue |
| 119 | if dim.Mul != 0 && dim.Div != 0 { |
| 120 | finalValue = (precisionValue * int64(dim.Mul)) / int64(dim.Div) |
| 121 | } |
| 122 | |
| 123 | s.metrics = append(s.metrics, MetricValue{ |
| 124 | Instance: *instance, |
| 125 | Dimension: dimName, |
| 126 | Value: finalValue, |
| 127 | Timestamp: time.Now(), |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // IsTimeFor checks if it's time to collect a specific context |
| 133 | func (s *CollectorState) IsTimeFor(contextName string) bool { |
no test coverage detected