Skip to content

Commit

Permalink
write metrics to file (#5135)
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacPD authored Dec 11, 2020
1 parent a999ca8 commit d781368
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/skaffold/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@ import (

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
)

type ExitCoder interface {
ExitCode() int
}

func main() {
var code int
if err := app.Run(os.Stdout, os.Stderr); err != nil {
if errors.Is(err, context.Canceled) {
logrus.Debugln("ignore error since context is cancelled:", err)
} else {
color.Red.Fprintln(os.Stderr, err)
os.Exit(exitCode(err))
code = exitCode(err)
}
}
if err := instrumentation.ExportMetrics(code); err != nil {
logrus.Debugf("error exporting metrics %v", err)
}
os.Exit(code)
}

func exitCode(err error) int {
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (

DefaultSkaffoldDir = ".skaffold"
DefaultCacheFile = "cache"
DefaultMetricFile = "metrics"

DefaultRPCPort = 50051
DefaultRPCHTTPPort = 50052
Expand Down
38 changes: 38 additions & 0 deletions pkg/skaffold/instrumentation/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ limitations under the License.
package instrumentation

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"time"

"github.com/mitchellh/go-homedir"
flag "github.com/spf13/pflag"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
Expand All @@ -43,6 +50,7 @@ type skaffoldMeter struct {
SyncType map[string]bool
DevIterations []devIteration
StartTime time.Time
Duration time.Duration
ErrorCode proto.StatusCode
}

Expand All @@ -64,6 +72,7 @@ var (
ExitCode: 0,
ErrorCode: proto.StatusCode_OK,
}
skipExport = os.Getenv("SKAFFOLD_EXPORT_METRICS")
)

func InitMeter(runCtx *runcontext.RunContext, config *latest.SkaffoldConfig) {
Expand Down Expand Up @@ -97,3 +106,32 @@ func AddDevIterationErr(i int, errorCode proto.StatusCode) {
func AddFlag(flag *flag.Flag) {
meter.EnumFlags[flag.Name] = flag
}

func ExportMetrics(exitCode int) error {
home, err := homedir.Dir()
if err != nil {
return fmt.Errorf("retrieving home directory: %w", err)
}
meter.ExitCode = exitCode
meter.Duration = time.Since(meter.StartTime)
return exportMetrics(filepath.Join(home, constants.DefaultSkaffoldDir, constants.DefaultMetricFile), meter)
}

func exportMetrics(filename string, meter skaffoldMeter) error {
if skipExport != "true" || meter.Command == "" {
return nil
}
b, err := ioutil.ReadFile(filename)
if err != nil && !os.IsNotExist(err) {
return err
}

var meters []skaffoldMeter
err = json.Unmarshal(b, &meters)
if err != nil {
meters = []skaffoldMeter{}
}
meters = append(meters, meter)
b, _ = json.Marshal(meters)
return ioutil.WriteFile(filename, b, 0666)
}
117 changes: 117 additions & 0 deletions pkg/skaffold/instrumentation/meter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2020 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package instrumentation

import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"time"

"github.com/spf13/pflag"

"github.com/GoogleContainerTools/skaffold/proto"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestExportMetrics(t *testing.T) {
startTime, _ := time.Parse(time.ANSIC, "Mon Jan 2 15:04:05 -0700 MST 2006")
validMeter := skaffoldMeter{
Command: "build",
BuildArtifacts: 5,
Version: "vTest.0",
Arch: "test arch",
OS: "test os",
Builders: map[string]bool{"docker": true, "buildpacks": true},
EnumFlags: map[string]*pflag.Flag{"test": {Name: "test", Shorthand: "t"}},
StartTime: startTime,
Duration: time.Minute,
}
validMeterBytes, _ := json.Marshal(validMeter)

tests := []struct {
name string
meter skaffoldMeter
savedMetrics []byte
shouldSkip bool
shouldFailUnmarshal bool
}{
{
name: "skips exporting if command is not set",
shouldSkip: true,
},
{
name: "saves meter to a new file",
meter: validMeter,
},
{
name: "meter is appended to previously saved metrics",
meter: skaffoldMeter{
Command: "dev",
Version: "vTest.1",
Arch: "test arch 2",
OS: "test os 2",
PlatformType: "test platform",
Deployers: []string{"test helm", "test kpt"},
SyncType: map[string]bool{"manual": true},
EnumFlags: map[string]*pflag.Flag{"test_run": {Name: "test_run", Shorthand: "r"}},
ErrorCode: proto.StatusCode_BUILD_CANCELLED,
StartTime: startTime.Add(time.Hour * 24 * 30),
Duration: time.Minute,
},
savedMetrics: validMeterBytes,
},
{
name: "meter does not re-save invalid metrics",
meter: validMeter,
savedMetrics: []byte("[{\"Command\":\"run\", Invalid\": 10000000000010202301230}]"),
shouldFailUnmarshal: true,
},
}
for _, test := range tests {
testutil.Run(t, test.name, func(t *testutil.T) {
t.Override(&skipExport, "true")
filename := "metrics"
tmp := t.NewTempDir()
var savedMetrics []skaffoldMeter
_ = json.Unmarshal(test.savedMetrics, &savedMetrics)

if len(test.savedMetrics) > 0 {
err := ioutil.WriteFile(tmp.Path(filename), test.savedMetrics, 0666)
if err != nil {
t.Error(err)
}
}
_ = exportMetrics(tmp.Path(filename), test.meter)

if test.shouldSkip {
_, err := os.Stat(filename)
t.CheckDeepEqual(true, os.IsNotExist(err))
} else {
b, _ := ioutil.ReadFile(tmp.Path(filename))
var actual []skaffoldMeter
_ = json.Unmarshal(b, &actual)
expected := append(savedMetrics, test.meter)
if test.shouldFailUnmarshal {
expected = []skaffoldMeter{test.meter}
}
t.CheckDeepEqual(expected, actual)
}
})
}
}

0 comments on commit d781368

Please sign in to comment.