forked from grafana/grafana-plugin-sdk-go
/
setup.go
56 lines (49 loc) · 1.58 KB
/
setup.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
package backend
import (
"fmt"
"net/http"
"net/http/pprof"
"os"
)
const (
// PluginProfilerEnv is a constant for the GF_PLUGINS_PROFILER environment variable used to enable pprof.
PluginProfilerEnv = "GF_PLUGINS_PROFILER"
// PluginProfilerPortEnv is a constant for the GF_PLUGINS_PROFILER_PORT environment variable use to specify a pprof port (default 6060).
PluginProfilerPortEnv = "GF_PLUGINS_PROFILER_PORT"
)
// SetupPluginEnvironment will read the environment variables and apply the
// standard environment behavior.
//
// As the SDK evolves, this will likely change.
//
// Currently this function enables and configures profiling with pprof.
func SetupPluginEnvironment(pluginID string) {
// Enable profiler
profilerEnabled := false
if value, ok := os.LookupEnv(PluginProfilerEnv); ok {
// compare value to plugin name
if value == pluginID {
profilerEnabled = true
}
}
Logger.Info("Profiler", "enabled", profilerEnabled)
if profilerEnabled {
profilerPort := "6060"
if value, ok := os.LookupEnv(PluginProfilerPortEnv); ok {
profilerPort = value
}
Logger.Info("Profiler", "port", profilerPort)
portConfig := fmt.Sprintf(":%s", profilerPort)
r := http.NewServeMux()
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
go func() {
if err := http.ListenAndServe(portConfig, r); err != nil {
Logger.Error("Error Running profiler: %s", err.Error())
}
}()
}
}