Add spans to CRI runtime service and related client methods

This adds otel spans to CRI service mainly targeting mutating apis which includes:
* Sandbox apis - RunPodSandbox, StopPodSandbox, RemovePodSandbox
* Container apis - CreateContainer, StartContainer, StopContainer, RemoveContainer
* Attach, Exec and Exec Sync
* Containerd client methods: container.go, client.go, process.go and task.go

Signed-off-by: Swagat Bora <sbora@amazon.com>
This commit is contained in:
Swagat Bora
2022-11-17 17:52:08 +00:00
parent 45d8917089
commit c0cdcb34f1
17 changed files with 287 additions and 13 deletions

View File

@@ -31,11 +31,16 @@ import (
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
"github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/containerd/v2/pkg/tracing"
"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"
bolt "go.etcd.io/bbolt"
)
const (
spanContainerPrefix = "metadata.containers"
)
type containerStore struct {
db *DB
}
@@ -116,6 +121,11 @@ func (s *containerStore) List(ctx context.Context, fs ...string) ([]containers.C
}
func (s *containerStore) Create(ctx context.Context, container containers.Container) (containers.Container, error) {
ctx, span := tracing.StartSpan(ctx,
tracing.Name(spanContainerPrefix, "Create"),
tracing.WithAttribute("container.id", container.ID),
)
defer span.End()
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return containers.Container{}, err
@@ -145,6 +155,9 @@ func (s *containerStore) Create(ctx context.Context, container containers.Contai
return fmt.Errorf("failed to write container %q: %w", container.ID, err)
}
span.SetAttributes(
tracing.Attribute("container.createdAt", container.CreatedAt.Format(time.RFC3339)),
)
return nil
}); err != nil {
return containers.Container{}, err
@@ -154,6 +167,11 @@ func (s *containerStore) Create(ctx context.Context, container containers.Contai
}
func (s *containerStore) Update(ctx context.Context, container containers.Container, fieldpaths ...string) (containers.Container, error) {
ctx, span := tracing.StartSpan(ctx,
tracing.Name(spanContainerPrefix, "Update"),
tracing.WithAttribute("container.id", container.ID),
)
defer span.End()
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return containers.Container{}, err
@@ -245,6 +263,10 @@ func (s *containerStore) Update(ctx context.Context, container containers.Contai
return fmt.Errorf("failed to write container %q: %w", container.ID, err)
}
span.SetAttributes(
tracing.Attribute("container.createdAt", updated.CreatedAt.Format(time.RFC3339)),
tracing.Attribute("container.updatedAt", updated.UpdatedAt.Format(time.RFC3339)),
)
return nil
}); err != nil {
return containers.Container{}, err
@@ -254,6 +276,12 @@ func (s *containerStore) Update(ctx context.Context, container containers.Contai
}
func (s *containerStore) Delete(ctx context.Context, id string) error {
ctx, span := tracing.StartSpan(ctx,
tracing.Name(spanContainerPrefix, "Delete"),
tracing.WithAttribute("container.id", id),
)
defer span.End()
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return err

View File

@@ -28,11 +28,16 @@ import (
"github.com/containerd/containerd/v2/pkg/filters"
"github.com/containerd/containerd/v2/pkg/identifiers"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/tracing"
"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"
"go.etcd.io/bbolt"
)
const (
spanSandboxPrefix = "metadata.sandbox"
)
type sandboxStore struct {
db *DB
}
@@ -46,6 +51,11 @@ func NewSandboxStore(db *DB) api.Store {
// Create a sandbox record in the store
func (s *sandboxStore) Create(ctx context.Context, sandbox api.Sandbox) (api.Sandbox, error) {
ctx, span := tracing.StartSpan(ctx,
tracing.Name(spanSandboxPrefix, "Create"),
tracing.WithAttribute("sandbox.id", sandbox.ID),
)
defer span.End()
ns, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return api.Sandbox{}, err
@@ -68,6 +78,9 @@ func (s *sandboxStore) Create(ctx context.Context, sandbox api.Sandbox) (api.San
return fmt.Errorf("write error: %w", err)
}
span.SetAttributes(
tracing.Attribute("sandbox.CreatedAt", sandbox.CreatedAt.Format(time.RFC3339)),
)
return nil
}); err != nil {
return api.Sandbox{}, err
@@ -78,6 +91,11 @@ func (s *sandboxStore) Create(ctx context.Context, sandbox api.Sandbox) (api.San
// Update the sandbox with the provided sandbox object and fields
func (s *sandboxStore) Update(ctx context.Context, sandbox api.Sandbox, fieldpaths ...string) (api.Sandbox, error) {
ctx, span := tracing.StartSpan(ctx,
tracing.Name(spanSandboxPrefix, "Update"),
tracing.WithAttribute("sandbox.id", sandbox.ID),
)
defer span.End()
ns, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return api.Sandbox{}, err
@@ -142,6 +160,10 @@ func (s *sandboxStore) Update(ctx context.Context, sandbox api.Sandbox, fieldpat
return err
}
span.SetAttributes(
tracing.Attribute("sandbox.CreatedAt", updated.CreatedAt.Format(time.RFC3339)),
tracing.Attribute("sandbox.UpdatedAt", updated.UpdatedAt.Format(time.RFC3339)),
)
ret = updated
return nil
}); err != nil {
@@ -227,6 +249,11 @@ func (s *sandboxStore) List(ctx context.Context, fields ...string) ([]api.Sandbo
// Delete a sandbox from metadata store using the id
func (s *sandboxStore) Delete(ctx context.Context, id string) error {
ctx, span := tracing.StartSpan(ctx,
tracing.Name(spanSandboxPrefix, "Delete"),
tracing.WithAttribute("sandbox.id", id),
)
defer span.End()
ns, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return err