Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c451716
PMM-10385 New properties in getQueryExamples.
JiriCtvrtka Oct 18, 2022
88bf2a6
PMM-10385 Fix test.
JiriCtvrtka Oct 18, 2022
4a2ec65
PMM-10385 Lint.
JiriCtvrtka Oct 18, 2022
9b8b7b0
PMM-10385 Fix test.
JiriCtvrtka Oct 18, 2022
b69b87e
PMM-10385 Fix test.
JiriCtvrtka Oct 18, 2022
6ee0484
PMM-10385 Format.
JiriCtvrtka Oct 18, 2022
211c8da
PMM-10385 Format.
JiriCtvrtka Oct 18, 2022
cd8eb9a
PMM-10385 Format.
JiriCtvrtka Oct 18, 2022
bbcabba
PMM-10385 Format.
JiriCtvrtka Oct 18, 2022
7f28dff
PMM-10385 Fix sql parsing.
JiriCtvrtka Oct 19, 2022
de8c217
PMM-10385 Lint.
JiriCtvrtka Oct 19, 2022
bba68c9
PMM-10385 Changes.
JiriCtvrtka Oct 20, 2022
134d055
PMM-10385 Fix.
JiriCtvrtka Oct 20, 2022
f5b018d
PMM-10385 Fix for get query by queryid.
JiriCtvrtka Oct 31, 2022
af0d090
PMM-10385 Changes.
JiriCtvrtka Oct 31, 2022
96c5be2
PMM-10385 Changes.
JiriCtvrtka Nov 1, 2022
c04c55a
PMM-10385 Fix.
JiriCtvrtka Nov 1, 2022
6b4eb85
PMM-10385 Changes.
JiriCtvrtka Nov 1, 2022
9f8b1b9
PMM-10385 Fix.
JiriCtvrtka Nov 1, 2022
86b818a
PMM-10385 Formatting.
JiriCtvrtka Nov 1, 2022
d19ec65
PMM-10385 Mod.
JiriCtvrtka Nov 2, 2022
0c2ea54
PMM-10385 Change of behavior.
JiriCtvrtka Nov 3, 2022
c3cceae
PMM-10385 Fix format.
JiriCtvrtka Nov 3, 2022
52c0162
PMM-10385 Remove test.
JiriCtvrtka Nov 3, 2022
7769b6d
PMM-10385 Fix typo.
JiriCtvrtka Nov 3, 2022
b11491a
PMM-10385 Fix tests.
JiriCtvrtka Nov 3, 2022
d1b0242
PMM-10385 Mod.
JiriCtvrtka Nov 3, 2022
551858d
PMM-10385 Regenerate data.
JiriCtvrtka Nov 3, 2022
d1b93c0
PMM-10385 Gen.
JiriCtvrtka Nov 3, 2022
362cdb8
PMM-10385 Format.
JiriCtvrtka Nov 3, 2022
9bb4907
PMM-10385 Format.
JiriCtvrtka Nov 8, 2022
c2b8b17
Revert "PMM-10385 Format."
JiriCtvrtka Nov 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions exporters/slow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func main() {

mb := &qanpb.MetricsBucket{
Queryid: v.Id,
ExplainFingerprint: v.ExplainFingerprint,
PlaceholdersCount: v.PlaceholdersCount,
Fingerprint: v.Fingerprint,
Database: "",
Schema: v.Db,
Expand Down
38 changes: 25 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ require (
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
github.com/jmoiron/sqlx v1.3.5
github.com/percona/pmm v0.0.0-20220701073443-67c3185e5a50
github.com/percona/pmm v0.0.0-20221103135124-581aef30c9ec
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.2
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.5
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c
google.golang.org/grpc v1.48.0
github.com/prometheus/client_golang v1.13.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1
gopkg.in/alecthomas/kingpin.v2 v2.2.6
vitess.io/vitess v0.15.0
)

require (
Expand All @@ -30,15 +31,26 @@ require (
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mwitkow/go-proto-validators v0.3.2 // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20180928155321-4b5a12d3ff02 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/philhofer/fwd v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.35.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220728213248-dd149ef739b9 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tinylib/msgp v1.1.1 // indirect
github.com/uber/jaeger-client-go v2.16.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect
gopkg.in/DataDog/dd-trace-go.v1 v1.17.0 // indirect
gopkg.in/reform.v1 v1.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.20.6 // indirect
)
209 changes: 161 additions & 48 deletions go.sum

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions migrations/bindata.go

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

3 changes: 3 additions & 0 deletions migrations/sql/16_explain_columns.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE metrics
DROP COLUMN `explain_fingerprint`,
DROP COLUMN `placeholders_count`;
3 changes: 3 additions & 0 deletions migrations/sql/16_explain_columns.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE metrics
ADD COLUMN `explain_fingerprint` String,
ADD COLUMN `placeholders_count` UInt32;
2 changes: 1 addition & 1 deletion models/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

const queryTimeout = 30 * time.Second

//nolint
// nolint
var sparklinePointAllFields = []string{
"point",
"timestamp",
Expand Down
4 changes: 4 additions & 0 deletions models/data_ingestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const insertSQL = `
INSERT INTO metrics
(
queryid,
explain_fingerprint,
placeholders_count,
service_name,
database,
schema,
Expand Down Expand Up @@ -264,6 +266,8 @@ const insertSQL = `
)
VALUES (
:queryid,
:explain_fingerprint,
:placeholders_count,
:service_name,
:database,
:schema,
Expand Down
62 changes: 58 additions & 4 deletions models/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ GROUP BY point
ORDER BY point ASC;
`

//nolint
// nolint
var tmplMetricsSparklines = template.Must(template.New("queryMetricsSparklines").Funcs(funcMap).Parse(queryMetricsSparklinesTmpl))

// SelectSparklines selects datapoint for sparklines.
Expand Down Expand Up @@ -520,7 +520,7 @@ func (m *Metrics) SelectSparklines(ctx context.Context, periodStartFromSec, peri
}

const queryExampleTmpl = `
SELECT schema AS schema, tables, service_id, service_type, example, toUInt8(example_format) AS example_format,
SELECT schema AS schema, tables, service_id, service_type, queryid, explain_fingerprint, placeholders_count, example, toUInt8(example_format) AS example_format,
is_truncated, toUInt8(example_type) AS example_type, example_metrics
FROM metrics
WHERE period_start >= :period_start_from AND period_start <= :period_start_to
Expand All @@ -538,7 +538,7 @@ SELECT schema AS schema, tables, service_id, service_type, example, toUInt8(exam
LIMIT :limit
`

//nolint
// nolint
var tmplQueryExample = template.Must(template.New("queryExampleTmpl").Funcs(funcMap).Parse(queryExampleTmpl))

// SelectQueryExamples selects query examples and related stuff for given time range.
Expand Down Expand Up @@ -587,6 +587,9 @@ func (m *Metrics) SelectQueryExamples(ctx context.Context, periodStartFrom, peri
&row.Tables,
&row.ServiceId,
&row.ServiceType,
&row.QueryId,
&row.ExplainFingerprint,
&row.PlaceholdersCount,
&row.Example,
&row.ExampleFormat,
&row.IsTruncated,
Expand All @@ -596,6 +599,7 @@ func (m *Metrics) SelectQueryExamples(ctx context.Context, periodStartFrom, peri
if err != nil {
return nil, errors.Wrap(err, "failed to scan query example for object details")
}

res.QueryExamples = append(res.QueryExamples, &row)
}

Expand All @@ -618,7 +622,7 @@ SELECT service_name, database, schema, username, client_host, replication_set, c
container_id, agent_id, agent_type, labels.key, labels.value, cmd_type, top_queryid, application_name, planid
`

//nolint
// nolint
var tmplObjectDetailsLabels = template.Must(template.New("queryObjectDetailsLabelsTmpl").Funcs(funcMap).Parse(queryObjectDetailsLabelsTmpl))

type queryRowsLabels struct {
Expand Down Expand Up @@ -972,3 +976,53 @@ func (m *Metrics) QueryExists(ctx context.Context, serviceID, query string) (boo

return false, nil
}

const queryByQueryIDTmpl = `SELECT explain_fingerprint, placeholders_count FROM metrics
WHERE service_id = :service_id AND queryid = :query_id LIMIT 1;
`

// ExplainFingerprintByQueryID get explain fingerprint and placeholders count for given queryid.
func (m *Metrics) ExplainFingerprintByQueryID(ctx context.Context, serviceID, queryID string) (*qanpb.ExplainFingerprintByQueryIDReply, error) {
arg := map[string]interface{}{
"service_id": serviceID,
"query_id": queryID,
}

var queryBuffer bytes.Buffer
queryBuffer.WriteString(queryByQueryIDTmpl)

res := &qanpb.ExplainFingerprintByQueryIDReply{}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
ExplainFingerprint, PlaceholdersCount are missing in ExplainFingerprintByQueryIDReply (exhaustivestruct)

query, args, err := sqlx.Named(queryBuffer.String(), arg)
if err != nil {
return res, errors.Wrap(err, cannotPrepare)
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return res, errors.Wrap(err, cannotPopulate)
}
query = m.db.Rebind(query)

queryCtx, cancel := context.WithTimeout(ctx, queryTimeout)
defer cancel()

rows, err := m.db.QueryxContext(queryCtx, query, args...)
if err != nil {
return res, errors.Wrap(err, cannotExecute)
}
defer rows.Close() //nolint:errcheck

for rows.Next() {
err = rows.Scan(
&res.ExplainFingerprint,
&res.PlaceholdersCount,
)

if err != nil {
return res, errors.Wrap(err, "failed to scan query")
}

return res, nil
}

return res, nil
}
4 changes: 2 additions & 2 deletions models/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ ORDER BY {{ .Order }}
LIMIT :offset, :limit
`

//nolint
// nolint
var tmplQueryReport = template.Must(template.New("queryReportTmpl").Funcs(funcMap).Parse(queryReportTmpl))

func inSlice(slice []string, val string) bool {
Expand Down Expand Up @@ -261,7 +261,7 @@ GROUP BY point
ORDER BY point ASC;
`

//nolint
// nolint
var tmplQueryReportSparklines = template.Must(template.New("queryReportSparklines").Funcs(funcMap).Parse(queryReportSparklinesTmpl))

// SelectSparklines selects datapoint for sparklines.
Expand Down
2 changes: 1 addition & 1 deletion services/analytics/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func isBoolMetric(name string) bool {
return ok
}

//nolint
// nolint
var specialColumnNames = map[string]struct{}{
"load": {},
"num_queries": {},
Expand Down
14 changes: 14 additions & 0 deletions services/analytics/object_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,17 @@ func (s *Service) QueryExists(ctx context.Context, in *qanpb.QueryExistsRequest)

return wrapperspb.Bool(resp), nil
}

// ExplainFingerprintByQueryID get explain fingerprint and placeholders count by query ID.
func (s *Service) ExplainFingerprintByQueryID(ctx context.Context, in *qanpb.ExplainFingerprintByQueryIDRequest) (*qanpb.ExplainFingerprintByQueryIDReply, error) {
res, err := s.mm.ExplainFingerprintByQueryID(
ctx,
in.Serviceid,
in.QueryId,
)
if err != nil {
return nil, fmt.Errorf("error in checking query:%v", err)
}

return res, nil
}
5 changes: 5 additions & 0 deletions test_data/GetQueryExample_no_group.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"example": "SELECT @@GLOBAL.slow_query_log_file",
"exampleFormat": "EXAMPLE",
"exampleType": "RANDOM",
"queryId": "B305F6354FA21F2A",
"serviceId": "service_id1",
"serviceType": "service_type1",
"schema": "schema29",
Expand All @@ -15,6 +16,7 @@
"example": "SELECT @@GLOBAL.slow_query_log_file",
"exampleFormat": "EXAMPLE",
"exampleType": "RANDOM",
"queryId": "B305F6354FA21F2A",
"serviceId": "service_id1",
"serviceType": "service_type1",
"schema": "schema37",
Expand All @@ -26,6 +28,7 @@
"example": "SELECT @@GLOBAL.slow_query_log_file",
"exampleFormat": "EXAMPLE",
"exampleType": "RANDOM",
"queryId": "B305F6354FA21F2A",
"serviceId": "service_id1",
"serviceType": "service_type1",
"schema": "schema58",
Expand All @@ -37,6 +40,7 @@
"example": "SELECT @@GLOBAL.slow_query_log_file",
"exampleFormat": "EXAMPLE",
"exampleType": "RANDOM",
"queryId": "B305F6354FA21F2A",
"serviceId": "service_id1",
"serviceType": "service_type1",
"schema": "schema60",
Expand All @@ -48,6 +52,7 @@
"example": "SELECT @@GLOBAL.slow_query_log_file",
"exampleFormat": "EXAMPLE",
"exampleType": "RANDOM",
"queryId": "B305F6354FA21F2A",
"serviceId": "service_id1",
"serviceType": "service_type1",
"schema": "schema65",
Expand Down
1 change: 1 addition & 0 deletions test_data/GetQueryExample_no_limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"example": "SELECT @@GLOBAL.slow_query_log_file",
"exampleFormat": "EXAMPLE",
"exampleType": "RANDOM",
"queryId": "B305F6354FA21F2A",
"serviceId": "service_id1",
"serviceType": "service_type1",
"schema": "schema29",
Expand Down