Files
Pulse/pkg/metrics/retained_numeric.go
pulse-triage[bot] 105fb05cc3 Decode native retained metric numbers without per-point reflection
Keep database/sql conversion for atypical values and rejection of NULL, malformed and overflowing observations. Compare native and fallback scans against primitive destinations and protect the allocation-free native path.

Contract-Neutral: Retained observations, numeric acceptance and rejection, SQL, sampling, source choice, extrema and access boundaries are unchanged; only application-side numeric assignment avoids per-observation reflection.
Change-source: pulse-maintainer
2026-10-24 17:32:56 +01:00

47 lines
1.1 KiB
Go

package metrics
import (
"database/sql"
"fmt"
)
// Retained rows normally carry int64 timestamps/ordinals and float64 values.
// Assign those types without database/sql's per-observation reflection. Keep its
// ordinary numeric conversion for older or atypically typed observations; this
// is an application-side decoder, not a different SQL or driver contract.
type retainedInt64 int64
func (n *retainedInt64) Scan(src any) error {
if value, ok := src.(int64); ok {
*n = retainedInt64(value)
return nil
}
var compatible sql.NullInt64
if err := compatible.Scan(src); err != nil {
return err
}
if !compatible.Valid {
return fmt.Errorf("converting NULL to int64 is unsupported")
}
*n = retainedInt64(compatible.Int64)
return nil
}
type retainedFloat64 float64
func (n *retainedFloat64) Scan(src any) error {
if value, ok := src.(float64); ok {
*n = retainedFloat64(value)
return nil
}
var compatible sql.NullFloat64
if err := compatible.Scan(src); err != nil {
return err
}
if !compatible.Valid {
return fmt.Errorf("converting NULL to float64 is unsupported")
}
*n = retainedFloat64(compatible.Float64)
return nil
}