Skip to content

Commit

Permalink
collect some meaning-full data for deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Nov 4, 2021
1 parent 244e240 commit 001f60b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 36 deletions.
5 changes: 4 additions & 1 deletion cli/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func newStore() (*domain.FileStore, error) {
if project == nil {
return nil, domain.ErrProjectNotFound
}
log.SetStage(fs.Workspace(), project, nil)
return fs, nil
}

Expand All @@ -110,9 +111,11 @@ func newStoreWithStage(stageName string) (*domain.FileStore, error) {
if len(project.Stages) == 0 {
return nil, log.Wrapf("No stages in project")
}
if fs.Stage(stageName) == nil {
stage := fs.Stage(stageName)
if stage == nil {
return nil, log.Wrapf("Stage %s not found", stageName)
}
log.SetStage(fs.Workspace(), project, stage)
return fs, nil
}

Expand Down
38 changes: 27 additions & 11 deletions cli/controller/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ type Deploy struct {
store *domain.FileStore
stage *domain.Stage

buildDuration time.Duration
uploadDuration time.Duration
uploadBytes int64
updateDuration time.Duration
buildDuration time.Duration
lastBuildDuration time.Duration
uploadDuration time.Duration
uploadBytes int64
updateDuration time.Duration
}

func NewDeploy(a DeployArgs) (*Deploy, error) {
Expand Down Expand Up @@ -69,6 +70,20 @@ func (d *Deploy) Deploy() error {
if err := d.deploy(); err != nil {
return log.Wrap(err)
}
ui.Info("Build time: %v, upload: %v (%s), update: %v",
d.buildDuration.Round(time.Millisecond),
d.uploadDuration.Round(time.Millisecond),
formatFileSizeUnits(d.uploadBytes),
d.updateDuration.Round(time.Millisecond))
log.Event(domain.Event{Deploy: &domain.Deploy{
UpdatedFunctions: len(d.diff.UpdatedFunctions()),
UpdatedPublicSites: len(d.diff.UpdatedPublicSites()),
InfrastructureChanged: d.diff.InfrastructureChanged(),
BuildDuration: toMS(d.buildDuration),
UploadDuration: toMS(d.uploadDuration),
UploadBytes: int(d.uploadBytes),
UpdateDuration: toMS(d.updateDuration),
}})
return nil
}

Expand Down Expand Up @@ -117,12 +132,6 @@ func (d *Deploy) deploy() error {
if err := d.store.Store(); err != nil {
return log.Wrap(err)
}

ui.Info("Build time: %v, upload: %v (%s), update: %v",
d.buildDuration.Round(time.Millisecond),
d.uploadDuration.Round(time.Millisecond),
formatFileSizeUnits(d.uploadBytes),
d.updateDuration.Round(time.Millisecond))
return nil
}

Expand Down Expand Up @@ -217,7 +226,10 @@ func (d *Deploy) updateStage(rsp dto.DeployResponse) {
}

func (d *Deploy) buildTimer(cb func() error) error {
return timer(&d.buildDuration, cb)
before := d.buildDuration
err := timer(&d.buildDuration, cb)
d.lastBuildDuration = d.buildDuration - before
return err
}

func (d *Deploy) uploadTimer(cb func() error) error {
Expand Down Expand Up @@ -254,3 +266,7 @@ func formatFileSizeUnits(b int64) string {
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}

func toMS(d time.Duration) int {
return int(d / time.Millisecond)
}
18 changes: 12 additions & 6 deletions cli/controller/deploy_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ func (d *Deploy) localFunctions() ([]domain.Resource, error) {
return nil, log.Wrap(err)
}
binaryPath := path.Join(funcDir, BinaryName)
hash, err := fileHash(binaryPath)
hash, bytes, err := fileHash(binaryPath)
if err != nil {
return nil, log.Wrap(err, "failed to hash %s", binaryPath)
}
localFuncs = append(localFuncs, domain.Resource{
Name: n,
Hash: hash,
})
log.Event(domain.Event{GoBuild: &domain.GoBuild{
Name: n,
Duration: toMS(d.lastBuildDuration),
Size: int(bytes),
}})
}
return localFuncs, nil
}
Expand Down Expand Up @@ -102,18 +107,19 @@ func (d *Deploy) uploadBinaryToS3(key, binaryPath string) error {
return nil
}

func fileHash(path string) (string, error) {
func fileHash(path string) (string, int64, error) {
f, err := os.Open(path)
if err != nil {
return "", err
return "", 0, err
}
defer f.Close()

h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
var bytes int64
if bytes, err = io.Copy(h, f); err != nil {
return "", 0, err
}
return hex.EncodeToString(h.Sum(nil))[:HashCharacters], nil
return hex.EncodeToString(h.Sum(nil))[:HashCharacters], bytes, nil
}

func createZipForFile(path, name string) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/controller/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func printRspHeaders(rsp *http.Response) {
}

func printApiErrorHeader(rsp *http.Response) {
header := "X-Api-Error"
header := "X-Api-Error" // TODO remove magic string, use constant same in all places and libraries
apiErr := rsp.Header.Get(header)
if apiErr != "" {
ui.Info("%s: %s", header, apiErr)
Expand Down
22 changes: 19 additions & 3 deletions cli/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func Open() error {
func startEventCollector() {
var cc domain.CliCommand
cc.Start()
cc.Args = os.Args
cc.Version = domain.Version()
// TODO add other attributes

// start net connection in another thread
// it will hopefully be ready by end of the commnad
Expand Down Expand Up @@ -71,6 +68,25 @@ func Close() {
}
}

func SetStage(w *domain.Workspace, p *domain.Project, s *domain.Stage) {
if w != nil {
cliCommand.Workspace.Name = w.Name
cliCommand.Workspace.Nodes = len(w.Nodes)
}
if p != nil {
cliCommand.Project.Name = p.Name
cliCommand.Project.Stages = len(p.Stages)
}
if s != nil {
cliCommand.Stage.Name = s.Name
cliCommand.Stage.Node = s.NodeName
cliCommand.Stage.Functions = len(s.Functions)
if s.Public != nil {
cliCommand.Stage.PublicFolders = len(s.Public.Sites)
}
}
}

func sendEvents() error {
cliCommand.End()
buf, err := cliCommand.Marshal()
Expand Down
57 changes: 43 additions & 14 deletions domain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package domain

import (
"encoding/json"
"os"
"os/user"
"runtime"
"time"

jsoniter "github.com/json-iterator/go"
Expand All @@ -16,16 +19,30 @@ import (

// event raised after execution of a cli command
type CliCommand struct {
Timestamp int64 `short:"t,omitempty" json:"timestamp,omitempty"`
Duration int64 `short:"d,omitempty" json:"duration,omitempty"`
Version string `short:"v,omitempty" json:"version,omitempty"`
Command string `short:"c,omitempty" json:"command,omitempty"`
Args []string `short:"a,omitempty" json:"args,omitempty"`
Workspace string `short:"w,omitempty" json:"workspace,omitempty"`
Project string `short:"p,omitempty" json:"project,omitempty"`
Stage string `short:"s,omitempty" json:"stage,omitempty"`
Errors []CliError `short:"r,omitempty" json:"errors,omitempty"`
Events []Event `short:"e,omitempty" json:"events,omitempty"`
Timestamp int64 `short:"t,omitempty" json:"timestamp,omitempty"`
Duration int64 `short:"d,omitempty" json:"duration,omitempty"`
Version string `short:"v,omitempty" json:"version,omitempty"`
Command string `short:"c,omitempty" json:"command,omitempty"`
Args []string `short:"a,omitempty" json:"args,omitempty"`
OS string `short:"o,omitempty" json:"os,omitempty"`
ARCH string `short:"h,omitempty" json:"arch,omitempty"`
Username string `short:"u,omitempty" json:"username,omitempty"`
Workspace struct {
Name string `short:"n,omitempty" json:"name,omitempty"`
Nodes int `short:"o,omitempty" json:"nodes,omitempty"`
} `short:"w,omitempty" json:"workspace,omitempty"`
Project struct {
Name string `short:"n,omitempty" json:"name,omitempty"`
Stages int `short:"s,omitempty" json:"stages,omitempty"`
} `short:"p,omitempty" json:"project,omitempty"`
Stage struct {
Name string `short:"n,omitempty" json:"name,omitempty"`
Node string `short:"o,omitempty" json:"node,omitempty"`
Functions int `short:"f,omitempty" json:"functions,omitempty"`
PublicFolders int `short:"p,omitempty" json:"publicFolders,omitempty"`
} `short:"s,omitempty" json:"stage,omitempty"`
Errors []CliError `short:"r,omitempty" json:"errors,omitempty"`
Events []Event `short:"e,omitempty" json:"events,omitempty"`
}

type CliError struct {
Expand Down Expand Up @@ -74,13 +91,19 @@ type Event struct {
}

type GoBuild struct {
Name string `short:"n,omitempty" json:"name,omitempty"`
Duration int `short:"d,omitempty" json:"duration,omitempty"`
Size int `short:"s,omitempty" json:"size,omitempty"`
}

type Deploy struct {
BuildDuration int64 `short:"b,omitempty" json:"buildDuration,omitempty"`
UploadDuration int64 `short:"u,omitempty" json:"uploadDuration,omitempty"`
UploadMiB int64 `short:"m,omitempty" json:"uploadMiB,omitempty"`
UpdateDuration int64 `short:"d,omitempty" json:"updateDuration,omitempty"`
UpdatedFunctions int `short:"f,omitempty" json:"updatedFunctions,omitempty"`
UpdatedPublicSites int `short:"s,omitempty" json:"updatedPublicSites,omitempty"`
InfrastructureChanged bool `short:"i,omitempty" json:"infrastructureChanged,omitempty"`
BuildDuration int `short:"b,omitempty" json:"buildDuration,omitempty"`
UploadDuration int `short:"u,omitempty" json:"uploadDuration,omitempty"`
UploadBytes int `short:"m,omitempty" json:"uploadbytes,omitempty"`
UpdateDuration int `short:"d,omitempty" json:"updateDuration,omitempty"`
}

type Signal struct {
Expand Down Expand Up @@ -113,7 +136,13 @@ func (c *CliCommand) Start() {
}

func (c *CliCommand) End() {
u, _ := user.Current()
c.Duration = nowMS() - c.Timestamp
c.Args = os.Args
c.Version = Version()
c.OS = runtime.GOOS
c.ARCH = runtime.GOARCH
c.Username = u.Username
}

func nowMS() int64 {
Expand Down

0 comments on commit 001f60b

Please sign in to comment.