metrics: measure context transfers for local source operations

Measure the transfer size and duration of context transfers for various
categories of local source transfers from the progress stream that's
returned during the build.

Local source transfers are split into one of four categories:
* context
* dockerfile
* dockerignore
* namedcontext

Named contexts that are different names will be categorized under the
same metric.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg
2024-01-24 11:48:14 -06:00
parent c9d1c41d20
commit 97052cf203
3 changed files with 171 additions and 2 deletions

View File

@ -6,11 +6,14 @@ import (
"sync"
"github.com/containerd/console"
"github.com/docker/buildx/util/confutil"
"github.com/docker/buildx/util/logutil"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
type Printer struct {
@ -24,6 +27,7 @@ type Printer struct {
warnings []client.VertexWarning
logMu sync.Mutex
logSourceMap map[digest.Digest]interface{}
metrics *metricWriter
// TODO: remove once we can use result context to pass build ref
// see https://github.com/docker/buildx/pull/1861
@ -49,6 +53,9 @@ func (p *Printer) Unpause() {
func (p *Printer) Write(s *client.SolveStatus) {
p.status <- s
if p.metrics != nil {
p.metrics.Write(s)
}
}
func (p *Printer) Warnings() []client.VertexWarning {
@ -96,7 +103,8 @@ func NewPrinter(ctx context.Context, out console.File, mode progressui.DisplayMo
}
pw := &Printer{
ready: make(chan struct{}),
ready: make(chan struct{}),
metrics: opt.mw,
}
go func() {
for {
@ -147,6 +155,7 @@ func (p *Printer) BuildRefs() map[string]string {
type printerOpts struct {
displayOpts []progressui.DisplayOpt
mw *metricWriter
onclose func()
}
@ -165,6 +174,14 @@ func WithDesc(text string, console string) PrinterOpt {
}
}
func WithMetrics(mp metric.MeterProvider, attrs attribute.Set) PrinterOpt {
return func(opt *printerOpts) {
if confutil.IsExperimental() {
opt.mw = newMetrics(mp, attrs)
}
}
}
func WithOnClose(onclose func()) PrinterOpt {
return func(opt *printerOpts) {
opt.onclose = onclose