Skip to content

Commit

Permalink
Add metrics for npm vendor dependencies
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 569031543
Change-Id: I31b8b2fcd1a487b9690d2b55cce18ac1a8b3ac29
  • Loading branch information
kappratiksha authored and copybara-github committed Sep 28, 2023
1 parent df4a28b commit bfe003f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/nodejs/npm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go_binary(
],
deps = [
"//pkg/ar",
"//pkg/buildermetrics",
"//pkg/cache",
"//pkg/devmode",
"//pkg/gcpbuildpack",
Expand Down
6 changes: 6 additions & 0 deletions cmd/nodejs/npm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/GoogleCloudPlatform/buildpacks/pkg/ar"
"github.com/GoogleCloudPlatform/buildpacks/pkg/buildermetrics"
"github.com/GoogleCloudPlatform/buildpacks/pkg/cache"
"github.com/GoogleCloudPlatform/buildpacks/pkg/devmode"
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
Expand Down Expand Up @@ -54,6 +55,10 @@ func buildFn(ctx *gcp.Context) error {
return fmt.Errorf("creating layer: %w", err)
}
nm := filepath.Join(ml.Path, "node_modules")
if nmExists, _ := ctx.FileExists("node_modules"); nmExists {
buildermetrics.GlobalBuilderMetrics().GetCounter(buildermetrics.NpmNodeModulesCounterID).Increment(1)

}
vendorNpmDeps := nodejs.IsUsingVendoredDependencies()
if !vendorNpmDeps {
if err := ctx.RemoveAll("node_modules"); err != nil {
Expand Down Expand Up @@ -95,6 +100,7 @@ func buildFn(ctx *gcp.Context) error {
}

if vendorNpmDeps {
buildermetrics.GlobalBuilderMetrics().GetCounter(buildermetrics.NpmVendorDependenciesCounterID).Increment(1)
if _, err := ctx.Exec([]string{"npm", "rebuild"}, gcp.WithEnv("NODE_ENV="+buildNodeEnv), gcp.WithUserAttribution); err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/buildermetrics/counterids.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
NpmBuildUsageCounterID CounterID = "3"
NpmGoogleNodeRunScriptsUsageCounterID CounterID = "4"
PipVendorDependenciesCounterID CounterID = "5"
NpmNodeModulesCounterID CounterID = "6"
NpmVendorDependenciesCounterID CounterID = "7"
)

var (
Expand All @@ -63,6 +65,14 @@ var (
"vendor_pip_dependencies_uses",
"The number of times GOOGLE_VENDOR_PIP_DEPENDENCIES is used by developers",
},
NpmNodeModulesCounterID: Descriptor{
"npm_node_modules_uses",
"The number of times node_modules directory exist in source code",
},
NpmVendorDependenciesCounterID: Descriptor{
"vendor_npm_dependencies_uses",
"The number of times GOOGLE_VENDOR_NPM_DEPENDENCIES is used by developers",
},
}
)

Expand Down

0 comments on commit bfe003f

Please sign in to comment.