Repository: etcd-io/etcd
File: server/etcdserver/v3_server.go (Go)

Code before:
func (s *EtcdServer) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
	resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{DeleteRange: r})
	if err != nil {
		return nil, err
	}
	return resp.(*pb.DeleteRangeResponse), nil
}

// firstCompareKey returns first non-empty key in the list of comparison operations.
func firstCompareKey(c []*pb.Compare) string {
	for _, op := range c {
		key := string(op.GetKey())
		if key != "" {
			return key
		}
	}
	return ""
}

func (s *EtcdServer) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
	readOnly := txn.IsTxnReadonly(r)

	var span trace.Span
	ctx, span = traceutil.Tracer.Start(ctx, "txn", trace.WithAttributes(
		attribute.String("first_compare_key", firstCompareKey(r.GetCompare())),
		attribute.Int("compare_len", len(r.GetCompare())),
		attribute.Int("success_len", len(r.GetSuccess())),
		attribute.Int("failure_len", len(r.GetFailure())),
		attribute.Bool("read_only", readOnly),
	))
	defer span.End()

	ctx, trace := traceutil.EnsureTrace(ctx, s.Logger(), "transaction",
		traceutil.Field{Key: "read_only", Value: readOnly},
	)
	if readOnly {
		if !txn.IsTxnSerializable(r) {
			err := s.linearizableReadNotify(ctx)
			trace.Step("agreement among raft nodes before linearized reading")
			if err != nil {
				return nil, err
			}
		}
		var resp *pb.TxnResponse
		var err error
		chk := func(ai *auth.AuthInfo) error {
			return txn.CheckTxnAuth(s.authStore, ai, r)
		}

		defer func(start time.Time) {
			txn.WarnOfExpensiveReadOnlyTxnRequest(s.Logger(), s.Cfg.WarningApplyDuration, start, r, resp, err)

Review comment (suggestion):
Maybe add a type of comparison and value we compare?
Like `attribute.Int("compare_first_op", "equal")`, `attribute.Int("compare_first_source", "mod_rev")`, `attribute.Int("compare_first_value", "42")`, 

Code after:
func (s *EtcdServer) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
	resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{DeleteRange: r})
	if err != nil {
		return nil, err
	}
	return resp.(*pb.DeleteRangeResponse), nil
}

// firstCompareKey returns first non-empty key in the list of comparison operations.
func firstCompareKey(c []*pb.Compare) string {
	for _, op := range c {
		key := string(op.GetKey())
		if key != "" {
			return key
		}
	}
	return ""
}

func (s *EtcdServer) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
	readOnly := txn.IsTxnReadonly(r)

	var span trace.Span
	ctx, span = traceutil.Tracer.Start(ctx, "txn", trace.WithAttributes(
		attribute.String("compare_first_key", firstCompareKey(r.GetCompare())),
		attribute.Int("compare_len", len(r.GetCompare())),
		attribute.Int("success_len", len(r.GetSuccess())),
		attribute.Int("failure_len", len(r.GetFailure())),
		attribute.Bool("read_only", readOnly),
	))
	defer span.End()

	ctx, trace := traceutil.EnsureTrace(ctx, s.Logger(), "transaction",
		traceutil.Field{Key: "read_only", Value: readOnly},
	)
	if readOnly {
		if !txn.IsTxnSerializable(r) {
			err := s.linearizableReadNotify(ctx)
			trace.Step("agreement among raft nodes before linearized reading")
			if err != nil {
				return nil, err
			}
		}
		var resp *pb.TxnResponse
		var err error
		chk := func(ai *auth.AuthInfo) error {
			return txn.CheckTxnAuth(s.authStore, ai, r)
		}

		defer func(start time.Time) {
			txn.WarnOfExpensiveReadOnlyTxnRequest(s.Logger(), s.Cfg.WarningApplyDuration, start, r, resp, err)