@@ -92,6 +92,11 @@ func Wrap(d driver.Driver, options ...TraceOption) driver.Driver {
9292 for _ , option := range options {
9393 option (& o )
9494 }
95+ if o .InstanceName == "" {
96+ o .InstanceName = defaultInstanceName
97+ } else {
98+ o .DefaultAttributes = append (o .DefaultAttributes , trace .StringAttribute ("sql.instance" , o .InstanceName ))
99+ }
95100 if o .QueryParams && ! o .Query {
96101 o .QueryParams = false
97102 }
@@ -113,6 +118,11 @@ func WrapConn(c driver.Conn, options ...TraceOption) driver.Conn {
113118 for _ , option := range options {
114119 option (& o )
115120 }
121+ if o .InstanceName == "" {
122+ o .InstanceName = defaultInstanceName
123+ } else {
124+ o .DefaultAttributes = append (o .DefaultAttributes , trace .StringAttribute ("sql.instance" , o .InstanceName ))
125+ }
116126 return wrapConn (c , o )
117127}
118128
@@ -123,7 +133,7 @@ type ocConn struct {
123133}
124134
125135func (c ocConn ) Ping (ctx context.Context ) (err error ) {
126- defer recordCallStats (ctx , "go.sql.ping" )(err )
136+ defer recordCallStats (ctx , "go.sql.ping" , c . options . InstanceName )(err )
127137
128138 if c .options .Ping && (c .options .AllowRoot || trace .FromContext (ctx ) != nil ) {
129139 var span * trace.Span
@@ -154,7 +164,7 @@ func (c ocConn) Ping(ctx context.Context) (err error) {
154164}
155165
156166func (c ocConn ) Exec (query string , args []driver.Value ) (res driver.Result , err error ) {
157- defer recordCallStats (context .Background (), "go.sql.exec" )(err )
167+ defer recordCallStats (context .Background (), "go.sql.exec" , c . options . InstanceName )(err )
158168
159169 if exec , ok := c .parent .(driver.Execer ); ok {
160170 if ! c .options .AllowRoot {
@@ -198,7 +208,7 @@ func (c ocConn) Exec(query string, args []driver.Value) (res driver.Result, err
198208}
199209
200210func (c ocConn ) ExecContext (ctx context.Context , query string , args []driver.NamedValue ) (res driver.Result , err error ) {
201- defer recordCallStats (ctx , "go.sql.exec" )(err )
211+ defer recordCallStats (ctx , "go.sql.exec" , c . options . InstanceName )(err )
202212
203213 if execCtx , ok := c .parent .(driver.ExecerContext ); ok {
204214 parentSpan := trace .FromContext (ctx )
@@ -243,7 +253,7 @@ func (c ocConn) ExecContext(ctx context.Context, query string, args []driver.Nam
243253}
244254
245255func (c ocConn ) Query (query string , args []driver.Value ) (rows driver.Rows , err error ) {
246- defer recordCallStats (context .Background (), "go.sql.query" )(err )
256+ defer recordCallStats (context .Background (), "go.sql.query" , c . options . InstanceName )(err )
247257
248258 if queryer , ok := c .parent .(driver.Queryer ); ok {
249259 if ! c .options .AllowRoot {
@@ -288,7 +298,7 @@ func (c ocConn) Query(query string, args []driver.Value) (rows driver.Rows, err
288298}
289299
290300func (c ocConn ) QueryContext (ctx context.Context , query string , args []driver.NamedValue ) (rows driver.Rows , err error ) {
291- defer recordCallStats (ctx , "go.sql.query" )(err )
301+ defer recordCallStats (ctx , "go.sql.query" , c . options . InstanceName )(err )
292302
293303 if queryerCtx , ok := c .parent .(driver.QueryerContext ); ok {
294304 parentSpan := trace .FromContext (ctx )
@@ -334,7 +344,7 @@ func (c ocConn) QueryContext(ctx context.Context, query string, args []driver.Na
334344}
335345
336346func (c ocConn ) Prepare (query string ) (stmt driver.Stmt , err error ) {
337- defer recordCallStats (context .Background (), "go.sql.prepare" )(err )
347+ defer recordCallStats (context .Background (), "go.sql.prepare" , c . options . InstanceName )(err )
338348
339349 if c .options .AllowRoot {
340350 _ , span := trace .StartSpan (context .Background (), "sql:prepare" ,
@@ -373,7 +383,7 @@ func (c *ocConn) Begin() (driver.Tx, error) {
373383}
374384
375385func (c * ocConn ) PrepareContext (ctx context.Context , query string ) (stmt driver.Stmt , err error ) {
376- defer recordCallStats (ctx , "go.sql.prepare" )(err )
386+ defer recordCallStats (ctx , "go.sql.prepare" , c . options . InstanceName )(err )
377387
378388 var span * trace.Span
379389 attrs := append ([]trace.Attribute (nil ), c .options .DefaultAttributes ... )
@@ -409,7 +419,7 @@ func (c *ocConn) PrepareContext(ctx context.Context, query string) (stmt driver.
409419}
410420
411421func (c * ocConn ) BeginTx (ctx context.Context , opts driver.TxOptions ) (tx driver.Tx , err error ) {
412- defer recordCallStats (ctx , "go.sql.begin" )(err )
422+ defer recordCallStats (ctx , "go.sql.begin" , c . options . InstanceName )(err )
413423
414424 if ! c .options .AllowRoot && trace .FromContext (ctx ) == nil {
415425 if connBeginTx , ok := c .parent .(driver.ConnBeginTx ); ok {
@@ -518,7 +528,7 @@ type ocStmt struct {
518528}
519529
520530func (s ocStmt ) Exec (args []driver.Value ) (res driver.Result , err error ) {
521- defer recordCallStats (context .Background (), "go.sql.stmt.exec" )(err )
531+ defer recordCallStats (context .Background (), "go.sql.stmt.exec" , s . options . InstanceName )(err )
522532
523533 if ! s .options .AllowRoot {
524534 return s .parent .Exec (args )
@@ -568,7 +578,7 @@ func (s ocStmt) NumInput() int {
568578}
569579
570580func (s ocStmt ) Query (args []driver.Value ) (rows driver.Rows , err error ) {
571- defer recordCallStats (context .Background (), "go.sql.stmt.query" )(err )
581+ defer recordCallStats (context .Background (), "go.sql.stmt.query" , s . options . InstanceName )(err )
572582
573583 if ! s .options .AllowRoot {
574584 return s .parent .Query (args )
@@ -609,7 +619,7 @@ func (s ocStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
609619}
610620
611621func (s ocStmt ) ExecContext (ctx context.Context , args []driver.NamedValue ) (res driver.Result , err error ) {
612- defer recordCallStats (ctx , "go.sql.stmt.exec" )(err )
622+ defer recordCallStats (ctx , "go.sql.stmt.exec" , s . options . InstanceName )(err )
613623
614624 parentSpan := trace .FromContext (ctx )
615625 if ! s .options .AllowRoot && parentSpan == nil {
@@ -654,7 +664,7 @@ func (s ocStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res
654664}
655665
656666func (s ocStmt ) QueryContext (ctx context.Context , args []driver.NamedValue ) (rows driver.Rows , err error ) {
657- defer recordCallStats (ctx , "go.sql.stmt.query" )(err )
667+ defer recordCallStats (ctx , "go.sql.stmt.query" , s . options . InstanceName )(err )
658668
659669 parentSpan := trace .FromContext (ctx )
660670 if ! s .options .AllowRoot && parentSpan == nil {
@@ -863,7 +873,7 @@ type ocTx struct {
863873}
864874
865875func (t ocTx ) Commit () (err error ) {
866- defer recordCallStats (context .Background (), "go.sql.commit" )(err )
876+ defer recordCallStats (context .Background (), "go.sql.commit" , t . options . InstanceName )(err )
867877
868878 _ , span := trace .StartSpan (t .ctx , "sql:commit" ,
869879 trace .WithSpanKind (trace .SpanKindClient ),
@@ -882,7 +892,7 @@ func (t ocTx) Commit() (err error) {
882892}
883893
884894func (t ocTx ) Rollback () (err error ) {
885- defer recordCallStats (context .Background (), "go.sql.rollback" )(err )
895+ defer recordCallStats (context .Background (), "go.sql.rollback" , t . options . InstanceName )(err )
886896
887897 _ , span := trace .StartSpan (t .ctx , "sql:rollback" ,
888898 trace .WithSpanKind (trace .SpanKindClient ),
0 commit comments