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: Vladislav Byrgazov <vladislav.byrgazov@xored.com>
  • Loading branch information
Vladislav Byrgazov committed Jul 25, 2024
1 parent 591ed9b commit 3fcf1cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ docker build .
* `NSM_FORWARDER_NETWORK_SERVICE_NAME` - the default service name for forwarder discovering (default: "forwarder")
* `NSM_OPEN_TELEMETRY_ENDPOINT` - OpenTelemetry Collector Endpoint (default: "otel-collector.observability.svc.cluster.local:4317")
* `NSM_METRICS_EXPORT_INTERVAL` - interval between mertics exports (default: "10s")
* `NSM_PPROF_ENABLED` - is pprof enabled (default: "false")
* `NSM_PPROF_PORT` - pprof port (default: "6060")

# 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 @@ -37,4 +37,6 @@ type Config struct {
ForwarderNetworkServiceName string `default:"forwarder" desc:"the default service name for forwarder discovering" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint" split_words:"true"`
MetricsExportInterval time.Duration `default:"10s" desc:"interval between mertics exports" split_words:"true"`
PprofEnabled bool `default:"false" desc:"is pprof enabled" split_words:"true"`
PprofPort string `default:"6060" desc:"pprof port" split_words:"true"`
}
29 changes: 27 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020-2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,9 +20,12 @@ package main

import (
"context"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -88,6 +91,28 @@ func main() {
}()
}

// Configure pprof
if cfg.PprofEnabled {
log.FromContext(ctx).Infof("Profiler is enabled. Listening on %s", cfg.PprofPort)
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)
server := &http.Server{
Addr: "localhost:" + cfg.PprofPort,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
go func() {
if err = server.ListenAndServe(); err != nil {
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
}
}()
}

err = manager.RunNsmgr(ctx, cfg)
if err != nil {
log.FromContext(ctx).Fatalf("error executing rootCmd: %v", err)
Expand Down

0 comments on commit 3fcf1cc

Please sign in to comment.