From 32acc9f91f44eb95fb403ba7bcdf68afd5cf335e Mon Sep 17 00:00:00 2001 From: Johannes Dillmann Date: Mon, 14 Feb 2022 15:16:45 +0100 Subject: [PATCH] Collect buildTool in cnbBuild metrics (#3530) --- cmd/cnbBuild.go | 6 +++- cmd/getConfig.go | 71 +++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/cmd/cnbBuild.go b/cmd/cnbBuild.go index 36fbadf83..7ffb16292 100644 --- a/cmd/cnbBuild.go +++ b/cmd/cnbBuild.go @@ -53,6 +53,7 @@ type cnbBuildTelemetryData struct { BuildEnv cnbBuildTelemetryDataBuildEnv `json:"buildEnv"` Buildpacks cnbBuildTelemetryDataBuildpacks `json:"buildpacks"` ProjectDescriptor cnbBuildTelemetryDataProjectDescriptor `json:"projectDescriptor"` + BuildTool string `json:"buildTool"` } type cnbBuildTelemetryDataBuildEnv struct { @@ -323,7 +324,7 @@ func addConfigTelemetryData(utils cnbutils.BuildUtils, data *cnbBuildTelemetryDa data.ImageTag = config.ContainerImageTag data.AdditionalTags = config.AdditionalTags data.BindingKeys = bindingKeys - data.Path, _, _ = config.resolvePath(utils) // ignore error here, telemetry problems should fail the build + data.Path, _, _ = config.resolvePath(utils) // ignore error here, telemetry problems should not fail the build configKeys := data.BuildEnv.KeysFromConfig overallKeys := data.BuildEnv.KeysOverall @@ -334,6 +335,9 @@ func addConfigTelemetryData(utils cnbutils.BuildUtils, data *cnbBuildTelemetryDa data.BuildEnv.KeysFromConfig = configKeys data.BuildEnv.KeysOverall = overallKeys + buildTool, _ := getBuildToolFromStageConfig("cnbBuild") // ignore error here, telemetry problems should not fail the build + data.BuildTool = buildTool + data.Buildpacks.FromConfig = privacy.FilterBuildpacks(config.Buildpacks) } diff --git a/cmd/getConfig.go b/cmd/getConfig.go index b9fcc9981..9b48500dd 100644 --- a/cmd/getConfig.go +++ b/cmd/getConfig.go @@ -76,7 +76,6 @@ func ConfigCommand() *cobra.Command { } func getDockerImageValue(stepName string) (string, error) { - configOptions.contextConfig = true configOptions.stepName = stepName stepConfig, err := getConfig() @@ -93,38 +92,60 @@ func getDockerImageValue(stepName string) (string, error) { return dockerImageValue, nil } +func getBuildToolFromStageConfig(stepName string) (string, error) { + configOptions.contextConfig = true + configOptions.stepName = stepName + stageConfig, err := getStageConfig() + if err != nil { + return "", err + } + + buildTool, ok := stageConfig.Config["buildTool"].(string) + if !ok { + log.Entry().Infof("Config value of %v to compare with is not a string", stageConfig.Config["buildTool"]) + } + + return buildTool, nil +} + +func getStageConfig() (config.StepConfig, error) { + myConfig := config.Config{} + stepConfig := config.StepConfig{} + projectConfigFile := getProjectConfigFile(GeneralConfig.CustomConfig) + + customConfig, err := configOptions.openFile(projectConfigFile, GeneralConfig.GitHubAccessTokens) + if err != nil { + if !os.IsNotExist(err) { + return stepConfig, errors.Wrapf(err, "config: open configuration file '%v' failed", projectConfigFile) + } + customConfig = nil + } + + defaultConfig := []io.ReadCloser{} + for _, f := range GeneralConfig.DefaultConfig { + fc, err := configOptions.openFile(f, GeneralConfig.GitHubAccessTokens) + // only create error for non-default values + if err != nil && f != ".pipeline/defaults.yaml" { + return stepConfig, errors.Wrapf(err, "config: getting defaults failed: '%v'", f) + } + if err == nil { + defaultConfig = append(defaultConfig, fc) + } + } + + return myConfig.GetStageConfig(GeneralConfig.ParametersJSON, customConfig, defaultConfig, GeneralConfig.IgnoreCustomDefaults, configOptions.stageConfigAcceptedParameters, GeneralConfig.StageName) +} + func getConfig() (config.StepConfig, error) { var myConfig config.Config var stepConfig config.StepConfig + var err error if configOptions.stageConfig { - projectConfigFile := getProjectConfigFile(GeneralConfig.CustomConfig) - - customConfig, err := configOptions.openFile(projectConfigFile, GeneralConfig.GitHubAccessTokens) - if err != nil { - if !os.IsNotExist(err) { - return stepConfig, errors.Wrapf(err, "config: open configuration file '%v' failed", projectConfigFile) - } - customConfig = nil - } - - defaultConfig := []io.ReadCloser{} - for _, f := range GeneralConfig.DefaultConfig { - fc, err := configOptions.openFile(f, GeneralConfig.GitHubAccessTokens) - // only create error for non-default values - if err != nil && f != ".pipeline/defaults.yaml" { - return stepConfig, errors.Wrapf(err, "config: getting defaults failed: '%v'", f) - } - if err == nil { - defaultConfig = append(defaultConfig, fc) - } - } - - stepConfig, err = myConfig.GetStageConfig(GeneralConfig.ParametersJSON, customConfig, defaultConfig, GeneralConfig.IgnoreCustomDefaults, configOptions.stageConfigAcceptedParameters, GeneralConfig.StageName) + stepConfig, err = getStageConfig() if err != nil { return stepConfig, errors.Wrap(err, "getting stage config failed") } - } else { log.Entry().Infof("Printing stepName %s", configOptions.stepName) if GeneralConfig.MetaDataResolver == nil {