add SpanAttribute

Signed-off-by: Swagat Bora <sbora@amazon.com>
This commit is contained in:
Swagat Bora 2022-11-03 16:50:50 +00:00
parent 3b87d46ce2
commit ee64926a72
11 changed files with 114 additions and 96 deletions

View File

@ -36,7 +36,6 @@ import (
"github.com/containerd/imgcrypt" "github.com/containerd/imgcrypt"
"github.com/containerd/imgcrypt/images/encryption" "github.com/containerd/imgcrypt/images/encryption"
imagespec "github.com/opencontainers/image-spec/specs-go/v1" imagespec "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel/attribute"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
"github.com/containerd/containerd" "github.com/containerd/containerd"
@ -137,8 +136,8 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
} }
log.G(ctx).Debugf("PullImage %q with snapshotter %s", ref, snapshotter) log.G(ctx).Debugf("PullImage %q with snapshotter %s", ref, snapshotter)
span.SetAttributes( span.SetAttributes(
attribute.String("image.ref", ref), tracing.SpanAttribute("image.ref", ref),
attribute.String("snapshotter.name", snapshotter), tracing.SpanAttribute("snapshotter.name", snapshotter),
) )
pullOpts := []containerd.RemoteOpt{ pullOpts := []containerd.RemoteOpt{

View File

@ -23,7 +23,6 @@ import (
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/tracing" "github.com/containerd/containerd/tracing"
"go.opentelemetry.io/otel/attribute"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
) )
@ -45,7 +44,7 @@ func (c *criService) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequ
} }
return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err) return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err)
} }
span.SetAttributes(attribute.String("image.id", image.ID)) span.SetAttributes(tracing.SpanAttribute("image.id", image.ID))
// Remove all image references. // Remove all image references.
for i, ref := range image.References { for i, ref := range image.References {
var opts []images.DeleteOpt var opts []images.DeleteOpt

View File

@ -25,7 +25,6 @@ import (
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
imagestore "github.com/containerd/containerd/pkg/cri/store/image" imagestore "github.com/containerd/containerd/pkg/cri/store/image"
"github.com/containerd/containerd/tracing" "github.com/containerd/containerd/tracing"
"go.opentelemetry.io/otel/attribute"
imagespec "github.com/opencontainers/image-spec/specs-go/v1" imagespec "github.com/opencontainers/image-spec/specs-go/v1"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@ -45,7 +44,7 @@ func (c *criService) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ
} }
return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err) return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err)
} }
span.SetAttributes(attribute.String("image.id", image.ID)) span.SetAttributes(tracing.SpanAttribute("image.id", image.ID))
// TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot // TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot
// doesn't exist? // doesn't exist?

View File

@ -36,7 +36,6 @@ import (
"github.com/containerd/imgcrypt" "github.com/containerd/imgcrypt"
"github.com/containerd/imgcrypt/images/encryption" "github.com/containerd/imgcrypt/images/encryption"
imagespec "github.com/opencontainers/image-spec/specs-go/v1" imagespec "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel/attribute"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
"github.com/containerd/containerd" "github.com/containerd/containerd"
@ -137,8 +136,8 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
} }
log.G(ctx).Debugf("PullImage %q with snapshotter %s", ref, snapshotter) log.G(ctx).Debugf("PullImage %q with snapshotter %s", ref, snapshotter)
span.SetAttributes( span.SetAttributes(
attribute.String("image.ref", ref), tracing.SpanAttribute("image.ref", ref),
attribute.String("snapshotter.name", snapshotter), tracing.SpanAttribute("snapshotter.name", snapshotter),
) )
pullOpts := []containerd.RemoteOpt{ pullOpts := []containerd.RemoteOpt{
containerd.WithSchema1Conversion, //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility. containerd.WithSchema1Conversion, //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.

View File

@ -23,7 +23,6 @@ import (
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/tracing" "github.com/containerd/containerd/tracing"
"go.opentelemetry.io/otel/attribute"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
) )
@ -45,7 +44,7 @@ func (c *criService) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequ
} }
return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err) return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err)
} }
span.SetAttributes(attribute.String("image.id", image.ID)) span.SetAttributes(tracing.SpanAttribute("image.id", image.ID))
// Remove all image references. // Remove all image references.
for i, ref := range image.References { for i, ref := range image.References {
var opts []images.DeleteOpt var opts []images.DeleteOpt

View File

@ -25,7 +25,6 @@ import (
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
imagestore "github.com/containerd/containerd/pkg/cri/store/image" imagestore "github.com/containerd/containerd/pkg/cri/store/image"
"github.com/containerd/containerd/tracing" "github.com/containerd/containerd/tracing"
"go.opentelemetry.io/otel/attribute"
imagespec "github.com/opencontainers/image-spec/specs-go/v1" imagespec "github.com/opencontainers/image-spec/specs-go/v1"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@ -45,7 +44,7 @@ func (c *criService) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ
} }
return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err) return nil, fmt.Errorf("can not resolve %q locally: %w", r.GetImage().GetImage(), err)
} }
span.SetAttributes(attribute.String("image.id", image.ID)) span.SetAttributes(tracing.SpanAttribute("image.id", image.ID))
// TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot // TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot
// doesn't exist? // doesn't exist?

View File

@ -42,7 +42,6 @@ import (
"github.com/opencontainers/image-spec/identity" "github.com/opencontainers/image-spec/identity"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore" "golang.org/x/sync/semaphore"
) )
@ -166,8 +165,8 @@ func (u *Unpacker) Unpack(h images.Handler) images.Handler {
ctx, span := tracing.StartSpan(ctx, "pkg.unpack.unpacker.UnpackHandler") ctx, span := tracing.StartSpan(ctx, "pkg.unpack.unpacker.UnpackHandler")
defer span.End() defer span.End()
span.SetAttributes( span.SetAttributes(
attribute.String("descriptor.media.type", desc.MediaType), tracing.SpanAttribute("descriptor.media.type", desc.MediaType),
attribute.String("descriptor.digest", desc.Digest.String())) tracing.SpanAttribute("descriptor.digest", desc.Digest.String()))
unlock, err := u.lockBlobDescriptor(ctx, desc) unlock, err := u.lockBlobDescriptor(ctx, desc)
if err != nil { if err != nil {
return nil, err return nil, err
@ -186,7 +185,7 @@ func (u *Unpacker) Unpack(h images.Handler) images.Handler {
// the config // the config
for i, child := range children { for i, child := range children {
span.SetAttributes( span.SetAttributes(
attribute.StringSlice("descriptor.child."+strconv.Itoa(i), []string{child.MediaType, child.Digest.String()}), tracing.SpanAttribute("descriptor.child."+strconv.Itoa(i), []string{child.MediaType, child.Digest.String()}),
) )
if images.IsLayerType(child.MediaType) { if images.IsLayerType(child.MediaType) {
manifestLayers = append(manifestLayers, child) manifestLayers = append(manifestLayers, child)
@ -412,9 +411,9 @@ func (u *Unpacker) unpack(
for i, desc := range layers { for i, desc := range layers {
_, layerSpan := tracing.StartSpan(ctx, "pkg.unpack.unpacker.unpackLayer") _, layerSpan := tracing.StartSpan(ctx, "pkg.unpack.unpacker.unpackLayer")
layerSpan.SetAttributes( layerSpan.SetAttributes(
attribute.String("layer.media.type", desc.MediaType), tracing.SpanAttribute("layer.media.type", desc.MediaType),
attribute.Int64("layer.media.size", desc.Size), tracing.SpanAttribute("layer.media.size", desc.Size),
attribute.String("layer.media.digest", desc.Digest.String()), tracing.SpanAttribute("layer.media.digest", desc.Digest.String()),
) )
if err := doUnpackFn(i, desc); err != nil { if err := doUnpackFn(i, desc); err != nil {
layerSpan.RecordError(err) layerSpan.RecordError(err)
@ -448,9 +447,9 @@ func (u *Unpacker) fetch(ctx context.Context, h images.Handler, layers []ocispec
for i, desc := range layers { for i, desc := range layers {
ctx2, layerSpan := tracing.StartSpan(ctx2, "pkg.unpack.unpacker.fetchLayer") ctx2, layerSpan := tracing.StartSpan(ctx2, "pkg.unpack.unpacker.fetchLayer")
layerSpan.SetAttributes( layerSpan.SetAttributes(
attribute.String("layer.media.type", desc.MediaType), tracing.SpanAttribute("layer.media.type", desc.MediaType),
attribute.Int64("layer.media.size", desc.Size), tracing.SpanAttribute("layer.media.size", desc.Size),
attribute.String("layer.media.digest", desc.Digest.String()), tracing.SpanAttribute("layer.media.digest", desc.Digest.String()),
) )
desc := desc desc := desc
i := i i := i

15
pull.go
View File

@ -30,7 +30,6 @@ import (
"github.com/containerd/containerd/remotes/docker/schema1" //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility. "github.com/containerd/containerd/remotes/docker/schema1" //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
"github.com/containerd/containerd/tracing" "github.com/containerd/containerd/tracing"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/sync/semaphore" "golang.org/x/sync/semaphore"
) )
@ -64,11 +63,11 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
} }
span.SetAttributes( span.SetAttributes(
attribute.String("image.ref", ref), tracing.SpanAttribute("image.ref", ref),
attribute.Bool("unpack", pullCtx.Unpack), tracing.SpanAttribute("unpack", pullCtx.Unpack),
attribute.Int("max.concurrent.downloads", pullCtx.MaxConcurrentDownloads), tracing.SpanAttribute("max.concurrent.downloads", pullCtx.MaxConcurrentDownloads),
attribute.Int("max.concurrent.upload.layers", pullCtx.MaxConcurrentUploadedLayers), tracing.SpanAttribute("max.concurrent.upload.layers", pullCtx.MaxConcurrentUploadedLayers),
attribute.Int("platforms.count", len(pullCtx.Platforms)), tracing.SpanAttribute("platforms.count", len(pullCtx.Platforms)),
) )
ctx, done, err := c.WithLease(ctx) ctx, done, err := c.WithLease(ctx)
@ -84,7 +83,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to resolve snapshotter: %w", err) return nil, fmt.Errorf("unable to resolve snapshotter: %w", err)
} }
span.SetAttributes(attribute.String("snapshotter.name", snapshotterName)) span.SetAttributes(tracing.SpanAttribute("snapshotter.name", snapshotterName))
var uconfig UnpackConfig var uconfig UnpackConfig
for _, opt := range pullCtx.UnpackOpts { for _, opt := range pullCtx.UnpackOpts {
if err := opt(ctx, &uconfig); err != nil { if err := opt(ctx, &uconfig); err != nil {
@ -157,7 +156,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
} }
i := NewImageWithPlatform(c, img, pullCtx.PlatformMatcher) i := NewImageWithPlatform(c, img, pullCtx.PlatformMatcher)
span.SetAttributes(attribute.String("image.ref", i.Name())) span.SetAttributes(tracing.SpanAttribute("image.ref", i.Name()))
if unpacker != nil && ur.Unpacks == 0 { if unpacker != nil && ur.Unpacks == 0 {
// Unpack was tried previously but nothing was unpacked // Unpack was tried previously but nothing was unpacked

85
tracing/helpers.go Normal file
View File

@ -0,0 +1,85 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tracing
import (
"encoding/json"
"fmt"
"go.opentelemetry.io/otel/attribute"
)
func any(k string, v interface{}) attribute.KeyValue {
if v == nil {
return attribute.String(k, "<nil>")
}
switch typed := v.(type) {
case bool:
return attribute.Bool(k, typed)
case []bool:
return attribute.BoolSlice(k, typed)
case int:
return attribute.Int(k, typed)
case []int:
return attribute.IntSlice(k, typed)
case int8:
return attribute.Int(k, int(typed))
case []int8:
ls := make([]int, 0, len(typed))
for _, i := range typed {
ls = append(ls, int(i))
}
return attribute.IntSlice(k, ls)
case int16:
return attribute.Int(k, int(typed))
case []int16:
ls := make([]int, 0, len(typed))
for _, i := range typed {
ls = append(ls, int(i))
}
return attribute.IntSlice(k, ls)
case int32:
return attribute.Int64(k, int64(typed))
case []int32:
ls := make([]int64, 0, len(typed))
for _, i := range typed {
ls = append(ls, int64(i))
}
return attribute.Int64Slice(k, ls)
case int64:
return attribute.Int64(k, typed)
case []int64:
return attribute.Int64Slice(k, typed)
case float64:
return attribute.Float64(k, typed)
case []float64:
return attribute.Float64Slice(k, typed)
case string:
return attribute.String(k, typed)
case []string:
return attribute.StringSlice(k, typed)
}
if stringer, ok := v.(fmt.Stringer); ok {
return attribute.String(k, stringer.String())
}
if b, err := json.Marshal(v); b != nil && err == nil {
return attribute.String(k, string(b))
}
return attribute.String(k, fmt.Sprintf("%v", v))
}

View File

@ -17,9 +17,6 @@
package tracing package tracing
import ( import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
@ -67,64 +64,3 @@ func logrusDataToAttrs(data logrus.Fields) []attribute.KeyValue {
} }
return attrs return attrs
} }
func any(k string, v interface{}) attribute.KeyValue {
if v == nil {
return attribute.String(k, "<nil>")
}
switch typed := v.(type) {
case bool:
return attribute.Bool(k, typed)
case []bool:
return attribute.BoolSlice(k, typed)
case int:
return attribute.Int(k, typed)
case []int:
return attribute.IntSlice(k, typed)
case int8:
return attribute.Int(k, int(typed))
case []int8:
ls := make([]int, 0, len(typed))
for _, i := range typed {
ls = append(ls, int(i))
}
return attribute.IntSlice(k, ls)
case int16:
return attribute.Int(k, int(typed))
case []int16:
ls := make([]int, 0, len(typed))
for _, i := range typed {
ls = append(ls, int(i))
}
return attribute.IntSlice(k, ls)
case int32:
return attribute.Int64(k, int64(typed))
case []int32:
ls := make([]int64, 0, len(typed))
for _, i := range typed {
ls = append(ls, int64(i))
}
return attribute.Int64Slice(k, ls)
case int64:
return attribute.Int64(k, typed)
case []int64:
return attribute.Int64Slice(k, typed)
case float64:
return attribute.Float64(k, typed)
case []float64:
return attribute.Float64Slice(k, typed)
case string:
return attribute.String(k, typed)
case []string:
return attribute.StringSlice(k, typed)
}
if stringer, ok := v.(fmt.Stringer); ok {
return attribute.String(k, stringer.String())
}
if b, err := json.Marshal(v); b != nil && err == nil {
return attribute.String(k, string(b))
}
return attribute.String(k, fmt.Sprintf("%v", v))
}

View File

@ -20,6 +20,7 @@ import (
"context" "context"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
@ -52,3 +53,7 @@ func SetSpanStatus(span trace.Span, err error) {
span.SetStatus(codes.Ok, "") span.SetStatus(codes.Ok, "")
} }
} }
func SpanAttribute(k string, v interface{}) attribute.KeyValue {
return any(k, v)
}