github.com/yandex/pandora@v0.5.32/examples/custom_pandora/custom_main.go (about) 1 package main 2 3 import ( 4 "math/rand" 5 "time" 6 7 "github.com/spf13/afero" 8 "github.com/yandex/pandora/cli" 9 phttp "github.com/yandex/pandora/components/phttp/import" 10 "github.com/yandex/pandora/core" 11 coreimport "github.com/yandex/pandora/core/import" 12 "github.com/yandex/pandora/core/register" 13 "go.uber.org/zap" 14 ) 15 16 type Ammo struct { 17 URL string 18 QueryParam string 19 } 20 21 type Sample struct { 22 URL string 23 ShootTimeSeconds float64 24 } 25 26 type GunConfig struct { 27 Target string `validate:"required"` // Configuration will fail, without target defined 28 } 29 30 type Gun struct { 31 // Configured on construction. 32 conf GunConfig 33 34 // Configured on Bind, before shooting. 35 aggr core.Aggregator // May be your custom Aggregator. 36 core.GunDeps 37 } 38 39 func NewGun(conf GunConfig) *Gun { 40 return &Gun{conf: conf} 41 } 42 43 func (g *Gun) Bind(aggr core.Aggregator, deps core.GunDeps) error { 44 g.aggr = aggr 45 g.GunDeps = deps 46 return nil 47 } 48 49 func (g *Gun) Shoot(ammo core.Ammo) { 50 customAmmo := ammo.(*Ammo) // Shoot will panic on unexpected ammo type. Panic cancels shooting. 51 g.shoot(customAmmo) 52 } 53 54 func (g *Gun) shoot(ammo *Ammo) { 55 start := time.Now() 56 defer func() { 57 g.aggr.Report(Sample{ammo.URL, time.Since(start).Seconds()}) 58 }() 59 // Put your shoot logic here. 60 g.Log.Info("Custom shoot", zap.String("target", g.conf.Target), zap.Any("ammo", ammo)) 61 time.Sleep(time.Duration(rand.Float64() * float64(time.Second))) 62 } 63 64 func main() { 65 // Standard imports. 66 fs := afero.NewOsFs() 67 coreimport.Import(fs) 68 69 // May not be imported, if you don't need http guns and etc. 70 phttp.Import(fs) 71 72 // Custom imports. Integrate your custom types into configuration system. 73 coreimport.RegisterCustomJSONProvider("my-custom-provider-name", func() core.Ammo { return &Ammo{} }) 74 75 register.Gun("my-custom-gun-name", NewGun, func() GunConfig { 76 return GunConfig{ 77 Target: "default target", 78 } 79 }) 80 register.Gun("my-custom/no-default", NewGun) 81 82 cli.Run() 83 }