Skip to content

Commit

Permalink
feat(bunotel): add option to enable formatting of queries (#547)
Browse files Browse the repository at this point in the history
* feat(bunotel): add option to enable formatting of queries

Disable formatting by default (breaing change)
and add bunotel option that enables formatting.

Formatting has potential to leak information into the traces
as such not formatting the query by default seems like
safer option.
  • Loading branch information
matoous committed May 28, 2022
1 parent 77dcee4 commit b9c768c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 10 additions & 0 deletions extra/bunotel/option.go
Expand Up @@ -20,3 +20,13 @@ func WithDBName(name string) Option {
h.attrs = append(h.attrs, semconv.DBNameKey.String(name))
}
}

// WithFormattedQueries enables formatting of the query that is added
// as the statement attribute to the trace.
// This means that all placeholders and arguments will be filled first
// and the query will contain all information as sent to the database.
func WithFormattedQueries(format bool) Option {
return func(h *QueryHook) {
h.formatQueries = format
}
}
9 changes: 5 additions & 4 deletions extra/bunotel/otel.go
Expand Up @@ -33,7 +33,8 @@ var (
)

type QueryHook struct {
attrs []attribute.KeyValue
attrs []attribute.KeyValue
formatQueries bool
}

var _ bun.QueryHook = (*QueryHook)(nil)
Expand Down Expand Up @@ -83,7 +84,7 @@ func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
span.SetName(operation)
defer span.End()

query := eventQuery(event)
query := h.eventQuery(event)
fn, file, line := funcFileLine("github.com/uptrace/bun")

attrs := make([]attribute.KeyValue, 0, 10)
Expand Down Expand Up @@ -141,13 +142,13 @@ func funcFileLine(pkg string) (string, string, int) {
return fn, file, line
}

func eventQuery(event *bun.QueryEvent) string {
func (h *QueryHook) eventQuery(event *bun.QueryEvent) string {
const softQueryLimit = 8000
const hardQueryLimit = 16000

var query string

if len(event.Query) <= softQueryLimit {
if h.formatQueries && len(event.Query) <= softQueryLimit {
query = event.Query
} else {
query = unformattedQuery(event)
Expand Down

0 comments on commit b9c768c

Please sign in to comment.