Skip to content

Commit 37bc2de

Browse files
added spans to graphql
1 parent 9c32b4b commit 37bc2de

10 files changed

Lines changed: 123 additions & 115 deletions

File tree

dgraph/cmd/zero/assign.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
2+
* SPDX-FileCopyrightText: Hypermode Inc. <[email protected]>
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

66
package zero
77

88
import (
99
"context"
10+
"fmt"
1011
"math/rand"
1112
"time"
1213

1314
"github.com/golang/glog"
1415
"github.com/pkg/errors"
15-
otrace "go.opencensus.io/trace"
16+
"go.opentelemetry.io/otel"
17+
attribute "go.opentelemetry.io/otel/attribute"
1618
"google.golang.org/grpc/metadata"
1719

1820
"github.com/hypermodeinc/dgraph/v24/protos/pb"
@@ -175,7 +177,7 @@ func (s *Server) AssignIds(ctx context.Context, num *pb.Num) (*pb.AssignedIds, e
175177
if ctx.Err() != nil {
176178
return &emptyAssignedIds, ctx.Err()
177179
}
178-
ctx, span := otrace.StartSpan(ctx, "Zero.AssignIds")
180+
ctx, span := otel.Tracer("").Start(ctx, "Zero.AssignIds")
179181
defer span.End()
180182

181183
rateLimit := func() error {
@@ -215,11 +217,13 @@ func (s *Server) AssignIds(ctx context.Context, num *pb.Num) (*pb.AssignedIds, e
215217
if err := rateLimit(); err != nil {
216218
return err
217219
}
218-
span.Annotatef(nil, "Zero leader leasing %d ids", num.GetVal())
220+
span.SetAttributes(attribute.String("tablet", "predicate"))
221+
span.AddEvent(fmt.Sprintf("Zero leader leasing %d ids", num.GetVal()))
219222
reply, err = s.lease(ctx, num)
220223
return err
221224
}
222-
span.Annotate(nil, "Not Zero leader")
225+
span.SetAttributes(attribute.String("Not Zero leader", "true"))
226+
span.AddEvent("Not Zero leader")
223227
// I'm not the leader and this request was forwarded to me by a peer, who thought I'm the
224228
// leader.
225229
if num.Forwarded {
@@ -230,7 +234,7 @@ func (s *Server) AssignIds(ctx context.Context, num *pb.Num) (*pb.AssignedIds, e
230234
if pl == nil {
231235
return errors.Errorf("No healthy connection found to Leader of group zero")
232236
}
233-
span.Annotatef(nil, "Sending request to %v", pl.Addr)
237+
span.AddEvent(fmt.Sprintf("Sending request to %v", pl.Addr))
234238
zc := pb.NewZeroClient(pl.Get())
235239
num.Forwarded = true
236240
// pass on the incoming metadata to the zero leader.
@@ -269,7 +273,7 @@ func (s *Server) AssignIds(ctx context.Context, num *pb.Num) (*pb.AssignedIds, e
269273
case <-ctx.Done():
270274
return &emptyAssignedIds, ctx.Err()
271275
case err := <-c:
272-
span.Annotatef(nil, "Error while leasing %+v: %v", num, err)
276+
span.AddEvent(fmt.Sprintf("Error while leasing %+v: %v", num, err))
273277
return reply, err
274278
}
275279
}

dgraph/cmd/zero/oracle.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
2+
* SPDX-FileCopyrightText: Hypermode Inc. <[email protected]>
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

66
package zero
77

88
import (
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

339342
func (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
493496
func (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

Comments
 (0)