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 <vladislav.byrgazov@xored.com>

* Address review comments

Signed-off-by: Vladislav Byrgazov <vladislav.byrgazov@xored.com>

---------

Signed-off-by: Vladislav Byrgazov <vladislav.byrgazov@xored.com>
Co-authored-by: Vladislav Byrgazov <vladislav.byrgazov@xored.com>
  • Loading branch information
Ex4amp1e and Vladislav Byrgazov committed Aug 8, 2024
1 parent 3016313 commit ce2c8f5
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 ce2c8f5

Please sign in to comment.