Skip to content

Commit e7b465c

Browse files
author
Vitaliy Guschin
committed
Add an image with profiling enabled.
Signed-off-by: Vitaliy Guschin <[email protected]>
1 parent 41f4bc6 commit e7b465c

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

.github/workflows/release.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ jobs:
4343
secrets:
4444
token: ${{ secrets.GITHUB_TOKEN }}
4545

46+
docker-profiler:
47+
needs: [get-tag, check-gomod-deps]
48+
uses: networkservicemesh/.github/.github/workflows/docker-release.yaml@main
49+
with:
50+
tag: ${{ needs.get-tag.outputs.tag }}-pprof
51+
build-args: BUILD_TAGS=profiler
52+
secrets:
53+
token: ${{ secrets.GITHUB_TOKEN }}
54+
4655
update-deployments-k8s:
4756
name: Update deployments-k8s
4857
needs: [get-tag, create-release]

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ENV GO111MODULE=on
66
ENV CGO_ENABLED=0
77
ENV GOBIN=/bin
88
ARG BUILDARCH=amd64
9+
ARG BUILD_TAGS=""
910
RUN rm -r /etc/vpp
1011
RUN go install github.com/go-delve/delve/cmd/[email protected]
1112
RUN go install github.com/grpc-ecosystem/[email protected]
@@ -23,7 +24,7 @@ COPY ./internal/afxdp/afxdp.c ./internal/afxdp/
2324
RUN clang -O3 -g -Wextra -Wall -target bpf -I/usr/include/$(uname -m)-linux-gnu -I/usr/include -c -o /bin/afxdp.o ./internal/afxdp/afxdp.c
2425
RUN go build ./internal/imports
2526
COPY . .
26-
RUN go build -o /bin/forwarder .
27+
RUN go build -tags "$BUILD_TAGS" -o /bin/forwarder .
2728

2829
FROM build as test
2930
CMD go test -test.v ./...

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func main() {
9292
logrus.SetFormatter(&nested.Formatter{})
9393
ctx = log.WithLog(ctx, logruslogger.New(ctx, map[string]interface{}{"cmd": os.Args[0]}))
9494

95+
// Start profiling server
96+
startProfiler(ctx)
97+
9598
// ********************************************************************************
9699
// Debug self if necessary
97100
// ********************************************************************************

profiler_off.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
//go:build !profiler
18+
19+
package main
20+
21+
import "context"
22+
23+
func startProfiler(_ context.Context) {}

profiler_on.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2024 Pragmagic Inc. and/or its affiliates.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at:
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
//go:build profiler
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"net/http"
25+
_ "net/http/pprof" // #nosec
26+
"time"
27+
28+
"github.com/networkservicemesh/sdk/pkg/tools/log"
29+
)
30+
31+
func startProfiler(ctx context.Context) {
32+
go func() {
33+
profilerHTTPPort := 6060
34+
log.FromContext(ctx).Infof("Profiler is enabled. Starting HTTP server on %d", profilerHTTPPort)
35+
36+
address := fmt.Sprintf("localhost:%d", profilerHTTPPort)
37+
38+
server := &http.Server{
39+
Addr: address,
40+
ReadTimeout: 10 * time.Second,
41+
WriteTimeout: 10 * time.Second,
42+
}
43+
44+
if err := server.ListenAndServe(); err != nil {
45+
log.FromContext(ctx).Errorf("Failed to start profiler: %s", err.Error())
46+
}
47+
}()
48+
}

0 commit comments

Comments
 (0)