MCPcopy Create free account
hub / github.com/dispatchrun/wzprof / Sample

Function Sample

sample.go:71–92  ·  view source on GitHub ↗

Sample returns a function listener factory which creates listeners where calls to their Before/After methods is sampled at the given sample rate. Giving a zero or negative sampling rate disables the function listeners entirely. Giving a sampling rate of one or more disables sampling, function list

(sampleRate float64, factory experimental.FunctionListenerFactory)

Source from the content-addressed store, hash-verified

69// Giving a sampling rate of one or more disables sampling, function listeners
70// are invoked for all function calls.
71func Sample(sampleRate float64, factory experimental.FunctionListenerFactory) experimental.FunctionListenerFactory {
72 if sampleRate <= 0 {
73 return emptyFunctionListenerFactory{}
74 }
75 if sampleRate >= 1 {
76 return factory
77 }
78 cycle := uint32(math.Ceil(1 / sampleRate))
79 return experimental.FunctionListenerFactoryFunc(func(def api.FunctionDefinition) experimental.FunctionListener {
80 lstn := factory.NewFunctionListener(def)
81 if lstn == nil {
82 return nil
83 }
84 sampled := &sampledFunctionListener{
85 cycle: cycle,
86 count: cycle,
87 lstn: lstn,
88 }
89 sampled.stack.bits = sampled.bits[:]
90 return sampled
91 })
92}
93
94type emptyFunctionListenerFactory struct{}
95

Callers 3

runMethod · 0.92

Calls 1

NewFunctionListenerMethod · 0.45

Tested by 2