Skip to content

Commit

Permalink
Add sdk/pkg/tools/pprof (#1654)
Browse files Browse the repository at this point in the history
* Add sdk/pkg/tools/pprof

Signed-off-by: Vladislav Byrgazov <[email protected]>

* Address review comments

Signed-off-by: Vladislav Byrgazov <[email protected]>

---------

Signed-off-by: Vladislav Byrgazov <[email protected]>
Co-authored-by: Vladislav Byrgazov <[email protected]>
  • Loading branch information
2 people authored and denis-tingaikin committed Sep 17, 2024
1 parent dcfb9f0 commit 74cca57
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pkg/tools/pprof/pprof.go
Original file line number Diff line number Diff line change
@@ -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()
}()
}

0 comments on commit 74cca57

Please sign in to comment.