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

chore: add telemetry support #1225

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 15 additions & 14 deletions cmd/lifecycle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"errors"

"github.com/BurntSushi/toml"
"github.com/microsoft/ApplicationInsights-Go/appinsights"

"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
Expand Down Expand Up @@ -75,19 +75,20 @@ func (b *buildCmd) Exec() error {

func (b *buildCmd) build(group buildpack.Group, plan files.Plan, analyzedMD files.Analyzed) error {
builder := &phase.Builder{
AppDir: b.AppDir,
BuildConfigDir: b.BuildConfigDir,
LayersDir: b.LayersDir,
PlatformDir: b.PlatformDir,
BuildExecutor: &buildpack.DefaultBuildExecutor{},
DirStore: platform.NewDirStore(b.BuildpacksDir, ""),
Group: group,
Logger: cmd.DefaultLogger,
Out: cmd.Stdout,
Err: cmd.Stderr,
Plan: plan,
PlatformAPI: b.PlatformAPI,
AnalyzeMD: analyzedMD,
AppDir: b.AppDir,
BuildConfigDir: b.BuildConfigDir,
LayersDir: b.LayersDir,
PlatformDir: b.PlatformDir,
BuildExecutor: &buildpack.DefaultBuildExecutor{},
DirStore: platform.NewDirStore(b.BuildpacksDir, ""),
Group: group,
Logger: cmd.DefaultLogger,
Out: cmd.Stdout,
Err: cmd.Stderr,
Plan: plan,
PlatformAPI: b.PlatformAPI,
AnalyzeMD: analyzedMD,
TelemetryClient: appinsights.NewTelemetryClient("<change to instrumentation key>"),
}
md, err := builder.Build()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/go-containerregistry v0.16.1
github.com/google/uuid v1.3.1
github.com/heroku/color v0.0.6
github.com/microsoft/ApplicationInsights-Go v0.4.4
github.com/moby/buildkit v0.12.2
github.com/pkg/errors v0.9.1
github.com/sclevine/spec v1.4.0
Expand All @@ -25,6 +26,7 @@ require (
require (
cloud.google.com/go/compute v1.23.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
code.cloudfoundry.org/clock v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down Expand Up @@ -73,6 +75,7 @@ require (
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/ePirat/docker-credential-gitlabci v1.0.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gofrs/uuid v3.3.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
code.cloudfoundry.org/clock v0.0.0-20180518195852-02e53af36e6c/go.mod h1:QD9Lzhd/ux6eNQVUDVRJX/RKTigpewimNYBi7ivZKY8=
code.cloudfoundry.org/clock v1.0.0 h1:kFXWQM4bxYvdBw2X8BbBeXwQNgfoWv1vqAk2ZZyBN2o=
code.cloudfoundry.org/clock v1.0.0/go.mod h1:QD9Lzhd/ux6eNQVUDVRJX/RKTigpewimNYBi7ivZKY8=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
Expand Down Expand Up @@ -201,6 +204,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84=
github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down Expand Up @@ -336,6 +341,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/microsoft/ApplicationInsights-Go v0.4.4 h1:G4+H9WNs6ygSCe6sUyxRc2U81TI5Es90b2t/MwX5KqY=
github.com/microsoft/ApplicationInsights-Go v0.4.4/go.mod h1:fKRUseBqkw6bDiXTs3ESTiU/4YTIHsQS4W3fP2ieF4U=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand Down Expand Up @@ -368,6 +375,7 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
Expand Down Expand Up @@ -438,6 +446,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
Expand Down
57 changes: 45 additions & 12 deletions phase/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package phase

import (
"fmt"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -36,22 +40,24 @@ type BuildEnv interface {
}

type Builder struct {
AppDir string
BuildConfigDir string
LayersDir string
PlatformDir string
BuildExecutor buildpack.BuildExecutor
DirStore DirStore
Group buildpack.Group
Logger log.Logger
Out, Err io.Writer
Plan files.Plan
PlatformAPI *api.Version
AnalyzeMD files.Analyzed
AppDir string
BuildConfigDir string
LayersDir string
PlatformDir string
BuildExecutor buildpack.BuildExecutor
DirStore DirStore
Group buildpack.Group
Logger log.Logger
Out, Err io.Writer
Plan files.Plan
PlatformAPI *api.Version
AnalyzeMD files.Analyzed
TelemetryClient appinsights.TelemetryClient
}

func (b *Builder) Build() (*files.BuildMetadata, error) {
defer log.NewMeasurement("Builder", b.Logger)()
defer b.shutdown()

// ensure layers SBOM directory is removed
if err := os.RemoveAll(filepath.Join(b.LayersDir, "sbom")); err != nil {
Expand All @@ -76,6 +82,11 @@ func (b *Builder) Build() (*files.BuildMetadata, error) {
filteredPlan := b.Plan

for _, bp := range b.Group.Group {
start := time.Now()
buildEvent := appinsights.NewEventTelemetry("lifecycle.build")
buildEvent.Properties["correlationId"] = os.Getenv("CORRELATION_ID")
buildEvent.Properties["buildpackId"] = bp.ID
buildEvent.Properties["buildpackVersion"] = bp.Version
b.Logger.Debugf("Running build for buildpack %s", bp)

b.Logger.Debug("Looking up buildpack")
Expand All @@ -89,6 +100,16 @@ func (b *Builder) Build() (*files.BuildMetadata, error) {

br, err := b.BuildExecutor.Build(*bpTOML, inputs, b.Logger)
if err != nil {
buildEvent.Properties["succeeded"] = "false"
buildEvent.Properties["error"] = err.Error()
if e, ok := err.(*buildpack.Error); ok {
if execErr, ok := e.RootError.(*exec.ExitError); ok {
buildEvent.Properties["exitCode"] = strconv.Itoa(execErr.ExitCode())
}
}

buildEvent.Properties["durationInMs"] = strconv.FormatInt(time.Since(start).Milliseconds(), 10)
b.TelemetryClient.Track(buildEvent)
return nil, err
}

Expand All @@ -108,6 +129,9 @@ func (b *Builder) Build() (*files.BuildMetadata, error) {
}

b.Logger.Debugf("Finished running build for buildpack %s", bp)
buildEvent.Properties["succeeded"] = "true"
buildEvent.Properties["durationInMs"] = strconv.FormatInt(time.Since(start).Milliseconds(), 10)
b.TelemetryClient.Track(buildEvent)
}

if b.PlatformAPI.AtLeast("0.8") {
Expand Down Expand Up @@ -147,6 +171,15 @@ func (b *Builder) Build() (*files.BuildMetadata, error) {
}, nil
}

func (b *Builder) shutdown() {
select {
case <-b.TelemetryClient.Channel().Close(10 * time.Second):
// If we got here, then all telemetry was submitted
// successfully, and we can proceed to exiting.
case <-time.After(30 * time.Second):
}
}

func (b *Builder) getBuildInputs() buildpack.BuildInputs {
return buildpack.BuildInputs{
AppDir: b.AppDir,
Expand Down
Loading