Skip to content
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: 3 additions & 0 deletions cmd/werf/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func NewCmd(ctx context.Context) *cobra.Command {
}

func runMain(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()

global_warnings.PostponeMultiwerfNotUpToDateWarning(ctx)
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/bundle/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func NewCmd(ctx context.Context) *cobra.Command {
}

func runPublish(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
global_warnings.PostponeMultiwerfNotUpToDateWarning(ctx)
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
Expand Down
69 changes: 69 additions & 0 deletions cmd/werf/common/build_report_operations_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package common

import (
"bytes"
"context"
"io"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
"github.com/spf13/cobra"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/v2/pkg/config"
"github.com/werf/werf/v2/pkg/opstats"
)

var _ = Describe("build report operations option", func() {
Expand Down Expand Up @@ -54,4 +59,68 @@ var _ = Describe("build report operations option", func() {
Entry("enabled", true),
Entry("disabled", false),
)

Describe("InitOperationsStatistics", func() {
newCtx := func(acceptedLevel level.Level, out io.Writer) context.Context {
logger := logboek.NewLogger(out, out)
logger.SetAcceptedLevel(acceptedLevel)
return logboek.NewContext(context.Background(), logger)
}

It("does not install a collector when disabled", func() {
cmdData := &CmdData{BuildReportOperations: lo.ToPtr(false)}
ctx, finish := InitOperationsStatistics(newCtx(level.Default, io.Discard), cmdData)
Expect(opstats.FromContext(ctx)).To(BeNil())
Expect(finish).NotTo(BeNil())
})

It("installs a command-scoped collector when the flag is set", func() {
cmdData := &CmdData{BuildReportOperations: lo.ToPtr(true)}
ctx, _ := InitOperationsStatistics(newCtx(level.Default, io.Discard), cmdData)
Expect(opstats.FromContext(ctx)).NotTo(BeNil())
})

It("installs a collector under debug logging without the flag", func() {
cmdData := &CmdData{BuildReportOperations: lo.ToPtr(false)}
ctx, _ := InitOperationsStatistics(newCtx(level.Debug, io.Discard), cmdData)
Expect(opstats.FromContext(ctx)).NotTo(BeNil())
})

It("prints the summary with the command time label on finish", func() {
var out bytes.Buffer
cmdData := &CmdData{BuildReportOperations: lo.ToPtr(true)}
ctx, finish := InitOperationsStatistics(newCtx(level.Default, &out), cmdData)

opstats.Observe(ctx, opstats.OperationConfigRender)()
finish()

Expect(out.String()).To(ContainSubstring("Operations summary"))
Expect(out.String()).To(ContainSubstring("config render"))
Expect(out.String()).To(ContainSubstring("command time:"))
})

It("prints the summary even when the accepted log level was lowered after installation", func() {
var out bytes.Buffer
cmdData := &CmdData{BuildReportOperations: lo.ToPtr(true)}
ctx, finish := InitOperationsStatistics(newCtx(level.Default, &out), cmdData)

opstats.Observe(ctx, opstats.OperationConfigRender)()
logboek.Context(ctx).SetAcceptedLevel(level.Error)
finish()

Expect(out.String()).To(ContainSubstring("command time:"))
})

It("keeps the summary suppressed when the command started quiet", func() {
var out bytes.Buffer
cmdData := &CmdData{BuildReportOperations: lo.ToPtr(true)}
ctx, finish := InitOperationsStatistics(newCtx(level.Error, &out), cmdData)

opstats.Observe(ctx, opstats.OperationConfigRender)()
logboek.Context(ctx).SetAcceptedLevel(level.Default)
finish()

Expect(out.String()).To(BeEmpty())
})
})
})
4 changes: 3 additions & 1 deletion cmd/werf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/werf/werf/v2/pkg/git_repo"
"github.com/werf/werf/v2/pkg/giterminism_manager"
"github.com/werf/werf/v2/pkg/logging"
"github.com/werf/werf/v2/pkg/opstats"
"github.com/werf/werf/v2/pkg/storage"
"github.com/werf/werf/v2/pkg/storage/manager"
"github.com/werf/werf/v2/pkg/true_git"
Expand Down Expand Up @@ -192,7 +193,7 @@ func SetupUseBuildReport(cmdData *CmdData, cmd *cobra.Command) {

func SetupBuildReportOperations(cmdData *CmdData, cmd *cobra.Command) {
cmdData.BuildReportOperations = new(bool)
cmd.Flags().BoolVarP(cmdData.BuildReportOperations, "build-report-operations", "", util.GetBoolEnvironmentDefaultFalse("WERF_BUILD_REPORT_OPERATIONS"), fmt.Sprintf("Collect low-level operations statistics: add Operations and StageCache sections to the build report and print operations summary after the build (by default $WERF_BUILD_REPORT_OPERATIONS or %t). Also enabled by --log-debug", DefaultBuildReportOperations))
cmd.Flags().BoolVarP(cmdData.BuildReportOperations, "build-report-operations", "", util.GetBoolEnvironmentDefaultFalse("WERF_BUILD_REPORT_OPERATIONS"), fmt.Sprintf("Collect low-level operations statistics for the whole command run: add Operations and StageCache sections to the build report and print operations summary before the command exits (by default $WERF_BUILD_REPORT_OPERATIONS or %t). Also enabled by --log-debug", DefaultBuildReportOperations))
}

func GetSaveBuildReport(cmdData *CmdData) bool {
Expand Down Expand Up @@ -1474,6 +1475,7 @@ func GetIntrospectOptions(cmdData *CmdData, werfConfig *config.WerfConfig) (buil
}

func GetGiterminismManager(ctx context.Context, cmdData *CmdData) (*giterminism_manager.Manager, error) {
defer opstats.Observe(ctx, opstats.OperationGiterminismInit)()
printGlobalWarningIfDevInCI(ctx, cmdData)
manager := new(giterminism_manager.Manager)
if err := logboek.Context(ctx).Info().LogProcess("Initialize giterminism manager").
Expand Down
31 changes: 31 additions & 0 deletions cmd/werf/common/operations_statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package common

import (
"context"
"time"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/v2/pkg/opstats"
)

// InitOperationsStatistics installs the operations statistics collector into the context for the
// whole command run when enabled by --build-report-operations or debug logging. The returned
// function prints the summary blocks and must be deferred by the command. It restores the
// accepted log level captured here: nelm actions lower it globally (e.g. render sets Error),
// which would otherwise silently drop the summary printed after them.
func InitOperationsStatistics(ctx context.Context, cmdData *CmdData) (context.Context, func()) {
if !GetBuildReportOperations(cmdData) && !logboek.Context(ctx).IsAcceptedLevel(level.Debug) {
return ctx, func() {}
}

collector := opstats.NewCollector()
ctx = opstats.NewContext(ctx, collector)
startedAt := time.Now()
acceptedLevel := logboek.Context(ctx).AcceptedLevel()

return ctx, func() {
logboek.Context(ctx).SetAcceptedLevel(acceptedLevel)
opstats.LogSummary(ctx, collector, "command time", time.Since(startedAt))
}
}
2 changes: 2 additions & 0 deletions cmd/werf/converge/converge.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ werf converge --repo registry.mydomain.com/web --env production`,
}

func runMain(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
global_warnings.PostponeMultiwerfNotUpToDateWarning(ctx)

commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/helm/get_autogenerated_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func NewGetAutogeneratedValuesCmd(ctx context.Context) *cobra.Command {
}

func runGetServiceValues(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
InitTrueGitWithOptions: &common.InitTrueGitOptions{
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func NewCmd(ctx context.Context) *cobra.Command {
}

func runLint(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
global_warnings.PostponeMultiwerfNotUpToDateWarning(ctx)

commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ werf plan --repo registry.mydomain.com/web --env production`,
}

func runMain(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
global_warnings.PostponeMultiwerfNotUpToDateWarning(ctx)
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func NewCmd(ctx context.Context) *cobra.Command {
}

func runRender(ctx context.Context, imageNameListFromArgs []string) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
InitTrueGitWithOptions: &common.InitTrueGitOptions{
Expand Down
3 changes: 3 additions & 0 deletions cmd/werf/sbom/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.LogVersion()

return common.LogRunningTime(func() error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()

if tagFlag != "" {
return runGetByTag(ctx, tagFlag)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/werf/stages/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func NewCmd(ctx context.Context) *cobra.Command {
}

func runCopy(ctx context.Context, cmdData copyCmdData) error {
ctx, logOperationsSummaryFn := common.InitOperationsStatistics(ctx, &commonCmdData)
defer logOperationsSummaryFn()
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
InitWerf: true,
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ werf build [IMAGE_NAME...] [options]
When enabled, the private elf key must be specified with --elf-pgp-private-key-base64
or --elf-pgp-private-key-fingerprint option
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_bundle_publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ werf bundle publish [IMAGE_NAME...] [options]
When enabled, the private elf key must be specified with --elf-pgp-private-key-base64
or --elf-pgp-private-key-fingerprint option
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_converge.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ werf converge --repo registry.mydomain.com/web --env production
When enabled, the private elf key must be specified with --elf-pgp-private-key-base64
or --elf-pgp-private-key-fingerprint option
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ werf helm get-autogenerated-values [IMAGE_NAME...] [options]
--backend-network=""
Network mode for the build containers ($WERF_BACKEND_NETWORK or nothing by default)
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ werf lint [IMAGE_NAME...] [options]
--backend-network=""
Network mode for the build containers ($WERF_BACKEND_NETWORK or nothing by default)
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ werf plan --repo registry.mydomain.com/web --env production
When enabled, the private elf key must be specified with --elf-pgp-private-key-base64
or --elf-pgp-private-key-fingerprint option
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_render.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ werf render [IMAGE_NAME...] [options]
When enabled, the private elf key must be specified with --elf-pgp-private-key-base64
or --elf-pgp-private-key-fingerprint option
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_sbom_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ werf sbom get [IMAGE_NAME] [options]
storage volume usage while performing garbage collection of local backend images
(detect local backend storage path by default or use $WERF_BACKEND_STORAGE_PATH)
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/reference/cli/werf_stages_copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ werf stages copy [options]
storage volume usage while performing garbage collection of local backend images
(detect local backend storage path by default or use $WERF_BACKEND_STORAGE_PATH)
--build-report-operations=false
Collect low-level operations statistics: add Operations and StageCache sections to the
build report and print operations summary after the build (by default
$WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
Collect low-level operations statistics for the whole command run: add Operations and
StageCache sections to the build report and print operations summary before the command
exits (by default $WERF_BUILD_REPORT_OPERATIONS or false). Also enabled by --log-debug
--build-report-path=""
Change build report path and format (by default $WERF_BUILD_REPORT_PATH or
".werf-build-report.json" if not set). Extension must be either .json for JSON format
Expand Down
Loading
Loading