Skip to content

Commit 3ceb40f

Browse files
author
Oluwole Fadeyi
authored
Merge pull request #286 from jetstack/add-pprof
Add pprof profiling endpoints to the agent
2 parents 5a2f9c9 + c0002b5 commit 3ceb40f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

cmd/agent.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ func init() {
117117
os.Getenv("API_TOKEN"),
118118
"Token used for authentication when API tokens are in use on the backend",
119119
)
120+
agentCmd.PersistentFlags().BoolVarP(
121+
&agent.Profiling,
122+
"enable-pprof",
123+
"",
124+
false,
125+
"Enables the pprof profiling server on the agent (port: 6060).",
126+
)
120127
}

pkg/agent/run.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io/ioutil"
910
"log"
11+
"net/http"
12+
_ "net/http/pprof"
1013
"net/url"
1114
"os"
1215
"strings"
@@ -50,6 +53,9 @@ var StrictMode bool
5053
// APIToken is an authentication token used for the backend API as an alternative to oauth flows.
5154
var APIToken string
5255

56+
// Profiling flag enabled pprof endpoints to run on the agent
57+
var Profiling bool
58+
5359
// schema version of the data sent by the agent.
5460
// The new default version is v2.
5561
// In v2 the agent posts data readings using api.gathereredResources
@@ -64,6 +70,16 @@ func Run(cmd *cobra.Command, args []string) {
6470
defer cancel()
6571
config, preflightClient := getConfiguration()
6672

73+
if Profiling {
74+
log.Printf("pprof profiling was enabled.\nRunning profiling on port :6060")
75+
go func() {
76+
err := http.ListenAndServe(":6060", nil)
77+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
78+
log.Fatalf("failed to run pprof profiler: %s", err)
79+
}
80+
}()
81+
}
82+
6783
dataGatherers := map[string]datagatherer.DataGatherer{}
6884
var wg sync.WaitGroup
6985

0 commit comments

Comments
 (0)