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)
| 69 | // Giving a sampling rate of one or more disables sampling, function listeners |
| 70 | // are invoked for all function calls. |
| 71 | func 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 | |
| 94 | type emptyFunctionListenerFactory struct{} |
| 95 |