-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose pprof endpoints in debug mode (#2503)
* feat: Expose pprof endpoints in debug mode * Adding documentation * fix linter issue * Change port --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5915e35
commit 2613710
Showing
9 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,79 @@ | ||
package api_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api" | ||
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config" | ||
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric" | ||
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service" | ||
"github.com/thomaspoignant/go-feature-flag/notifier" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestPprofEndpointsStarts(t *testing.T) { | ||
type test struct { | ||
name string | ||
MonitoringPort int | ||
Debug bool | ||
expectedStatusCode int | ||
} | ||
tests := []test{ | ||
{ | ||
name: "pprof available in proxy port", | ||
Debug: true, | ||
expectedStatusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "pprof available in monitoring port", | ||
Debug: true, | ||
MonitoringPort: 1032, | ||
expectedStatusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "pprof not available ii debug not enabled", | ||
Debug: false, | ||
MonitoringPort: 1032, | ||
expectedStatusCode: http.StatusNotFound, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
z, err := zap.NewProduction() | ||
require.NoError(t, err) | ||
c := &config.Config{ | ||
Retriever: &config.RetrieverConf{ | ||
Kind: "file", | ||
Path: "../../../testdata/flag-config.yaml", | ||
}, | ||
MonitoringPort: tt.MonitoringPort, | ||
ListenPort: 1031, | ||
Debug: tt.Debug, | ||
} | ||
|
||
goff, err := service.NewGoFeatureFlagClient(c, z, []notifier.Notifier{}) | ||
require.NoError(t, err) | ||
apiServer := api.New(c, service.Services{ | ||
MonitoringService: service.NewMonitoring(goff), | ||
WebsocketService: service.NewWebsocketService(), | ||
GOFeatureFlagService: goff, | ||
Metrics: metric.Metrics{}, | ||
}, z) | ||
|
||
portToCheck := c.ListenPort | ||
if tt.MonitoringPort != 0 { | ||
portToCheck = tt.MonitoringPort | ||
} | ||
|
||
go apiServer.Start() | ||
defer apiServer.Stop() | ||
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/debug/pprof/heap", portToCheck)) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expectedStatusCode, resp.StatusCode) | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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 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 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,19 @@ | ||
// nolint: lll | ||
package modeldocs | ||
|
||
import "github.com/labstack/echo/v4" | ||
|
||
// FakePprofController is a fake endpoint for swagger documentation of pprof endpoint | ||
// | ||
// @Summary pprof endpoint | ||
// @Tags Profiling | ||
// @Description This endpoint is provided by the echo pprof middleware. | ||
// @Description To know more please check this blogpost from the GO team https://go.dev/blog/pprof. | ||
// @Description Visit the page /debug/pprof/ to see the available endpoints, all endpoint are not in the swagger documentation because they are standard pprof endpoints. | ||
// @Description This endpoint is only available in debug mode. | ||
// @Produce plain | ||
// @Success 200 {object} string | ||
// @Router /debug/pprof/ [get] | ||
func FakePprofController(_ echo.Context) { | ||
// This is a fake controller, the real entry point is provided by the prometheus middleware. | ||
} |
This file contains 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,32 @@ | ||
--- | ||
sidebar_position: 81 | ||
title: Profiling | ||
description: Profiling of the relay proxy. | ||
--- | ||
|
||
## Profiling | ||
|
||
The **relay proxy** is able to expose profiling information. | ||
This is useful to understand the performance of the service and solve potential issues. | ||
|
||
The information are exposed on the `/debug/pprof` endpoint, and we are using the default `net/http/pprof` package | ||
to expose the information. | ||
|
||
:::warning | ||
By default the profiling endpoints are disabled. | ||
You have to run the relay proxy in debug mode if you want to enable them. | ||
::: | ||
|
||
List of endpoints exposed is available http://localhost:1031/debug/pprof/ | ||
|
||
### Enable profiling | ||
|
||
In your relay proxy configuration file you need to set the `debug` field to `true`. | ||
|
||
```yaml {5} | ||
retriever: | ||
kind: file | ||
path: /goff/flags.yaml # Location of your feature flag files | ||
# ... | ||
debug: true | ||
``` |