Skip to content

Commit

Permalink
start pprofile in 9094
Browse files Browse the repository at this point in the history
Signed-off-by: Chun Lin Yang <clyang@cn.ibm.com>
  • Loading branch information
clyang82 committed Dec 28, 2018
1 parent a35c1c7 commit 0acc8bd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
13 changes: 10 additions & 3 deletions galley/cmd/galley/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
readinessProbeController probe.Controller
monitoringPort uint
enableProfiling bool
pprofPort uint
)

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -95,7 +96,12 @@ func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
go validation.RunValidation(validationArgs, printf, fatalf, flags.kubeConfig, livenessProbeController, readinessProbeController)
}
galleyStop := make(chan struct{})
go server.StartSelfMonitoring(galleyStop, monitoringPort, enableProfiling)
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 @@ -124,8 +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().BoolVar(&enableProfiling, "enableProfiling", true,
"Enable pprof for Galley")
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
11 changes: 1 addition & 10 deletions galley/pkg/server/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"net"
"net/http"
"net/http/pprof"

"github.com/prometheus/client_golang/prometheus"
ocprom "go.opencensus.io/exporter/prometheus"
Expand All @@ -33,7 +32,7 @@ const (
)

//StartSelfMonitoring start the self monitoring for Galley
func StartSelfMonitoring(stop <-chan struct{}, port uint, enableProfiling bool) {
func StartSelfMonitoring(stop <-chan struct{}, port uint) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
if err != nil {
scope.Errorf("Unable to listen on monitoring port %v: %v", port, err)
Expand All @@ -58,14 +57,6 @@ func StartSelfMonitoring(stop <-chan struct{}, port uint, enableProfiling bool)

version.Info.RecordComponentBuildTag("galley")

if enableProfiling {
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,
}
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 0acc8bd

Please sign in to comment.