Skip to content

Commit 1333ce6

Browse files
authored
feat: add pprof endpoints (#454)
1 parent 7eab83c commit 1333ce6

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
delay = 3000
33

44
# The default port for the frontend devserver is 3000, therefore we always set the value here
5-
full_bin = "API_URL=http://localhost:8080 CORS_ALLOW_ORIGINS=http://localhost:3000 ./tmp/main"
5+
full_bin = "ENABLE_PPROF=true API_URL=http://localhost:8080 CORS_ALLOW_ORIGINS=http://localhost:3000 ./tmp/main"

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"env": {
1212
"GIN_MODE": "debug",
1313
"API_URL": "http://localhost:8080",
14-
"CORS_ALLOW_ORIGINS": "http://localhost:3000"
14+
"CORS_ALLOW_ORIGINS": "http://localhost:3000",
15+
"ENABLE_PPROF": "true"
1516
},
1617
"console": "integratedTerminal"
1718
}

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ See [docs/upgrading.md](docs/upgrading.md).
2525

2626
The backend can be configured with the following environment variables.
2727

28-
| Name | Type | Default | Description |
29-
| -------------------- | ------------------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
30-
| `API_URL` | `string` | _none, must be set_ | The URL of the API, e.g. `https://ez.example.com/api` |
31-
| `GIN_MODE` | One of `release`, `debug` | `release` | The mode that gin runs in. Only set this to `debug` on your development environment! |
32-
| `PORT` | `number` | `8080` | The port the backend listens on |
33-
| `LOG_FORMAT` | One of `json`, `human` | `json` if `GIN_MODE` is `release`, otherwise `human` | If log output is written human readable or as JSON. |
34-
| `CORS_ALLOW_ORIGINS` | `string` | `""` | hosts that are allowed to use cross origin requests, separated by spaces. Only set this when your frontend runs on a different host and/or port than the backend! |
28+
| Name | Type | Default | Description |
29+
| -------------------- | ------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
30+
| `API_URL` | `string` | _none, must be set_ | The URL of the API, e.g. `https://ez.example.com/api` |
31+
| `GIN_MODE` | One of `release`, `debug` | `release` | The mode that gin runs in. Only set this to `debug` on your development environment! |
32+
| `PORT` | `number` | `8080` | The port the backend listens on |
33+
| `LOG_FORMAT` | One of `json`, `human` | `json` if `GIN_MODE` is `release`, otherwise `human` | If log output is written human readable or as JSON. |
34+
| `CORS_ALLOW_ORIGINS` | `string` | `""` | hosts that are allowed to use cross origin requests, separated by spaces. Only set this when your frontend runs on a different host and/or port than the backend! |
35+
| `ENABLE_PPROF` | `bool` | `false` | If set to `true`, pprof profiles for application profiling are made available at `/debug/pprof`. :warning: If you do not know what this means, do not turn this on. |
3536

3637
### Deployment methods
3738

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ require (
2121
gorm.io/gorm v1.24.1
2222
)
2323

24+
require github.com/gin-contrib/pprof v1.4.0
25+
2426
require (
2527
github.com/KyleBanks/depth v1.2.1 // indirect
2628
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d
1919
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
2020
github.com/gin-contrib/logger v0.2.5 h1:nl6dfoCafxNmXkY3JGZvr1+ky+W8mlVz4YRkyh2Mkg0=
2121
github.com/gin-contrib/logger v0.2.5/go.mod h1:GozUC4j0E1cF+jTjouvD3tHmYmxyg1Wz6LqKYR+D1aM=
22+
github.com/gin-contrib/pprof v1.4.0 h1:XxiBSf5jWZ5i16lNOPbMTVdgHBdhfGRD5PZ1LWazzvg=
23+
github.com/gin-contrib/pprof v1.4.0/go.mod h1:RrehPJasUVBPK6yTUwOl8/NP6i0vbUgmxtis+Z5KE90=
2224
github.com/gin-contrib/requestid v0.0.6 h1:mGcxTnHQ45F6QU5HQRgQUDsAfHprD3P7g2uZ4cSZo9o=
2325
github.com/gin-contrib/requestid v0.0.6/go.mod h1:9i4vKATX/CdggbkY252dPVasgVucy/ggBeELXuQztm4=
2426
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=

pkg/router/router.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/envelope-zero/backend/pkg/httputil"
1414
"github.com/gin-contrib/cors"
1515
"github.com/gin-contrib/logger"
16+
"github.com/gin-contrib/pprof"
1617
"github.com/gin-contrib/requestid"
1718
"github.com/gin-gonic/gin"
1819
"github.com/rs/zerolog"
@@ -107,9 +108,14 @@ func AttachRoutes(co controllers.Controller, group *gin.RouterGroup) {
107108
group.GET("", GetRoot)
108109
group.OPTIONS("", OptionsRoot)
109110
group.GET("/version", GetVersion)
110-
111111
group.OPTIONS("/version", OptionsVersion)
112112

113+
// pprof performance profiles
114+
enablePprof, ok := os.LookupEnv("ENABLE_PPROF")
115+
if ok && enablePprof == "true" {
116+
pprof.RouteRegister(group, "debug/pprof")
117+
}
118+
113119
group.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
114120

115121
// API v1 setup

pkg/router/router_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ func TestGinMode(t *testing.T) {
4343
os.Unsetenv("API_URL")
4444
}
4545

46+
func TetsPprofOff(t *testing.T) {
47+
os.Setenv("ENABLE_PPROF", "false")
48+
os.Setenv("API_URL", "http://example.com")
49+
50+
r, err := router.Config()
51+
assert.Nil(t, err, "Error on router initialization")
52+
53+
db, err := database.Connect(":memory:?_pragma=foreign_keys(1)")
54+
assert.Nil(t, err, "Error on database connection")
55+
56+
router.AttachRoutes(controllers.Controller{DB: db}, r.Group("/"))
57+
58+
for _, r := range r.Routes() {
59+
assert.NotContains(t, r.Path, "pprof", "pprof routes are registered erroneously! Route: %s", r)
60+
}
61+
62+
os.Unsetenv("ENABLE_PPROF")
63+
}
64+
4665
func TestEnvUnset(t *testing.T) {
4766
_, err := router.Config()
4867

0 commit comments

Comments
 (0)