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 29, 2024
1 parent e081e3d commit ca1067a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 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_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 @@ -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"`
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"`
}
2 changes: 2 additions & 0 deletions internal/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import (
_ "google.golang.org/grpc/health/grpc_health_v1"
_ "google.golang.org/grpc/peer"
_ "net"
_ "net/http"
_ "net/http/pprof"
_ "net/url"
_ "os"
_ "os/signal"
Expand Down
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +22,7 @@ package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -33,6 +36,9 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
"github.com/networkservicemesh/sdk/pkg/tools/opentelemetry"

"net/http"
_ "net/http/pprof" // #nosec
)

func main() {
Expand Down Expand Up @@ -75,6 +81,19 @@ func main() {
logrus.SetLevel(level)
log.EnableTracing(true)

// 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)
// nolint:gosec
if err = http.ListenAndServe(address, nil); err != nil {
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
}
}()
}

// Configure Open Telemetry
if opentelemetry.IsEnabled() {
collectorAddress := cfg.OpenTelemetryEndpoint
Expand Down

0 comments on commit ca1067a

Please sign in to comment.