NewHandler returns a http handler allowing the profiler to be exposed on a pprof-compatible http endpoint. The sample rate is a value between 0 and 1 used to scale the profile results based on the sampling rate applied to the profiler so the resulting values remain representative. The symbolizer p
(sampleRate float64)
| 151 | // The symbolizer passed as argument is used to resolve names of program |
| 152 | // locations recorded in the profile. |
| 153 | func (p *CPUProfiler) NewHandler(sampleRate float64) http.Handler { |
| 154 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 155 | duration := 30 * time.Second |
| 156 | |
| 157 | if seconds := r.FormValue("seconds"); seconds != "" { |
| 158 | n, err := strconv.ParseInt(seconds, 10, 64) |
| 159 | if err == nil && n > 0 { |
| 160 | duration = time.Duration(n) * time.Second |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | ctx := r.Context() |
| 165 | deadline, ok := ctx.Deadline() |
| 166 | if ok { |
| 167 | if timeout := time.Until(deadline); duration > timeout { |
| 168 | serveError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout") |
| 169 | return |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if !p.StartProfile() { |
| 174 | serveError(w, http.StatusInternalServerError, "Could not enable CPU profiling: profiler already running") |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | timer := time.NewTimer(duration) |
| 179 | select { |
| 180 | case <-timer.C: |
| 181 | case <-ctx.Done(): |
| 182 | } |
| 183 | timer.Stop() |
| 184 | serveProfile(w, p.StopProfile(sampleRate)) |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | // NewFunctionListener returns a function listener suited to record CPU timings |
| 189 | // of calls to the function passed as argument. |
nothing calls this directly
no test coverage detected