Skip to content

Commit b8de211

Browse files
committed
renames; image pull count all and add registry/status as label
1 parent ad81ce9 commit b8de211

4 files changed

Lines changed: 41 additions & 39 deletions

File tree

pkg/cri/sbserver/image_pull.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,32 @@ import (
9494

9595
// PullImage pulls an image with authentication config.
9696
func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (*runtime.PullImageResponse, error) {
97-
inProgresImagePulling.Inc()
98-
defer inProgresImagePulling.Dec()
97+
inProgressImagePulls.Inc()
98+
defer inProgressImagePulls.Dec()
9999
startTime := time.Now()
100+
var domain string
101+
var err error
102+
defer func() {
103+
if err != nil {
104+
imagePulls.WithValues("failed", domain).Inc()
105+
} else {
106+
imagePulls.WithValues("succeed", domain).Inc()
107+
}
108+
}()
100109
imageRef := r.GetImage().GetImage()
101110
namedRef, err := distribution.ParseDockerRef(imageRef)
102111
if err != nil {
103-
imagePullingError.WithValues(imageRef).Inc()
104112
return nil, fmt.Errorf("failed to parse image reference %q: %w", imageRef, err)
105113
}
106114
ref := namedRef.String()
107115
if ref != imageRef {
108116
log.G(ctx).Debugf("PullImage using normalized image ref: %q", ref)
109117
}
118+
imageNamed, _ := distribution.ParseNamed(ref)
119+
domain = distribution.Domain(imageNamed)
110120

111121
imagePullProgressTimeout, err := time.ParseDuration(c.config.ImagePullProgressTimeout)
112122
if err != nil {
113-
imagePullingError.WithValues(imageRef).Inc()
114123
return nil, fmt.Errorf("failed to parse image_pull_progress_timeout %q: %w", c.config.ImagePullProgressTimeout, err)
115124
}
116125

@@ -136,7 +145,6 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
136145
defer pcancel()
137146
snapshotter, err := c.snapshotterFromPodSandboxConfig(ctx, ref, r.SandboxConfig)
138147
if err != nil {
139-
imagePullingError.WithValues(imageRef).Inc()
140148
return nil, err
141149
}
142150
log.G(ctx).Debugf("PullImage %q with snapshotter %s", ref, snapshotter)
@@ -170,13 +178,11 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
170178
image, err := c.client.Pull(pctx, ref, pullOpts...)
171179
pcancel()
172180
if err != nil {
173-
imagePullingError.WithValues(imageRef).Inc()
174181
return nil, fmt.Errorf("failed to pull and unpack image %q: %w", ref, err)
175182
}
176183

177184
configDesc, err := image.Config(ctx)
178185
if err != nil {
179-
imagePullingError.WithValues(imageRef).Inc()
180186
return nil, fmt.Errorf("get image config descriptor: %w", err)
181187
}
182188
imageID := configDesc.Digest.String()
@@ -187,25 +193,20 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
187193
continue
188194
}
189195
if err := c.createImageReference(ctx, r, image.Target()); err != nil {
190-
imagePullingError.WithValues(imageRef).Inc()
191196
return nil, fmt.Errorf("failed to create image reference %q: %w", r, err)
192197
}
193198
// Update image store to reflect the newest state in containerd.
194199
// No need to use `updateImage`, because the image reference must
195200
// have been managed by the cri plugin.
196201
if err := c.imageStore.Update(ctx, r); err != nil {
197-
imagePullingError.WithValues(imageRef).Inc()
198202
return nil, fmt.Errorf("failed to update image store %q: %w", r, err)
199203
}
200204
}
201205

202-
imageNamed, _ := distribution.ParseNamed(ref)
203-
domain := distribution.Domain(imageNamed)
204-
205206
size, _ := image.Size(ctx)
206207

207208
secondsPulling1MiB := time.Since(startTime).Seconds() / float64(size/1.0/1024/1024)
208-
imagePullThoughput.WithValues(domain).Update(time.Duration(secondsPulling1MiB) * time.Second)
209+
imagePullThroughput.WithValues(domain).Update(time.Duration(secondsPulling1MiB) * time.Second)
209210

210211
log.G(ctx).Debugf("Pulled image %q with image id %q, repo tag %q, repo digest %q, size %q in %v s", imageRef, imageID,
211212
repoTag, repoDigest, strconv.FormatInt(size, 10), time.Since(startTime).Seconds())

pkg/cri/sbserver/metrics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ var (
3535
containerStopTimer metrics.LabeledTimer
3636
containerStartTimer metrics.LabeledTimer
3737

38-
imagePullingError metrics.LabeledCounter
39-
inProgresImagePulling metrics.Gauge
38+
imagePulls metrics.LabeledCounter
39+
inProgressImagePulls metrics.Gauge
4040
// pull time / (image size / 1MBi)
41-
imagePullThoughput metrics.LabeledTimer
41+
imagePullThroughput metrics.LabeledTimer
4242
)
4343

4444
func init() {
@@ -59,9 +59,9 @@ func init() {
5959
containerStopTimer = ns.NewLabeledTimer("container_stop", "time to stop a container", "runtime")
6060
containerStartTimer = ns.NewLabeledTimer("container_start", "time to start a container", "runtime")
6161

62-
imagePullingError = ns.NewLabeledCounter("image_pulling_error", "error count of image pulling by image name", "image_name")
63-
inProgresImagePulling = ns.NewGauge("image_pulling_in_progress", "in progress pulls", metrics.Total)
64-
imagePullThoughput = ns.NewLabeledTimer("image_pulling_thoughtput", "image pulling duration for 1MiB", "registry")
62+
imagePulls = ns.NewLabeledCounter("image_pulling", "count of image pulling by registry", "status", "registry")
63+
inProgressImagePulls = ns.NewGauge("image_pulling_in_progress", "in progress pulls", metrics.Total)
64+
imagePullThroughput = ns.NewLabeledTimer("image_pulling_throughput", "image pulling duration for 1MiB", "registry")
6565

6666
metrics.Register(ns)
6767
}

pkg/cri/server/image_pull.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,33 @@ import (
9494

9595
// PullImage pulls an image with authentication config.
9696
func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (*runtime.PullImageResponse, error) {
97-
inProgresImagePulling.Inc()
98-
defer inProgresImagePulling.Dec()
97+
inProgressImagePulls.Inc()
98+
defer inProgressImagePulls.Dec()
99+
var domain string
100+
var err error
101+
defer func() {
102+
if err != nil {
103+
imagePulls.WithValues("failed", domain).Inc()
104+
} else {
105+
imagePulls.WithValues("succeed", domain).Inc()
106+
}
107+
}()
108+
99109
startTime := time.Now()
100110
imageRef := r.GetImage().GetImage()
101111
namedRef, err := distribution.ParseDockerRef(imageRef)
102112
if err != nil {
103-
imagePullingError.WithValues(imageRef).Inc()
104113
return nil, fmt.Errorf("failed to parse image reference %q: %w", imageRef, err)
105114
}
106115
ref := namedRef.String()
107116
if ref != imageRef {
108117
log.G(ctx).Debugf("PullImage using normalized image ref: %q", ref)
109118
}
119+
imageNamed, _ := distribution.ParseNamed(ref)
120+
domain = distribution.Domain(imageNamed)
110121

111122
imagePullProgressTimeout, err := time.ParseDuration(c.config.ImagePullProgressTimeout)
112123
if err != nil {
113-
imagePullingError.WithValues(imageRef).Inc()
114124
return nil, fmt.Errorf("failed to parse image_pull_progress_timeout %q: %w", c.config.ImagePullProgressTimeout, err)
115125
}
116126

@@ -136,7 +146,6 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
136146
defer pcancel()
137147
snapshotter, err := c.snapshotterFromPodSandboxConfig(ctx, ref, r.SandboxConfig)
138148
if err != nil {
139-
imagePullingError.WithValues(imageRef).Inc()
140149
return nil, err
141150
}
142151
log.G(ctx).Debugf("PullImage %q with snapshotter %s", ref, snapshotter)
@@ -170,13 +179,11 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
170179
image, err := c.client.Pull(pctx, ref, pullOpts...)
171180
pcancel()
172181
if err != nil {
173-
imagePullingError.WithValues(imageRef).Inc()
174182
return nil, fmt.Errorf("failed to pull and unpack image %q: %w", ref, err)
175183
}
176184

177185
configDesc, err := image.Config(ctx)
178186
if err != nil {
179-
imagePullingError.WithValues(imageRef).Inc()
180187
return nil, fmt.Errorf("get image config descriptor: %w", err)
181188
}
182189
imageID := configDesc.Digest.String()
@@ -187,25 +194,19 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
187194
continue
188195
}
189196
if err := c.createImageReference(ctx, r, image.Target()); err != nil {
190-
imagePullingError.WithValues(imageRef).Inc()
191197
return nil, fmt.Errorf("failed to create image reference %q: %w", r, err)
192198
}
193199
// Update image store to reflect the newest state in containerd.
194200
// No need to use `updateImage`, because the image reference must
195201
// have been managed by the cri plugin.
196202
if err := c.imageStore.Update(ctx, r); err != nil {
197-
imagePullingError.WithValues(imageRef).Inc()
198203
return nil, fmt.Errorf("failed to update image store %q: %w", r, err)
199204
}
200205
}
201-
202-
imageNamed, _ := distribution.ParseNamed(ref)
203-
domain := distribution.Domain(imageNamed)
204-
205206
size, _ := image.Size(ctx)
206207

207208
secondsPulling1MiB := time.Since(startTime).Seconds() / float64(size/1.0/1024/1024)
208-
imagePullThoughput.WithValues(domain).Update(time.Duration(secondsPulling1MiB) * time.Second)
209+
imagePullThroughput.WithValues(domain).Update(time.Duration(secondsPulling1MiB) * time.Second)
209210

210211
log.G(ctx).Debugf("Pulled image %q with image id %q, repo tag %q, repo digest %q, size %q in %v s", imageRef, imageID,
211212
repoTag, repoDigest, strconv.FormatInt(size, 10), time.Since(startTime).Seconds())

pkg/cri/server/metrics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ var (
3535
containerStopTimer metrics.LabeledTimer
3636
containerStartTimer metrics.LabeledTimer
3737

38-
imagePullingError metrics.LabeledCounter
39-
inProgresImagePulling metrics.Gauge
38+
imagePulls metrics.LabeledCounter
39+
inProgressImagePulls metrics.Gauge
4040
// pull time / (image size / 1MBi)
41-
imagePullThoughput metrics.LabeledTimer
41+
imagePullThroughput metrics.LabeledTimer
4242
)
4343

4444
func init() {
@@ -59,9 +59,9 @@ func init() {
5959
containerStopTimer = ns.NewLabeledTimer("container_stop", "time to stop a container", "runtime")
6060
containerStartTimer = ns.NewLabeledTimer("container_start", "time to start a container", "runtime")
6161

62-
imagePullingError = ns.NewLabeledCounter("image_pulling_error", "error count of image pulling by image name", "image_name")
63-
inProgresImagePulling = ns.NewGauge("image_pulling_in_progress", "in progress pulls", metrics.Total)
64-
imagePullThoughput = ns.NewLabeledTimer("image_pulling_thoughtput", "image pulling duration for 1MiB", "registry")
62+
imagePulls = ns.NewLabeledCounter("image_pulling", "count of image pulling by registry", "status", "registry")
63+
inProgressImagePulls = ns.NewGauge("image_pulling_in_progress", "in progress pulls", metrics.Total)
64+
imagePullThroughput = ns.NewLabeledTimer("image_pulling_throughput", "image pulling duration for 1MiB", "registry")
6565

6666
metrics.Register(ns)
6767
}

0 commit comments

Comments
 (0)