Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests for creating metrics, fix bug related to unmarshalling flags #5169

Merged
merged 2 commits into from
Dec 22, 2020
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ require (
github.com/spf13/pflag v1.0.5
github.com/tektoncd/pipeline v0.5.1-0.20190731183258-9d7e37e85bf8
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9
go.opentelemetry.io/otel v0.13.0
go.opentelemetry.io/otel/exporters/stdout v0.13.0
go.opentelemetry.io/otel/sdk v0.13.0
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9
golang.org/x/mod v0.3.0
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1274,10 +1274,19 @@ go.opentelemetry.io/otel v0.13.0 h1:2isEnyzjjJZq6r2EKMsFj4TxiQiexsM04AVhwbR/oBA=
go.opentelemetry.io/otel v0.13.0/go.mod h1:dlSNewoRYikTkotEnxdmuBHgzT+k/idJSfDv/FxEnOY=
go.opentelemetry.io/otel v0.14.0 h1:YFBEfjCk9MTjaytCNSUkp9Q8lF7QJezA06T71FbQxLQ=
go.opentelemetry.io/otel v0.14.0/go.mod h1:vH5xEuwy7Rts0GNtsCW3HYQoZDY+OmBJ6t1bFGGlxgw=
go.opentelemetry.io/otel v0.15.0 h1:CZFy2lPhxd4HlhZnYK8gRyDotksO3Ip9rBweY1vVYJw=
go.opentelemetry.io/otel v0.15.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA=
go.opentelemetry.io/otel/exporters/stdout v0.13.0 h1:A+XiGIPQbGoJoBOJfKAKnZyiUSjSWvL3XWETUvtom5k=
go.opentelemetry.io/otel/exporters/stdout v0.13.0/go.mod h1:JJt8RpNY6K+ft9ir3iKpceCvT/rhzJXEExGrWFCbv1o=
go.opentelemetry.io/otel/exporters/stdout v0.14.0/go.mod h1:KG9w470+KbZZexYbC/g3TPKgluS0VgBJHh4KlnJpG18=
go.opentelemetry.io/otel/exporters/stdout v0.15.0 h1:/i7NvRnB+L7R/uxwpfolovicyBFnFa527NBs2yIhPUo=
go.opentelemetry.io/otel/exporters/stdout v0.15.0/go.mod h1:1d+FA51tyW9NDD0VXUsk5K5S3LAOt9GBWU3TNelHhxA=
go.opentelemetry.io/otel/sdk v0.13.0 h1:4VCfpKamZ8GtnepXxMRurSpHpMKkcxhtO33z1S4rGDQ=
go.opentelemetry.io/otel/sdk v0.13.0/go.mod h1:dKvLH8Uu8LcEPlSAUsfW7kMGaJBhk/1NYvpPZ6wIMbU=
go.opentelemetry.io/otel/sdk v0.14.0 h1:Pqgd85y5XhyvHQlOxkKW+FD4DAX7AoeaNIDKC2VhfHQ=
go.opentelemetry.io/otel/sdk v0.14.0/go.mod h1:kGO5pEMSNqSJppHAm8b73zztLxB5fgDQnD56/dl5xqE=
go.opentelemetry.io/otel/sdk v0.15.0 h1:Hf2dl1Ad9Hn03qjcAuAq51GP5Pv1SV5puIkS2nRhdd8=
go.opentelemetry.io/otel/sdk v0.15.0/go.mod h1:Qudkwgq81OcA9GYVlbyZ62wkLieeS1eWxIL0ufxgwoc=
go.starlark.net v0.0.0-20190528202925-30ae18b8564f/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down
46 changes: 22 additions & 24 deletions pkg/skaffold/instrumentation/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/statik"

// import embedded secret for uploading metrics
// import embedded secret for uploading metrics
_ "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/secret/statik"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand All @@ -61,7 +61,7 @@ type skaffoldMeter struct {
Arch string
PlatformType string
Deployers []string
EnumFlags map[string]*flag.Flag
EnumFlags map[string]string
Builders map[string]int
SyncType map[string]bool
DevIterations []devIteration
Expand All @@ -71,15 +71,15 @@ type skaffoldMeter struct {
}

type devIteration struct {
intent string
errorCode proto.StatusCode
Intent string
ErrorCode proto.StatusCode
}

var (
meter = skaffoldMeter{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
EnumFlags: map[string]*flag.Flag{},
EnumFlags: map[string]string{},
Builders: map[string]int{},
SyncType: map[string]bool{},
DevIterations: []devIteration{},
Expand All @@ -92,6 +92,7 @@ var (
meteredCommands = util.NewStringSet()
doesBuild = util.NewStringSet()
doesDeploy = util.NewStringSet()
initExporter = initCloudMonitoringExporterMetrics
isOnline bool
)

Expand All @@ -113,11 +114,7 @@ func init() {
func InitMeterFromConfig(config *latest.SkaffoldConfig) {
meter.PlatformType = yamltags.GetYamlTag(config.Build.BuildType)
for _, artifact := range config.Pipeline.Build.Artifacts {
if _, ok := meter.Builders[yamltags.GetYamlTag(artifact.ArtifactType)]; ok {
meter.Builders[yamltags.GetYamlTag(artifact.ArtifactType)]++
} else {
meter.Builders[yamltags.GetYamlTag(artifact.ArtifactType)] = 1
}
meter.Builders[yamltags.GetYamlTag(artifact.ArtifactType)]++
if artifact.Sync != nil {
meter.SyncType[yamltags.GetYamlTag(artifact.Sync)] = true
}
Expand All @@ -137,18 +134,20 @@ func SetErrorCode(errorCode proto.StatusCode) {
}

func AddDevIteration(intent string) {
meter.DevIterations = append(meter.DevIterations, devIteration{intent: intent})
meter.DevIterations = append(meter.DevIterations, devIteration{Intent: intent})
}

func AddDevIterationErr(i int, errorCode proto.StatusCode) {
if len(meter.DevIterations) == i {
meter.DevIterations = append(meter.DevIterations, devIteration{intent: "error"})
meter.DevIterations = append(meter.DevIterations, devIteration{Intent: "error"})
}
meter.DevIterations[i].errorCode = errorCode
meter.DevIterations[i].ErrorCode = errorCode
}

func AddFlag(flag *flag.Flag) {
meter.EnumFlags[flag.Name] = flag
if flag.Changed {
meter.EnumFlags[flag.Name] = flag.Value.String()
}
}

func ExportMetrics(exitCode int) error {
Expand All @@ -168,7 +167,7 @@ func ExportMetrics(exitCode int) error {

func exportMetrics(ctx context.Context, filename string, meter skaffoldMeter) error {
logrus.Debug("exporting metrics")
p, err := initCloudMonitoringExporterMetrics()
p, err := initExporter()
if p == nil {
return err
}
Expand All @@ -189,11 +188,13 @@ func exportMetrics(ctx context.Context, filename string, meter skaffoldMeter) er
return ioutil.WriteFile(filename, b, 0666)
}

start := time.Now()
p.Start()
for _, m := range meters {
createMetrics(ctx, m)
}
p.Stop()
logrus.Debugf("metrics uploading complete in %s", time.Since(start).String())

if fileExists {
return os.Remove(filename)
Expand Down Expand Up @@ -221,8 +222,8 @@ func initCloudMonitoringExporterMetrics() (*push.Controller, error) {

var c creds
err = json.Unmarshal(b, &c)
if err != nil {
return nil, fmt.Errorf("error unmarsharling metrics credentials: %v", err)
if c.ProjectID == "" || err != nil {
return nil, fmt.Errorf("no project id found in metrics credentials")
}

formatter := func(desc *metric.Descriptor) string {
Expand Down Expand Up @@ -295,14 +296,11 @@ func commandMetrics(ctx context.Context, meter skaffoldMeter, m metric.Meter, ra
counts := make(map[string]map[proto.StatusCode]int)

for _, iteration := range meter.DevIterations {
if _, ok := counts[iteration.intent]; !ok {
counts[iteration.intent] = make(map[proto.StatusCode]int)
}
m := counts[iteration.intent]
if _, ok := m[iteration.errorCode]; !ok {
m[iteration.errorCode] = 0
if _, ok := counts[iteration.Intent]; !ok {
counts[iteration.Intent] = make(map[proto.StatusCode]int)
}
m[iteration.errorCode]++
m := counts[iteration.Intent]
m[iteration.ErrorCode]++
}
for intention, errorCounts := range counts {
for errorCode, count := range errorCounts {
Expand Down
Loading