From 363e9a43f3832276f33dab0428026fa307ff36f3 Mon Sep 17 00:00:00 2001 From: Vladislav Byrgazov Date: Wed, 14 Aug 2024 12:12:29 +0500 Subject: [PATCH] Add configuration for turning profiling on/off Signed-off-by: Vladislav Byrgazov --- .golangci.yml | 4 ++-- README.md | 7 +++++++ go.mod | 1 + go.sum | 2 ++ internal/config/config.go | 24 ++++++++++++++++++++++++ main.go | 19 +++++++++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 internal/config/config.go diff --git a/.golangci.yml b/.golangci.yml index ced258c..e7f104e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 diff --git a/README.md b/README.md index 57f0fc3..d288f15 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/go.mod b/go.mod index aebe210..9e220f5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a1e39ad..0f20b3e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..5093e22 --- /dev/null +++ b/internal/config/config.go @@ -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"` +} diff --git a/main.go b/main.go index e13a7e2..a42abc9 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ 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" @@ -38,8 +39,10 @@ import ( "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" @@ -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{})