go.temporal.io/server@v1.23.0/common/pprof/pprof.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package pprof 26 27 import ( 28 "fmt" 29 "net" 30 "net/http" 31 _ "net/http/pprof" // DO NOT REMOVE THE LINE 32 "sync/atomic" 33 34 "go.temporal.io/server/common/config" 35 "go.temporal.io/server/common/log" 36 "go.temporal.io/server/common/log/tag" 37 ) 38 39 const ( 40 pprofNotInitialized int32 = 0 41 pprofInitialized int32 = 1 42 ) 43 44 type ( 45 // PProfInitializerImpl initialize the pprof based on config 46 PProfInitializerImpl struct { 47 PProf *config.PProf 48 Logger log.Logger 49 } 50 ) 51 52 // the pprof should only be initialized once per process 53 // otherwise, the caller / worker will experience weird issue 54 var pprofStatus = pprofNotInitialized 55 56 // NewInitializer create a new instance of PProf Initializer 57 func NewInitializer(cfg *config.PProf, logger log.Logger) *PProfInitializerImpl { 58 return &PProfInitializerImpl{ 59 PProf: cfg, 60 Logger: logger, 61 } 62 } 63 64 // Start the pprof based on config 65 func (initializer *PProfInitializerImpl) Start() error { 66 port := initializer.PProf.Port 67 if port == 0 { 68 initializer.Logger.Info("PProf not started due to port not set") 69 return nil 70 } 71 host := initializer.PProf.Host 72 if host == "" { 73 // default to localhost which will favor ipv4 on dual stack 74 // environments - configure host as `::1` to bind on ipv6 localhost 75 host = "localhost" 76 } 77 78 hostPort := net.JoinHostPort(host, fmt.Sprint(port)) 79 80 if atomic.CompareAndSwapInt32(&pprofStatus, pprofNotInitialized, pprofInitialized) { 81 go func() { 82 initializer.Logger.Info("PProf listen on ", tag.Host(host), tag.Port(port)) 83 err := http.ListenAndServe(hostPort, nil) 84 if err != nil { 85 initializer.Logger.Error("listen and serve err", tag.Error(err)) 86 } 87 }() 88 } 89 return nil 90 }