Skip to content

Commit

Permalink
metrics: add build command duration metric
Browse files Browse the repository at this point in the history
This adds a build duration metric for the build command with attributes
related to the buildx driver, the error type (if any), and which options
were used to perform the build from a subset of the options.

This also refactors some of the utility methods used by the git tool to
determine filepaths into its own separate package so they can be reused
in another place.

Also adds a test to ensure the resource is initialized correctly and
doesn't error. The otel handler logging message is suppressed on buildx
invocations so we never see the error if there's a problem with the
schema url. It's so easy to mess up the schema url when upgrading OTEL
that we need a proper test to make sure we haven't broken the
functionality.

Signed-off-by: Jonathan A. Sternberg <[email protected]>
  • Loading branch information
jsternberg committed Feb 2, 2024
1 parent 082d5d7 commit 34a406a
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog
target.LocalDirs["context"] = inp.ContextPath
}
}
case isLocalDir(inp.ContextPath):
case IsLocalDir(inp.ContextPath):
target.LocalDirs["context"] = inp.ContextPath
switch inp.DockerfilePath {
case "-":
Expand Down
15 changes: 4 additions & 11 deletions build/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/docker/buildx/util/gitutil"
"github.com/docker/buildx/util/osutil"
"github.com/moby/buildkit/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -46,7 +47,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
if filepath.IsAbs(contextPath) {
wd = contextPath
} else {
wd, _ = filepath.Abs(filepath.Join(getWd(), contextPath))
wd, _ = filepath.Abs(filepath.Join(osutil.GetWd(), contextPath))
}
wd = gitutil.SanitizePath(wd)

Expand Down Expand Up @@ -104,7 +105,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
dockerfilePath = filepath.Join(wd, "Dockerfile")
}
if !filepath.IsAbs(dockerfilePath) {
dockerfilePath = filepath.Join(getWd(), dockerfilePath)
dockerfilePath = filepath.Join(osutil.GetWd(), dockerfilePath)
}
if r, err := filepath.Rel(root, dockerfilePath); err == nil && !strings.HasPrefix(r, "..") {
res["label:"+DockerfileLabel] = r
Expand All @@ -124,7 +125,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
if err != nil {
continue
}
if lp, err := getLongPathName(dir); err == nil {
if lp, err := osutil.GetLongPathName(dir); err == nil {
dir = lp
}
dir = gitutil.SanitizePath(dir)
Expand All @@ -134,11 +135,3 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
}
}, nil
}

func getWd() string {
wd, _ := os.Getwd()
if lp, err := getLongPathName(wd); err == nil {
return lp
}
return wd
}
9 changes: 0 additions & 9 deletions build/git_unix.go

This file was deleted.

2 changes: 1 addition & 1 deletion build/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func IsRemoteURL(c string) bool {
return false
}

func isLocalDir(c string) bool {
func IsLocalDir(c string) bool {
st, err := os.Stat(c)
return err == nil && st.IsDir()
}
Expand Down
96 changes: 96 additions & 0 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package commands
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/csv"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -13,6 +15,8 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/containerd/console"
"github.com/docker/buildx/build"
Expand All @@ -29,6 +33,7 @@ import (
"github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/desktop"
"github.com/docker/buildx/util/gitutil"
"github.com/docker/buildx/util/ioset"
"github.com/docker/buildx/util/metricutil"
"github.com/docker/buildx/util/progress"
Expand All @@ -52,6 +57,8 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"google.golang.org/grpc/codes"
)

Expand Down Expand Up @@ -211,6 +218,56 @@ func (o *buildOptions) toDisplayMode() (progressui.DisplayMode, error) {
return progress, nil
}

func buildMetricAttributes(b *builder.Builder, options *buildOptions) attribute.Set {
return attribute.NewSet(
attribute.String("driver.name", options.builder),
attribute.String("driver.type", b.Driver),
attribute.Stringer("build.options.hash", &buildOptionsHash{buildOptions: options}),
)
}

// buildOptionsHash computes a hash for the buildOptions when the String method is invoked.
// This is done so we can delay the computation of the hash until needed by OTEL using
// the fmt.Stringer interface.
type buildOptionsHash struct {
*buildOptions
result string
resultOnce sync.Once
}

func (o *buildOptionsHash) String() string {
o.resultOnce.Do(func() {
target := o.target
contextPath := o.contextPath
dockerfile := o.dockerfileName

var vcs string
if o.contextPath != "-" && build.IsLocalDir(o.contextPath) {
if gitc, err := gitutil.New(gitutil.WithWorkingDir(gitutil.ToAbs(o.contextPath))); err == nil {

Check failure on line 246 in commands/build.go

View workflow job for this annotation

GitHub Actions / test-unit (windows-2022)

undefined: gitutil.ToAbs

Check failure on line 246 in commands/build.go

View workflow job for this annotation

GitHub Actions / test-unit (windows-2022)

undefined: gitutil.ToAbs
// If the context path is an absolute path, try to use the git root directory
// to normalize it to a relative path.
if filepath.IsAbs(o.contextPath) {
if rootDir, err := gitc.RootDir(); err == nil {
contextPath, _ = filepath.Rel(rootDir, o.contextPath)
}
}

// Retrieve the remote url to differentiate the project based
// on the remote url.
vcs, _ = gitc.RemoteURL()
}
}

h := sha256.New()
for _, s := range []string{vcs, target, contextPath, dockerfile} {
_, _ = io.WriteString(h, s)
h.Write([]byte{0})
}
o.result = hex.EncodeToString(h.Sum(nil))
})
return o.result
}

func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) (err error) {
mp, err := metricutil.NewMeterProvider(ctx, dockerCli)
if err != nil {
Expand Down Expand Up @@ -279,6 +336,8 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
return err
}

attributes := buildMetricAttributes(b, &options)
done := timeBuildCommand(mp, attributes)
var resp *client.SolveResponse
var retErr error
if isExperimental() {
Expand All @@ -290,6 +349,8 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
if err := printer.Wait(); retErr == nil {
retErr = err
}

done(retErr)
if retErr != nil {
return retErr
}
Expand Down Expand Up @@ -930,3 +991,38 @@ func maybeJSONArray(v string) []string {
}
return []string{v}
}

// timeBuildCommand will start a timer for timing the build command. It records the time when the returned
// function is invoked into a metric.
func timeBuildCommand(mp metric.MeterProvider, attrs attribute.Set) func(err error) {
meter := metricutil.Meter(mp)
counter, _ := meter.Float64Counter("command.time",
metric.WithDescription("Measures the duration of the build command."),
metric.WithUnit("ms"),
)

start := time.Now()
return func(err error) {
dur := float64(time.Since(start)) / float64(time.Millisecond)
extraAttrs := attribute.NewSet()
if err != nil {
extraAttrs = attribute.NewSet(
attribute.String("error.type", otelErrorType(err)),
)
}
counter.Add(context.Background(), dur,
metric.WithAttributeSet(attrs),
metric.WithAttributeSet(extraAttrs),
)
}
}

// otelErrorType returns an attribute for the error type based on the error category.
// If nil, this function returns an invalid attribute.
func otelErrorType(err error) string {
name := "generic"
if errors.Is(err, context.Canceled) {
name = "canceled"
}
return name
}
8 changes: 8 additions & 0 deletions util/gitutil/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"regexp"
"strings"

"github.com/docker/buildx/util/osutil"
"github.com/moby/sys/mountinfo"
)

Expand Down Expand Up @@ -57,3 +58,10 @@ func SanitizePath(path string) string {
}
return filepath.Clean(path)
}

func ToAbs(path string) string {
if !filepath.IsAbs(path) {
path, _ = filepath.Abs(filepath.Join(osutil.GetWd(), path))
}
return SanitizePath(path)
}
6 changes: 5 additions & 1 deletion util/metricutil/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/docker/buildx/version"
"github.com/docker/cli/cli/command"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
Expand Down Expand Up @@ -86,14 +87,17 @@ func (m *MeterProvider) Report(ctx context.Context) {
var rm metricdata.ResourceMetrics
if err := m.reader.Collect(ctx, &rm); err != nil {
// Error when collecting metrics. Do not send any.
otel.Handle(err)
return
}

var eg errgroup.Group
for _, exp := range m.exporters {
exp := exp
eg.Go(func() error {
_ = exp.Export(ctx, &rm)
if err := exp.Export(ctx, &rm); err != nil {
otel.Handle(err)
}
_ = exp.Shutdown(ctx)
return nil
})
Expand Down
33 changes: 33 additions & 0 deletions util/metricutil/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package metricutil

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel"
)

func TestResource(t *testing.T) {
setErrorHandler(t)

// Ensure resource creation doesn't result in an error.
// This is because the schema urls for the various attributes need to be
// the same, but it's really easy to import the wrong package when upgrading
// otel to anew version and the buildx CLI swallows any visible errors.
res := Resource()

// Ensure an attribute is present.
assert.True(t, res.Set().HasValue("telemetry.sdk.version"), "resource attribute missing")
}

func setErrorHandler(tb testing.TB) {
tb.Helper()

errorHandler := otel.GetErrorHandler()
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
tb.Errorf("otel error: %s", err)
}))
tb.Cleanup(func() {
otel.SetErrorHandler(errorHandler)
})
}
15 changes: 15 additions & 0 deletions util/osutil/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package osutil

import "os"

// GetWd retrieves the current working directory.
//
// On Windows, this function will return the long path name
// version of the path.
func GetWd() string {
wd, _ := os.Getwd()
if lp, err := GetLongPathName(wd); err == nil {
return lp
}
return wd
}
9 changes: 9 additions & 0 deletions util/osutil/path_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !windows
// +build !windows

package osutil

// GetLongPathName is a no-op on non-Windows platforms.
func GetLongPathName(path string) (string, error) {
return path, nil
}
6 changes: 3 additions & 3 deletions build/git_windows.go → util/osutil/path_windows.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package build
package osutil

import "golang.org/x/sys/windows"

// getLongPathName converts Windows short pathnames to full pathnames.
// GetLongPathName converts Windows short pathnames to full pathnames.
// For example C:\Users\ADMIN~1 --> C:\Users\Administrator.
func getLongPathName(path string) (string, error) {
func GetLongPathName(path string) (string, error) {
// See https://groups.google.com/forum/#!topic/golang-dev/1tufzkruoTg
p, err := windows.UTF16FromString(path)
if err != nil {
Expand Down

0 comments on commit 34a406a

Please sign in to comment.