diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8fcf39e..db39030 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -43,6 +43,15 @@ jobs: secrets: token: ${{ secrets.GITHUB_TOKEN }} + docker-profiler: + needs: [get-tag, check-gomod-deps] + uses: networkservicemesh/.github/.github/workflows/docker-release.yaml@main + with: + tag: ${{ needs.get-tag.outputs.tag }}-pprof + build-args: BUILD_TAGS=profiler + secrets: + token: ${{ secrets.GITHUB_TOKEN }} + update-deployments-k8s: name: Update deployments-k8s needs: [get-tag, create-release] diff --git a/.golangci.yml b/.golangci.yml index e74df5d..43356ef 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -36,7 +36,7 @@ linters-settings: dupl: threshold: 150 funlen: - lines: 120 + lines: 130 statements: 60 goconst: min-len: 2 diff --git a/Dockerfile b/Dockerfile index fe7a802..cc58f2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ ENV GO111MODULE=on ENV CGO_ENABLED=0 ENV GOBIN=/bin ARG BUILDARCH=amd64 +ARG BUILD_TAGS="" RUN go install github.com/go-delve/delve/cmd/dlv@v1.8.2 ADD https://github.com/spiffe/spire/releases/download/v1.8.0/spire-1.8.0-linux-${BUILDARCH}-musl.tar.gz . RUN tar xzvf spire-1.8.0-linux-${BUILDARCH}-musl.tar.gz -C /bin --strip=2 spire-1.8.0/bin/spire-server spire-1.8.0/bin/spire-agent @@ -13,7 +14,7 @@ COPY go.mod go.sum ./ COPY ./pkg/internal/imports ./pkg/internal/imports RUN go build ./pkg/internal/imports COPY . . -RUN go build -o /bin/cmd-registry-k8s . +RUN go build -tags "$BUILD_TAGS" -o /bin/cmd-registry-k8s . FROM build as test CMD go test -test.v ./... diff --git a/README.md b/README.md index 0a7ecc3..e974cd2 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ docker build . * `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_KUBELET_QPS` - kubelet config settings (default: "205") +* `NSM_PROFILER_HTTP_PORT` - Profiling server HTTP port providing data in the format expected by the pprof visualization tool. Profiler is running only on images with the "-pprof" tag suffix (default: "6060") # Testing diff --git a/main.go b/main.go index 2e8bd9a..afd8798 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,8 @@ type Config struct { // FWD Refreshes: 1 refresh per sec. * 5 fwds // NSC Refreshes: 4 finds (in 1 refresh) per sec. * 40 nscs // Total: = 205 - KubeletQPS int `default:"205" desc:"kubelet config settings" split_words:"true"` + KubeletQPS int `default:"205" desc:"kubelet config settings" split_words:"true"` + ProfilerHTTPPort uint16 `default:"6060" desc:"Profiling server HTTP port providing data in the format expected by the pprof visualization tool. Profiler is running only on images with the '-pprof' tag suffix" split_words:"true"` } func main() { @@ -113,6 +114,9 @@ func main() { logrus.SetLevel(l) log.FromContext(ctx).Infof("Config: %#v", config) + // Start profiling server + startProfiler(ctx, config.ProfilerHTTPPort) + // Configure Open Telemetry if opentelemetry.IsEnabled() { collectorAddress := config.OpenTelemetryEndpoint diff --git a/profiler_off.go b/profiler_off.go new file mode 100644 index 0000000..fce981a --- /dev/null +++ b/profiler_off.go @@ -0,0 +1,23 @@ +// 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"); +// 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. + +//go:build !profiler + +package main + +import "context" + +func startProfiler(_ context.Context, _ uint16) {} diff --git a/profiler_on.go b/profiler_on.go new file mode 100644 index 0000000..ba0e05b --- /dev/null +++ b/profiler_on.go @@ -0,0 +1,47 @@ +// 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"); +// 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. + +//go:build profiler + +package main + +import ( + "context" + "fmt" + "net/http" + _ "net/http/pprof" // #nosec + "time" + + "github.com/networkservicemesh/sdk/pkg/tools/log" +) + +func startProfiler(ctx context.Context, profilerHTTPPort uint16) { + go func() { + log.FromContext(ctx).Infof("Profiler is enabled. Starting HTTP server on %d", profilerHTTPPort) + + address := fmt.Sprintf("localhost:%d", profilerHTTPPort) + + server := &http.Server{ + Addr: address, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + + if err := server.ListenAndServe(); err != nil { + log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error()) + } + }() +}