Skip to content

Commit 6dd3055

Browse files
authored
Cleanup tracing and propagate traceparent (#16)
Traceparent and baggage get propagated to overlaybd-tcmu via the image config file.
1 parent 7925902 commit 6dd3055

File tree

6 files changed

+104
-37
lines changed

6 files changed

+104
-37
lines changed

pkg/snapshot/overlay.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import (
3131

3232
"github.com/containerd/accelerated-container-image/pkg/label"
3333
"github.com/containerd/accelerated-container-image/pkg/snapshot/diskquota"
34+
"github.com/containerd/accelerated-container-image/pkg/tracing"
35+
"go.opentelemetry.io/otel/attribute"
36+
"go.opentelemetry.io/otel/trace"
3437

3538
mylog "github.com/containerd/accelerated-container-image/internal/log"
3639
"github.com/containerd/accelerated-container-image/pkg/metrics"
@@ -448,7 +451,6 @@ func (o *snapshotter) isPrepareRootfs(info snapshots.Info) bool {
448451
}
449452

450453
func (o *snapshotter) createMountPoint(ctx context.Context, kind snapshots.Kind, key string, parent string, opts ...snapshots.Opt) (_ []mount.Mount, retErr error) {
451-
452454
ctx, t, err := o.ms.TransactionContext(ctx, true)
453455
if err != nil {
454456
return nil, err
@@ -550,6 +552,7 @@ func (o *snapshotter) createMountPoint(ctx context.Context, kind snapshots.Kind,
550552
if err != nil {
551553
return nil, err
552554
}
555+
trace.SpanFromContext(ctx).AddEvent("identifyStorageType", trace.WithAttributes(attribute.Int("stype", int(stype))))
553556

554557
// Download blob
555558
downloadBlob := info.Labels[label.DownloadRemoteBlob]
@@ -994,6 +997,12 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
994997
}
995998

996999
func (o *snapshotter) commit(ctx context.Context, name, key string, opts ...snapshots.Opt) (string, snapshots.Info, error) {
1000+
ctx, span := tracing.GetDefaultTracer().Start(ctx, "snapshotter.commit", trace.WithAttributes(
1001+
attribute.String("snapshot_name", name),
1002+
attribute.String("key", key),
1003+
))
1004+
defer span.End()
1005+
9971006
id, _, _, err := storage.GetInfo(ctx, key)
9981007
if err != nil {
9991008
return "", snapshots.Info{}, err
@@ -1380,6 +1389,12 @@ func (o *snapshotter) getDiskQuotaSize(info *snapshots.Info) string {
13801389
}
13811390

13821391
func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ string, _ snapshots.Info, err error) {
1392+
ctx, span := tracing.GetDefaultTracer().Start(ctx, "snapshotter.createSnapshot", trace.WithAttributes(
1393+
attribute.String("kind", kind.String()),
1394+
attribute.String("parent", parent),
1395+
))
1396+
defer span.End()
1397+
13831398
var td, path string
13841399
defer func() {
13851400
if err != nil {

pkg/snapshot/storage.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"time"
3434

3535
sn "github.com/containerd/accelerated-container-image/pkg/types"
36+
"go.opentelemetry.io/otel"
37+
"go.opentelemetry.io/otel/propagation"
3638

3739
"github.com/containerd/accelerated-container-image/pkg/label"
3840
"github.com/containerd/accelerated-container-image/pkg/utils"
@@ -503,9 +505,12 @@ func (o *snapshotter) constructOverlayBDSpec(ctx context.Context, key string, wr
503505
return errors.Wrapf(err, "failed to identify storage of snapshot %s", key)
504506
}
505507

508+
var carrier = propagation.MapCarrier{}
509+
otel.GetTextMapPropagator().Inject(ctx, carrier)
506510
configJSON := sn.OverlayBDBSConfig{
507-
Lowers: []sn.OverlayBDBSConfigLower{},
508-
ResultFile: o.overlaybdInitDebuglogPath(id),
511+
Lowers: []sn.OverlayBDBSConfigLower{},
512+
ResultFile: o.overlaybdInitDebuglogPath(id),
513+
TraceContext: carrier,
509514
}
510515

511516
// load the parent's config and reuse the lowerdir

pkg/tracing/snapshotter.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ import (
2121

2222
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
2323
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
24-
"go.opentelemetry.io/otel"
2524
"go.opentelemetry.io/otel/attribute"
2625
"go.opentelemetry.io/otel/trace"
2726
)
2827

29-
const tracerName = "accelerated-container-image/snapshotter"
30-
3128
// TracingSnapshotter wraps a snapshotter service with OpenTelemetry tracing
3229
type TracingSnapshotter struct {
3330
server snapshotsapi.SnapshotsServer
@@ -45,7 +42,7 @@ func WithTracing(server snapshotsapi.SnapshotsServer) snapshotsapi.SnapshotsServ
4542
}
4643

4744
func (s *TracingSnapshotter) Prepare(ctx context.Context, pr *snapshotsapi.PrepareSnapshotRequest) (*snapshotsapi.PrepareSnapshotResponse, error) {
48-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Prepare", trace.WithAttributes(
45+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.Prepare", trace.WithAttributes(
4946
attribute.String("key", pr.Key),
5047
attribute.String("parent", pr.Parent),
5148
))
@@ -59,7 +56,7 @@ func (s *TracingSnapshotter) Prepare(ctx context.Context, pr *snapshotsapi.Prepa
5956
}
6057

6158
func (s *TracingSnapshotter) View(ctx context.Context, pr *snapshotsapi.ViewSnapshotRequest) (*snapshotsapi.ViewSnapshotResponse, error) {
62-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.View", trace.WithAttributes(
59+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.View", trace.WithAttributes(
6360
attribute.String("key", pr.Key),
6461
attribute.String("parent", pr.Parent),
6562
))
@@ -73,7 +70,7 @@ func (s *TracingSnapshotter) View(ctx context.Context, pr *snapshotsapi.ViewSnap
7370
}
7471

7572
func (s *TracingSnapshotter) Mounts(ctx context.Context, mr *snapshotsapi.MountsRequest) (*snapshotsapi.MountsResponse, error) {
76-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Mounts", trace.WithAttributes(
73+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.Mounts", trace.WithAttributes(
7774
attribute.String("key", mr.Key),
7875
))
7976
defer span.End()
@@ -86,7 +83,7 @@ func (s *TracingSnapshotter) Mounts(ctx context.Context, mr *snapshotsapi.Mounts
8683
}
8784

8885
func (s *TracingSnapshotter) Commit(ctx context.Context, cr *snapshotsapi.CommitSnapshotRequest) (*ptypes.Empty, error) {
89-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Commit", trace.WithAttributes(
86+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.Commit", trace.WithAttributes(
9087
attribute.String("name", cr.Name),
9188
attribute.String("key", cr.Key),
9289
))
@@ -100,7 +97,7 @@ func (s *TracingSnapshotter) Commit(ctx context.Context, cr *snapshotsapi.Commit
10097
}
10198

10299
func (s *TracingSnapshotter) Remove(ctx context.Context, rr *snapshotsapi.RemoveSnapshotRequest) (*ptypes.Empty, error) {
103-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Remove", trace.WithAttributes(
100+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.Remove", trace.WithAttributes(
104101
attribute.String("key", rr.Key),
105102
))
106103
defer span.End()
@@ -113,20 +110,12 @@ func (s *TracingSnapshotter) Remove(ctx context.Context, rr *snapshotsapi.Remove
113110
}
114111

115112
func (s *TracingSnapshotter) Stat(ctx context.Context, sr *snapshotsapi.StatSnapshotRequest) (*snapshotsapi.StatSnapshotResponse, error) {
116-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Stat", trace.WithAttributes(
117-
attribute.String("key", sr.Key),
118-
))
119-
defer span.End()
120-
121113
resp, err := s.server.Stat(ctx, sr)
122-
if err != nil {
123-
span.RecordError(err)
124-
}
125114
return resp, err
126115
}
127116

128117
func (s *TracingSnapshotter) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotRequest) (*snapshotsapi.UpdateSnapshotResponse, error) {
129-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Update", trace.WithAttributes(
118+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.Update", trace.WithAttributes(
130119
attribute.String("name", sr.Info.Name),
131120
))
132121
defer span.End()
@@ -139,7 +128,7 @@ func (s *TracingSnapshotter) Update(ctx context.Context, sr *snapshotsapi.Update
139128
}
140129

141130
func (s *TracingSnapshotter) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Snapshots_ListServer) error {
142-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ss.Context(), "snapshotter.List")
131+
ctx, span := GetDefaultTracer().Start(ss.Context(), "snapshotter.List")
143132
defer span.End()
144133

145134
err := s.server.List(sr, &tracingListServer{
@@ -162,26 +151,12 @@ func (t *tracingListServer) Context() context.Context {
162151
}
163152

164153
func (s *TracingSnapshotter) Usage(ctx context.Context, ur *snapshotsapi.UsageRequest) (*snapshotsapi.UsageResponse, error) {
165-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Usage", trace.WithAttributes(
166-
attribute.String("key", ur.Key),
167-
))
168-
defer span.End()
169-
170154
resp, err := s.server.Usage(ctx, ur)
171-
if err != nil {
172-
span.RecordError(err)
173-
}
174-
if resp != nil {
175-
span.SetAttributes(
176-
attribute.Int64("inodes", resp.Inodes),
177-
attribute.Int64("size", resp.Size),
178-
)
179-
}
180155
return resp, err
181156
}
182157

183158
func (s *TracingSnapshotter) Cleanup(ctx context.Context, cr *snapshotsapi.CleanupRequest) (*ptypes.Empty, error) {
184-
ctx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "snapshotter.Cleanup")
159+
ctx, span := GetDefaultTracer().Start(ctx, "snapshotter.Cleanup")
185160
defer span.End()
186161

187162
resp, err := s.server.Cleanup(ctx, cr)

pkg/tracing/tracing.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313
"go.opentelemetry.io/otel/sdk/resource"
1414
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1515
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
16+
"go.opentelemetry.io/otel/trace"
1617
)
1718

19+
const tracerName = "accelerated-container-image/snapshotter"
20+
1821
var (
1922
serviceName = os.Getenv("OTEL_SERVICE_NAME")
2023
)
@@ -62,3 +65,7 @@ func InitTracer(ctx context.Context) (func(context.Context) error, error) {
6265
// Return shutdown function
6366
return tp.Shutdown, nil
6467
}
68+
69+
func GetDefaultTracer() trace.Tracer {
70+
return otel.GetTracerProvider().Tracer(tracerName)
71+
}

pkg/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type OverlayBDBSConfig struct {
2424
ResultFile string `json:"resultFile"`
2525
AccelerationLayer bool `json:"accelerationLayer,omitempty"`
2626
RecordTracePath string `json:"recordTracePath,omitempty"`
27+
TraceContext map[string]string `json:"traceContext,omitempty"`
2728
}
2829

2930
// OverlayBDBSConfigLower

0 commit comments

Comments
 (0)