-
Notifications
You must be signed in to change notification settings - Fork 8
CLOUDP-317911 Add pprof integration in operator #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package pprof | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/http/pprof" | ||
"strconv" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/mongodb/mongodb-kubernetes/pkg/util" | ||
) | ||
|
||
type Runnable struct { | ||
port int | ||
log *zap.SugaredLogger | ||
} | ||
|
||
func NewRunnable(port int, log *zap.SugaredLogger) *Runnable { | ||
return &Runnable{ | ||
port: port, | ||
log: log, | ||
} | ||
} | ||
|
||
func (p *Runnable) Start(ctx context.Context) error { | ||
pprofAddress := fmt.Sprintf("localhost:%d", p.port) | ||
|
||
handler := http.NewServeMux() | ||
handler.HandleFunc("GET /debug/pprof/", pprof.Index) | ||
handler.HandleFunc("GET /debug/pprof/cmdline/", pprof.Cmdline) | ||
handler.HandleFunc("GET /debug/pprof/profile/", pprof.Profile) | ||
handler.HandleFunc("GET /debug/pprof/symbol/", pprof.Symbol) | ||
handler.HandleFunc("GET /debug/pprof/trace/", pprof.Trace) | ||
|
||
server := &http.Server{ | ||
Addr: pprofAddress, | ||
ReadHeaderTimeout: 10 * time.Second, | ||
Handler: handler, | ||
} | ||
|
||
go func() { | ||
p.log.Infof("Starting pprof server at %s", pprofAddress) | ||
if err := server.ListenAndServe(); err != nil { | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
p.log.Errorf("unable to start pprof server: %s", err.Error()) | ||
} | ||
} | ||
p.log.Info("pprof server stopped") | ||
}() | ||
|
||
go func() { | ||
<-ctx.Done() | ||
p.log.Info("Stopping pprof server") | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
p.log.Errorf("unable to shutdown pprof server: %s", err.Error()) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// IsPprofEnabled checks if pprof is enabled based on the MDB_OPERATOR_PPROF_ENABLED | ||
// and OPERATOR_ENV environment variables. It returns true if: | ||
// - MDB_OPERATOR_PPROF_ENABLED is set to true | ||
// - OPERATOR_ENV is set to dev or local and MDB_OPERATOR_PPROF_ENABLED is not set | ||
// Otherwise, it returns false. | ||
func IsPprofEnabled(pprofEnabledString string, operatorEnv util.OperatorEnvironment) (bool, error) { | ||
if pprofEnabledString != "" { | ||
pprofEnabled, err := strconv.ParseBool(pprofEnabledString) | ||
if err != nil { | ||
return false, fmt.Errorf("unable to parse %s environment variable: %w", util.OperatorPprofEnabledEnv, err) | ||
} | ||
|
||
return pprofEnabled, nil | ||
} | ||
|
||
return operatorEnv == util.OperatorEnvironmentDev || operatorEnv == util.OperatorEnvironmentLocal, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package pprof | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/mongodb/mongodb-kubernetes/pkg/util" | ||
) | ||
|
||
func TestIsPprofEnabled(t *testing.T) { | ||
tests := map[string]struct { | ||
pprofEnabledString string | ||
operatorEnv util.OperatorEnvironment | ||
expected bool | ||
expectedErrMsg string | ||
}{ | ||
"pprof enabled by default in dev": { | ||
operatorEnv: util.OperatorEnvironmentDev, | ||
expected: true, | ||
}, | ||
"pprof enabled by default in local": { | ||
operatorEnv: util.OperatorEnvironmentLocal, | ||
expected: true, | ||
}, | ||
"pprof disabled by default in prod": { | ||
operatorEnv: util.OperatorEnvironmentProd, | ||
expected: false, | ||
}, | ||
"pprof enabled in prod": { | ||
pprofEnabledString: "true", | ||
operatorEnv: util.OperatorEnvironmentProd, | ||
expected: true, | ||
}, | ||
"pprof explicitly enabled in dev": { | ||
pprofEnabledString: "true", | ||
operatorEnv: util.OperatorEnvironmentDev, | ||
expected: true, | ||
}, | ||
"pprof explicitly enabled in local": { | ||
pprofEnabledString: "true", | ||
operatorEnv: util.OperatorEnvironmentLocal, | ||
expected: true, | ||
}, | ||
"pprof disabled in dev": { | ||
pprofEnabledString: "false", | ||
operatorEnv: util.OperatorEnvironmentDev, | ||
expected: false, | ||
}, | ||
"pprof disabled in local": { | ||
pprofEnabledString: "false", | ||
operatorEnv: util.OperatorEnvironmentLocal, | ||
expected: false, | ||
}, | ||
"pprof disabled explicitly in prod": { | ||
pprofEnabledString: "false", | ||
operatorEnv: util.OperatorEnvironmentProd, | ||
expected: false, | ||
}, | ||
"pprof misconfigured": { | ||
pprofEnabledString: "false11", | ||
operatorEnv: util.OperatorEnvironmentProd, | ||
expected: false, | ||
expectedErrMsg: "unable to parse MDB_OPERATOR_PPROF_ENABLED environment variable: strconv.ParseBool: parsing \"false11\": invalid syntax", | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
result, err := IsPprofEnabled(test.pprofEnabledString, test.operatorEnv) | ||
if test.expectedErrMsg != "" { | ||
require.Error(t, err) | ||
assert.Equal(t, test.expectedErrMsg, err.Error()) | ||
} | ||
|
||
assert.Equal(t, test.expected, result) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.