Skip to content

Commit

Permalink
Merge pull request #46 from Ex4amp1e/add-pprof-enable-conf
Browse files Browse the repository at this point in the history
Add configuration for turning profiling on/off
  • Loading branch information
denis-tingaikin authored Aug 14, 2024
2 parents 2287796 + 363e9a4 commit aae0d72
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ linters-settings:
goimports:
local-prefixes: github.com/networkservicemesh
gocyclo:
min-complexity: 15
min-complexity: 20
maligned:
suggest-new: true
dupl:
threshold: 150
funlen:
Lines: 100
Lines: 120
Statements: 50
goconst:
min-len: 2
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Written in [Go](https://go.dev/)

The entire NSM dashboard deployment info see [here](https://github.com/networkservicemesh/deployments-k8s/tree/main/examples/observability/dashboard)

## Usage

### Environment config

* `NSM_PPROF_ENABLED` - is pprof enabled (default: "false")
* `NSM_PPROF_LISTEN_ON` - pprof URL to ListenAndServe (default: "localhost:6060")

## Dev/debug

### To run dashboard backend in the cluster:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/edwarnicke/genericsync v0.0.0-20220910010113-61a344f9bc29
github.com/gin-gonic/gin v1.9.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/networkservicemesh/api v1.13.1-0.20240424210452-d0df98851760
github.com/networkservicemesh/sdk v0.5.1-0.20240812103952-7e0cf2c383fb
github.com/sirupsen/logrus v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rH
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
Expand Down
24 changes: 24 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2023-2024 Pragmagic Inc. 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 config - contain environment variables used by cmd-dashboard-backend
package config

// Config - configuration for cmd-dashboard-backend
type Config struct {
PprofEnabled bool `default:"false" desc:"is pprof enabled" split_words:"true"`
PprofListenOn string `default:"localhost:6060" desc:"pprof URL to ListenAndServe" split_words:"true"`
}
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ import (

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/gin-gonic/gin"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/networkservicemesh/cmd-dashboard-backend/internal/config"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
"github.com/networkservicemesh/sdk/pkg/tools/pprofutils"
"github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
"github.com/networkservicemesh/sdk/pkg/tools/token"
"github.com/networkservicemesh/sdk/pkg/tools/tracing"
Expand All @@ -55,6 +58,22 @@ func main() {
)
defer cancel()

// Get cfg from environment
cfg := &config.Config{}
if err := envconfig.Usage("nsm", cfg); err != nil {
log.FromContext(ctx).Fatal(err)
}
if err := envconfig.Process("nsm", cfg); err != nil {
log.FromContext(ctx).Fatalf("error processing cfg from env: %+v", err)
}

log.FromContext(ctx).Infof("Using configuration: %v", cfg)

// Configure pprof
if cfg.PprofEnabled {
go pprofutils.ListenAndServe(ctx, cfg.PprofListenOn)
}

// Setup logger
log.EnableTracing(true)
logrus.SetFormatter(&nested.Formatter{})
Expand Down

0 comments on commit aae0d72

Please sign in to comment.