@@ -28,6 +28,10 @@ import (
2828 "github.com/containernetworking/cni/pkg/types"
2929 types100 "github.com/containernetworking/cni/pkg/types/100"
3030 "github.com/containernetworking/cni/pkg/version"
31+ "go.opentelemetry.io/otel"
32+ "go.opentelemetry.io/otel/attribute"
33+ "go.opentelemetry.io/otel/codes"
34+ "go.opentelemetry.io/otel/trace"
3135)
3236
3337type CNI interface {
@@ -86,6 +90,10 @@ type libcni struct {
8690 RWMutex
8791}
8892
93+ func tracer () trace.Tracer {
94+ return otel .Tracer ("github.com/containerd/go-cni" )
95+ }
96+
8997func defaultCNIConfig () * libcni {
9098 return & libcni {
9199 config : config {
@@ -165,6 +173,14 @@ func (c *libcni) Networks() []*Network {
165173
166174// Setup setups the network in the namespace and returns a Result
167175func (c * libcni ) Setup (ctx context.Context , id string , path string , opts ... NamespaceOpts ) (* Result , error ) {
176+ ctx , span := tracer ().Start (ctx , "cni.Setup" ,
177+ trace .WithAttributes (
178+ attribute .String ("cni.id" , id ),
179+ attribute .String ("cni.path" , path ),
180+ ),
181+ )
182+ defer span .End ()
183+
168184 c .RLock ()
169185 defer c .RUnlock ()
170186 if err := c .ready (); err != nil {
@@ -183,6 +199,14 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
183199
184200// SetupSerially setups the network in the namespace and returns a Result
185201func (c * libcni ) SetupSerially (ctx context.Context , id string , path string , opts ... NamespaceOpts ) (* Result , error ) {
202+ ctx , span := tracer ().Start (ctx , "cni.SetupSerially" ,
203+ trace .WithAttributes (
204+ attribute .String ("cni.id" , id ),
205+ attribute .String ("cni.path" , path ),
206+ ),
207+ )
208+ defer span .End ()
209+
186210 c .RLock ()
187211 defer c .RUnlock ()
188212 if err := c .ready (); err != nil {
@@ -202,7 +226,22 @@ func (c *libcni) SetupSerially(ctx context.Context, id string, path string, opts
202226func (c * libcni ) attachNetworksSerially (ctx context.Context , ns * Namespace ) ([]* types100.Result , error ) {
203227 var results []* types100.Result
204228 for _ , network := range c .networks {
205- r , err := network .Attach (ctx , ns )
229+ r , err := func () (* types100.Result , error ) {
230+ ctx , span := tracer ().Start (ctx , "cni.attachNetworksSerially" ,
231+ trace .WithAttributes (
232+ attribute .String ("cni.ifname" , network .ifName ),
233+ attribute .String ("cni.name" , network .config .Name ),
234+ ),
235+ )
236+ defer func () {
237+ span .End ()
238+ }()
239+ r , err := network .Attach (ctx , ns )
240+ if err != nil {
241+ span .SetStatus (codes .Error , err .Error ())
242+ }
243+ return r , err
244+ }()
206245 if err != nil {
207246 return nil , err
208247 }
@@ -218,8 +257,11 @@ type asynchAttachResult struct {
218257}
219258
220259func asynchAttach (ctx context.Context , index int , n * Network , ns * Namespace , wg * sync.WaitGroup , rc chan asynchAttachResult ) {
221- defer wg .Done ()
260+ wg .Done ()
222261 r , err := n .Attach (ctx , ns )
262+ if err != nil {
263+ trace .SpanFromContext (ctx ).SetStatus (codes .Error , err .Error ())
264+ }
223265 rc <- asynchAttachResult {index : index , res : r , err : err }
224266}
225267
@@ -231,7 +273,17 @@ func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100
231273
232274 for i , network := range c .networks {
233275 wg .Add (1 )
234- go asynchAttach (ctx , i , network , ns , & wg , rc )
276+ go func () {
277+ ctx , span := tracer ().Start (ctx , "cni.asynchAttach" ,
278+ trace .WithAttributes (
279+ attribute .Int ("cni.index" , i ),
280+ attribute .String ("cni.ifname" , network .ifName ),
281+ attribute .String ("cni.name" , network .config .Name ),
282+ ),
283+ )
284+ defer span .End ()
285+ asynchAttach (ctx , i , network , ns , & wg , rc )
286+ }()
235287 }
236288
237289 for range c .networks {
@@ -248,6 +300,14 @@ func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100
248300
249301// Remove removes the network config from the namespace
250302func (c * libcni ) Remove (ctx context.Context , id string , path string , opts ... NamespaceOpts ) error {
303+ ctx , span := tracer ().Start (ctx , "cni.Remove" ,
304+ trace .WithAttributes (
305+ attribute .String ("cni.id" , id ),
306+ attribute .String ("cni.path" , path ),
307+ ),
308+ )
309+ defer span .End ()
310+
251311 c .RLock ()
252312 defer c .RUnlock ()
253313 if err := c .ready (); err != nil {
@@ -278,6 +338,14 @@ func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...Nam
278338
279339// Check checks if the network is still in desired state
280340func (c * libcni ) Check (ctx context.Context , id string , path string , opts ... NamespaceOpts ) error {
341+ ctx , span := tracer ().Start (ctx , "cni.Check" ,
342+ trace .WithAttributes (
343+ attribute .String ("cni.id" , id ),
344+ attribute .String ("cni.path" , path ),
345+ ),
346+ )
347+ defer span .End ()
348+
281349 c .RLock ()
282350 defer c .RUnlock ()
283351 if err := c .ready (); err != nil {
0 commit comments