mirror of
https://github.com/SigNoz/signoz.git
synced 2026-11-29 03:30:40 +00:00
Compare commits
32 Commits
perf/uplot
...
tvats-tele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3721e28826 | ||
|
|
9f888978dc | ||
|
|
907e59f317 | ||
|
|
4f423ac50f | ||
|
|
e0bfc56556 | ||
|
|
8fe86ffe3f | ||
|
|
0b45c8d786 | ||
|
|
6265c0c1c8 | ||
|
|
cba0354826 | ||
|
|
400a66c83d | ||
|
|
be6bf4e0a5 | ||
|
|
85c6c1547e | ||
|
|
02591cce68 | ||
|
|
5874b393c3 | ||
|
|
de57bb88f5 | ||
|
|
2b6bce9530 | ||
|
|
d8cdb9a3bf | ||
|
|
243b3f2636 | ||
|
|
fec811acf1 | ||
|
|
1367e0ab40 | ||
|
|
9436d63ef2 | ||
|
|
e68f368c0f | ||
|
|
1c18a61845 | ||
|
|
985ed61a35 | ||
|
|
b7b1cbca87 | ||
|
|
68c6da8db8 | ||
|
|
9a8c15fa6d | ||
|
|
857185b41d | ||
|
|
7f6a2611d0 | ||
|
|
0cf34784b5 | ||
|
|
48cffc91a2 | ||
|
|
8bd3d6276d |
@@ -24,6 +24,14 @@ packages:
|
||||
filename: "schema_metadata_store.go"
|
||||
structname: 'Mock{{.InterfaceName}}'
|
||||
pkgname: '{{.SrcPackageName}}test'
|
||||
github.com/SigNoz/signoz/pkg/telemetrystore:
|
||||
interfaces:
|
||||
TelemetryStoreHook:
|
||||
config:
|
||||
dir: '{{.InterfaceDir}}/telemetrystorehooktest'
|
||||
filename: "hook.go"
|
||||
structname: 'Mock{{.InterfaceName}}'
|
||||
pkgname: 'telemetrystorehooktest'
|
||||
github.com/SigNoz/signoz/pkg/tokenizer:
|
||||
config:
|
||||
all: true
|
||||
|
||||
@@ -205,6 +205,8 @@ telemetrystore:
|
||||
max_bytes_to_read: 0
|
||||
max_result_rows: 0
|
||||
ignore_data_skipping_indices: ""
|
||||
# ClickHouse settings profiles queries can run with, for example clickhouse_query. Each must exist in ClickHouse.
|
||||
profiles: []
|
||||
|
||||
##################### Prometheus #####################
|
||||
prometheus:
|
||||
|
||||
@@ -10,6 +10,10 @@ import (
|
||||
|
||||
const DefaultMaxConcurrentQueries = 8
|
||||
|
||||
// ClickHouseSQLProfile is the ClickHouse settings profile that user-written ClickHouse SQL queries run with,
|
||||
// once it is listed in telemetrystore.clickhouse.profiles.
|
||||
const ClickHouseSQLProfile = "clickhouse_query"
|
||||
|
||||
// Config represents the configuration for the querier.
|
||||
type Config struct {
|
||||
// CacheTTL is the TTL for cached query results
|
||||
|
||||
@@ -233,7 +233,7 @@ func (q *querier) buildQueries(
|
||||
if !ok {
|
||||
return nil, nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "invalid clickhouse query spec %T", query.Spec)
|
||||
}
|
||||
chSQLQuery := newchSQLQuery(q.logger, q.telemetryStore, chQuery, nil, qbtypes.TimeRange{From: req.Start, To: req.End}, req.RequestType, tmplVars)
|
||||
chSQLQuery := newchSQLQuery(q.logger, q.telemetryStore.WithProfile(ClickHouseSQLProfile), chQuery, nil, qbtypes.TimeRange{From: req.Start, To: req.End}, req.RequestType, tmplVars)
|
||||
queries[chQuery.Name] = chSQLQuery
|
||||
case qbtypes.QueryTypeTraceOperator:
|
||||
traceOpQuery, ok := query.Spec.(qbtypes.QueryBuilderTraceOperator)
|
||||
@@ -925,7 +925,7 @@ func (q *querier) createRangedQuery(_ valuer.UUID, originalQuery qbtypes.Query,
|
||||
queryCopy := qt.query.Copy()
|
||||
argsCopy := make([]any, len(qt.args))
|
||||
copy(argsCopy, qt.args)
|
||||
return newchSQLQuery(q.logger, q.telemetryStore, queryCopy, argsCopy, timeRange, qt.kind, qt.vars)
|
||||
return newchSQLQuery(q.logger, q.telemetryStore.WithProfile(ClickHouseSQLProfile), queryCopy, argsCopy, timeRange, qt.kind, qt.vars)
|
||||
|
||||
// A builder query's window was moved into the shifted clock when it was
|
||||
// built, so a range taken from that window must not be shifted again.
|
||||
|
||||
@@ -26,6 +26,11 @@ var (
|
||||
ErrCodeNotAnAggregate = errors.MustNewCode("not_an_aggregate")
|
||||
ErrCodeIllegalAggregation = errors.MustNewCode("illegal_aggregation")
|
||||
ErrCodeInvalidRegexp = errors.MustNewCode("invalid_regexp")
|
||||
ErrCodeSettingNotAllowed = errors.MustNewCode("setting_not_allowed")
|
||||
ErrCodeSettingsProfileNotFound = errors.MustNewCode("settings_profile_not_found")
|
||||
ErrCodeResultTooLarge = errors.MustNewCode("result_too_large")
|
||||
ErrCodeReadTooLarge = errors.MustNewCode("read_too_large")
|
||||
ErrCodeReadonlyMode = errors.MustNewCode("readonly_mode")
|
||||
|
||||
ErrCodeQueryCanceled = errors.MustNewCode("query_canceled")
|
||||
ErrCodeQueryTimeout = errors.MustNewCode("query_timeout")
|
||||
@@ -111,11 +116,42 @@ var clickHouseExceptionWrappers = map[chproto.Error]func(cause error, ex *clickh
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeInvalidRegexp, "regular expression does not compile: %s", ex.Message).
|
||||
WithSuggestions("write the pattern in RE2 syntax, which has no lookaround or backreferences, and escape a backslash that is meant literally")
|
||||
},
|
||||
chproto.ErrTooManyRowsOrBytes: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeResultTooLarge, "query result too large: %s", ex.Message).
|
||||
WithSuggestions("add a LIMIT, narrow the time range or select fewer columns")
|
||||
},
|
||||
chproto.ErrTooManyRows: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeReadTooLarge, "query read too much data: %s", ex.Message).
|
||||
WithSuggestions("narrow the time range or add filters so the query reads less data")
|
||||
},
|
||||
chproto.ErrTooManyPartitions: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeReadTooLarge, "query read too much data: %s", ex.Message).
|
||||
WithSuggestions("narrow the time range or add filters so the query reads less data")
|
||||
},
|
||||
chproto.ErrTooManyBytes: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeReadTooLarge, "query read too much data: %s", ex.Message).
|
||||
WithSuggestions("narrow the time range or add filters so the query reads less data")
|
||||
},
|
||||
// A write, or a change to the readonly setting, under a read-only settings profile.
|
||||
chproto.ErrReadonly: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeReadonlyMode, "query not allowed in read-only mode: %s", ex.Message).
|
||||
WithSuggestions("run only SELECT queries and remove readonly from the SETTINGS clause")
|
||||
},
|
||||
chproto.ErrThereIsNoProfile: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapNotFoundf(cause, ErrCodeSettingsProfileNotFound, "settings profile not found: %s", ex.Message)
|
||||
},
|
||||
// A SETTINGS clause went past a constraint of the query's settings profile.
|
||||
chproto.ErrSettingConstraintViolation: func(cause error, ex *clickhouse.Exception) error {
|
||||
return errors.WrapInvalidInputf(cause, ErrCodeSettingNotAllowed, "setting not allowed: %s", ex.Message).
|
||||
WithSuggestions("remove the setting from the SETTINGS clause of the query")
|
||||
},
|
||||
}
|
||||
|
||||
type provider struct {
|
||||
settings factory.ScopedProviderSettings
|
||||
clickHouseConn clickhouse.Conn
|
||||
profile string
|
||||
profiles map[string]telemetrystore.TelemetryStore
|
||||
cluster string
|
||||
hooks []telemetrystore.TelemetryStoreHook
|
||||
}
|
||||
@@ -178,49 +214,81 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
|
||||
cluster: config.Clickhouse.Cluster,
|
||||
hooks: hooks,
|
||||
}
|
||||
|
||||
p.withProfiles(config.Clickhouse.Profiles)
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *provider) ClickhouseDB() clickhouse.Conn {
|
||||
return p
|
||||
// withProfiles gives every profile a copy of the provider that runs its queries with that profile over the
|
||||
// provider's pool.
|
||||
func (provider *provider) withProfiles(profiles []string) {
|
||||
provider.profiles = make(map[string]telemetrystore.TelemetryStore, len(profiles))
|
||||
for _, profile := range profiles {
|
||||
profiled := *provider
|
||||
profiled.profile = profile
|
||||
profiled.hooks = make([]telemetrystore.TelemetryStoreHook, len(provider.hooks))
|
||||
for i, hook := range provider.hooks {
|
||||
if profileHook, ok := hook.(telemetrystore.TelemetryStoreProfileHook); ok {
|
||||
hook = profileHook.WithProfile(profile)
|
||||
}
|
||||
profiled.hooks[i] = hook
|
||||
}
|
||||
provider.profiles[profile] = &profiled
|
||||
}
|
||||
}
|
||||
|
||||
func (p *provider) Estimate(ctx context.Context, stmt string, args ...any) ([]telemetrystoretypes.EstimateEntry, error) {
|
||||
return RunExplainEstimate(ctx, p, stmt, args...)
|
||||
func (provider *provider) WithProfile(profile string) telemetrystore.TelemetryStore {
|
||||
if store, ok := provider.profiles[profile]; ok {
|
||||
return store
|
||||
}
|
||||
return provider
|
||||
}
|
||||
|
||||
func (p *provider) Plan(ctx context.Context, stmt string, args ...any) error {
|
||||
return RunExplainPlan(ctx, p, stmt, args...)
|
||||
func (provider *provider) ClickhouseDB() clickhouse.Conn {
|
||||
return provider
|
||||
}
|
||||
|
||||
func (p *provider) Indexes(ctx context.Context, stmt string, args ...any) (telemetrystoretypes.Granules, bool, error) {
|
||||
return RunExplainIndexes(ctx, p, stmt, args...)
|
||||
func (provider *provider) Estimate(ctx context.Context, stmt string, args ...any) ([]telemetrystoretypes.EstimateEntry, error) {
|
||||
return RunExplainEstimate(ctx, provider, stmt, args...)
|
||||
}
|
||||
|
||||
func (p *provider) Cluster() string {
|
||||
return p.cluster
|
||||
func (provider *provider) Plan(ctx context.Context, stmt string, args ...any) error {
|
||||
return RunExplainPlan(ctx, provider, stmt, args...)
|
||||
}
|
||||
|
||||
func (p *provider) Close() error {
|
||||
return p.clickHouseConn.Close()
|
||||
func (provider *provider) Indexes(ctx context.Context, stmt string, args ...any) (telemetrystoretypes.Granules, bool, error) {
|
||||
return RunExplainIndexes(ctx, provider, stmt, args...)
|
||||
}
|
||||
|
||||
func (p *provider) Ping(ctx context.Context) error {
|
||||
return p.clickHouseConn.Ping(ctx)
|
||||
func (provider *provider) Cluster() string {
|
||||
return provider.cluster
|
||||
}
|
||||
|
||||
func (p *provider) Stats() driver.Stats {
|
||||
return p.clickHouseConn.Stats()
|
||||
// Close leaves the pool open for a profile store, which shares it with the store it was made from.
|
||||
func (provider *provider) Close() error {
|
||||
if provider.profile != "" {
|
||||
return nil
|
||||
}
|
||||
return provider.clickHouseConn.Close()
|
||||
}
|
||||
|
||||
func (p *provider) Query(ctx context.Context, query string, args ...interface{}) (driver.Rows, error) {
|
||||
func (provider *provider) Ping(ctx context.Context) error {
|
||||
return provider.clickHouseConn.Ping(ctx)
|
||||
}
|
||||
|
||||
func (provider *provider) Stats() driver.Stats {
|
||||
return provider.clickHouseConn.Stats()
|
||||
}
|
||||
|
||||
func (provider *provider) Query(ctx context.Context, query string, args ...interface{}) (driver.Rows, error) {
|
||||
event := telemetrystore.NewQueryEvent(query, args)
|
||||
|
||||
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
|
||||
rows, err := p.clickHouseConn.Query(ctx, query, args...)
|
||||
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
|
||||
rows, err := provider.clickHouseConn.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
event.Err = err
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
return nil, castError(err)
|
||||
}
|
||||
|
||||
@@ -228,71 +296,71 @@ func (p *provider) Query(ctx context.Context, query string, args ...interface{})
|
||||
Rows: telemetrystore.WrapRows(rows),
|
||||
ctx: ctx,
|
||||
event: event,
|
||||
onClose: func() { telemetrystore.WrapAfterQuery(p.hooks, ctx, event) },
|
||||
onClose: func() { telemetrystore.WrapAfterQuery(provider.hooks, ctx, event) },
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *provider) QueryRow(ctx context.Context, query string, args ...interface{}) driver.Row {
|
||||
func (provider *provider) QueryRow(ctx context.Context, query string, args ...interface{}) driver.Row {
|
||||
event := telemetrystore.NewQueryEvent(query, args)
|
||||
|
||||
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
|
||||
row := p.clickHouseConn.QueryRow(ctx, query, args...)
|
||||
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
|
||||
row := provider.clickHouseConn.QueryRow(ctx, query, args...)
|
||||
|
||||
if row == nil {
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
return nil
|
||||
}
|
||||
|
||||
event.Err = row.Err()
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
|
||||
return &rowWithCastError{Row: row}
|
||||
}
|
||||
|
||||
func (p *provider) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||
func (provider *provider) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||
event := telemetrystore.NewQueryEvent(query, args)
|
||||
|
||||
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
|
||||
err := p.clickHouseConn.Select(ctx, dest, query, args...)
|
||||
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
|
||||
err := provider.clickHouseConn.Select(ctx, dest, query, args...)
|
||||
|
||||
event.Err = err
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
|
||||
return castError(err)
|
||||
}
|
||||
|
||||
func (p *provider) Exec(ctx context.Context, query string, args ...interface{}) error {
|
||||
func (provider *provider) Exec(ctx context.Context, query string, args ...interface{}) error {
|
||||
event := telemetrystore.NewQueryEvent(query, args)
|
||||
|
||||
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
|
||||
err := p.clickHouseConn.Exec(ctx, query, args...)
|
||||
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
|
||||
err := provider.clickHouseConn.Exec(ctx, query, args...)
|
||||
|
||||
event.Err = err
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
|
||||
return castError(err)
|
||||
}
|
||||
|
||||
func (p *provider) AsyncInsert(ctx context.Context, query string, wait bool, args ...interface{}) error {
|
||||
func (provider *provider) AsyncInsert(ctx context.Context, query string, wait bool, args ...interface{}) error {
|
||||
event := telemetrystore.NewQueryEvent(query, args)
|
||||
|
||||
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
|
||||
err := p.clickHouseConn.Exec(clickhouse.Context(ctx, clickhouse.WithAsync(wait)), query, args...)
|
||||
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
|
||||
err := provider.clickHouseConn.Exec(clickhouse.Context(ctx, clickhouse.WithAsync(wait)), query, args...)
|
||||
|
||||
event.Err = err
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
|
||||
return castError(err)
|
||||
}
|
||||
|
||||
func (p *provider) PrepareBatch(ctx context.Context, query string, opts ...driver.PrepareBatchOption) (driver.Batch, error) {
|
||||
func (provider *provider) PrepareBatch(ctx context.Context, query string, opts ...driver.PrepareBatchOption) (driver.Batch, error) {
|
||||
event := telemetrystore.NewQueryEvent(query, nil)
|
||||
|
||||
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
|
||||
batch, err := p.clickHouseConn.PrepareBatch(ctx, query, opts...)
|
||||
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
|
||||
batch, err := provider.clickHouseConn.PrepareBatch(ctx, query, opts...)
|
||||
|
||||
event.Err = err
|
||||
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
|
||||
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
|
||||
|
||||
if batch == nil {
|
||||
return nil, castError(err)
|
||||
@@ -300,12 +368,12 @@ func (p *provider) PrepareBatch(ctx context.Context, query string, opts ...drive
|
||||
return &batchWithCastError{Batch: batch}, castError(err)
|
||||
}
|
||||
|
||||
func (p *provider) ServerVersion() (*driver.ServerVersion, error) {
|
||||
return p.clickHouseConn.ServerVersion()
|
||||
func (provider *provider) ServerVersion() (*driver.ServerVersion, error) {
|
||||
return provider.clickHouseConn.ServerVersion()
|
||||
}
|
||||
|
||||
func (p *provider) Contributors() []string {
|
||||
return p.clickHouseConn.Contributors()
|
||||
func (provider *provider) Contributors() []string {
|
||||
return provider.clickHouseConn.Contributors()
|
||||
}
|
||||
|
||||
func castError(err error) error {
|
||||
|
||||
79
pkg/telemetrystore/clickhousetelemetrystore/provider_test.go
Normal file
79
pkg/telemetrystore/clickhousetelemetrystore/provider_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package clickhousetelemetrystore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
chproto "github.com/ClickHouse/ch-go/proto"
|
||||
"github.com/ClickHouse/clickhouse-go/v2"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
cmock "github.com/SigNoz/clickhouse-go-mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/factory/factorytest"
|
||||
"github.com/SigNoz/signoz/pkg/telemetrystore"
|
||||
"github.com/SigNoz/signoz/pkg/telemetrystore/telemetrystorehook"
|
||||
"github.com/SigNoz/signoz/pkg/telemetrystore/telemetrystorehooktest"
|
||||
)
|
||||
|
||||
func TestCastError_ProfileErrors(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
code chproto.Error
|
||||
want errors.Code
|
||||
notFound bool
|
||||
}{
|
||||
{name: "SettingConstraintViolation_SettingNotAllowed", code: chproto.ErrSettingConstraintViolation, want: ErrCodeSettingNotAllowed},
|
||||
{name: "TooManyRowsOrBytes_ResultTooLarge", code: chproto.ErrTooManyRowsOrBytes, want: ErrCodeResultTooLarge},
|
||||
{name: "TooManyRows_ReadTooLarge", code: chproto.ErrTooManyRows, want: ErrCodeReadTooLarge},
|
||||
{name: "TooManyBytes_ReadTooLarge", code: chproto.ErrTooManyBytes, want: ErrCodeReadTooLarge},
|
||||
{name: "TooManyPartitions_ReadTooLarge", code: chproto.ErrTooManyPartitions, want: ErrCodeReadTooLarge},
|
||||
{name: "Readonly_ReadonlyMode", code: chproto.ErrReadonly, want: ErrCodeReadonlyMode},
|
||||
{name: "ThereIsNoProfile_SettingsProfileNotFound", code: chproto.ErrThereIsNoProfile, want: ErrCodeSettingsProfileNotFound, notFound: true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := castError(&clickhouse.Exception{Code: int32(testCase.code), Message: testCase.name})
|
||||
|
||||
require.Error(t, err)
|
||||
assert.True(t, errors.Asc(err, testCase.want), "got %v", err)
|
||||
assert.Equal(t, testCase.notFound, errors.Ast(err, errors.TypeNotFound), "got %v", err)
|
||||
assert.Equal(t, !testCase.notFound, errors.Ast(err, errors.TypeInvalidInput), "got %v", err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderWithProfile(t *testing.T) {
|
||||
conn, err := cmock.NewClickHouseWithQueryMatcher(&clickhouse.Options{}, sqlmock.QueryMatcherEqual)
|
||||
require.NoError(t, err)
|
||||
conn.ExpectClose()
|
||||
|
||||
settingsHook, err := telemetrystorehook.NewSettings(context.Background(), factorytest.NewSettings(), telemetrystore.Config{})
|
||||
require.NoError(t, err)
|
||||
otherHook := telemetrystorehooktest.NewMockTelemetryStoreHook(t)
|
||||
|
||||
p := &provider{clickHouseConn: conn, cluster: "cluster", hooks: []telemetrystore.TelemetryStoreHook{settingsHook, otherHook}}
|
||||
p.withProfiles([]string{"clickhouse_query"})
|
||||
|
||||
profiled, ok := p.WithProfile("clickhouse_query").(*provider)
|
||||
require.True(t, ok)
|
||||
assert.NotSame(t, p, profiled)
|
||||
assert.Equal(t, "clickhouse_query", profiled.profile)
|
||||
assert.Same(t, p.clickHouseConn, profiled.clickHouseConn)
|
||||
assert.Equal(t, "cluster", profiled.Cluster())
|
||||
assert.Empty(t, p.profile)
|
||||
require.Len(t, profiled.hooks, 2)
|
||||
assert.Equal(t, settingsHook.(telemetrystore.TelemetryStoreProfileHook).WithProfile("clickhouse_query"), profiled.hooks[0])
|
||||
assert.Same(t, otherHook, profiled.hooks[1])
|
||||
assert.Same(t, settingsHook, p.hooks[0])
|
||||
|
||||
assert.Same(t, p, p.WithProfile("query_range"))
|
||||
assert.Same(t, profiled, profiled.WithProfile("clickhouse_query"))
|
||||
|
||||
require.NoError(t, profiled.Close())
|
||||
require.NoError(t, p.Close())
|
||||
assert.NoError(t, conn.ExpectationsWereMet())
|
||||
}
|
||||
@@ -37,6 +37,10 @@ type ClickhouseConfig struct {
|
||||
|
||||
// QuerySettings is the query settings for clickhouse.
|
||||
QuerySettings QuerySettings `mapstructure:"settings"`
|
||||
|
||||
// Profiles are the settings profiles TelemetryStore.WithProfile can run queries with. Each must exist in
|
||||
// ClickHouse.
|
||||
Profiles []string `mapstructure:"profiles"`
|
||||
}
|
||||
|
||||
type QuerySettings struct {
|
||||
|
||||
@@ -11,6 +11,10 @@ type TelemetryStore interface {
|
||||
// ClickhouseDB returns the clickhouse connection, which can also EXPLAIN.
|
||||
ClickhouseDB() clickhouse.Conn
|
||||
|
||||
// WithProfile returns a store that runs every query with the ClickHouse settings profile, or this store
|
||||
// when the profile is not listed in the config.
|
||||
WithProfile(profile string) TelemetryStore
|
||||
|
||||
// Cluster returns the cluster name.
|
||||
Cluster() string
|
||||
|
||||
@@ -29,6 +33,12 @@ type TelemetryStoreHook interface {
|
||||
AfterQuery(ctx context.Context, event *QueryEvent)
|
||||
}
|
||||
|
||||
// TelemetryStoreProfileHook is a hook that TelemetryStore.WithProfile replaces with a copy for the profile.
|
||||
type TelemetryStoreProfileHook interface {
|
||||
TelemetryStoreHook
|
||||
WithProfile(profile string) TelemetryStoreHook
|
||||
}
|
||||
|
||||
func WrapBeforeQuery(hooks []TelemetryStoreHook, ctx context.Context, event *QueryEvent) context.Context {
|
||||
for _, hook := range hooks {
|
||||
ctx = hook.BeforeQuery(ctx, event)
|
||||
|
||||
@@ -4,13 +4,17 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/ClickHouse/clickhouse-go/v2"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/factory"
|
||||
"github.com/SigNoz/signoz/pkg/telemetrystore"
|
||||
"github.com/SigNoz/signoz/pkg/types/ctxtypes"
|
||||
)
|
||||
|
||||
var _ telemetrystore.TelemetryStoreProfileHook = (*provider)(nil)
|
||||
|
||||
type provider struct {
|
||||
settings telemetrystore.QuerySettings
|
||||
profile string
|
||||
}
|
||||
|
||||
func NewSettingsFactory() factory.ProviderFactory[telemetrystore.TelemetryStoreHook, telemetrystore.Config] {
|
||||
@@ -25,33 +29,33 @@ func NewSettings(ctx context.Context, providerSettings factory.ProviderSettings,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *provider) BeforeQuery(ctx context.Context, _ *telemetrystore.QueryEvent) context.Context {
|
||||
func (hook *provider) BeforeQuery(ctx context.Context, _ *telemetrystore.QueryEvent) context.Context {
|
||||
settings := clickhouse.Settings{}
|
||||
|
||||
settings["log_comment"] = ctxtypes.CommentFromContext(ctx).String()
|
||||
|
||||
if ctx.Value("enforce_max_result_rows") != nil {
|
||||
settings["max_result_rows"] = h.settings.MaxResultRows
|
||||
settings["max_result_rows"] = hook.settings.MaxResultRows
|
||||
}
|
||||
|
||||
if h.settings.MaxBytesToRead != 0 {
|
||||
settings["max_bytes_to_read"] = h.settings.MaxBytesToRead
|
||||
if hook.settings.MaxBytesToRead != 0 {
|
||||
settings["max_bytes_to_read"] = hook.settings.MaxBytesToRead
|
||||
}
|
||||
|
||||
if h.settings.MaxExecutionTime != 0 {
|
||||
settings["max_execution_time"] = h.settings.MaxExecutionTime
|
||||
if hook.settings.MaxExecutionTime != 0 {
|
||||
settings["max_execution_time"] = hook.settings.MaxExecutionTime
|
||||
}
|
||||
|
||||
if h.settings.MaxExecutionTimeLeaf != 0 {
|
||||
settings["max_execution_time_leaf"] = h.settings.MaxExecutionTimeLeaf
|
||||
if hook.settings.MaxExecutionTimeLeaf != 0 {
|
||||
settings["max_execution_time_leaf"] = hook.settings.MaxExecutionTimeLeaf
|
||||
}
|
||||
|
||||
if h.settings.TimeoutBeforeCheckingExecutionSpeed != 0 {
|
||||
settings["timeout_before_checking_execution_speed"] = h.settings.TimeoutBeforeCheckingExecutionSpeed
|
||||
if hook.settings.TimeoutBeforeCheckingExecutionSpeed != 0 {
|
||||
settings["timeout_before_checking_execution_speed"] = hook.settings.TimeoutBeforeCheckingExecutionSpeed
|
||||
}
|
||||
|
||||
if h.settings.IgnoreDataSkippingIndices != "" {
|
||||
settings["ignore_data_skipping_indices"] = h.settings.IgnoreDataSkippingIndices
|
||||
if hook.settings.IgnoreDataSkippingIndices != "" {
|
||||
settings["ignore_data_skipping_indices"] = hook.settings.IgnoreDataSkippingIndices
|
||||
}
|
||||
|
||||
if ctx.Value("clickhouse_max_threads") != nil {
|
||||
@@ -72,8 +76,20 @@ func (h *provider) BeforeQuery(ctx context.Context, _ *telemetrystore.QueryEvent
|
||||
settings["result_overflow_mode"] = ctx.Value("result_overflow_mode")
|
||||
}
|
||||
|
||||
if hook.profile != "" {
|
||||
// The server rejects profile as a plain setting over the native protocol but applies it as a custom
|
||||
// one. Every query sends it, so a SETTINGS clause in the query cannot escape its constraints.
|
||||
settings["profile"] = clickhouse.CustomSetting{Value: hook.profile}
|
||||
}
|
||||
|
||||
ctx = clickhouse.Context(ctx, clickhouse.WithSettings(settings))
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (h *provider) AfterQuery(ctx context.Context, event *telemetrystore.QueryEvent) {}
|
||||
func (hook *provider) WithProfile(profile string) telemetrystore.TelemetryStoreHook {
|
||||
profiled := *hook
|
||||
profiled.profile = profile
|
||||
return &profiled
|
||||
}
|
||||
|
||||
func (hook *provider) AfterQuery(ctx context.Context, event *telemetrystore.QueryEvent) {}
|
||||
|
||||
153
pkg/telemetrystore/telemetrystorehooktest/hook.go
Normal file
153
pkg/telemetrystore/telemetrystorehooktest/hook.go
Normal file
@@ -0,0 +1,153 @@
|
||||
// Code generated by mockery; DO NOT EDIT.
|
||||
// github.com/vektra/mockery
|
||||
// template: testify
|
||||
|
||||
package telemetrystorehooktest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/telemetrystore"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NewMockTelemetryStoreHook creates a new instance of MockTelemetryStoreHook. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockTelemetryStoreHook(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockTelemetryStoreHook {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
|
||||
mock := &MockTelemetryStoreHook{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
mock.AssertExpectations(t)
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockTelemetryStoreHook is an autogenerated mock type for the TelemetryStoreHook type
|
||||
type MockTelemetryStoreHook struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockTelemetryStoreHook_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockTelemetryStoreHook) EXPECT() *MockTelemetryStoreHook_Expecter {
|
||||
return &MockTelemetryStoreHook_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// AfterQuery provides a mock function for the type MockTelemetryStoreHook
|
||||
func (_mock *MockTelemetryStoreHook) AfterQuery(ctx context.Context, event *telemetrystore.QueryEvent) {
|
||||
_mock.Called(ctx, event)
|
||||
return
|
||||
}
|
||||
|
||||
// MockTelemetryStoreHook_AfterQuery_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AfterQuery'
|
||||
type MockTelemetryStoreHook_AfterQuery_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// AfterQuery is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - event *telemetrystore.QueryEvent
|
||||
func (_e *MockTelemetryStoreHook_Expecter) AfterQuery(ctx any, event any) *MockTelemetryStoreHook_AfterQuery_Call {
|
||||
return &MockTelemetryStoreHook_AfterQuery_Call{Call: _e.mock.On("AfterQuery", ctx, event)}
|
||||
}
|
||||
|
||||
func (_c *MockTelemetryStoreHook_AfterQuery_Call) Run(run func(ctx context.Context, event *telemetrystore.QueryEvent)) *MockTelemetryStoreHook_AfterQuery_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 *telemetrystore.QueryEvent
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(*telemetrystore.QueryEvent)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockTelemetryStoreHook_AfterQuery_Call) Return() *MockTelemetryStoreHook_AfterQuery_Call {
|
||||
_c.Call.Return()
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockTelemetryStoreHook_AfterQuery_Call) RunAndReturn(run func(ctx context.Context, event *telemetrystore.QueryEvent)) *MockTelemetryStoreHook_AfterQuery_Call {
|
||||
_c.Run(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// BeforeQuery provides a mock function for the type MockTelemetryStoreHook
|
||||
func (_mock *MockTelemetryStoreHook) BeforeQuery(ctx context.Context, event *telemetrystore.QueryEvent) context.Context {
|
||||
ret := _mock.Called(ctx, event)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for BeforeQuery")
|
||||
}
|
||||
|
||||
var r0 context.Context
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, *telemetrystore.QueryEvent) context.Context); ok {
|
||||
r0 = returnFunc(ctx, event)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(context.Context)
|
||||
}
|
||||
}
|
||||
return r0
|
||||
}
|
||||
|
||||
// MockTelemetryStoreHook_BeforeQuery_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BeforeQuery'
|
||||
type MockTelemetryStoreHook_BeforeQuery_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// BeforeQuery is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - event *telemetrystore.QueryEvent
|
||||
func (_e *MockTelemetryStoreHook_Expecter) BeforeQuery(ctx any, event any) *MockTelemetryStoreHook_BeforeQuery_Call {
|
||||
return &MockTelemetryStoreHook_BeforeQuery_Call{Call: _e.mock.On("BeforeQuery", ctx, event)}
|
||||
}
|
||||
|
||||
func (_c *MockTelemetryStoreHook_BeforeQuery_Call) Run(run func(ctx context.Context, event *telemetrystore.QueryEvent)) *MockTelemetryStoreHook_BeforeQuery_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 *telemetrystore.QueryEvent
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(*telemetrystore.QueryEvent)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockTelemetryStoreHook_BeforeQuery_Call) Return(context1 context.Context) *MockTelemetryStoreHook_BeforeQuery_Call {
|
||||
_c.Call.Return(context1)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockTelemetryStoreHook_BeforeQuery_Call) RunAndReturn(run func(ctx context.Context, event *telemetrystore.QueryEvent) context.Context) *MockTelemetryStoreHook_BeforeQuery_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
@@ -31,31 +31,36 @@ func New(_ telemetrystore.Config, matcher sqlmock.QueryMatcher) *Provider {
|
||||
}
|
||||
|
||||
// ClickhouseDB returns the mock Clickhouse connection.
|
||||
func (p *Provider) ClickhouseDB() clickhouse.Conn {
|
||||
return p.clickhouseDB.(clickhouse.Conn)
|
||||
func (provider *Provider) ClickhouseDB() clickhouse.Conn {
|
||||
return provider.clickhouseDB.(clickhouse.Conn)
|
||||
}
|
||||
|
||||
// WithProfile returns the same mock, so expectations set on it apply to profile queries.
|
||||
func (provider *Provider) WithProfile(_ string) telemetrystore.TelemetryStore {
|
||||
return provider
|
||||
}
|
||||
|
||||
// Cluster returns the cluster name.
|
||||
func (p *Provider) Cluster() string {
|
||||
func (provider *Provider) Cluster() string {
|
||||
return "cluster"
|
||||
}
|
||||
|
||||
// Estimate runs EXPLAIN ESTIMATE against the mock connection.
|
||||
func (p *Provider) Estimate(ctx context.Context, stmt string, args ...any) ([]telemetrystoretypes.EstimateEntry, error) {
|
||||
return clickhousetelemetrystore.RunExplainEstimate(ctx, p.clickhouseDB.(clickhouse.Conn), stmt, args...)
|
||||
func (provider *Provider) Estimate(ctx context.Context, stmt string, args ...any) ([]telemetrystoretypes.EstimateEntry, error) {
|
||||
return clickhousetelemetrystore.RunExplainEstimate(ctx, provider.clickhouseDB.(clickhouse.Conn), stmt, args...)
|
||||
}
|
||||
|
||||
// Plan runs EXPLAIN PLAN against the mock connection.
|
||||
func (p *Provider) Plan(ctx context.Context, stmt string, args ...any) error {
|
||||
return clickhousetelemetrystore.RunExplainPlan(ctx, p.clickhouseDB.(clickhouse.Conn), stmt, args...)
|
||||
func (provider *Provider) Plan(ctx context.Context, stmt string, args ...any) error {
|
||||
return clickhousetelemetrystore.RunExplainPlan(ctx, provider.clickhouseDB.(clickhouse.Conn), stmt, args...)
|
||||
}
|
||||
|
||||
// Indexes runs EXPLAIN indexes against the mock connection.
|
||||
func (p *Provider) Indexes(ctx context.Context, stmt string, args ...any) (telemetrystoretypes.Granules, bool, error) {
|
||||
return clickhousetelemetrystore.RunExplainIndexes(ctx, p.clickhouseDB.(clickhouse.Conn), stmt, args...)
|
||||
func (provider *Provider) Indexes(ctx context.Context, stmt string, args ...any) (telemetrystoretypes.Granules, bool, error) {
|
||||
return clickhousetelemetrystore.RunExplainIndexes(ctx, provider.clickhouseDB.(clickhouse.Conn), stmt, args...)
|
||||
}
|
||||
|
||||
// Mock returns the underlying Clickhouse mock instance for setting expectations.
|
||||
func (p *Provider) Mock() cmock.ClickConnMockCommon {
|
||||
return p.clickhouseDB
|
||||
func (provider *Provider) Mock() cmock.ClickConnMockCommon {
|
||||
return provider.clickhouseDB
|
||||
}
|
||||
|
||||
66
tests/fixtures/clickhouse.py
vendored
66
tests/fixtures/clickhouse.py
vendored
@@ -1,4 +1,7 @@
|
||||
import io
|
||||
import os
|
||||
import tarfile
|
||||
import time
|
||||
from collections.abc import Callable, Generator
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
@@ -595,3 +598,66 @@ def check_query_log(
|
||||
assert all(check_fn(q) for q in queries), f"query_log check failed for case '{case_name}'.\n" + "Queries:\n" + "\n---\n".join(queries)
|
||||
|
||||
return _check
|
||||
|
||||
|
||||
@pytest.fixture(name="clickhouse_settings_profile", scope="package")
|
||||
def clickhouse_settings_profile(
|
||||
clickhouse: types.TestContainerClickhouse,
|
||||
) -> Generator[Callable[[str, str], None], Any]:
|
||||
"""
|
||||
Yields create(name, settings) that defines a settings profile from the XML body of its
|
||||
<profiles> entry. Profiles are users.d files, since the integration user has no access
|
||||
management; ClickHouse reloads them without a restart. They are removed on teardown.
|
||||
"""
|
||||
container = docker.from_env().containers.get(clickhouse.container.id)
|
||||
names: list[str] = []
|
||||
|
||||
def wait_for_profile(name: str, exists: bool) -> None:
|
||||
deadline = time.monotonic() + 30
|
||||
while time.monotonic() < deadline:
|
||||
count = clickhouse.conn.query(
|
||||
"SELECT count() FROM system.settings_profiles WHERE name = {name:String}",
|
||||
parameters={"name": name},
|
||||
).result_rows[0][0]
|
||||
if (count == 1) == exists:
|
||||
return
|
||||
time.sleep(0.5)
|
||||
raise TimeoutError(f"settings profile {name} did not {'appear' if exists else 'disappear'}")
|
||||
|
||||
def create(name: str, settings: str) -> None:
|
||||
body = f"<clickhouse><profiles><{name}>{settings}</{name}></profiles></clickhouse>".encode()
|
||||
archive = io.BytesIO()
|
||||
with tarfile.open(fileobj=archive, mode="w") as tar:
|
||||
info = tarfile.TarInfo(f"{name}.xml")
|
||||
info.size = len(body)
|
||||
info.mode = 0o644
|
||||
tar.addfile(info, io.BytesIO(body))
|
||||
container.put_archive("/etc/clickhouse-server/users.d", archive.getvalue())
|
||||
names.append(name)
|
||||
wait_for_profile(name, exists=True)
|
||||
|
||||
yield create
|
||||
|
||||
for name in names:
|
||||
container.exec_run(["rm", "-f", f"/etc/clickhouse-server/users.d/{name}.xml"])
|
||||
for name in names:
|
||||
wait_for_profile(name, exists=False)
|
||||
|
||||
|
||||
@pytest.fixture(name="clickhouse_partitioned_table", scope="package")
|
||||
def clickhouse_partitioned_table(
|
||||
clickhouse: types.TestContainerClickhouse,
|
||||
) -> Generator[str, Any]:
|
||||
"""
|
||||
Yields a MergeTree table with one partition per day for 5 days, where the first day holds
|
||||
300000 rows, for queries that hit limits on partitions or rows read.
|
||||
"""
|
||||
table = "default.integration_partitioned"
|
||||
clickhouse.conn.command(f"CREATE TABLE IF NOT EXISTS {table} (day Date, value UInt8) ENGINE = MergeTree PARTITION BY day ORDER BY value")
|
||||
clickhouse.conn.command(f"TRUNCATE TABLE {table}")
|
||||
clickhouse.conn.command(f"INSERT INTO {table} SELECT toDate('2026-01-01'), 1 FROM numbers(300000)")
|
||||
clickhouse.conn.command(f"INSERT INTO {table} SELECT toDate('2026-01-02') + number, 1 FROM numbers(4)")
|
||||
|
||||
yield table
|
||||
|
||||
clickhouse.conn.command(f"DROP TABLE IF EXISTS {table}")
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
from collections.abc import Callable
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from http import HTTPStatus
|
||||
|
||||
import pytest
|
||||
|
||||
from fixtures import querier, types
|
||||
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
|
||||
|
||||
|
||||
def test_clickhouse_sql_runs_with_profile(
|
||||
signoz_clickhouse_query_profile: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
|
||||
|
||||
response = querier.make_query_request(
|
||||
signoz_clickhouse_query_profile,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
int((now - timedelta(hours=1)).timestamp() * 1000),
|
||||
int(now.timestamp() * 1000),
|
||||
[{"type": "clickhouse_sql", "spec": {"name": "A", "query": "SELECT toFloat64(getSetting('readonly')) AS readonly, toFloat64(getSetting('max_result_rows')) AS max_result_rows", "disabled": False}}],
|
||||
request_type=querier.RequestType.SCALAR,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
assert response.json()["data"]["data"]["results"][0]["data"][0] == [2, 100]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"settings",
|
||||
[
|
||||
"max_result_rows = 1000",
|
||||
"max_result_rows = 0",
|
||||
"max_result_bytes = 1000000",
|
||||
"max_bytes_to_read = 10000000",
|
||||
"max_rows_to_read = 10000000",
|
||||
"max_partitions_to_read = 100",
|
||||
"max_partitions_to_read = -1",
|
||||
"profile = 'default', max_result_rows = 1000",
|
||||
"max_result_rows = 1000, profile = 'default'",
|
||||
],
|
||||
ids=[
|
||||
"result_rows_above_max",
|
||||
"result_rows_zero_is_unlimited",
|
||||
"result_bytes_above_max",
|
||||
"bytes_to_read_above_max",
|
||||
"rows_to_read_above_max",
|
||||
"partitions_to_read_above_max",
|
||||
"partitions_to_read_unlimited",
|
||||
"switch_profile_then_raise",
|
||||
"raise_then_switch_profile",
|
||||
],
|
||||
)
|
||||
def test_clickhouse_sql_settings_cannot_escape_profile(
|
||||
signoz_clickhouse_query_profile: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
settings: str,
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
|
||||
|
||||
response = querier.make_query_request(
|
||||
signoz_clickhouse_query_profile,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
int((now - timedelta(hours=1)).timestamp() * 1000),
|
||||
int(now.timestamp() * 1000),
|
||||
[{"type": "clickhouse_sql", "spec": {"name": "A", "query": f"SELECT toFloat64(1) AS value SETTINGS {settings}", "disabled": False}}],
|
||||
request_type=querier.RequestType.SCALAR,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
|
||||
error = response.json()["error"]
|
||||
assert (error["code"], error["suggestions"]) == ("setting_not_allowed", ["remove the setting from the SETTINGS clause of the query"]), error
|
||||
|
||||
|
||||
RESULT_TOO_LARGE = "add a LIMIT, narrow the time range or select fewer columns"
|
||||
READ_TOO_LARGE = "narrow the time range or add filters so the query reads less data"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query,code,suggestion,reason",
|
||||
[
|
||||
("SELECT toFloat64(number) AS value FROM numbers(1000)", "result_too_large", RESULT_TOO_LARGE, "max rows"),
|
||||
("SELECT repeat(toString(number), 2000) AS text, toFloat64(number) AS value FROM numbers(90)", "result_too_large", RESULT_TOO_LARGE, "max bytes"),
|
||||
("SELECT toFloat64(count()) AS value FROM numbers(150000)", "read_too_large", READ_TOO_LARGE, "max bytes"),
|
||||
("SELECT toFloat64(sum(value)) AS value FROM {table} WHERE day = '2026-01-01'", "read_too_large", READ_TOO_LARGE, "max rows"),
|
||||
("SELECT toFloat64(sum(value)) AS value FROM {table} WHERE day > '2026-01-01'", "read_too_large", READ_TOO_LARGE, "partitions"),
|
||||
],
|
||||
ids=["result_rows", "result_bytes", "bytes_read", "rows_read", "partitions_read"],
|
||||
)
|
||||
def test_clickhouse_sql_over_profile_limit( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
||||
signoz_clickhouse_query_profile: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
clickhouse_partitioned_table: str,
|
||||
query: str,
|
||||
code: str,
|
||||
suggestion: str,
|
||||
reason: str,
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
|
||||
|
||||
response = querier.make_query_request(
|
||||
signoz_clickhouse_query_profile,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
int((now - timedelta(hours=1)).timestamp() * 1000),
|
||||
int(now.timestamp() * 1000),
|
||||
[{"type": "clickhouse_sql", "spec": {"name": "A", "query": query.format(table=clickhouse_partitioned_table), "disabled": False}}],
|
||||
request_type=querier.RequestType.SCALAR,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
|
||||
error = response.json()["error"]
|
||||
assert (error["code"], error["suggestions"]) == (code, [suggestion]), error
|
||||
assert reason in error["message"], error
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name,value",
|
||||
[("readonly", 2), ("max_result_rows", 50), ("max_threads", 1)],
|
||||
ids=["readonly_same_as_profile", "result_rows_below_max", "unconstrained_setting"],
|
||||
)
|
||||
def test_clickhouse_sql_settings_within_profile(
|
||||
signoz_clickhouse_query_profile: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
name: str,
|
||||
value: int,
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
|
||||
|
||||
response = querier.make_query_request(
|
||||
signoz_clickhouse_query_profile,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
int((now - timedelta(hours=1)).timestamp() * 1000),
|
||||
int(now.timestamp() * 1000),
|
||||
[{"type": "clickhouse_sql", "spec": {"name": "A", "query": f"SELECT toFloat64(getSetting('{name}')) AS value SETTINGS {name} = {value}", "disabled": False}}],
|
||||
request_type=querier.RequestType.SCALAR,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
assert response.json()["data"]["data"]["results"][0]["data"][0][0] == value
|
||||
|
||||
|
||||
def test_clickhouse_sql_lower_limit_in_settings_is_enforced(
|
||||
signoz_clickhouse_query_profile: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
|
||||
|
||||
response = querier.make_query_request(
|
||||
signoz_clickhouse_query_profile,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
int((now - timedelta(hours=1)).timestamp() * 1000),
|
||||
int(now.timestamp() * 1000),
|
||||
[{"type": "clickhouse_sql", "spec": {"name": "A", "query": "SELECT toFloat64(number) AS value FROM numbers(10) SETTINGS max_result_rows = 5", "disabled": False}}],
|
||||
request_type=querier.RequestType.SCALAR,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
|
||||
error = response.json()["error"]
|
||||
assert (error["code"], error["suggestions"]) == ("result_too_large", [RESULT_TOO_LARGE]), error
|
||||
assert "max rows: 5" in error["message"], error
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query",
|
||||
[
|
||||
"SELECT toFloat64(1) AS value SETTINGS readonly = 1",
|
||||
"SELECT toFloat64(1) AS value SETTINGS readonly = 0",
|
||||
"CREATE TABLE default.integration_readonly_probe (x UInt8) ENGINE = Memory",
|
||||
],
|
||||
ids=["readonly_stricter", "readonly_lifted", "write"],
|
||||
)
|
||||
def test_clickhouse_sql_readonly_profile_rejects(
|
||||
signoz_clickhouse_query_profile: types.SigNoz,
|
||||
create_user_admin: None, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
query: str,
|
||||
) -> None:
|
||||
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
|
||||
|
||||
response = querier.make_query_request(
|
||||
signoz_clickhouse_query_profile,
|
||||
get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD),
|
||||
int((now - timedelta(hours=1)).timestamp() * 1000),
|
||||
int(now.timestamp() * 1000),
|
||||
[{"type": "clickhouse_sql", "spec": {"name": "A", "query": query, "disabled": False}}],
|
||||
request_type=querier.RequestType.SCALAR,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
|
||||
error = response.json()["error"]
|
||||
assert (error["code"], error["suggestions"]) == ("readonly_mode", ["run only SELECT queries and remove readonly from the SETTINGS clause"]), error
|
||||
54
tests/integration/tests/queriercommon/conftest.py
Normal file
54
tests/integration/tests/queriercommon/conftest.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from collections.abc import Callable
|
||||
|
||||
import pytest
|
||||
from testcontainers.core.container import Network
|
||||
|
||||
from fixtures import types
|
||||
from fixtures.signoz import create_signoz
|
||||
|
||||
|
||||
@pytest.fixture(name="signoz_clickhouse_query_profile", scope="package")
|
||||
def signoz_clickhouse_query_profile(
|
||||
network: Network,
|
||||
migrator: types.Operation, # pylint: disable=unused-argument
|
||||
zeus: types.TestContainerDocker,
|
||||
gateway: types.TestContainerDocker,
|
||||
sqlstore: types.TestContainerSQL,
|
||||
clickhouse: types.TestContainerClickhouse,
|
||||
clickhouse_settings_profile: Callable[[str, str], None],
|
||||
request: pytest.FixtureRequest,
|
||||
pytestconfig: pytest.Config,
|
||||
) -> types.SigNoz:
|
||||
"""
|
||||
SigNoz whose clickhouse_sql queries run with the clickhouse_query profile, which must exist in
|
||||
ClickHouse before any query runs.
|
||||
"""
|
||||
clickhouse_settings_profile(
|
||||
"clickhouse_query",
|
||||
"<readonly>2</readonly>"
|
||||
"<max_result_rows>100</max_result_rows>"
|
||||
"<max_result_bytes>100000</max_result_bytes>"
|
||||
"<max_bytes_to_read>1000000</max_bytes_to_read>"
|
||||
"<max_rows_to_read>200000</max_rows_to_read>"
|
||||
"<max_partitions_to_read>2</max_partitions_to_read>"
|
||||
"<constraints>"
|
||||
"<max_result_rows><min>1</min><max>100</max></max_result_rows>"
|
||||
"<max_result_bytes><min>1</min><max>100000</max></max_result_bytes>"
|
||||
"<max_bytes_to_read><min>1</min><max>1000000</max></max_bytes_to_read>"
|
||||
"<max_rows_to_read><min>1</min><max>200000</max></max_rows_to_read>"
|
||||
"<max_partitions_to_read><min>1</min><max>2</max></max_partitions_to_read>"
|
||||
"</constraints>",
|
||||
)
|
||||
return create_signoz(
|
||||
network=network,
|
||||
zeus=zeus,
|
||||
gateway=gateway,
|
||||
sqlstore=sqlstore,
|
||||
clickhouse=clickhouse,
|
||||
request=request,
|
||||
pytestconfig=pytestconfig,
|
||||
cache_key="signoz-clickhouse-query-profile",
|
||||
env_overrides={
|
||||
"SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_PROFILES": "clickhouse_query",
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user