Last active
February 10, 2020 09:11
-
-
Save shogo82148/54fddd57a4d9ecb820a65547277643b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/xray/sql_context.go b/xray/sql_context.go | |
index 1148866..6ff2160 100644 | |
--- a/xray/sql_context.go | |
+++ b/xray/sql_context.go | |
@@ -102,7 +102,7 @@ type driverConn struct { | |
} | |
func (conn *driverConn) Ping(ctx context.Context) error { | |
- return Capture(ctx, conn.attr.dbname, func(ctx context.Context) error { | |
+ return Capture(ctx, "ping", func(ctx context.Context) error { | |
conn.attr.populate(ctx, "PING") | |
if p, ok := conn.Conn.(driver.Pinger); ok { | |
return p.Ping(ctx) | |
@@ -187,7 +187,7 @@ func (conn *driverConn) ExecContext(ctx context.Context, query string, args []dr | |
var err error | |
var result driver.Result | |
if execerCtx, ok := conn.Conn.(driver.ExecerContext); ok { | |
- err = Capture(ctx, conn.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "exec", func(ctx context.Context) error { | |
conn.attr.populate(ctx, query) | |
var err error | |
result, err = execerCtx.ExecContext(ctx, query, args) | |
@@ -203,7 +203,7 @@ func (conn *driverConn) ExecContext(ctx context.Context, query string, args []dr | |
if err0 != nil { | |
return nil, err0 | |
} | |
- err = Capture(ctx, conn.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "exec", func(ctx context.Context) error { | |
conn.attr.populate(ctx, query) | |
var err error | |
result, err = execer.Exec(query, dargs) | |
@@ -226,7 +226,7 @@ func (conn *driverConn) QueryContext(ctx context.Context, query string, args []d | |
var err error | |
var rows driver.Rows | |
if queryerCtx, ok := conn.Conn.(driver.QueryerContext); ok { | |
- err = Capture(ctx, conn.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "query", func(ctx context.Context) error { | |
conn.attr.populate(ctx, query) | |
var err error | |
rows, err = queryerCtx.QueryContext(ctx, query, args) | |
@@ -242,7 +242,7 @@ func (conn *driverConn) QueryContext(ctx context.Context, query string, args []d | |
if err0 != nil { | |
return nil, err0 | |
} | |
- err = Capture(ctx, conn.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "query", func(ctx context.Context) error { | |
conn.attr.populate(ctx, query) | |
var err error | |
rows, err = queryer.Query(query, dargs) | |
@@ -545,7 +545,7 @@ func (stmt *driverStmt) ExecContext(ctx context.Context, args []driver.NamedValu | |
var result driver.Result | |
var err error | |
if execerContext, ok := stmt.Stmt.(driver.StmtExecContext); ok { | |
- err = Capture(ctx, stmt.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "exec", func(ctx context.Context) error { | |
stmt.populate(ctx) | |
var err error | |
result, err = execerContext.ExecContext(ctx, args) | |
@@ -561,7 +561,7 @@ func (stmt *driverStmt) ExecContext(ctx context.Context, args []driver.NamedValu | |
if err0 != nil { | |
return nil, err0 | |
} | |
- err = Capture(ctx, stmt.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "exec", func(ctx context.Context) error { | |
stmt.populate(ctx) | |
var err error | |
result, err = stmt.Stmt.Exec(dargs) | |
@@ -582,7 +582,7 @@ func (stmt *driverStmt) QueryContext(ctx context.Context, args []driver.NamedVal | |
var result driver.Rows | |
var err error | |
if queryCtx, ok := stmt.Stmt.(driver.StmtQueryContext); ok { | |
- err = Capture(ctx, stmt.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "query", func(ctx context.Context) error { | |
stmt.populate(ctx) | |
var err error | |
result, err = queryCtx.QueryContext(ctx, args) | |
@@ -598,7 +598,7 @@ func (stmt *driverStmt) QueryContext(ctx context.Context, args []driver.NamedVal | |
if err0 != nil { | |
return nil, err0 | |
} | |
- err = Capture(ctx, stmt.attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "query", func(ctx context.Context) error { | |
stmt.populate(ctx) | |
var err error | |
result, err = stmt.Stmt.Query(dargs) | |
diff --git a/xray/sql_go110.go b/xray/sql_go110.go | |
index a034b80..187292b 100644 | |
--- a/xray/sql_go110.go | |
+++ b/xray/sql_go110.go | |
@@ -56,7 +56,7 @@ func (c *driverConnector) Connect(ctx context.Context) (driver.Conn, error) { | |
if err != nil { | |
return nil, err | |
} | |
- err = Capture(ctx, attr.dbname, func(ctx context.Context) error { | |
+ err = Capture(ctx, "connect", func(ctx context.Context) error { | |
attr.populate(ctx, "CONNECT") | |
var err error | |
rawConn, err = c.Connector.Connect(ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
"github.com/aws/aws-xray-sdk-go/xray" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func main() { | |
xray.Configure(xray.Config{ | |
DaemonAddr: "127.0.0.1:2000", // default | |
}) | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
ctx, root := xray.BeginSegment(ctx, "segment name") | |
root.Close(test(ctx)) | |
} | |
func test(ctx context.Context) error { | |
db, err := xray.SQLContext("mysql", "root:verysecret@tcp(127.0.0.1:3306)/test") | |
if err != nil { | |
return err | |
} | |
defer db.Close() | |
if _, err := db.ExecContext(ctx, "CREATE TABLE foo (bar int)"); err != nil { | |
return err | |
} | |
if err := tx(ctx, db); err != nil { | |
return err | |
} | |
if _, err := db.ExecContext(ctx, "DROP TABLE foo"); err != nil { | |
return err | |
} | |
return nil | |
} | |
func tx(ctx context.Context, db *sql.DB) error { | |
tx, err := db.BeginTx(ctx, nil) | |
if err != nil { | |
return err | |
} | |
defer tx.Rollback() | |
if _, err := db.ExecContext(ctx, "INSERT INTO foo (bar) VALUES (?)", 1); err != nil { | |
return err | |
} | |
rows, err := tx.QueryContext(ctx, "SELECT bar FROM foo WHERE bar = ?", 1) | |
if err != nil { | |
return err | |
} | |
defer rows.Close() | |
for rows.Next() { | |
var bar int | |
if err := rows.Scan(&bar); err != nil { | |
return err | |
} | |
} | |
if err := rows.Err(); err != nil { | |
return err | |
} | |
if err := tx.Commit(); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment