Skip to content

Commit 54b7b7f

Browse files
committed
feat: add metric for image push duration
1 parent 0d253fb commit 54b7b7f

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

internal/build/imgsrc/buildpacks_builder.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
packclient "github.com/buildpacks/pack/pkg/client"
1010
projectTypes "github.com/buildpacks/pack/pkg/project/types"
1111
"github.com/pkg/errors"
12+
"go.opentelemetry.io/otel/attribute"
13+
"go.opentelemetry.io/otel/trace"
14+
1215
"github.com/superfly/flyctl/internal/cmdfmt"
1316
"github.com/superfly/flyctl/internal/metrics"
1417
"github.com/superfly/flyctl/internal/tracing"
1518
"github.com/superfly/flyctl/iostreams"
1619
"github.com/superfly/flyctl/terminal"
17-
"go.opentelemetry.io/otel/attribute"
18-
"go.opentelemetry.io/otel/trace"
1920
)
2021

2122
type buildpacksBuilder struct{}
@@ -158,7 +159,9 @@ func (*buildpacksBuilder) Run(ctx context.Context, dockerFactory *dockerClientFa
158159
build.PushStart()
159160
cmdfmt.PrintBegin(streams.ErrOut, "Pushing image to fly")
160161

161-
if err := pushToFly(ctx, docker, streams, opts.Tag); err != nil {
162+
builderType, builderRegion := getBuilderInfo(ctx, dockerFactory)
163+
164+
if err := pushToFly(ctx, docker, streams, opts.Tag, builderType, builderRegion); err != nil {
162165
build.PushFinish()
163166
return nil, "", err
164167
}

internal/build/imgsrc/builtin_builder.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package imgsrc
22

33
import (
4-
"fmt"
5-
64
"context"
5+
"fmt"
76

87
"github.com/pkg/errors"
8+
99
"github.com/superfly/flyctl/internal/build/imgsrc/builtins"
1010
"github.com/superfly/flyctl/internal/cmdfmt"
1111
"github.com/superfly/flyctl/internal/metrics"
@@ -126,7 +126,9 @@ func (*builtinBuilder) Run(ctx context.Context, dockerFactory *dockerClientFacto
126126
build.PushStart()
127127
cmdfmt.PrintBegin(streams.ErrOut, "Pushing image to fly")
128128

129-
if err := pushToFly(ctx, docker, streams, opts.Tag); err != nil {
129+
builderType, builderRegion := getBuilderInfo(ctx, dockerFactory)
130+
131+
if err := pushToFly(ctx, docker, streams, opts.Tag, builderType, builderRegion); err != nil {
130132
build.PushFinish()
131133
return nil, "", err
132134
}

internal/build/imgsrc/dockerfile_builder.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525
"github.com/moby/buildkit/exporter/containerimage/exptypes"
2626
"github.com/moby/buildkit/session/secrets/secretsprovider"
2727
"github.com/pkg/errors"
28+
"go.opentelemetry.io/otel/attribute"
29+
"go.opentelemetry.io/otel/trace"
30+
"golang.org/x/sync/errgroup"
31+
2832
"github.com/superfly/flyctl/helpers"
2933
"github.com/superfly/flyctl/internal/buildinfo"
3034
"github.com/superfly/flyctl/internal/cmdfmt"
@@ -34,9 +38,6 @@ import (
3438
"github.com/superfly/flyctl/internal/tracing"
3539
"github.com/superfly/flyctl/iostreams"
3640
"github.com/superfly/flyctl/terminal"
37-
"go.opentelemetry.io/otel/attribute"
38-
"go.opentelemetry.io/otel/trace"
39-
"golang.org/x/sync/errgroup"
4041
)
4142

4243
type dockerfileBuilder struct{}
@@ -270,10 +271,12 @@ func (*dockerfileBuilder) Run(ctx context.Context, dockerFactory *dockerClientFa
270271
build.BuildFinish()
271272
cmdfmt.PrintDone(streams.ErrOut, "Building image done")
272273

274+
builderType, builderRegion := getBuilderInfo(ctx, dockerFactory)
275+
273276
if opts.Publish {
274277
build.PushStart()
275278
tb := render.NewTextBlock(ctx, "Pushing image to fly")
276-
if err := pushToFly(ctx, docker, streams, opts.Tag); err != nil {
279+
if err := pushToFly(ctx, docker, streams, opts.Tag, builderType, builderRegion); err != nil {
277280
build.PushFinish()
278281
return nil, "", err
279282
}
@@ -521,7 +524,7 @@ func runBuildKitBuild(ctx context.Context, docker *dockerclient.Client, opts Ima
521524
return res.ExporterResponse[exptypes.ExporterImageDigestKey], nil
522525
}
523526

524-
func pushToFly(ctx context.Context, docker *dockerclient.Client, streams *iostreams.IOStreams, tag string) (err error) {
527+
func pushToFly(ctx context.Context, docker *dockerclient.Client, streams *iostreams.IOStreams, tag string, builderType string, builderRegion string) (err error) {
525528
ctx, span := tracing.GetTracer().Start(ctx, "push_image_to_registry", trace.WithAttributes(attribute.String("tag", tag)))
526529
defer span.End()
527530

@@ -531,6 +534,12 @@ func pushToFly(ctx context.Context, docker *dockerclient.Client, streams *iostre
531534
}
532535
}()
533536

537+
// Add builder type and region to span attributes
538+
span.SetAttributes(
539+
attribute.String("builder.type", builderType),
540+
attribute.String("builder.region", builderRegion),
541+
)
542+
534543
metrics.Started(ctx, "image_push")
535544
sendImgPushMetrics := metrics.StartTiming(ctx, "image_push/duration")
536545

@@ -543,6 +552,9 @@ func pushToFly(ctx context.Context, docker *dockerclient.Client, streams *iostre
543552
return errors.Wrap(err, "error pushing image to registry")
544553
}
545554
defer pushResp.Close() // skipcq: GO-S2307
555+
556+
// Send enhanced metrics with builder type and region
557+
SendEnhancedImagePushMetrics(ctx, builderType, builderRegion)
546558
sendImgPushMetrics()
547559

548560
err = jsonmessage.DisplayJSONMessagesStream(pushResp, streams.ErrOut, streams.StderrFd(), streams.IsStderrTTY(), nil)
@@ -559,3 +571,12 @@ func pushToFly(ctx context.Context, docker *dockerclient.Client, streams *iostre
559571

560572
return nil
561573
}
574+
575+
// SendEnhancedImagePushMetrics sends enhanced metrics for image push operations
576+
// with builder type and region information
577+
func SendEnhancedImagePushMetrics(ctx context.Context, builderType, builderRegion string) {
578+
metrics.Send(ctx, "image_push/enhanced", map[string]interface{}{
579+
"builder_type": builderType,
580+
"builder_region": builderRegion,
581+
})
582+
}

internal/build/imgsrc/local_image_resolver.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
dockerclient "github.com/docker/docker/client"
1111
dockerparser "github.com/novln/docker-parser"
1212
"github.com/pkg/errors"
13+
"go.opentelemetry.io/otel/attribute"
14+
1315
"github.com/superfly/flyctl/internal/cmdfmt"
1416
"github.com/superfly/flyctl/internal/tracing"
1517
"github.com/superfly/flyctl/iostreams"
1618
"github.com/superfly/flyctl/terminal"
17-
"go.opentelemetry.io/otel/attribute"
1819
)
1920

2021
type localImageResolver struct{}
@@ -96,7 +97,9 @@ func (*localImageResolver) Run(ctx context.Context, dockerFactory *dockerClientF
9697

9798
cmdfmt.PrintBegin(streams.ErrOut, "Pushing image to fly")
9899

99-
if err := pushToFly(ctx, docker, streams, opts.Tag); err != nil {
100+
builderType, builderRegion := getBuilderInfo(ctx, dockerFactory)
101+
102+
if err := pushToFly(ctx, docker, streams, opts.Tag, builderType, builderRegion); err != nil {
100103
build.PushFinish()
101104
return nil, "", err
102105
}

internal/build/imgsrc/nixpacks_builder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/pkg/errors"
16+
1617
"github.com/superfly/flyctl/agent"
1718
"github.com/superfly/flyctl/flyctl"
1819
"github.com/superfly/flyctl/iostreams"
@@ -216,7 +217,9 @@ func (*nixpacksBuilder) Run(ctx context.Context, dockerFactory *dockerClientFact
216217
build.BuildFinish()
217218

218219
build.PushStart()
219-
if err := pushToFly(ctx, docker, streams, opts.Tag); err != nil {
220+
builderType, builderRegion := getBuilderInfo(ctx, dockerFactory)
221+
222+
if err := pushToFly(ctx, docker, streams, opts.Tag, builderType, builderRegion); err != nil {
220223
build.PushFinish()
221224
return nil, "", err
222225
}

0 commit comments

Comments
 (0)