-
Notifications
You must be signed in to change notification settings - Fork 34
/
profiler.go
75 lines (68 loc) · 2.47 KB
/
profiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"net/http/pprof"
"os"
"runtime"
"strconv"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
// setRuntimeConfig sets runtime config options from env vars
func setRuntimeConfig() (err error) {
var (
// BlockProfileRate is the fraction of goroutine blocking
// events that are reported in the blocking profile. The
// profiler aims to sample an average of one blocking event
// per rate nanoseconds spent blocked.
//
// To include every blocking event in the profile, pass rate = 1. To turn off profiling entirely, pass rate <= 0.
//
// https://golang.org/pkg/runtime/#SetBlockProfileRate
blockProfileRate int
// mutexProfileFraction is the rate of mutex contention events
// that are reported in the mutex profile. On average 1/rate
// events are reported. The previous rate is returned.
//
// To turn off profiling entirely, pass rate 0. To just read
// the current rate, pass rate < 0. (For n>1 the details of
// sampling may change.)
//
// https://golang.org/pkg/runtime/#SetMutexProfileFraction
mutexProfileFraction int
)
val, ok := os.LookupEnv("BLOCK_PROFILE_RATE")
if ok {
blockProfileRate, err = strconv.Atoi(val)
if err != nil {
return fmt.Errorf("failed to parse BLOCK_PROFILE_RATE as int: %w", err)
}
runtime.SetBlockProfileRate(blockProfileRate)
log.Infof("SetBlockProfileRate to %d", blockProfileRate)
} else {
log.Infof("Did not SetBlockProfileRate. BLOCK_PROFILE_RATE is not set.")
}
val, ok = os.LookupEnv("MUTEX_PROFILE_FRACTION")
if ok {
mutexProfileFraction, err = strconv.Atoi(val)
if err != nil {
return fmt.Errorf("failed to parse MUTEX_PROFILE_FRACTION as int: %w", err)
}
runtime.SetMutexProfileFraction(mutexProfileFraction)
log.Infof("SetMutexProfileFraction to %d", mutexProfileFraction)
} else {
log.Infof("Did not SetMutexProfileFraction. MUTEX_PROFILE_FRACTION is not set.")
}
return nil
}
// addProfilerHandlers adds debug pprof handlers
func addProfilerHandlers(router *mux.Router) {
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
}