-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #880 from Max-Cheng/main
🚀 Add fgprof profiler
- Loading branch information
Showing
12 changed files
with
422 additions
and
0 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,50 @@ | ||
name-template: 'Fgprof - v$RESOLVED_VERSION' | ||
tag-template: 'fgprof/v$RESOLVED_VERSION' | ||
tag-prefix: fgprof/v | ||
include-paths: | ||
- fgprof | ||
categories: | ||
- title: '❗ Breaking Changes' | ||
labels: | ||
- '❗ BreakingChange' | ||
- title: '🚀 New' | ||
labels: | ||
- '✏️ Feature' | ||
- title: '🧹 Updates' | ||
labels: | ||
- '🧹 Updates' | ||
- '🤖 Dependencies' | ||
- title: '🐛 Fixes' | ||
labels: | ||
- '☢️ Bug' | ||
- title: '📚 Documentation' | ||
labels: | ||
- '📒 Documentation' | ||
change-template: '- $TITLE (#$NUMBER)' | ||
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. | ||
exclude-contributors: | ||
- dependabot | ||
- dependabot[bot] | ||
version-resolver: | ||
major: | ||
labels: | ||
- 'major' | ||
- '❗ BreakingChange' | ||
minor: | ||
labels: | ||
- 'minor' | ||
- '✏️ Feature' | ||
patch: | ||
labels: | ||
- 'patch' | ||
- '📒 Documentation' | ||
- '☢️ Bug' | ||
- '🤖 Dependencies' | ||
- '🧹 Updates' | ||
default: patch | ||
template: | | ||
$CHANGES | ||
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...fgprof/v$RESOLVED_VERSION | ||
Thank you $CONTRIBUTORS for making this update possible. |
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 @@ | ||
name: Release Drafter Fgprof | ||
on: | ||
push: | ||
# branches to consider in the event; optional, defaults to all | ||
branches: | ||
- master | ||
- main | ||
paths: | ||
- 'fgprof/**' | ||
jobs: | ||
draft_release_casbin: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 30 | ||
steps: | ||
- uses: release-drafter/release-drafter@v5 | ||
with: | ||
config-name: release-drafter-fgprof.yml | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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 @@ | ||
name: "Test Fgprof" | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
paths: | ||
- 'fgprof/**' | ||
pull_request: | ||
paths: | ||
- 'fgprof/**' | ||
|
||
jobs: | ||
Tests: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go-version: | ||
- 1.19.x | ||
- 1.20.x | ||
- 1.21.x | ||
steps: | ||
- name: Fetch Repository | ||
uses: actions/checkout@v4 | ||
- name: Install Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '${{ matrix.go-version }}' | ||
- name: Run Test | ||
working-directory: ./fgprof | ||
run: go test -v -race ./... |
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,59 @@ | ||
--- | ||
id: fgprof | ||
--- | ||
|
||
# Fgprof | ||
|
||
![Release](https://img.shields.io/github/v/tag/gofiber/contrib?filter=fgprof*) | ||
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord) | ||
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg) | ||
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg) | ||
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg) | ||
|
||
[fgprof](https://github.com/felixge/fgprof) support for Fiber. | ||
|
||
**Note: Requires Go 1.19 and above** | ||
|
||
## Install | ||
|
||
This middleware supports Fiber v2. | ||
|
||
Using fgprof to profiling your Fiber app. | ||
|
||
``` | ||
go get -u github.com/gofiber/fiber/v2 | ||
go get -u github.com/gofiber/contrib/fgprof | ||
``` | ||
|
||
## Config | ||
|
||
| Property | Type | Description | Default | | ||
|----------|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|---------| | ||
| Next | `func(c *fiber.Ctx) bool` | A function to skip this middleware when returned `true`. | `nil` | | ||
| Prefix | `string`. | Prefix defines a URL prefix added before "/debug/fgprof". Note that it should start with (but not end with) a slash. Example: "/federated-fiber" | `""` | | ||
|
||
## Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gofiber/contrib/fgprof" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
app.Use(fgprof.New()) | ||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString("OK") | ||
}) | ||
log.Fatal(app.Listen(":3000")) | ||
} | ||
``` | ||
|
||
```bash | ||
go tool pprof -http=:8080 http://localhost:3000/debug/fgprof | ||
``` |
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,38 @@ | ||
package fgprof | ||
|
||
import "github.com/gofiber/fiber/v2" | ||
|
||
type Config struct { | ||
// Next defines a function to skip this middleware when returned true. | ||
// | ||
// Optional. Default: nil | ||
Next func(c *fiber.Ctx) bool | ||
|
||
// Prefix is the path where the fprof endpoints will be mounted. | ||
// Default Path is "/debug/fgprof" | ||
// | ||
// Optional. Default: "" | ||
Prefix string | ||
} | ||
|
||
// ConfigDefault is the default config | ||
var ConfigDefault = Config{ | ||
Next: nil, | ||
} | ||
|
||
func configDefault(config ...Config) Config { | ||
// Return default config if nothing provided | ||
if len(config) < 1 { | ||
return ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
// Set default values | ||
if cfg.Next == nil { | ||
cfg.Next = ConfigDefault.Next | ||
} | ||
|
||
return cfg | ||
} |
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,29 @@ | ||
package fgprof | ||
|
||
import ( | ||
"github.com/felixge/fgprof" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/middleware/adaptor" | ||
) | ||
|
||
func New(conf ...Config) fiber.Handler { | ||
// Set default config | ||
cfg := configDefault(conf...) | ||
|
||
fgProfPath := cfg.Prefix + "/debug/fgprof" | ||
|
||
var fgprofHandler = adaptor.HTTPHandler(fgprof.Handler()) | ||
|
||
// Return new handler | ||
return func(c *fiber.Ctx) error { | ||
// Don't execute middleware if Next returns true | ||
if cfg.Next != nil && cfg.Next(c) { | ||
return c.Next() | ||
} | ||
|
||
if c.Path() == fgProfPath { | ||
return fgprofHandler(c) | ||
} | ||
return c.Next() | ||
} | ||
} |
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,109 @@ | ||
package fgprof | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/utils" | ||
"io" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
// go test -run Test_Non_Fgprof_Path | ||
func Test_Non_Fgprof_Path(t *testing.T) { | ||
app := fiber.New() | ||
app.Use(New()) | ||
|
||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString("escaped") | ||
}) | ||
|
||
resp, err := app.Test(httptest.NewRequest("GET", "/", nil)) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 200, resp.StatusCode) | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, "escaped", string(body)) | ||
} | ||
|
||
// go test -run Test_Non_Fgprof_Path_WithPrefix | ||
func Test_Non_Fgprof_Path_WithPrefix(t *testing.T) { | ||
app := fiber.New() | ||
app.Use(New(Config{ | ||
Prefix: "/prefix", | ||
})) | ||
|
||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString("escaped") | ||
}) | ||
|
||
resp, err := app.Test(httptest.NewRequest("GET", "/", nil)) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 200, resp.StatusCode) | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, "escaped", string(body)) | ||
} | ||
|
||
// go test -run Test_Fgprof_Path | ||
func Test_Fgprof_Path(t *testing.T) { | ||
app := fiber.New() | ||
app.Use(New()) | ||
|
||
// Default fgprof interval is 30 seconds | ||
resp, err := app.Test(httptest.NewRequest("GET", "/debug/fgprof?seconds=1", nil), 1500) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 200, resp.StatusCode) | ||
} | ||
|
||
// go test -run Test_Fgprof_Path_WithPrefix | ||
func Test_Fgprof_Path_WithPrefix(t *testing.T) { | ||
app := fiber.New() | ||
app.Use(New(Config{ | ||
Prefix: "/test", | ||
})) | ||
app.Get("/", func(c *fiber.Ctx) error { | ||
return c.SendString("escaped") | ||
}) | ||
|
||
// Non fgprof prefix path | ||
resp, err := app.Test(httptest.NewRequest("GET", "/prefix/debug/fgprof?seconds=1", nil), 1500) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 404, resp.StatusCode) | ||
// Fgprof prefix path | ||
resp, err = app.Test(httptest.NewRequest("GET", "/test/debug/fgprof?seconds=1", nil), 1500) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 200, resp.StatusCode) | ||
} | ||
|
||
// go test -run Test_Fgprof_Next | ||
func Test_Fgprof_Next(t *testing.T) { | ||
app := fiber.New() | ||
|
||
app.Use(New(Config{ | ||
Next: func(_ *fiber.Ctx) bool { | ||
return true | ||
}, | ||
})) | ||
|
||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/debug/pprof/", nil)) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 404, resp.StatusCode) | ||
} | ||
|
||
// go test -run Test_Fgprof_Next_WithPrefix | ||
func Test_Fgprof_Next_WithPrefix(t *testing.T) { | ||
app := fiber.New() | ||
|
||
app.Use(New(Config{ | ||
Next: func(_ *fiber.Ctx) bool { | ||
return true | ||
}, | ||
Prefix: "/federated-fiber", | ||
})) | ||
|
||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/federated-fiber/debug/pprof/", nil)) | ||
utils.AssertEqual(t, nil, err) | ||
utils.AssertEqual(t, 404, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module github.com/gofiber/contrib/fgprof | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/felixge/fgprof v0.9.3 | ||
github.com/gofiber/fiber/v2 v2.51.0 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.0.6 // indirect | ||
github.com/google/pprof v0.0.0-20231205033806-a5a03c77bf08 // indirect | ||
github.com/google/uuid v1.4.0 // indirect | ||
github.com/klauspost/compress v1.17.4 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-runewidth v0.0.15 // indirect | ||
github.com/rivo/uniseg v0.4.4 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasthttp v1.51.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
) |
Oops, something went wrong.