Skip to content

Commit b2681df

Browse files
committed
shim: Move ttrpc interceptors to plugins
This makes it so we don't need to import otelttrpc unless the shim is compiled with the `shim_tracing` build tag. This way otel is no longer compiled into the binary at all unless `shim_tracing` is set. Signed-off-by: Brian Goff <[email protected]>
1 parent 6ffdabf commit b2681df

File tree

3 files changed

+66
-18
lines changed

3 files changed

+66
-18
lines changed

integration/failpoint/cmd/containerd-shim-runc-fp-v1/plugin_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func init() {
7474
}
7575

7676
var (
77-
_ = shim.TTRPCServerOptioner(&taskServiceWithFp{})
77+
_ = shim.TTRPCServerUnaryOptioner(&taskServiceWithFp{})
7878
)
7979

8080
type taskServiceWithFp struct {
@@ -87,7 +87,7 @@ func (s *taskServiceWithFp) RegisterTTRPC(server *ttrpc.Server) error {
8787
return nil
8888
}
8989

90-
func (s *taskServiceWithFp) UnaryInterceptor() ttrpc.UnaryServerInterceptor {
90+
func (s *taskServiceWithFp) UnaryServerInterceptor() ttrpc.UnaryServerInterceptor {
9191
return func(ctx context.Context, unmarshal ttrpc.Unmarshaler, info *ttrpc.UnaryServerInfo, method ttrpc.Method) (interface{}, error) {
9292
methodName := filepath.Base(info.FullMethod)
9393
if fp, ok := s.fps[methodName]; ok {

pkg/shim/shim.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"github.com/containerd/containerd/v2/plugins"
4444
"github.com/containerd/containerd/v2/version"
4545
"github.com/containerd/log"
46-
"github.com/containerd/otelttrpc"
4746
"github.com/containerd/plugin"
4847
"github.com/containerd/plugin/registry"
4948
"github.com/containerd/ttrpc"
@@ -112,10 +111,12 @@ type TTRPCService interface {
112111
RegisterTTRPC(*ttrpc.Server) error
113112
}
114113

115-
type TTRPCServerOptioner interface {
116-
TTRPCService
114+
type TTRPCServerUnaryOptioner interface {
115+
UnaryServerInterceptor() ttrpc.UnaryServerInterceptor
116+
}
117117

118-
UnaryInterceptor() ttrpc.UnaryServerInterceptor
118+
type TTRPCClientUnaryOptioner interface {
119+
UnaryClientInterceptor() ttrpc.UnaryClientInterceptor
119120
}
120121

121122
var (
@@ -249,13 +250,6 @@ func run(ctx context.Context, manager Manager, config Config) error {
249250
}
250251

251252
ttrpcAddress := os.Getenv(ttrpcAddressEnv)
252-
publisher, err := NewPublisher(ttrpcAddress, WithPublishTTRPCOpts(
253-
ttrpc.WithUnaryClientInterceptor(otelttrpc.UnaryClientInterceptor()),
254-
))
255-
if err != nil {
256-
return err
257-
}
258-
defer publisher.Close()
259253

260254
ctx = namespaces.WithNamespace(ctx, namespaceFlag)
261255
ctx = context.WithValue(ctx, OptsKey{}, Opts{BundlePath: bundlePath, Debug: debugFlag})
@@ -333,7 +327,15 @@ func run(ctx context.Context, manager Manager, config Config) error {
333327
Type: plugins.EventPlugin,
334328
ID: "publisher",
335329
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
336-
return publisher, nil
330+
return NewPublisher(ttrpcAddress, func(cfg *publisherConfig) {
331+
p, _ := ic.GetByID(plugins.TTRPCPlugin, "otelttrpc")
332+
if p == nil {
333+
return
334+
}
335+
336+
opts := ttrpc.WithUnaryClientInterceptor(p.(TTRPCClientUnaryOptioner).UnaryClientInterceptor())
337+
WithPublishTTRPCOpts(opts)(cfg)
338+
})
337339
},
338340
})
339341

@@ -389,20 +391,19 @@ func run(ctx context.Context, manager Manager, config Config) error {
389391
if src, ok := instance.(TTRPCService); ok {
390392
log.G(ctx).WithField("id", pID).Debug("registering ttrpc service")
391393
ttrpcServices = append(ttrpcServices, src)
394+
}
392395

396+
if src, ok := instance.(TTRPCServerUnaryOptioner); ok {
397+
ttrpcUnaryInterceptors = append(ttrpcUnaryInterceptors, src.UnaryServerInterceptor())
393398
}
394399

395-
if src, ok := instance.(TTRPCServerOptioner); ok {
396-
ttrpcUnaryInterceptors = append(ttrpcUnaryInterceptors, src.UnaryInterceptor())
397400
}
398401
}
399402

400403
if len(ttrpcServices) == 0 {
401404
return fmt.Errorf("required that ttrpc service")
402405
}
403406

404-
ttrpcUnaryInterceptors = append(ttrpcUnaryInterceptors, otelttrpc.UnaryServerInterceptor())
405-
406407
unaryInterceptor := chainUnaryServerInterceptors(ttrpcUnaryInterceptors...)
407408
server, err := newServer(ttrpc.WithUnaryServerInterceptor(unaryInterceptor))
408409
if err != nil {

pkg/tracing/plugin/ttrpc.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 plugin
18+
19+
import (
20+
"github.com/containerd/containerd/v2/plugins"
21+
"github.com/containerd/otelttrpc"
22+
"github.com/containerd/plugin"
23+
"github.com/containerd/plugin/registry"
24+
"github.com/containerd/ttrpc"
25+
)
26+
27+
func init() {
28+
const pluginName = "otelttrpc"
29+
30+
registry.Register(&plugin.Registration{
31+
ID: pluginName,
32+
Type: plugins.TTRPCPlugin,
33+
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
34+
return otelttrpcopts{}, nil
35+
},
36+
})
37+
}
38+
39+
type otelttrpcopts struct{}
40+
41+
func (otelttrpcopts) UnaryServerInterceptor() ttrpc.UnaryServerInterceptor {
42+
return otelttrpc.UnaryServerInterceptor()
43+
}
44+
45+
func (otelttrpcopts) UnaryClientInterceptor() ttrpc.UnaryClientInterceptor {
46+
return otelttrpc.UnaryClientInterceptor()
47+
}

0 commit comments

Comments
 (0)