Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: BeforeQuery event.Query event.QueryTemplate both contains params #555

Open
yangyile1990 opened this issue Jun 2, 2022 · 3 comments
Labels
question Further information is requested stale

Comments

@yangyile1990
Copy link

func (a *bunApmHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
fmt.Println("Q", event.Query) //this contains the params
fmt.Println("T", event.QueryTemplate) // this also contains the params

But I wang like this:
select name from xxx where id=?
select score from yyy where name = '?'
I don't want the param.

I think this is a bug.

@vmihailenco
Copy link
Member

Please show the code.

@vmihailenco vmihailenco added the question Further information is requested label Jun 7, 2022
@yangyile1990
Copy link
Author

yangyile1990 commented Jun 13, 2022

package apm

import (
	"context"
	"fmt"
	"github.com/uptrace/bun" // version v1.1.5
	"go.elastic.co/apm/module/apmsql/v2"
	"go.elastic.co/apm/v2"
)

func InstrumentBun(db *bun.DB, driver string) {
	db.AddQueryHook(&bunApmHook{driver: driver})
}

type bunApmHook struct {
	driver string
}

func (a *bunApmHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
	fmt.Println("Q", event.Query)         //TODO this sql has params
	fmt.Println("T", event.QueryTemplate) //TODO this sql also has params
	if sql := event.Query; sql != "" {
		tx := apm.TransactionFromContext(ctx)
		spanType := "db." + a.driver + ".query"
		span := tx.StartSpan(apmsql.QuerySignature(string(sql)), spanType, nil)
		span.Context.SetDatabase(apm.DatabaseSpanContext{
			Statement: string(sql),

			// Static
			Type:     "sql",      //固定写sql就行
			User:     "user",     //这里不能获得访问DB的 username
			Instance: "instance", //这里不能获得DB的 instance
		})

		return apm.ContextWithSpan(ctx, span)
	}
	return ctx
}

func (a *bunApmHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
	if span := apm.SpanFromContext(ctx); span != nil {
		span.End()
	}
}

//what I want is sql like this: select a_column from a_table where token=? //The token is a secret key so I don't want it shown in apm or in log.

Copy link

github-actions bot commented Nov 7, 2024

This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed.

@github-actions github-actions bot added the stale label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

2 participants