@@ -93,9 +93,16 @@ type Driver struct { //nolint:maligned
9393 panicCallback func (e interface {})
9494}
9595
96+ func (d * Driver ) trace () * trace.Driver {
97+ if d .config != nil {
98+ return d .config .Trace ()
99+ }
100+ return & trace.Driver {}
101+ }
102+
96103// Close closes Driver and clear resources
97104func (d * Driver ) Close (ctx context.Context ) (finalErr error ) {
98- onDone := trace .DriverOnClose (d .config . Trace (), & ctx , stack .FunctionID (0 ))
105+ onDone := trace .DriverOnClose (d .trace (), & ctx , stack .FunctionID ("" ))
99106 defer func () {
100107 onDone (finalErr )
101108 }()
@@ -201,15 +208,30 @@ func (d *Driver) Topic() topic.Client {
201208//
202209// See sugar.DSN helper for make dsn from endpoint and database
203210func Open (ctx context.Context , dsn string , opts ... Option ) (_ * Driver , err error ) {
204- return open (
205- ctx ,
206- append (
207- []Option {
208- WithConnectionString (dsn ),
209- },
210- opts ... ,
211- )... ,
211+ d , err := newConnectionFromOptions (ctx , append (
212+ []Option {
213+ WithConnectionString (dsn ),
214+ },
215+ opts ... ,
216+ )... )
217+ if err != nil {
218+ return nil , xerrors .WithStackTrace (err )
219+ }
220+
221+ onDone := trace .DriverOnInit (
222+ d .trace (), & ctx ,
223+ stack .FunctionID ("" ),
224+ d .config .Endpoint (), d .config .Database (), d .config .Secure (),
212225 )
226+ defer func () {
227+ onDone (err )
228+ }()
229+
230+ if err = d .connect (ctx ); err != nil {
231+ return nil , xerrors .WithStackTrace (err )
232+ }
233+
234+ return d , nil
213235}
214236
215237func MustOpen (ctx context.Context , dsn string , opts ... Option ) * Driver {
@@ -224,7 +246,25 @@ func MustOpen(ctx context.Context, dsn string, opts ...Option) *Driver {
224246//
225247// Deprecated: use Open with required param connectionString instead
226248func New (ctx context.Context , opts ... Option ) (_ * Driver , err error ) {
227- return open (ctx , opts ... )
249+ d , err := newConnectionFromOptions (ctx , opts ... )
250+ if err != nil {
251+ return nil , xerrors .WithStackTrace (err )
252+ }
253+
254+ onDone := trace .DriverOnInit (
255+ d .trace (), & ctx ,
256+ stack .FunctionID ("" ),
257+ d .config .Endpoint (), d .config .Database (), d .config .Secure (),
258+ )
259+ defer func () {
260+ onDone (err )
261+ }()
262+
263+ if err = d .connect (ctx ); err != nil {
264+ return nil , xerrors .WithStackTrace (err )
265+ }
266+
267+ return d , nil
228268}
229269
230270func newConnectionFromOptions (ctx context.Context , opts ... Option ) (_ * Driver , err error ) {
@@ -287,23 +327,15 @@ func newConnectionFromOptions(ctx context.Context, opts ...Option) (_ *Driver, e
287327 return d , nil
288328}
289329
290- func connect (ctx context.Context , d * Driver ) error {
291- var err error
292-
330+ func (d * Driver ) connect (ctx context.Context ) (err error ) {
293331 if d .config .Endpoint () == "" {
294332 return xerrors .WithStackTrace (errors .New ("configuration: empty dial address" ))
295333 }
334+
296335 if d .config .Database () == "" {
297336 return xerrors .WithStackTrace (errors .New ("configuration: empty database" ))
298337 }
299338
300- onDone := trace .DriverOnInit (
301- d .config .Trace (), & ctx , stack .FunctionID (2 ), d .config .Endpoint (), d .config .Database (), d .config .Secure (),
302- )
303- defer func () {
304- onDone (err )
305- }()
306-
307339 if d .userInfo != nil {
308340 d .config = d .config .With (config .WithCredentials (
309341 credentials .NewStaticCredentials (
@@ -443,18 +475,6 @@ func connect(ctx context.Context, d *Driver) error {
443475 return nil
444476}
445477
446- func open (ctx context.Context , opts ... Option ) (_ * Driver , err error ) {
447- d , err := newConnectionFromOptions (ctx , opts ... )
448- if err != nil {
449- return nil , xerrors .WithStackTrace (err )
450- }
451- err = connect (ctx , d )
452- if err != nil {
453- return nil , xerrors .WithStackTrace (err )
454- }
455- return d , nil
456- }
457-
458478// GRPCConn casts *ydb.Driver to grpc.ClientConnInterface for executing
459479// unary and streaming RPC over internal driver balancer.
460480//
0 commit comments