Skip to content

Commit

Permalink
add pprofparser package (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyee authored Sep 5, 2024
1 parent 2a87297 commit e4b548b
Show file tree
Hide file tree
Showing 81 changed files with 13,496 additions and 7 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/go-ping/ping v1.1.0
github.com/gobwas/ws v1.1.0
github.com/gogo/protobuf v1.3.2
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
Expand All @@ -25,6 +26,7 @@ require (
github.com/itchyny/timefmt-go v0.1.5
github.com/mssola/user_agent v0.6.0
github.com/oschwald/geoip2-golang v1.9.0
github.com/pierrec/lz4/v4 v4.1.18
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.44.0
Expand Down
829 changes: 822 additions & 7 deletions go.sum

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions pprofparser/cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cfg

import (
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)

const (
DefaultWorkDir = "/usr/local/pprofparser"
DefaultCfgName = "conf.yml"
DefaultCfgPath = DefaultWorkDir + "/" + DefaultCfgName
)

const (
EnvLocal = "local"
EnvDev = "dev"
EnvTest = "test"
EnvPre = "pre"
EnvProduction = "prod"
)

var (
Cfg *Config
)

func Load(file string) error {
reader, err := os.Open(file)
if err != nil {
var wd string
if wd, err = os.Getwd(); err == nil {
cfgFile := filepath.Join(wd, "cfg", DefaultCfgName)
reader, err = os.Open(cfgFile)
}
if err != nil {
return fmt.Errorf("read config file fail: %w", err)
}
}
defer func() {
_ = reader.Close()
}()

decoder := yaml.NewDecoder(reader)
var cfg Config
if err := decoder.Decode(&cfg); err != nil {
return fmt.Errorf("decode yaml config file fail: %w", err)
}
Cfg = &cfg
return nil
}

type Config struct {
Serv Server `yaml:"server"`
Log Log `yaml:"log"`
Gin Gin `yaml:"gin"`
Oss Oss `yaml:"oss"`
Storage Storage `yaml:"storage"`
}

// Server configuration
type Server struct {
Addr string `yaml:"addr"`
Port string `yaml:"port"`
}

// Log log configuration
type Log struct {
Path string `yaml:"path"`
File string `yaml:"file"`
Level string `yaml:"level"`
}

// Gin gin configuration
type Gin struct {
RunMode string `yaml:"run_mode"`
Log string `yaml:"log"`
ErrorLog string `yaml:"error_log"`
}

// Oss aliyun oss configuration
type Oss struct {
Host string `yaml:"host"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
ProfileBucket string `yaml:"profile_bucket"`
ProfileDir string `yaml:"profile_dir"`
}

type Disk struct {
ProfileDir string `yaml:"profile_dir"`
}

type Storage struct {
Disk Disk `yaml:"disk"`
Oss Oss `yaml:"oss"`
}
14 changes: 14 additions & 0 deletions pprofparser/cfg/cfg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cfg

import (
"fmt"
"testing"
)

func TestInitConfig(t *testing.T) {
err := Load("testdata/conf.yml")
if err != nil {
t.Error(err)
}
fmt.Printf("%+#v\n", Cfg)
}
17 changes: 17 additions & 0 deletions pprofparser/cfg/testdata/conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ============= PPROF PARSER CONFIG =============
server:
addr: 0.0.0.0
port: 9634

log:
path: /usr/local/pprofparser
file: main.log
level: info

gin:
log: /usr/local/pprofparser/gin.log
error_log: /usr/local/pprofparser/gin_error.log

storage:
disk:
profile_dir: '/data/profile'
Loading

0 comments on commit e4b548b

Please sign in to comment.