go-agent icon indicating copy to clipboard operation
go-agent copied to clipboard

v3/integrations/nrpq - datastore segments should populate ParameterizedQuery field

Open garciademarina opened this issue 3 years ago • 0 comments

Summary

When using v3/integrations/nrpq the datastore segment is automatically instrumented however ParameterizedQuery field is always empty.

Desired Behaviour

The nrpq integration should also saved the parametrized query (ParameterizedQuery) in the datastore segment.

Possible Solution

We did a new integration package in order to save ParameterizedQuery automatically by using a custom/modified parse.ParseQuery function. The parse.ParseQuery function receives a pointer to the segment and a query as parameters, it populates the table name and the operation (select, insert, ...), because we also have the query passed as a parameter we are saving it there.

func ParseQuery(segment *newrelic.DatastoreSegment, query string) {
	s := cCommentRegex.ReplaceAllString(query, "")
	s = lineCommentRegex.ReplaceAllString(s, "")
	s = sqlPrefixRegex.ReplaceAllString(s, "")
	op := strings.ToLower(firstWordRegex.FindString(s))
	if rg, ok := sqlOperations[op]; ok {
		segment.Operation = op
		if nil != rg {
			if m := rg.FindStringSubmatch(s); len(m) > 1 {
				segment.Collection = extractTable(m[1])
			}
		}
		segment.ParameterizedQuery = query // <-- save parameterized query 
	}
}

Additional context

We don't get much information about the queries because they are being grouped like 'select' on 'table' using 'Postgres'.

garciademarina avatar Jan 14 '22 08:01 garciademarina