// instantFromCumulativeMetric calculates the value of an instantaneous metric from two // points of a cumulative metric, such as cpu/usage. // The inputs are the value and timestamp of the newer cumulative datapoint, // and a pointer to a TimePoint holding the previous cumulative datapoint. func instantFromCumulativeMetric(value uint64, stamp time.Time, prev *store.TimePoint) (uint64, error) { if prev == nil { return uint64(0), fmt.Errorf("unable to calculate instant metric with nil previous TimePoint") } tdelta := uint64(stamp.Sub(prev.Timestamp).Nanoseconds()) if tdelta == 0 { return uint64(0), fmt.Errorf("cumulative metric timestamps are identical") } vdelta := (value - prev.Value.(uint64)) * 1024 instaVal := vdelta / tdelta prev.Value = value prev.Timestamp = stamp return instaVal, nil }
// instantFromCumulativeMetric calculates the value of an instantaneous metric from two // points of a cumulative metric, such as cpu/usage. // The inputs are the value and timestamp of the newer cumulative datapoint, // and a pointer to a TimePoint holding the previous cumulative datapoint. func instantFromCumulativeMetric(value uint64, stamp time.Time, prev *store.TimePoint) (uint64, error) { if prev == nil { return uint64(0), fmt.Errorf("unable to calculate instant metric with nil previous TimePoint") } tdelta := uint64(stamp.Sub(prev.Timestamp).Nanoseconds()) if tdelta == 0 { return uint64(0), fmt.Errorf("cumulative metric timestamps are identical") } // Divide metric by nanoseconds that have elapsed, multiply by 1000 to get an unsigned metric vdelta := (value - prev.Value.(uint64)) * 1000 instaVal := vdelta / tdelta prev.Value = value prev.Timestamp = stamp return instaVal, nil }
// instantFromCumulativeMetric calculates the value of an instantaneous metric from two // points of a cumulative metric, such as cpu/usage. // The inputs are the value and timestamp of the newer cumulative datapoint, // and a pointer to a TimePoint holding the previous cumulative datapoint. func instantFromCumulativeMetric(value uint64, stamp time.Time, prev *store.TimePoint) (uint64, error) { if prev == nil { return uint64(0), fmt.Errorf("unable to calculate instant metric with nil previous TimePoint") } if !stamp.After(prev.Timestamp) { return uint64(0), fmt.Errorf("the previous TimePoint is not earlier in time than the newer one") } tdelta := uint64(stamp.Sub(prev.Timestamp).Nanoseconds()) // Divide metric by nanoseconds that have elapsed, multiply by 1000 to get an unsigned metric if value < prev.Value.(uint64) { return uint64(0), fmt.Errorf("the provided value %d is less than the previous one %d", value, prev.Value.(uint64)) } vdelta := (value - prev.Value.(uint64)) * 1000 instaVal := vdelta / tdelta prev.Value = value prev.Timestamp = stamp return instaVal, nil }