Skip to content

Commit

Permalink
Add pprof for galley (#10047)
Browse files Browse the repository at this point in the history
* Enable pprof to galley

Signed-off-by: Chun Lin Yang <clyang@cn.ibm.com>

* start pprofile in 9094

Signed-off-by: Chun Lin Yang <clyang@cn.ibm.com>
  • Loading branch information
clyang82 authored and istio-testing committed Jan 6, 2019
1 parent 9e011f4 commit ee30c79
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions galley/cmd/galley/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
livenessProbeController probe.Controller
readinessProbeController probe.Controller
monitoringPort uint
enableProfiling bool
pprofPort uint
)

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -95,6 +97,11 @@ func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
}
galleyStop := make(chan struct{})
go server.StartSelfMonitoring(galleyStop, monitoringPort)

if enableProfiling {
go server.StartProfiling(galleyStop, pprofPort)
}

go server.StartProbeCheck(livenessProbeController, readinessProbeController, galleyStop)
istiocmd.WaitSignal(galleyStop)

Expand Down Expand Up @@ -123,6 +130,9 @@ func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
"Interval of updating file for the Galley readiness probe.")
rootCmd.PersistentFlags().UintVar(&monitoringPort, "monitoringPort", 9093,
"Port to use for exposing self-monitoring information")
rootCmd.PersistentFlags().UintVar(&pprofPort, "pprofPort", 9094, "Port to use for exposing profiling")
rootCmd.PersistentFlags().BoolVar(&enableProfiling, "enableProfiling", false,
"Enable profiling for Galley")

//server config
rootCmd.PersistentFlags().StringVarP(&serverArgs.APIAddress, "server-address", "", serverArgs.APIAddress,
Expand Down
58 changes: 58 additions & 0 deletions galley/pkg/server/profiling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"fmt"
"net"
"net/http"
"net/http/pprof"
)

//StartProfiling start the profiling for Galley
func StartProfiling(stop <-chan struct{}, port uint) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
if err != nil {
scope.Errorf("Unable to listen on profiling port %v: %v", port, err)
return
}

mux := http.NewServeMux()

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//Redirect ROOT to /debug/pprof/ for convenience
http.Redirect(w, r, "/debug/pprof/", http.StatusSeeOther)
})
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

server := &http.Server{
Handler: mux,
}

go func() {
if err := server.Serve(lis); err != nil {
scope.Errorf("Profiling http server failed: %v", err)
return
}
}()

<-stop
err = server.Close()
scope.Debugf("Profiling http server terminated: %v", err)
}

0 comments on commit ee30c79

Please sign in to comment.