Skip to content

Commit

Permalink
Instrument (* DB).queryDC from database/sql which is an internal func…
Browse files Browse the repository at this point in the history
…tion used by most of the outer API's
  • Loading branch information
RonFed committed Jul 26, 2023
1 parent 427ce9a commit 8520c7b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
20 changes: 8 additions & 12 deletions pkg/instrumentors/bpf/database/sql/bpf/probe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ struct {


// This instrumentation attaches uprobe to the following function:
// func (c *Conn) QueryContext(ctx context.Context, query string, args ...any)
SEC("uprobe/QueryContext")
int uprobe_Query_Context(struct pt_regs *ctx) {
bpf_printk("uprobe_Query_Context !!\n");
// func (db *DB) queryDC(ctx, txctx context.Context, dc *driverConn, releaseConn func(error), query string, args []any)
SEC("uprobe/queryDC")
int uprobe_queryDC(struct pt_regs *ctx) {
// argument positions
u64 context_ptr_pos = 3;
u64 query_str_ptr_pos = 4;
u64 query_str_len_pos = 5;
u64 query_str_ptr_pos = 8;
u64 query_str_len_pos = 9;

struct sql_request_t sql_request = {0};
sql_request.start_time = bpf_ktime_get_ns();
Expand Down Expand Up @@ -68,16 +67,13 @@ int uprobe_Query_Context(struct pt_regs *ctx) {
}

// This instrumentation attaches uprobe to the following function:
// func (c *Conn) QueryContext(ctx context.Context, query string, args ...any)
SEC("uprobe/QueryContext")
int uuprobe_Query_Context_Returns(struct pt_regs *ctx) {
bpf_printk("uuprobe_Query_Context_Returns !!\n");

// func (db *DB) queryDC(ctx, txctx context.Context, dc *driverConn, releaseConn func(error), query string, args []any)
SEC("uprobe/queryDC")
int uuprobe_QueryDC_Returns(struct pt_regs *ctx) {
u64 context_ptr_pos = 3;
void *goroutine = get_goroutine_address(ctx, context_ptr_pos);
void *sqlReq_ptr = bpf_map_lookup_elem(&context_to_sql_events, &goroutine);

// TODO : is this copy necessery (why not to cast sqlReq_ptr ?)
struct sql_request_t sqlReq = {0};
bpf_probe_read(&sqlReq, sizeof(sqlReq), sqlReq_ptr);
sqlReq.end_time = bpf_ktime_get_ns();
Expand Down
13 changes: 6 additions & 7 deletions pkg/instrumentors/bpf/database/sql/bpf_bpfel_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions pkg/instrumentors/bpf/database/sql/bpf_bpfel_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions pkg/instrumentors/bpf/database/sql/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func New() *Instrumentor {

// LibraryName returns the database/sql/ package name.
func (h *Instrumentor) LibraryName() string {
return "database/sql/sql"
return "database/sql"
}

// FuncNames returns the function names from "database/sql/" that are instrumented.
// FuncNames returns the function names from "database/sql" that are instrumented.
func (h *Instrumentor) FuncNames() []string {
return []string{"database/sql.(*Conn).QueryContext"}
return []string{"database/sql.(*DB).queryDC"}
}

// Load loads all instrumentation offsets.
Expand Down Expand Up @@ -96,7 +96,7 @@ func (h *Instrumentor) Load(ctx *context.InstrumentorContext) error {
return err
}

up, err := ctx.Executable.Uprobe("", h.bpfObjects.UprobeQueryContext, &link.UprobeOptions{
up, err := ctx.Executable.Uprobe("", h.bpfObjects.UprobeQueryDC, &link.UprobeOptions{
Address: offset,
})

Expand All @@ -113,7 +113,7 @@ func (h *Instrumentor) Load(ctx *context.InstrumentorContext) error {
}

for _, ret := range retOffsets {
retProbe, err := ctx.Executable.Uprobe("", h.bpfObjects.UuprobeQueryContextReturns, &link.UprobeOptions{
retProbe, err := ctx.Executable.Uprobe("", h.bpfObjects.UuprobeQueryDC_Returns, &link.UprobeOptions{
Address: ret,
})
if err != nil {
Expand Down Expand Up @@ -183,7 +183,6 @@ func (h *Instrumentor) convertEvent(e *Event) *events.Event {

return &events.Event{
Library: h.LibraryName(),
// TODO : something better to puth in Name
Name: "DB",
Kind: trace.SpanKindClient,
StartTime: int64(e.StartTime),
Expand Down

0 comments on commit 8520c7b

Please sign in to comment.