diff --git a/pkg/tools/pprof/pprof.go b/pkg/tools/pprof/pprof.go new file mode 100644 index 000000000..9adc18ecc --- /dev/null +++ b/pkg/tools/pprof/pprof.go @@ -0,0 +1,58 @@ +// Copyright (c) 2024 Cisco and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// 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 pprof provides ability to enable pprof if required +package pprof + +import ( + "context" + "fmt" + "net/http" + "net/http/pprof" + "time" + + "github.com/networkservicemesh/sdk/pkg/tools/log" +) + +// Init - configures pprof http handlers +func Init(ctx context.Context, port uint16) { + log.FromContext(ctx).Infof("Profiler is enabled. Listening on %d", port) + mux := http.NewServeMux() + 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) + mux.Handle("/debug/pprof/allocs", pprof.Handler("allocs")) + mux.Handle("/debug/pprof/block", pprof.Handler("block")) + mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) + mux.Handle("/debug/pprof/heap", pprof.Handler("heap")) + mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex")) + mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) + server := &http.Server{ + Addr: fmt.Sprintf("localhost:%d", port), + Handler: mux, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + if err := server.ListenAndServe(); err != nil { + log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error()) + } + go func() { + <-ctx.Done() + _ = server.Close() + }() +}