From 74cca57a04e5afda55fa3545a13c48df1fd4f565 Mon Sep 17 00:00:00 2001 From: Vladislav Byrgazov <58589910+Ex4amp1e@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:34:33 +0500 Subject: [PATCH] Add sdk/pkg/tools/pprof (#1654) * Add sdk/pkg/tools/pprof Signed-off-by: Vladislav Byrgazov * Address review comments Signed-off-by: Vladislav Byrgazov --------- Signed-off-by: Vladislav Byrgazov Co-authored-by: Vladislav Byrgazov --- pkg/tools/pprof/pprof.go | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 pkg/tools/pprof/pprof.go 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() + }() +}