This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add info regarding - database type and address - approximation for queries results size
- Loading branch information
embs
committed
Nov 26, 2017
1 parent
9de2c86
commit b352f92
Showing
6 changed files
with
167 additions
and
28 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package catalogue | ||
|
||
import( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
type Database interface { | ||
Close() error | ||
Ping() error | ||
Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
Prepare(query string) (StmtMiddleware, error) | ||
Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) | ||
} | ||
|
||
// SqlxDb meets the Database interface requirements | ||
type SqlxDb struct { | ||
// db is a reference for the underlying database implementation | ||
Db *sqlx.DB | ||
} | ||
|
||
func (sqlxdb *SqlxDb) Close() error { | ||
return sqlxdb.Db.Close() | ||
} | ||
|
||
func (sqlxdb *SqlxDb) Ping() error { | ||
return sqlxdb.Db.Ping() | ||
} | ||
|
||
func (sqlxdb *SqlxDb) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error { | ||
return sqlxdb.Db.Select(dest, query, args...) | ||
} | ||
|
||
func (sqlxdb *SqlxDb) Prepare(query string) (StmtMiddleware, error) { | ||
sel, err := sqlxdb.Db.Prepare(query) | ||
return StmtMiddleware{next: sel}, err | ||
} | ||
|
||
func (sqlxdb *SqlxDb) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error { | ||
return sqlxdb.Db.Get(dest, query, args...) | ||
} | ||
|
||
func (sqlxdb *SqlxDb) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { | ||
return sqlxdb.Db.Query(query, args...) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package catalogue | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"unsafe" | ||
|
||
stdopentracing "github.com/opentracing/opentracing-go" | ||
) | ||
|
||
// Middleware decorates a database. | ||
type DbMiddleware func(Database) Database | ||
|
||
// DbTracingMiddleware traces database calls. | ||
func DbTracingMiddleware() DbMiddleware { | ||
return func(next Database) Database { | ||
return dbTracingMiddleware{ | ||
next: next, | ||
} | ||
} | ||
} | ||
|
||
type dbTracingMiddleware struct { | ||
next Database | ||
} | ||
|
||
type StmtMiddleware struct { | ||
next *sql.Stmt | ||
} | ||
|
||
func (stmt StmtMiddleware) Close() error { | ||
return stmt.next.Close() | ||
} | ||
|
||
func (stmt StmtMiddleware) QueryRow(ctx context.Context, args ...interface{}) *sql.Row { | ||
span := startSpan(ctx, "rows from database") | ||
rows := stmt.next.QueryRow(args...) | ||
finishSpan(span, unsafe.Sizeof(rows)) | ||
return rows | ||
} | ||
|
||
func (mw dbTracingMiddleware) Close() error { | ||
return mw.next.Close() | ||
} | ||
|
||
func (mw dbTracingMiddleware) Ping() error { | ||
return mw.next.Ping() | ||
} | ||
|
||
func (mw dbTracingMiddleware) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error { | ||
span := startSpan(ctx, "socks from database") | ||
err := mw.next.Select(ctx, dest, query, args...) | ||
finishSpan(span, unsafe.Sizeof(dest)) | ||
return err | ||
} | ||
|
||
func (mw dbTracingMiddleware) Prepare(query string) (StmtMiddleware, error) { | ||
return mw.next.Prepare(query) | ||
} | ||
|
||
func (mw dbTracingMiddleware) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error { | ||
span := startSpan(ctx, "get from database") | ||
err := mw.next.Get(ctx, dest, query, args...) | ||
finishSpan(span, unsafe.Sizeof(dest)) | ||
return err | ||
} | ||
|
||
func (mw dbTracingMiddleware) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { | ||
span := startSpan(ctx, "query from database") | ||
rows, err := mw.next.Query(ctx, query, args...) | ||
finishSpan(span, unsafe.Sizeof(rows)) | ||
return rows, err | ||
} | ||
|
||
func startSpan(ctx context.Context, n string) stdopentracing.Span { | ||
var span stdopentracing.Span | ||
span, ctx = stdopentracing.StartSpanFromContext(ctx, n) | ||
span.SetTag("span.kind", "client") | ||
span.SetTag("db.type", "mysql") | ||
span.SetTag("peer.address", "catalogue-db:3306") | ||
return span | ||
} | ||
|
||
func finishSpan(span stdopentracing.Span, size uintptr) { | ||
span.SetTag("db.query.result.size", size) | ||
span.Finish() | ||
} |
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
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
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