11/*
2- * SPDX-FileCopyrightText: © Hypermode Inc. <[email protected] > 2+ * SPDX-FileCopyrightText: Hypermode Inc. <[email protected] > 33 * SPDX-License-Identifier: Apache-2.0
44 */
55
66package zero
77
88import (
99 "context"
10+ "fmt"
1011 "math/rand"
1112 "strconv"
1213 "strings"
1314 "time"
1415
1516 "github.com/golang/glog"
1617 "github.com/pkg/errors"
17- otrace "go.opencensus.io/trace"
18+ "go.opentelemetry.io/otel"
19+ attribute "go.opentelemetry.io/otel/attribute"
20+ trace "go.opentelemetry.io/otel/trace"
1821
1922 "github.com/dgraph-io/badger/v4/y"
2023 "github.com/dgraph-io/dgo/v240/protos/api"
@@ -337,8 +340,8 @@ func (s *Server) proposeTxn(ctx context.Context, src *api.TxnContext) error {
337340}
338341
339342func (s * Server ) commit (ctx context.Context , src * api.TxnContext ) error {
340- span := otrace . FromContext (ctx )
341- span .Annotate ([]otrace. Attribute { otrace . Int64Attribute ("startTs" , int64 (src .StartTs ))}, "" )
343+ span := trace . SpanFromContext (ctx )
344+ span .SetAttributes ( attribute . Int64 ("startTs" , int64 (src .StartTs )))
342345 if src .Aborted {
343346 return s .proposeTxn (ctx , src )
344347 }
@@ -348,8 +351,7 @@ func (s *Server) commit(ctx context.Context, src *api.TxnContext) error {
348351 conflict := s .orc .hasConflict (src )
349352 s .orc .RUnlock ()
350353 if conflict {
351- span .Annotate ([]otrace.Attribute {otrace .BoolAttribute ("abort" , true )},
352- "Oracle found conflict" )
354+ span .SetAttributes (attribute .Bool ("abort" , true ))
353355 src .Aborted = true
354356 return s .proposeTxn (ctx , src )
355357 }
@@ -384,7 +386,7 @@ func (s *Server) commit(ctx context.Context, src *api.TxnContext) error {
384386 return nil
385387 }
386388 if err := checkPreds (); err != nil {
387- span .Annotate ([]otrace. Attribute { otrace . BoolAttribute ("abort" , true )}, err . Error ( ))
389+ span .SetAttributes ( attribute . Bool ("abort" , true ))
388390 src .Aborted = true
389391 return s .proposeTxn (ctx , src )
390392 }
@@ -397,15 +399,16 @@ func (s *Server) commit(ctx context.Context, src *api.TxnContext) error {
397399 src .CommitTs = assigned .StartId
398400 // Mark the transaction as done, irrespective of whether the proposal succeeded or not.
399401 defer s .orc .doneUntil .Done (src .CommitTs )
400- span .Annotatef ([]otrace.Attribute {otrace .Int64Attribute ("commitTs" , int64 (src .CommitTs ))},
401- "Node Id: %d. Proposing TxnContext: %+v" , s .Node .Id , src )
402+ span .SetAttributes (attribute .Int64 ("commitTs" , int64 (src .CommitTs )))
403+ span .SetAttributes (attribute .Int64 ("nodeId" , int64 (s .Node .Id )))
404+ span .SetAttributes (attribute .String ("txnContext" , fmt .Sprintf ("%+v" , src )))
402405
403406 if err := s .orc .commit (src ); err != nil {
404- span .Annotatef ( nil , "Found a conflict. Aborting." )
407+ span .SetAttributes ( attribute . Bool ( "abort" , true ) )
405408 src .Aborted = true
406409 }
407410 if err := ctx .Err (); err != nil {
408- span .Annotatef ( nil , "Aborting txn due to context timing out." )
411+ span .SetAttributes ( attribute . Bool ( "abort" , true ) )
409412 src .Aborted = true
410413 }
411414 // Propose txn should be used to set watermark as done.
@@ -420,15 +423,15 @@ func (s *Server) CommitOrAbort(ctx context.Context, src *api.TxnContext) (*api.T
420423 if ctx .Err () != nil {
421424 return nil , ctx .Err ()
422425 }
423- ctx , span := otrace . StartSpan (ctx , "Zero.CommitOrAbort" )
426+ ctx , span := otel . Tracer ( "" ). Start (ctx , "Zero.CommitOrAbort" )
424427 defer span .End ()
425428
426429 if ! s .Node .AmLeader () {
427430 return nil , errors .Errorf ("Only leader can decide to commit or abort" )
428431 }
429432 err := s .commit (ctx , src )
430433 if err != nil {
431- span .Annotate ([]otrace. Attribute { otrace . BoolAttribute ("error" , true )}, err . Error ( ))
434+ span .SetAttributes ( attribute . Bool ("error" , true ))
432435 }
433436 return src , err
434437}
@@ -491,17 +494,19 @@ func (s *Server) TryAbort(ctx context.Context,
491494
492495// Timestamps is used to assign startTs for a new transaction
493496func (s * Server ) Timestamps (ctx context.Context , num * pb.Num ) (* pb.AssignedIds , error ) {
494- ctx , span := otrace . StartSpan (ctx , "Zero.Timestamps" )
497+ ctx , span := otel . Tracer ( "" ). Start (ctx , "Zero.Timestamps" )
495498 defer span .End ()
496499
497- span .Annotatef (nil , "Zero id: %d. Timestamp request: %+v" , s .Node .Id , num )
500+ span .SetAttributes (attribute .Int64 ("zeroId" , int64 (s .Node .Id )))
501+ span .SetAttributes (attribute .String ("timestampRequest" , fmt .Sprintf ("%+v" , num )))
498502 if ctx .Err () != nil {
499503 return & emptyAssignedIds , ctx .Err ()
500504 }
501505
502506 num .Type = pb .Num_TXN_TS
503507 reply , err := s .lease (ctx , num )
504- span .Annotatef (nil , "Response: %+v. Error: %v" , reply , err )
508+ span .SetAttributes (attribute .String ("response" , fmt .Sprintf ("%+v" , reply )))
509+ span .SetAttributes (attribute .String ("error" , fmt .Sprintf ("%v" , err )))
505510
506511 switch err {
507512 case nil :
0 commit comments