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 <vitaliy.guschin@spirent.com>
  • Loading branch information
Vitaliy Guschin committed May 23, 2024
1 parent e081e3d commit 5e6d393
Show file tree
Hide file tree
Showing 5 changed files with 28 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 @@ -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
18 changes: 18 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"
)

func main() {
Expand Down Expand Up @@ -75,6 +81,18 @@ 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)
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 5e6d393

Please sign in to comment.