Skip to content

Commit 82c8526

Browse files
committed
shim: add OTEL tracing plugin for trace context propagation
Register a host-side OTel tracing plugin in the shim that configures a TracerProvider with an OTLP HTTP exporter, enabling end-to-end trace context propagation across the shim's ttrpc calls. Signed-off-by: David Scott <dave@recoil.org>
1 parent f33a53e commit 82c8526

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

cmd/containerd-shim-nerdbox-v1/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
_ "github.com/containerd/nerdbox/plugins/shim/sandbox"
2727
_ "github.com/containerd/nerdbox/plugins/shim/streaming"
2828
_ "github.com/containerd/nerdbox/plugins/shim/task"
29+
_ "github.com/containerd/nerdbox/plugins/shim/tracing"
2930
_ "github.com/containerd/nerdbox/plugins/shim/transfer"
3031
_ "github.com/containerd/nerdbox/plugins/vm/libkrun"
3132
)

plugins/shim/tracing/plugin.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tracing
18+
19+
import (
20+
"context"
21+
"time"
22+
23+
"github.com/containerd/containerd/v2/pkg/shutdown"
24+
cplugins "github.com/containerd/containerd/v2/plugins"
25+
"github.com/containerd/log"
26+
"github.com/containerd/otelttrpc"
27+
"github.com/containerd/plugin"
28+
"github.com/containerd/plugin/registry"
29+
"github.com/containerd/ttrpc"
30+
"go.opentelemetry.io/otel"
31+
"go.opentelemetry.io/otel/propagation"
32+
sdktrace "go.opentelemetry.io/otel/sdk/trace"
33+
34+
"github.com/containerd/nerdbox/internal/tracing"
35+
"github.com/containerd/nerdbox/plugins"
36+
)
37+
38+
func init() {
39+
registry.Register(&plugin.Registration{
40+
Type: plugins.TTRPCPlugin,
41+
ID: "tracing",
42+
Requires: []plugin.Type{
43+
cplugins.InternalPlugin,
44+
},
45+
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
46+
ctx := ic.Context
47+
48+
otel.SetTextMapPropagator(propagation.TraceContext{})
49+
50+
exp := tracing.NewRelayExporter(ctx)
51+
if exp == nil {
52+
log.G(ctx).Debug("OTEL_EXPORTER_OTLP_ENDPOINT not set, shim tracing disabled")
53+
return &interceptor{}, nil
54+
}
55+
56+
tp := sdktrace.NewTracerProvider(
57+
sdktrace.WithBatcher(exp,
58+
sdktrace.WithBatchTimeout(100*time.Millisecond),
59+
),
60+
)
61+
otel.SetTracerProvider(tp)
62+
63+
ss, err := ic.GetByID(cplugins.InternalPlugin, "shutdown")
64+
if err != nil {
65+
return nil, err
66+
}
67+
ss.(shutdown.Service).RegisterCallback(func(ctx context.Context) error {
68+
return tp.Shutdown(ctx)
69+
})
70+
71+
return &interceptor{}, nil
72+
},
73+
})
74+
}
75+
76+
type interceptor struct{}
77+
78+
func (i *interceptor) UnaryServerInterceptor() ttrpc.UnaryServerInterceptor {
79+
return otelttrpc.UnaryServerInterceptor()
80+
}

0 commit comments

Comments
 (0)