Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
package collector

import "strings"
import "github.com/grafana/alloy/internal/component/database_observability"

var defaultExcludedSchemas = []string{"mysql", "performance_schema", "sys", "information_schema"}
var excludedSchemas = []string{"mysql", "performance_schema", "sys", "information_schema"}

var defaultExclusionClause = buildExclusionClause(defaultExcludedSchemas)
var exclusionClause = database_observability.BuildExclusionClause(excludedSchemas)

func buildExcludedSchemasClause(excludedSchemas []string) string {
if len(excludedSchemas) == 0 {
return defaultExclusionClause
func buildExcludedSchemasClause(schemas []string) string {
if len(schemas) == 0 {
return exclusionClause
}

allSchemas := make([]string, 0, len(defaultExcludedSchemas)+len(excludedSchemas))
allSchemas = append(allSchemas, defaultExcludedSchemas...)
allSchemas = append(allSchemas, excludedSchemas...)

return buildExclusionClause(allSchemas)
}

func buildExclusionClause(schemas []string) string {
escaped := make([]string, len(schemas))
for i, schema := range schemas {
escaped[i] = escapeSQLString(schema)
}
return "(" + strings.Join(escaped, ", ") + ")"
}

// escapeSQLString escapes single quotes by doubling them to prevent SQL injection.
func escapeSQLString(s string) string {
escaped := strings.ReplaceAll(s, "'", "''")
return "'" + escaped + "'"
all := make([]string, 0, len(excludedSchemas)+len(schemas))
all = append(all, excludedSchemas...)
all = append(all, schemas...)
return database_observability.BuildExclusionClause(all)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ func TestExplainPlans(t *testing.T) {

t.Run("uses argument value on first request", func(t *testing.T) {
nextSeen := lastSeen.Add(time.Second * 45)
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_text",
Expand All @@ -1551,7 +1551,7 @@ func TestExplainPlans(t *testing.T) {
})

t.Run("uses oldest last seen value on subsequent requests", func(t *testing.T) {
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_text",
Expand Down Expand Up @@ -1594,7 +1594,7 @@ func TestExplainPlans(t *testing.T) {

t.Run("skips truncated queries", func(t *testing.T) {
logBuffer.Reset()
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down Expand Up @@ -1629,7 +1629,7 @@ func TestExplainPlans(t *testing.T) {
t.Run("skips non-select queries", func(t *testing.T) {
lokiClient.Clear()
logBuffer.Reset()
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down Expand Up @@ -1678,7 +1678,7 @@ func TestExplainPlans(t *testing.T) {

t.Run("skips no row result", func(t *testing.T) {
logBuffer.Reset()
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down Expand Up @@ -1710,7 +1710,7 @@ func TestExplainPlans(t *testing.T) {
t.Run("passes queries beginning in select", func(t *testing.T) {
lokiClient.Clear()
logBuffer.Reset()
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down Expand Up @@ -1747,7 +1747,7 @@ func TestExplainPlans(t *testing.T) {
t.Run("passes queries beginning in with", func(t *testing.T) {
lokiClient.Clear()
logBuffer.Reset()
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down Expand Up @@ -1809,7 +1809,7 @@ func TestQueryFailureDenylist(t *testing.T) {
})
require.NoError(t, err)

mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down Expand Up @@ -1841,7 +1841,7 @@ func TestQueryFailureDenylist(t *testing.T) {
lokiClient.Clear()
logBuffer.Reset()

mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, defaultExclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
mock.ExpectQuery(fmt.Sprintf(selectDigestsForExplainPlan, exclusionClause)).WithArgs(lastSeen).RowsWillBeClosed().WillReturnRows(sqlmock.NewRows([]string{
"schema_name",
"digest",
"query_sample_text",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func TestQueryTables(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, collector)

mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, defaultExclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, exclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
WillReturnRows(
sqlmock.NewRows([]string{
"digest",
Expand Down Expand Up @@ -439,15 +439,15 @@ func TestQueryTablesSQLDriverErrors(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, collector)

mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, defaultExclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, exclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
WillReturnRows(
sqlmock.NewRows([]string{
"digest", // not enough columns
}).AddRow(
"abc123",
))

mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, defaultExclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, exclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
WillReturnRows(
sqlmock.NewRows([]string{
"digest",
Expand Down Expand Up @@ -505,7 +505,7 @@ func TestQueryTablesSQLDriverErrors(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, collector)

mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, defaultExclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, exclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
WillReturnRows(
sqlmock.NewRows([]string{
"digest",
Expand Down Expand Up @@ -568,9 +568,9 @@ func TestQueryTablesSQLDriverErrors(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, collector)

mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, defaultExclusionClause, 250)).WithoutArgs().WillReturnError(fmt.Errorf("connection error"))
mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, exclusionClause, 250)).WithoutArgs().WillReturnError(fmt.Errorf("connection error"))

mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, defaultExclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
mock.ExpectQuery(fmt.Sprintf(selectQueryTablesSamples, exclusionClause, 250)).WithoutArgs().RowsWillBeClosed().
WillReturnRows(
sqlmock.NewRows([]string{
"digest",
Expand Down
Loading
Loading