Skip to content

Commit

Permalink
Add an image with profiling enabled.
Browse files Browse the repository at this point in the history
Signed-off-by: Vitaliy Guschin <[email protected]>
  • Loading branch information
Vitaliy Guschin committed Jun 26, 2024
1 parent e081e3d commit f90edad
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
uses: networkservicemesh/.github/.github/workflows/docker-release.yaml@main
with:
tag: ${{ needs.get-tag.outputs.tag }}
build_with_profiler: true
secrets:
token: ${{ secrets.GITHUB_TOKEN }}

Expand Down
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ ENV GO111MODULE=on
ENV CGO_ENABLED=0
ENV GOBIN=/bin
ARG BUILDARCH=amd64
ARG ENABLE_PROFILER=false
RUN go install github.com/go-delve/delve/cmd/[email protected]
RUN go install github.com/grpc-ecosystem/[email protected]
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 ./
COPY ./local ./local
COPY ./internal/imports ./internal/imports
RUN go build ./internal/imports
COPY . .
RUN go build -o /bin/nsmgr .
RUN if [ "$ENABLE_PROFILER" = "true" ]; then \
go build -tags profiler -o /bin/nsmgr . ; \
else \
go build -o /bin/nsmgr . ; \
fi

FROM build as test
CMD go test -test.v ./...
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ docker build .
* `NSM_FORWARDER_NETWORK_SERVICE_NAME` - the default service name for forwarder discovering (default: "forwarder")
* `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_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

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ type Config struct {
ForwarderNetworkServiceName string `default:"forwarder" desc:"the default service name for forwarder discovering" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint" split_words:"true"`
MetricsExportInterval time.Duration `default:"10s" desc:"interval between mertics exports" 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"`
}
2 changes: 2 additions & 0 deletions internal/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import (
_ "google.golang.org/grpc/health/grpc_health_v1"
_ "google.golang.org/grpc/peer"
_ "net"
_ "net/http"
_ "net/http/pprof"
_ "net/url"
_ "os"
_ "os/signal"
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -75,6 +77,9 @@ func main() {
logrus.SetLevel(level)
log.EnableTracing(true)

// Start profiling server
startProfiler(ctx, cfg.ProfilerHTTPPort)

// Configure Open Telemetry
if opentelemetry.IsEnabled() {
collectorAddress := cfg.OpenTelemetryEndpoint
Expand Down
7 changes: 7 additions & 0 deletions profiler_off.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !profiler

Check failure on line 1 in profiler_off.go

View workflow job for this annotation

GitHub Actions / golangci-lint / golangci-lint

Missed header for check (goheader)

package main

import "context"

func startProfiler(ctx context.Context, profilerHTTPPort uint16) {}
31 changes: 31 additions & 0 deletions profiler_on.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//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())
}
}()
}

0 comments on commit f90edad

Please sign in to comment.