1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
11
"github.com/juju/errors"
14
"github.com/juju/juju/worker"
15
"github.com/juju/juju/worker/introspection/pprof"
18
// Config describes the arguments required to create the introspection worker.
23
// Validate checks the config values to assert they are valid to create the worker.
24
func (c *Config) Validate() error {
25
if c.SocketName == "" {
26
return errors.NotValidf("empty SocketName")
31
// socketListener is a worker and constructed with NewWorker.
32
type socketListener struct {
34
listener *net.UnixListener
37
// NewWorker starts an http server listening on an abstract domain socket
38
// which will be created with the specified name.
39
func NewWorker(config Config) (worker.Worker, error) {
40
if err := config.Validate(); err != nil {
41
return nil, errors.Trace(err)
43
if runtime.GOOS != "linux" {
44
return nil, errors.NotSupportedf("os %q", runtime.GOOS)
47
path := "@" + config.SocketName
48
addr, err := net.ResolveUnixAddr("unix", path)
50
return nil, errors.Annotate(err, "unable to resolve unix socket")
53
l, err := net.ListenUnix("unix", addr)
55
return nil, errors.Annotate(err, "unable to listen on unix socket")
57
logger.Debugf("introspection worker listening on %q", path)
67
func (w *socketListener) serve() {
68
mux := http.NewServeMux()
69
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
70
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
71
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
72
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
78
logger.Debugf("stats worker now servering")
80
logger.Debugf("stats worker servering finished")
83
func (w *socketListener) run() {
85
logger.Debugf("stats worker closing listener")
90
// Kill implements worker.Worker.
91
func (w *socketListener) Kill() {
95
// Wait implements worker.Worker.
96
func (w *socketListener) Wait() error {