Skip to content

Commit

Permalink
Add configuration for turning profiling on/off.
Browse files Browse the repository at this point in the history
Signed-off-by: Vitaliy Guschin <[email protected]>
  • Loading branch information
Vitaliy Guschin committed May 23, 2024
1 parent 41f4bc6 commit 21e6c42
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ linters-settings:
simple: true
range-loops: true
for-loops: false
gosec:
excludes:
- G108
- G114
gocritic:
enabled-checks:
- appendAssign
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ docker build .
* `NSM_CGROUP_PATH` - path to the host cgroup directory
* `NSM_VFIO_PATH` - path to the host VFIO directory
* `NSM_MECHANISM_PRIORITY` - sets priorities for mechanisms
* `NSM_ENABLE_PROFILER` - Enable pprof package HTTP server for profiling runtime data.
* `NSM_PROFILER_HTTP_PORT` - Profiling server HTTP port providing data in the format expected by the pprof visualization tool.

# Testing

Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Config struct {
CgroupPath string `default:"/host/sys/fs/cgroup/devices" desc:"path to the host cgroup directory" split_words:"true"`
VFIOPath string `default:"/host/dev/vfio" desc:"path to the host VFIO directory" split_words:"true"`
MechanismPriority []string `default:"" desc:"sets priorities for mechanisms" split_words:"true"`
EnableProfiler bool `default:"false" desc:"Enable pprof package HTTP server for profiling runtime data. The handled paths all begin with /debug/pprof/." split_words:"true"`
ProfilerHTTPPort uint16 `default:"6060" desc:"Profiling server HTTP port providing data in the format expected by the pprof visualization tool." split_words:"true"`
}

// Process reads config from env
Expand Down
2 changes: 2 additions & 0 deletions internal/imports/imports_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package main
import (
"context"
"crypto/tls"
"fmt"
"os"
"os/signal"
"path"
Expand Down Expand Up @@ -70,6 +71,9 @@ import (
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/devicecfg"
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/vppinit"
"github.com/networkservicemesh/cmd-forwarder-vpp/internal/xconnectns"

"net/http"
_ "net/http/pprof"
)

func main() {
Expand Down Expand Up @@ -134,6 +138,18 @@ func main() {
log.EnableTracing(true)
log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 1: get config from environment")

// Enable profiling server
if cfg.EnableProfiler {
go func() {
log.FromContext(ctx).Infof("Profiler is enabled. Starting HTTP server on %d", cfg.ProfilerHTTPPort)

address := fmt.Sprintf("localhost:%d", cfg.ProfilerHTTPPort)
if err = http.ListenAndServe(address, nil); err != nil {
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
}
}()
}

// ********************************************************************************
// Configure Open Telemetry
// ********************************************************************************
Expand Down

0 comments on commit 21e6c42

Please sign in to comment.