update k8s.io/utils to fix util tracing panic

This commit is contained in:
David Ashpole
2022-11-07 19:30:21 +00:00
parent f8750e2faf
commit 3f1511c8e9
56 changed files with 125 additions and 87 deletions

View File

@@ -53,6 +53,7 @@ func Int(i int) *int {
}
// IntPtr is a function variable referring to Int.
//
// Deprecated: Use Int instead.
var IntPtr = Int // for back-compat
@@ -66,6 +67,7 @@ func IntDeref(ptr *int, def int) int {
}
// IntPtrDerefOr is a function variable referring to IntDeref.
//
// Deprecated: Use IntDeref instead.
var IntPtrDerefOr = IntDeref // for back-compat
@@ -75,6 +77,7 @@ func Int32(i int32) *int32 {
}
// Int32Ptr is a function variable referring to Int32.
//
// Deprecated: Use Int32 instead.
var Int32Ptr = Int32 // for back-compat
@@ -88,6 +91,7 @@ func Int32Deref(ptr *int32, def int32) int32 {
}
// Int32PtrDerefOr is a function variable referring to Int32Deref.
//
// Deprecated: Use Int32Deref instead.
var Int32PtrDerefOr = Int32Deref // for back-compat
@@ -109,6 +113,7 @@ func Uint(i uint) *uint {
}
// UintPtr is a function variable referring to Uint.
//
// Deprecated: Use Uint instead.
var UintPtr = Uint // for back-compat
@@ -122,6 +127,7 @@ func UintDeref(ptr *uint, def uint) uint {
}
// UintPtrDerefOr is a function variable referring to UintDeref.
//
// Deprecated: Use UintDeref instead.
var UintPtrDerefOr = UintDeref // for back-compat
@@ -131,6 +137,7 @@ func Uint32(i uint32) *uint32 {
}
// Uint32Ptr is a function variable referring to Uint32.
//
// Deprecated: Use Uint32 instead.
var Uint32Ptr = Uint32 // for back-compat
@@ -144,6 +151,7 @@ func Uint32Deref(ptr *uint32, def uint32) uint32 {
}
// Uint32PtrDerefOr is a function variable referring to Uint32Deref.
//
// Deprecated: Use Uint32Deref instead.
var Uint32PtrDerefOr = Uint32Deref // for back-compat
@@ -165,6 +173,7 @@ func Int64(i int64) *int64 {
}
// Int64Ptr is a function variable referring to Int64.
//
// Deprecated: Use Int64 instead.
var Int64Ptr = Int64 // for back-compat
@@ -178,6 +187,7 @@ func Int64Deref(ptr *int64, def int64) int64 {
}
// Int64PtrDerefOr is a function variable referring to Int64Deref.
//
// Deprecated: Use Int64Deref instead.
var Int64PtrDerefOr = Int64Deref // for back-compat
@@ -199,6 +209,7 @@ func Uint64(i uint64) *uint64 {
}
// Uint64Ptr is a function variable referring to Uint64.
//
// Deprecated: Use Uint64 instead.
var Uint64Ptr = Uint64 // for back-compat
@@ -212,6 +223,7 @@ func Uint64Deref(ptr *uint64, def uint64) uint64 {
}
// Uint64PtrDerefOr is a function variable referring to Uint64Deref.
//
// Deprecated: Use Uint64Deref instead.
var Uint64PtrDerefOr = Uint64Deref // for back-compat
@@ -233,6 +245,7 @@ func Bool(b bool) *bool {
}
// BoolPtr is a function variable referring to Bool.
//
// Deprecated: Use Bool instead.
var BoolPtr = Bool // for back-compat
@@ -246,6 +259,7 @@ func BoolDeref(ptr *bool, def bool) bool {
}
// BoolPtrDerefOr is a function variable referring to BoolDeref.
//
// Deprecated: Use BoolDeref instead.
var BoolPtrDerefOr = BoolDeref // for back-compat
@@ -267,6 +281,7 @@ func String(s string) *string {
}
// StringPtr is a function variable referring to String.
//
// Deprecated: Use String instead.
var StringPtr = String // for back-compat
@@ -280,6 +295,7 @@ func StringDeref(ptr *string, def string) string {
}
// StringPtrDerefOr is a function variable referring to StringDeref.
//
// Deprecated: Use StringDeref instead.
var StringPtrDerefOr = StringDeref // for back-compat
@@ -301,6 +317,7 @@ func Float32(i float32) *float32 {
}
// Float32Ptr is a function variable referring to Float32.
//
// Deprecated: Use Float32 instead.
var Float32Ptr = Float32
@@ -314,6 +331,7 @@ func Float32Deref(ptr *float32, def float32) float32 {
}
// Float32PtrDerefOr is a function variable referring to Float32Deref.
//
// Deprecated: Use Float32Deref instead.
var Float32PtrDerefOr = Float32Deref // for back-compat
@@ -335,6 +353,7 @@ func Float64(i float64) *float64 {
}
// Float64Ptr is a function variable referring to Float64.
//
// Deprecated: Use Float64 instead.
var Float64Ptr = Float64
@@ -348,6 +367,7 @@ func Float64Deref(ptr *float64, def float64) float64 {
}
// Float64PtrDerefOr is a function variable referring to Float64Deref.
//
// Deprecated: Use Float64Deref instead.
var Float64PtrDerefOr = Float64Deref // for back-compat

30
vendor/k8s.io/utils/trace/trace.go generated vendored
View File

@@ -21,6 +21,7 @@ import (
"context"
"fmt"
"math/rand"
"sync"
"time"
"k8s.io/klog/v2"
@@ -93,13 +94,16 @@ func (s traceStep) writeItem(b *bytes.Buffer, formatter string, startTime time.T
// Trace keeps track of a set of "steps" and allows us to log a specific
// step if it took longer than its share of the total allowed time
type Trace struct {
// constant fields
name string
fields []Field
threshold *time.Duration
startTime time.Time
endTime *time.Time
traceItems []traceItem
parentTrace *Trace
// fields guarded by a lock
lock sync.RWMutex
threshold *time.Duration
endTime *time.Time
traceItems []traceItem
}
func (t *Trace) time() time.Time {
@@ -138,6 +142,8 @@ func New(name string, fields ...Field) *Trace {
// how long it took. The Fields add key value pairs to provide additional details about the trace
// step.
func (t *Trace) Step(msg string, fields ...Field) {
t.lock.Lock()
defer t.lock.Unlock()
if t.traceItems == nil {
// traces almost always have less than 6 steps, do this to avoid more than a single allocation
t.traceItems = make([]traceItem, 0, 6)
@@ -153,7 +159,9 @@ func (t *Trace) Nest(msg string, fields ...Field) *Trace {
newTrace := New(msg, fields...)
if t != nil {
newTrace.parentTrace = t
t.lock.Lock()
t.traceItems = append(t.traceItems, newTrace)
t.lock.Unlock()
}
return newTrace
}
@@ -163,7 +171,9 @@ func (t *Trace) Nest(msg string, fields ...Field) *Trace {
// is logged.
func (t *Trace) Log() {
endTime := time.Now()
t.lock.Lock()
t.endTime = &endTime
t.lock.Unlock()
// an explicit logging request should dump all the steps out at the higher level
if t.parentTrace == nil { // We don't start logging until Log or LogIfLong is called on the root trace
t.logTrace()
@@ -178,13 +188,17 @@ func (t *Trace) Log() {
// If the Trace is nested it is not immediately logged. Instead, it is logged when the trace it
// is nested within is logged.
func (t *Trace) LogIfLong(threshold time.Duration) {
t.lock.Lock()
t.threshold = &threshold
t.lock.Unlock()
t.Log()
}
// logTopLevelTraces finds all traces in a hierarchy of nested traces that should be logged but do not have any
// parents that will be logged, due to threshold limits, and logs them as top level traces.
func (t *Trace) logTrace() {
t.lock.RLock()
defer t.lock.RUnlock()
if t.durationIsWithinThreshold() {
var buffer bytes.Buffer
traceNum := rand.Int31()
@@ -244,9 +258,13 @@ func (t *Trace) calculateStepThreshold() *time.Duration {
traceThreshold := *t.threshold
for _, s := range t.traceItems {
nestedTrace, ok := s.(*Trace)
if ok && nestedTrace.threshold != nil {
traceThreshold = traceThreshold - *nestedTrace.threshold
lenTrace--
if ok {
nestedTrace.lock.RLock()
if nestedTrace.threshold != nil {
traceThreshold = traceThreshold - *nestedTrace.threshold
lenTrace--
}
nestedTrace.lock.RUnlock()
}
}