Skip to content

Commit 03e0e4c

Browse files
author
Maksym Pavlenko
authored
Merge pull request containerd#10186 from cpuguy83/propagate_traces_to_shim
Propagate trace contexts to shims
2 parents 03db11c + 17d4a13 commit 03e0e4c

File tree

24 files changed

+2865
-5
lines changed

24 files changed

+2865
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//go:build shim_tracing
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package main
20+
21+
import _ "github.com/containerd/containerd/v2/pkg/tracing/plugin"

cmd/containerd-shim-runc-v2/manager/manager_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func newCommand(ctx context.Context, id, containerdAddress, containerdTTRPCAddre
104104
cmd := exec.Command(self, args...)
105105
cmd.Dir = cwd
106106
cmd.Env = append(os.Environ(), "GOMAXPROCS=4")
107+
cmd.Env = append(cmd.Env, "OTEL_SERVICE_NAME=containerd-shim-"+id)
107108
cmd.SysProcAttr = &syscall.SysProcAttr{
108109
Setpgid: true,
109110
}

cmd/containerd/server/server_linux.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
2727
"github.com/containerd/containerd/v2/pkg/sys"
2828
"github.com/containerd/log"
29+
"github.com/containerd/otelttrpc"
2930
"github.com/containerd/ttrpc"
3031
specs "github.com/opencontainers/runtime-spec/specs-go"
3132
)
@@ -66,5 +67,8 @@ func apply(ctx context.Context, config *srvconfig.Config) error {
6667
}
6768

6869
func newTTRPCServer() (*ttrpc.Server, error) {
69-
return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()))
70+
return ttrpc.NewServer(
71+
ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()),
72+
ttrpc.WithUnaryServerInterceptor(otelttrpc.UnaryServerInterceptor()),
73+
)
7074
}

cmd/containerd/server/server_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
23+
"github.com/containerd/otelttrpc"
2324
"github.com/containerd/ttrpc"
2425
)
2526

@@ -28,5 +29,7 @@ func apply(_ context.Context, _ *srvconfig.Config) error {
2829
}
2930

3031
func newTTRPCServer() (*ttrpc.Server, error) {
31-
return ttrpc.NewServer()
32+
return ttrpc.NewServer(
33+
ttrpc.WithUnaryServerInterceptor(otelttrpc.UnaryServerInterceptor()),
34+
)
3235
}

core/runtime/v2/shim.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"strings"
2929
"time"
3030

31+
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
3132
"google.golang.org/grpc"
3233
"google.golang.org/grpc/connectivity"
3334
"google.golang.org/grpc/credentials/insecure"
@@ -46,6 +47,7 @@ import (
4647
"github.com/containerd/containerd/v2/pkg/timeout"
4748
"github.com/containerd/errdefs"
4849
"github.com/containerd/log"
50+
"github.com/containerd/otelttrpc"
4951
"github.com/containerd/ttrpc"
5052
"github.com/containerd/typeurl/v2"
5153
)
@@ -273,10 +275,16 @@ func makeConnection(ctx context.Context, id string, params client.BootstrapParam
273275
}
274276
}()
275277

276-
return ttrpc.NewClient(conn, ttrpc.WithOnClose(onClose)), nil
278+
return ttrpc.NewClient(
279+
conn,
280+
ttrpc.WithOnClose(onClose),
281+
ttrpc.WithUnaryClientInterceptor(otelttrpc.UnaryClientInterceptor()),
282+
), nil
277283
case "grpc":
278284
gopts := []grpc.DialOption{
279285
grpc.WithTransportCredentials(insecure.NewCredentials()),
286+
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), //nolint:staticcheck // Ignore SA1019. Deprecation assumes use of [grpc.NewClient] but we are not using that here.
287+
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()), //nolint:staticcheck // Ignore SA1019. Deprecation assumes use of [grpc.NewClient] but we are not using that here.
280288
}
281289
return grpcDialContext(params.Address, onClose, gopts...)
282290
default:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/containerd/imgcrypt v1.2.0-rc1
2323
github.com/containerd/log v0.1.0
2424
github.com/containerd/nri v0.6.1
25+
github.com/containerd/otelttrpc v0.0.0-20240305015340-ea5083fda723
2526
github.com/containerd/platforms v0.2.1
2627
github.com/containerd/plugin v0.1.0
2728
github.com/containerd/ttrpc v1.2.5

go.sum

Lines changed: 1415 additions & 0 deletions
Large diffs are not rendered by default.

pkg/shim/publisher.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,20 @@ type item struct {
4343
count int
4444
}
4545

46+
type publisherConfig struct {
47+
ttrpcOpts []ttrpc.ClientOpts
48+
}
49+
50+
type PublisherOpts func(*publisherConfig)
51+
52+
func WithPublishTTRPCOpts(opts ...ttrpc.ClientOpts) PublisherOpts {
53+
return func(cfg *publisherConfig) {
54+
cfg.ttrpcOpts = append(cfg.ttrpcOpts, opts...)
55+
}
56+
}
57+
4658
// NewPublisher creates a new remote events publisher
47-
func NewPublisher(address string) (*RemoteEventsPublisher, error) {
59+
func NewPublisher(address string, opts ...PublisherOpts) (*RemoteEventsPublisher, error) {
4860
client, err := ttrpcutil.NewClient(address)
4961
if err != nil {
5062
return nil, err

pkg/shim/shim.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ 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"
4647
"github.com/containerd/plugin"
4748
"github.com/containerd/plugin/registry"
4849
"github.com/containerd/ttrpc"
@@ -248,7 +249,9 @@ func run(ctx context.Context, manager Manager, config Config) error {
248249
}
249250

250251
ttrpcAddress := os.Getenv(ttrpcAddressEnv)
251-
publisher, err := NewPublisher(ttrpcAddress)
252+
publisher, err := NewPublisher(ttrpcAddress, WithPublishTTRPCOpts(
253+
ttrpc.WithUnaryClientInterceptor(otelttrpc.UnaryClientInterceptor()),
254+
))
252255
if err != nil {
253256
return err
254257
}
@@ -398,6 +401,8 @@ func run(ctx context.Context, manager Manager, config Config) error {
398401
return fmt.Errorf("required that ttrpc service")
399402
}
400403

404+
ttrpcUnaryInterceptors = append(ttrpcUnaryInterceptors, otelttrpc.UnaryServerInterceptor())
405+
401406
unaryInterceptor := chainUnaryServerInterceptors(ttrpcUnaryInterceptors...)
402407
server, err := newServer(ttrpc.WithUnaryServerInterceptor(unaryInterceptor))
403408
if err != nil {

pkg/tracing/plugin/otlp.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ func newTracer(ctx context.Context, procs []trace.SpanProcessor) (io.Closer, err
203203
}
204204

205205
func warnTraceConfig(ic *plugin.InitContext) error {
206+
if ic.Config == nil {
207+
return nil
208+
}
206209
ctx := ic.Context
207210
cfg := ic.Config.(*TraceConfig)
208211
var warn bool
@@ -227,6 +230,9 @@ func warnTraceConfig(ic *plugin.InitContext) error {
227230
}
228231

229232
func warnOTLPConfig(ic *plugin.InitContext) error {
233+
if ic.Config == nil {
234+
return nil
235+
}
230236
ctx := ic.Context
231237
cfg := ic.Config.(*OTLPConfig)
232238
var warn bool

0 commit comments

Comments
 (0)