Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: '1.22'

- name: Build
run: go build -v ./...
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ storage/*
!logs/readme.md
*.yaml
*.exe
.vscode
*.code-workspace
4 changes: 2 additions & 2 deletions cmd/prof/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-kratos/kratos/v2/log"
"github.com/matteo-gz/prof/internal/conf"
"github.com/matteo-gz/prof/internal/server"
"github.com/matteo-gz/prof/pkg/appx"
"github.com/matteo-gz/prof/internal/appx"
"github.com/matteo-gz/prof/pkg/logx"
)

Expand Down Expand Up @@ -49,6 +49,6 @@ func main() {
panic(err2)
}
}
func newApp(logger log.Logger, hsx server.InterFace) *appx.App {
func newApp(logger log.Logger, hsx server.Interface) *appx.App {
return appx.New(logger, hsx)
}
2 changes: 1 addition & 1 deletion cmd/prof/wrie.go → cmd/prof/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/matteo-gz/prof/internal/data"
"github.com/matteo-gz/prof/internal/server"
"github.com/matteo-gz/prof/internal/service"
"github.com/matteo-gz/prof/pkg/appx"
"github.com/matteo-gz/prof/internal/appx"
)

func wireApp(*conf.Bs, *conf.Server, *conf.Data, log.Logger) (*appx.App, func(), error) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/prof/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions cmd/tool/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/matteo-gz/prof

go 1.17
go 1.22

require (
github.com/gabriel-vasile/mimetype v1.4.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
Expand Down Expand Up @@ -162,7 +161,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
4 changes: 2 additions & 2 deletions pkg/appx/app.go → internal/appx/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type App struct {
hs server.InterFace
hs server.Interface
logger log.Logger
ctx context.Context
}
Expand Down Expand Up @@ -41,7 +41,7 @@ func (app *App) sig() (err error) {
}
}

func New(logger log.Logger, hs server.InterFace) *App {
func New(logger log.Logger, hs server.Interface) *App {
return &App{
hs: hs,
logger: logger,
Expand Down
235 changes: 235 additions & 0 deletions internal/biz/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package biz

import (
"context"
"fmt"
"net/url"
"strconv"
"sync"

"github.com/go-kratos/kratos/v2/log"
"github.com/matteo-gz/prof/pkg/pproftype"
)

// BatchParams holds the structured parameters for a batch profiling run.
type BatchParams struct {
URL string `json:"url"`
SamplingSeconds int `json:"sampling_seconds"`
SnapshotMode string `json:"snapshot_mode"`
DeltaSeconds int `json:"delta_seconds"`
TraceEnabled bool `json:"trace_enabled"`
TraceSeconds int `json:"trace_seconds"`
Types []string `json:"types"`
}

type urlTask struct {
url string
timeout int
}

type batch struct {
oriUrl string
url string
samplingSeconds int
snapshotMode string
deltaSeconds int
traceEnabled bool
traceSeconds int
selectedTypes map[string]bool
fileList []string
taskList []urlTask
log *log.Helper
denyPrivateIP bool
}

const maxTime = 180

func clampSeconds(v, defaultVal int) int {
if v <= 0 {
return defaultVal
}
if v > maxTime {
return maxTime
}
return v
}

func buildSelectedTypes(types []string) map[string]bool {
if len(types) == 0 {
return nil
}
m := make(map[string]bool, len(types))
for _, t := range types {
if pproftype.IsValidType(t) {
m[t] = true
}
}
if len(m) == 0 {
return nil
}
return m
}

func newBatchFromParams(p BatchParams, log *log.Helper, denyPrivateIP bool, defaultSampling, defaultDelta, defaultTrace int) *batch {
mode := p.SnapshotMode
if mode != "delta" {
mode = "snapshot"
}
sel := buildSelectedTypes(p.Types)
traceEnabled := p.TraceEnabled
if sel != nil {
traceEnabled = sel[pproftype.Trace]
}
return &batch{
oriUrl: p.URL,
samplingSeconds: clampSeconds(p.SamplingSeconds, defaultSampling),
snapshotMode: mode,
deltaSeconds: clampSeconds(p.DeltaSeconds, defaultDelta),
traceEnabled: traceEnabled,
traceSeconds: clampSeconds(p.TraceSeconds, defaultTrace),
selectedTypes: sel,
log: log,
denyPrivateIP: denyPrivateIP,
}
}

// newBatchLegacy creates a batch from a legacy URL containing ?seconds=N.
func newBatchLegacy(rawURL string, log *log.Helper, denyPrivateIP bool, defaultSampling, defaultDelta, defaultTrace int) *batch {
return &batch{
oriUrl: rawURL,
samplingSeconds: defaultSampling,
deltaSeconds: defaultDelta,
traceEnabled: false,
traceSeconds: defaultTrace,
snapshotMode: "snapshot",
log: log,
denyPrivateIP: denyPrivateIP,
}
}

type saveFile func(url string, contentType string, data []byte) (relativePath string, err error)

func (b *batch) Start(ctx context.Context, fn saveFile) (res []Cse, err error) {
if err = b.setUrl(); err != nil {
return
}
b.setUrlList()
wg := sync.WaitGroup{}
taskL := len(b.taskList)
wg.Add(taskL)
ch := make(chan Cse, taskL)
for _, t := range b.taskList {
go b.run(ctx, t, ch, &wg, fn)
}
wg.Wait()
for {
if taskL == 0 {
break
}
taskL--
res = append(res, <-ch)
}
return
}

// Cse combines a string result and an error.
type Cse struct {
S string
E error
}

func (b *batch) run(ctx context.Context, task urlTask, ch chan Cse, wg *sync.WaitGroup, fn saveFile) {
defer wg.Done()
data, contentType, err := curlGet(ctx, task.url, task.timeout)
if err != nil {
ch <- Cse{"", err}
return
}
relativePath, err := fn(task.url, contentType, data)
if err != nil {
ch <- Cse{"", err}
return
}
ch <- Cse{relativePath, nil}
}

func (uc *Usecase) DealRun(ctx context.Context, p BatchParams) (res []Cse, err error) {
b := newBatchFromParams(p, uc.log, uc.denyPrivateIP, uc.samplingSeconds, uc.deltaSeconds, uc.traceSeconds)
return b.Start(ctx, uc.repo.CreateFile)
}

// DealRunLegacy handles the old URL-based batch request for backward compatibility.
func (uc *Usecase) DealRunLegacy(ctx context.Context, uri string) (res []Cse, err error) {
b := newBatchLegacy(uri, uc.log, uc.denyPrivateIP, uc.samplingSeconds, uc.deltaSeconds, uc.traceSeconds)
return b.Start(ctx, uc.repo.CreateFile)
}

func (b *batch) setUrl() (err error) {
uri, err := url.QueryUnescape(b.oriUrl)
if err != nil {
return
}
if err = validateURL(uri, b.denyPrivateIP); err != nil {
return
}
urlP, err := url.Parse(uri)
if err != nil {
return
}
q := urlP.Query()

// Legacy path: extract seconds from URL if present and sampling/delta not yet set.
if seconds := q.Get("seconds"); seconds != "" {
q.Del("seconds")
if v, e := strconv.Atoi(seconds); e == nil {
if b.samplingSeconds <= 0 {
b.samplingSeconds = clampSeconds(v, 30)
}
if b.deltaSeconds <= 0 {
b.deltaSeconds = clampSeconds(v, 10)
}
}
}

urlP.RawQuery = q.Encode()
b.url = urlP.String()
return
}

func (b *batch) setUrlList() {
for _, v := range pproftype.List {
family := pproftype.Family(v)

if b.selectedTypes != nil && family != pproftype.FamilyMeta {
if !b.selectedTypes[v] {
continue
}
}

var u string
var timeout int
switch family {
case pproftype.FamilySampling:
u = fmt.Sprintf("%s/%s?seconds=%d", b.url, v, b.samplingSeconds)
timeout = b.samplingSeconds + 5
case pproftype.FamilyTrace:
if !b.traceEnabled {
continue
}
u = fmt.Sprintf("%s/%s?seconds=%d", b.url, v, b.traceSeconds)
timeout = b.traceSeconds + 5
case pproftype.FamilySnapshot:
if b.snapshotMode == "delta" {
u = fmt.Sprintf("%s/%s?seconds=%d", b.url, v, b.deltaSeconds)
timeout = b.deltaSeconds + 5
} else {
u = fmt.Sprintf("%s/%s", b.url, v)
timeout = 10
}
case pproftype.FamilyMeta:
u = fmt.Sprintf("%s/%s", b.url, v)
timeout = 5
}
b.taskList = append(b.taskList, urlTask{url: u, timeout: timeout})
}
}
Loading
Loading