From ee2f57a91fcefa516c707e9048c9afbc91acc1ad Mon Sep 17 00:00:00 2001 From: Vitaliy Guschin Date: Thu, 23 May 2024 12:08:04 +0400 Subject: [PATCH] Add an image with profiling enabled. Signed-off-by: Vitaliy Guschin --- .github/workflows/release.yml | 9 +++++++ Dockerfile | 4 +-- main.go | 5 ++++ profiler_off.go | 23 +++++++++++++++++ profiler_on.go | 48 +++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 profiler_off.go create mode 100644 profiler_on.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8fcf39e9..db390305 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/Dockerfile b/Dockerfile index c6edeb04..9b69dcb2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,12 +3,12 @@ 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 RUN go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.25 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 - FROM go as build WORKDIR /build COPY go.mod go.sum ./ @@ -16,7 +16,7 @@ COPY ./local ./local COPY ./internal/imports ./internal/imports RUN go build ./internal/imports COPY . . -RUN go build -o /bin/nsmgr . +RUN go build -tags "$BUILD_TAGS" -o /bin/nsmgr . FROM build as test CMD go test -test.v ./... diff --git a/main.go b/main.go index 7e945672..4196a1d7 100644 --- a/main.go +++ b/main.go @@ -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"); @@ -50,6 +52,9 @@ func main() { // Setup logging ctx = log.WithLog(ctx, logruslogger.New(ctx, map[string]interface{}{"cmd": os.Args[0]})) + // Start profiling server + startProfiler(ctx) + // ******************************************************************************** // Debug self if necessary // ******************************************************************************** diff --git a/profiler_off.go b/profiler_off.go new file mode 100644 index 00000000..2f22abfa --- /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) {} diff --git a/profiler_on.go b/profiler_on.go new file mode 100644 index 00000000..883a3d5c --- /dev/null +++ b/profiler_on.go @@ -0,0 +1,48 @@ +// 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) { + go func() { + profilerHTTPPort := 6060 + 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()) + } + }() +}