Compare commits

..

4 Commits

Author SHA1 Message Date
Naman Verma
2a96645af6 fix(rules): keep rule labels from overriding engine labels (#13612)
Some checks are pending
build-staging / prepare (push) Waiting to run
build-staging / js-build (push) Blocked by required conditions
build-staging / go-build (push) Blocked by required conditions
build-staging / staging (push) Blocked by required conditions
cacheci / tests (push) Waiting to run
Release Drafter / update_release_draft (push) Waiting to run
<!--A few plain bullets saying what changed and why, for a reviewer
skimming it - not a wall of text, not a restatement of the diff, not
generated boilerplate.-->
#### Description

A rule label named threshold.name or severity replaced the value the
evaluator stamped. Rule Labels aren't supposed to be treated in a
special way like this.

<!--Reference issues using `Closes #issue-number` to enable automatic
closure on merge. -->
#### Issues closed by this PR

Part of https://github.com/SigNoz/pulse-pod/issues/653
2026-11-17 10:33:44 +00:00
hardik-signoz
900796a573 test(e2e): cover users FGA on the members page (#13615)
#### Description

- Adds `members.spec.ts` TC-04 to TC-07, each running as a member
holding only a custom role:
  - TC-04: `list:user` without `create:user`, invite button denied
- TC-05: no `list:user`, list shows "not authorized" and search is
denied
- TC-06: invited member without `factor-password:list`, "Invite Link
Unavailable"
- TC-07: self edit, adds then removes a role (needs `attach` and
`detach` on user and role)
- Drivers: `guestSettings` fixture (settings pages on the signed-in
guest page), `createGrantedUser` seed, members locators, `addRole` and
`removeRole`.
- Unit, story and integration tests already cover the rest of Users FGA,
see the plan on the issue.

#### Issues closed by this PR

Closes SigNoz/keystone-pod#212

#### Additional Information

- Each spec was checked by granting the missing permission and
confirming it fails.
- Run locally against hosted staging backend with the local frontend
(`--no-deps`). Not run on the pytest bootstrap stack.
2026-11-17 06:54:50 +00:00
Swapnil Nakade
2175611902 feat(authz): enable FGA for org preference routes (#13590)
<!--A few plain bullets saying what changed and why, for a reviewer
skimming it - not a wall of text, not a restatement of the diff, not
generated boilerplate.-->
#### Description
- Adds resource defs to the three `/api/v1/org/preferences` routes so
org preference updates emit audit events.
- Moves them from `AdminAccess` to `CheckResources`, scoped to
`org-preference:list`, `org-preference:read` and
`org-preference:update`. Admin keeps sole access.
- Migration 150 backfills the admin `org-preference` tuples for orgs
created before the grant existed. The registry and stored roles already
carry the grant, so it writes tuples only.
- The preference name is recorded as the resource ID with a wildcard
selector, since names are not UUIDs.

<!--Reference issues using `Closes #issue-number` to enable automatic
closure on merge. -->
#### Issues closed by this PR
contributes to https://github.com/SigNoz/keystone-pod/issues/10
contributes to https://github.com/SigNoz/keystone-pod/issues/43
2026-11-17 05:35:50 +00:00
hardik-signoz
29d4f7fb8d chore(sentry): exclude anonymous sentry errors (#13599)
#### Description

- Drop Sentry events whose throwing frame is `<anonymous>` (code loaded
without a script URL, such as extension or automation scripts).
- The existing `hasNoAppFrames` filter missed them: Sentry's bundled
`setTimeout` wrapper counts as an app frame.
- Matching the top frame instead of the error message keeps real bugs
reportable.

#### Issues closed by this PR

Closes SigNoz/keystone-pod#218
2026-11-17 05:34:07 +00:00
31 changed files with 679 additions and 737 deletions

View File

@@ -205,11 +205,6 @@ telemetrystore:
max_bytes_to_read: 0
max_result_rows: 0
ignore_data_skipping_indices: ""
# Stores queries can run with, for example clickhouse_query. Each runs as the ClickHouse user in its dsn, under
# that user's settings profile and grants.
profiles: {}
# clickhouse_query:
# dsn: tcp://signoz_query:password@localhost:9000
##################### Prometheus #####################
prometheus:

View File

@@ -11992,8 +11992,9 @@ components:
additionalProperties:
type: string
description: Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*;
values may use Go templates such as {{$labels.service_name}}. severity
is the conventional routing label.
values may use Go templates such as {{$labels.service_name}}. A label
named after one the engine writes, such as severity or threshold.name,
is ignored.
type: object
notificationSettings:
allOf:
@@ -12110,8 +12111,9 @@ components:
additionalProperties:
type: string
description: Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*;
values may use Go templates such as {{$labels.service_name}}. severity
is the conventional routing label.
values may use Go templates such as {{$labels.service_name}}. A label
named after one the engine writes, such as severity or threshold.name,
is ignored.
type: object
notificationSettings:
allOf:
@@ -16567,9 +16569,9 @@ paths:
description: Internal Server Error
security:
- api_key:
- ADMIN
- org-preference:list
- tokenizer:
- ADMIN
- org-preference:list
summary: List org preferences
tags:
- preferences
@@ -16632,9 +16634,9 @@ paths:
description: Internal Server Error
security:
- api_key:
- ADMIN
- org-preference:read
- tokenizer:
- ADMIN
- org-preference:read
summary: Get org preference
tags:
- preferences
@@ -16689,9 +16691,9 @@ paths:
description: Internal Server Error
security:
- api_key:
- ADMIN
- org-preference:update
- tokenizer:
- ADMIN
- org-preference:update
summary: Update org preference
tags:
- preferences

View File

@@ -267,9 +267,7 @@ func (r *AnomalyRule) Eval(ctx context.Context, ts time.Time) (int, error) {
lb := ruletypes.NewBuilder(smpl.Metric...).Del(ruletypes.MetricNameLabel).Del(ruletypes.TemporalityLabel)
resultLabels := ruletypes.NewBuilder(smpl.Metric...).Del(ruletypes.MetricNameLabel).Del(ruletypes.TemporalityLabel).Labels()
for name, value := range r.Labels().Map() {
lb.Set(name, expand(value))
}
lb.SetRuleLabels(r.Labels().Map(), expand)
lb.Set(ruletypes.AlertNameLabel, r.Name())
lb.Set(ruletypes.AlertRuleIDLabel, r.ID())

View File

@@ -2,9 +2,9 @@ import type { ErrorEvent, StackFrame } from '@sentry/react';
import { AxiosError, AxiosHeaders } from 'axios';
import {
hasNoAppFrames,
isInjectedScriptError,
isExpectedRequestFailure,
isExternalIntegrationError,
isThirdPartyScriptError,
SENTRY_IGNORE_ERRORS,
} from '../sentryFilters';
@@ -27,7 +27,7 @@ function eventWithFrames(...filenames: string[]): ErrorEvent {
};
}
describe('isExternalIntegrationError', () => {
describe('isThirdPartyScriptError', () => {
it.each([
[
'PostHog recorder',
@@ -39,14 +39,12 @@ describe('isExternalIntegrationError', () => {
],
['Appcues', 'https://fast.appcues.com/123456.js'],
])('drops an error with a %s frame', (_, url) => {
expect(isExternalIntegrationError(eventWithFrames(APP_BUNDLE, url))).toBe(
true,
);
expect(isThirdPartyScriptError(eventWithFrames(APP_BUNDLE, url))).toBe(true);
});
it('drops it when the top frame is our bundle', () => {
expect(
isExternalIntegrationError(
isThirdPartyScriptError(
eventWithFrames(
'https://us-assets.i.posthog.com/static/posthog-recorder.js?v=1.298.0',
'<anonymous>',
@@ -58,13 +56,13 @@ describe('isExternalIntegrationError', () => {
it('keeps an error from our bundle only', () => {
expect(
isExternalIntegrationError(eventWithFrames(APP_BUNDLE, '<anonymous>')),
isThirdPartyScriptError(eventWithFrames(APP_BUNDLE, '<anonymous>')),
).toBe(false);
});
it('keeps an error from a host that only contains an integration name', () => {
expect(
isExternalIntegrationError(
isThirdPartyScriptError(
eventWithFrames(
'https://posthog.example.com/app.js',
'https://notusepylon.com/widget.js',
@@ -74,11 +72,11 @@ describe('isExternalIntegrationError', () => {
});
it('keeps an error without a stack trace', () => {
expect(isExternalIntegrationError({ type: undefined })).toBe(false);
expect(isThirdPartyScriptError({ type: undefined })).toBe(false);
});
});
describe('hasNoAppFrames', () => {
describe('isInjectedScriptError', () => {
const PAGE_URL = 'https://example.us.signoz.cloud/dashboard/01a081e1';
function eventWithStackFrames(frames: StackFrame[]): ErrorEvent {
@@ -92,7 +90,7 @@ describe('hasNoAppFrames', () => {
it('drops a script appended to the page', () => {
expect(
hasNoAppFrames(
isInjectedScriptError(
eventWithStackFrames([
{ filename: PAGE_URL, lineno: 1, colno: 30, function: 'global code' },
{ filename: '[native code]', function: 'eval' },
@@ -104,7 +102,7 @@ describe('hasNoAppFrames', () => {
it('drops a browser extension error', () => {
expect(
hasNoAppFrames(
isInjectedScriptError(
eventWithStackFrames([
{ filename: 'chrome-extension://abc/content.js', lineno: 4, colno: 12 },
]),
@@ -112,21 +110,46 @@ describe('hasNoAppFrames', () => {
).toBe(true);
});
it('drops an error thrown from injected code behind the Sentry wrapper', () => {
expect(
isInjectedScriptError(eventWithFrames(APP_BUNDLE, '<anonymous>')),
).toBe(true);
});
it('keeps an error thrown from our bundle after an anonymous frame', () => {
expect(
isInjectedScriptError(eventWithFrames('<anonymous>', APP_BUNDLE)),
).toBe(false);
});
it('keeps an error with a frame from our bundle', () => {
expect(hasNoAppFrames(eventWithFrames(PAGE_URL, APP_BUNDLE))).toBe(false);
expect(isInjectedScriptError(eventWithFrames(PAGE_URL, APP_BUNDLE))).toBe(
false,
);
});
it('keeps an error without a stack trace', () => {
expect(hasNoAppFrames({ type: undefined })).toBe(false);
expect(isInjectedScriptError({ type: undefined })).toBe(false);
});
it('keeps an error whose frames have no position', () => {
expect(
hasNoAppFrames(
isInjectedScriptError(
eventWithStackFrames([{ filename: '[native code]', function: 'eval' }]),
),
).toBe(false);
});
it('keeps an error a builtin threw for our bundle', () => {
expect(
isInjectedScriptError(
eventWithStackFrames([
{ filename: APP_BUNDLE, lineno: 1, colno: 1 },
{ filename: '<anonymous>', function: 'JSON.parse' },
]),
),
).toBe(false);
});
});
describe('SENTRY_IGNORE_ERRORS', () => {

View File

@@ -44,9 +44,9 @@ import defaultRoutes, {
} from './routes';
import type { EnabledRouteKeys } from './RouteGate';
import {
hasNoAppFrames,
isInjectedScriptError,
isExpectedRequestFailure,
isExternalIntegrationError,
isThirdPartyScriptError,
SENTRY_IGNORE_ERRORS,
} from './sentryFilters';
@@ -360,12 +360,12 @@ function App(): JSX.Element {
return null;
}
if (isExternalIntegrationError(event)) {
if (isThirdPartyScriptError(event)) {
return null;
}
// Dev serves source from /src/, so every frame would look foreign there.
if (IS_PROD && hasNoAppFrames(event)) {
if (IS_PROD && isInjectedScriptError(event)) {
return null;
}

View File

@@ -28,10 +28,15 @@ export function isExpectedRequestFailure(error: unknown): boolean {
}
const APP_ASSETS_PATH = '/assets/';
const ANONYMOUS_FILENAME = '<anonymous>';
// Frames without a position are `[native code]`, which says nothing about the
// script that threw.
export function hasNoAppFrames(event: ErrorEvent): boolean {
// Frames without a position are `[native code]` or a browser builtin such as
// `JSON.parse (<anonymous>)`, which say nothing about the script that threw.
// A positioned `<anonymous>` throwing (last) frame is code loaded without a
// URL: the DevTools console, CDP, or an extension's `executeScript`. Sentry's
// own timer wrapper is bundled with the app, so checking for any app frame
// alone misses it.
export function isInjectedScriptError(event: ErrorEvent): boolean {
const frames = (event.exception?.values ?? [])
.flatMap((exception) => exception.stacktrace?.frames ?? [])
.filter(
@@ -40,12 +45,13 @@ export function hasNoAppFrames(event: ErrorEvent): boolean {
return (
frames.length > 0 &&
!frames.some((frame) => frame.filename?.includes(APP_ASSETS_PATH))
(frames[frames.length - 1].filename === ANONYMOUS_FILENAME ||
!frames.some((frame) => frame.filename?.includes(APP_ASSETS_PATH)))
);
}
// PostHog's lazy recorder, the Pylon widget and Appcues.
const EXTERNAL_SCRIPT_URLS = [
const THIRD_PARTY_SCRIPT_URLS = [
/^https?:\/\/[^/]+\.posthog\.com\//,
/^https?:\/\/(?:[^/]+\.)?usepylon\.com\//,
/^https?:\/\/(?:[^/]+\.)?appcues\.(?:com|net)\//,
@@ -53,10 +59,10 @@ const EXTERNAL_SCRIPT_URLS = [
// Checks every frame, not only the top one: a failed fetch from the recorder
// tops out in Sentry's own fetch wrapper, which is bundled with the app.
export function isExternalIntegrationError(event: ErrorEvent): boolean {
export function isThirdPartyScriptError(event: ErrorEvent): boolean {
return (event.exception?.values ?? []).some((exception) =>
(exception.stacktrace?.frames ?? []).some((frame) =>
EXTERNAL_SCRIPT_URLS.some((url) => url.test(frame.filename ?? '')),
THIRD_PARTY_SCRIPT_URLS.some((url) => url.test(frame.filename ?? '')),
),
);
}

View File

@@ -13156,7 +13156,7 @@ export interface RuletypesNotificationSettingsDTO {
export type RuletypesPostableRuleDTOAnnotations = { [key: string]: string };
/**
* Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. severity is the conventional routing label.
* Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. A label named after one the engine writes, such as severity or threshold.name, is ignored.
*/
export type RuletypesPostableRuleDTOLabels = { [key: string]: string };
@@ -13273,7 +13273,7 @@ export interface RuletypesPostableRuleDTO {
evaluation: RuletypesEvaluationEnvelopeDTO;
/**
* @type object
* @description Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. severity is the conventional routing label.
* @description Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. A label named after one the engine writes, such as severity or threshold.name, is ignored.
*/
labels?: RuletypesPostableRuleDTOLabels;
/**
@@ -13306,7 +13306,7 @@ export interface RuletypesPostableRuleViewDTO {
export type RuletypesRuleDTOAnnotations = { [key: string]: string };
/**
* Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. severity is the conventional routing label.
* Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. A label named after one the engine writes, such as severity or threshold.name, is ignored.
*/
export type RuletypesRuleDTOLabels = { [key: string]: string };
@@ -13365,7 +13365,7 @@ export interface RuletypesRuleDTO {
id: string;
/**
* @type object
* @description Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. severity is the conventional routing label.
* @description Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. A label named after one the engine writes, such as severity or threshold.name, is ignored.
*/
labels?: RuletypesRuleDTOLabels;
/**

View File

@@ -5,6 +5,8 @@ import (
"github.com/SigNoz/signoz/pkg/http/handler"
"github.com/SigNoz/signoz/pkg/types"
"github.com/SigNoz/signoz/pkg/types/authtypes"
"github.com/SigNoz/signoz/pkg/types/coretypes"
"github.com/SigNoz/signoz/pkg/types/preferencetypes"
"github.com/gorilla/mux"
)
@@ -61,7 +63,7 @@ func (provider *provider) addPreferenceRoutes(router *mux.Router) error {
return err
}
if err := router.Handle("/api/v1/org/preferences", handler.New(provider.authzMiddleware.AdminAccess(provider.preferenceHandler.ListByOrg), handler.OpenAPIDef{
if err := router.Handle("/api/v1/org/preferences", handler.New(provider.authzMiddleware.CheckResources(provider.preferenceHandler.ListByOrg, authtypes.SigNozAdminRoleName), handler.OpenAPIDef{
ID: "ListOrgPreferences",
Tags: []string{"preferences"},
Summary: "List org preferences",
@@ -73,12 +75,17 @@ func (provider *provider) addPreferenceRoutes(router *mux.Router) error {
SuccessStatusCode: http.StatusOK,
ErrorStatusCodes: []int{},
Deprecated: false,
SecuritySchemes: newSecuritySchemes(types.RoleAdmin),
})).Methods(http.MethodGet).GetError(); err != nil {
SecuritySchemes: newScopedSecuritySchemes([]string{coretypes.ResourceMetaResourceOrgPreference.Scope(coretypes.VerbList)}),
}, handler.WithResourceDefs(handler.BasicResourceDef{
Resource: coretypes.ResourceMetaResourceOrgPreference,
Verb: coretypes.VerbList,
Category: coretypes.ActionCategoryDataAccess,
Selector: coretypes.WildcardSelector,
}))).Methods(http.MethodGet).GetError(); err != nil {
return err
}
if err := router.Handle("/api/v1/org/preferences/{name}", handler.New(provider.authzMiddleware.AdminAccess(provider.preferenceHandler.GetByOrg), handler.OpenAPIDef{
if err := router.Handle("/api/v1/org/preferences/{name}", handler.New(provider.authzMiddleware.CheckResources(provider.preferenceHandler.GetByOrg, authtypes.SigNozAdminRoleName), handler.OpenAPIDef{
ID: "GetOrgPreference",
Tags: []string{"preferences"},
Summary: "Get org preference",
@@ -90,12 +97,18 @@ func (provider *provider) addPreferenceRoutes(router *mux.Router) error {
SuccessStatusCode: http.StatusOK,
ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusNotFound},
Deprecated: false,
SecuritySchemes: newSecuritySchemes(types.RoleAdmin),
})).Methods(http.MethodGet).GetError(); err != nil {
SecuritySchemes: newScopedSecuritySchemes([]string{coretypes.ResourceMetaResourceOrgPreference.Scope(coretypes.VerbRead)}),
}, handler.WithResourceDefs(handler.BasicResourceDef{
Resource: coretypes.ResourceMetaResourceOrgPreference,
Verb: coretypes.VerbRead,
Category: coretypes.ActionCategoryDataAccess,
ID: coretypes.PathParam("name"),
Selector: coretypes.WildcardSelector,
}))).Methods(http.MethodGet).GetError(); err != nil {
return err
}
if err := router.Handle("/api/v1/org/preferences/{name}", handler.New(provider.authzMiddleware.AdminAccess(provider.preferenceHandler.UpdateByOrg), handler.OpenAPIDef{
if err := router.Handle("/api/v1/org/preferences/{name}", handler.New(provider.authzMiddleware.CheckResources(provider.preferenceHandler.UpdateByOrg, authtypes.SigNozAdminRoleName), handler.OpenAPIDef{
ID: "UpdateOrgPreference",
Tags: []string{"preferences"},
Summary: "Update org preference",
@@ -107,8 +120,14 @@ func (provider *provider) addPreferenceRoutes(router *mux.Router) error {
SuccessStatusCode: http.StatusNoContent,
ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusNotFound},
Deprecated: false,
SecuritySchemes: newSecuritySchemes(types.RoleAdmin),
})).Methods(http.MethodPut).GetError(); err != nil {
SecuritySchemes: newScopedSecuritySchemes([]string{coretypes.ResourceMetaResourceOrgPreference.Scope(coretypes.VerbUpdate)}),
}, handler.WithResourceDefs(handler.BasicResourceDef{
Resource: coretypes.ResourceMetaResourceOrgPreference,
Verb: coretypes.VerbUpdate,
Category: coretypes.ActionCategoryConfigurationChange,
ID: coretypes.PathParam("name"),
Selector: coretypes.WildcardSelector,
}))).Methods(http.MethodPut).GetError(); err != nil {
return err
}

View File

@@ -10,10 +10,6 @@ import (
const DefaultMaxConcurrentQueries = 8
// ClickHouseSQLProfile is the telemetrystore.clickhouse.profiles entry that user-written ClickHouse SQL
// queries run with, once it is in the config.
const ClickHouseSQLProfile = "clickhouse_query"
// Config represents the configuration for the querier.
type Config struct {
// CacheTTL is the TTL for cached query results

View File

@@ -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.WithProfile(ClickHouseSQLProfile), chQuery, nil, qbtypes.TimeRange{From: req.Start, To: req.End}, req.RequestType, tmplVars)
chSQLQuery := newchSQLQuery(q.logger, q.telemetryStore, 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.WithProfile(ClickHouseSQLProfile), queryCopy, argsCopy, timeRange, qt.kind, qt.vars)
return newchSQLQuery(q.logger, q.telemetryStore, 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.

View File

@@ -201,9 +201,7 @@ func (r *PromRule) Eval(ctx context.Context, ts time.Time) (int, error) {
lb := ruletypes.NewBuilder(result.Metric...).Del(ruletypes.MetricNameLabel)
resultLabels := ruletypes.NewBuilder(result.Metric...).Del(ruletypes.MetricNameLabel).Labels()
for name, value := range r.labels.Map() {
lb.Set(name, expand(value))
}
lb.SetRuleLabels(r.labels.Map(), expand)
lb.Set(ruletypes.AlertNameLabel, r.Name())
lb.Set(ruletypes.AlertRuleIDLabel, r.ID())

View File

@@ -278,9 +278,7 @@ func (r *ThresholdRule) Eval(ctx context.Context, ts time.Time) (int, error) {
lb := ruletypes.NewBuilder(smpl.Metric...).Del(ruletypes.MetricNameLabel).Del(ruletypes.TemporalityLabel)
resultLabels := ruletypes.NewBuilder(smpl.Metric...).Del(ruletypes.MetricNameLabel).Del(ruletypes.TemporalityLabel).Labels()
for name, value := range r.labels.Map() {
lb.Set(name, expand(value))
}
lb.SetRuleLabels(r.labels.Map(), expand)
lb.Set(ruletypes.AlertNameLabel, r.Name())
lb.Set(ruletypes.AlertRuleIDLabel, r.ID())

View File

@@ -277,6 +277,7 @@ func NewSQLMigrationProviderFactories(
sqlmigration.NewRemoveUnsupportedQuickFiltersFactory(),
sqlmigration.NewMigrateRelatedLinksTemplateFactory(),
sqlmigration.NewAddOrganizationTuplesFactory(sqlstore),
sqlmigration.NewAddOrgPreferenceTuplesFactory(sqlstore),
)
}

View File

@@ -0,0 +1,133 @@
package sqlmigration
import (
"context"
"database/sql"
"time"
"github.com/oklog/ulid/v2"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/migrate"
"github.com/SigNoz/signoz/pkg/factory"
"github.com/SigNoz/signoz/pkg/sqlstore"
)
type addOrgPreferenceTuples struct {
sqlstore sqlstore.SQLStore
}
func NewAddOrgPreferenceTuplesFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[SQLMigration, Config] {
return factory.NewProviderFactory(factory.MustNewName("add_org_preference_tuples"), func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {
return &addOrgPreferenceTuples{sqlstore: sqlstore}, nil
})
}
func (migration *addOrgPreferenceTuples) Register(migrations *migrate.Migrations) error {
return migrations.Register(migration.Up, migration.Down)
}
func (migration *addOrgPreferenceTuples) Up(ctx context.Context, db *bun.DB) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
var storeID string
err = tx.QueryRowContext(ctx, `SELECT id FROM store WHERE name = ? LIMIT 1`, "signoz").Scan(&storeID)
if err != nil {
return err
}
var orgIDs []string
err = tx.NewSelect().
Table("organizations").
Column("id").
Scan(ctx, &orgIDs)
if err != nil && err != sql.ErrNoRows {
return err
}
isPG := migration.sqlstore.BunDB().Dialect().Name() == dialect.PG
tuples := []migrationTuple{
{"signoz-admin", "metaresource", "org-preference", "read"},
{"signoz-admin", "metaresource", "org-preference", "update"},
{"signoz-admin", "metaresource", "org-preference", "delete"},
{"signoz-admin", "metaresource", "org-preference", "create"},
{"signoz-admin", "metaresource", "org-preference", "list"},
}
for _, orgID := range orgIDs {
for _, tuple := range tuples {
entropy := ulid.DefaultEntropy()
now := time.Now().UTC()
tupleID := ulid.MustNew(ulid.Timestamp(now), entropy).String()
objectID := "organization/" + orgID + "/" + tuple.objectName + "/*"
roleSubject := "organization/" + orgID + "/role/" + tuple.roleName
if isPG {
user := "role:" + roleSubject + "#assignee"
result, err := tx.ExecContext(ctx, `
INSERT INTO tuple (store, object_type, object_id, relation, _user, user_type, ulid, inserted_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (store, object_type, object_id, relation, _user) DO NOTHING`,
storeID, tuple.objectType, objectID, tuple.relation, user, "userset", tupleID, now,
)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
continue
}
_, err = tx.ExecContext(ctx, `
INSERT INTO changelog (store, object_type, object_id, relation, _user, operation, ulid, inserted_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (store, ulid, object_type) DO NOTHING`,
storeID, tuple.objectType, objectID, tuple.relation, user, 0, tupleID, now,
)
if err != nil {
return err
}
} else {
result, err := tx.ExecContext(ctx, `
INSERT INTO tuple (store, object_type, object_id, relation, user_object_type, user_object_id, user_relation, user_type, ulid, inserted_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (store, object_type, object_id, relation, user_object_type, user_object_id, user_relation) DO NOTHING`,
storeID, tuple.objectType, objectID, tuple.relation, "role", roleSubject, "assignee", "userset", tupleID, now,
)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
continue
}
_, err = tx.ExecContext(ctx, `
INSERT INTO changelog (store, object_type, object_id, relation, user_object_type, user_object_id, user_relation, operation, ulid, inserted_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (store, ulid, object_type) DO NOTHING`,
storeID, tuple.objectType, objectID, tuple.relation, "role", roleSubject, "assignee", 0, tupleID, now,
)
if err != nil {
return err
}
}
}
}
return tx.Commit()
}
func (migration *addOrgPreferenceTuples) Down(context.Context, *bun.DB) error {
return nil
}

View File

@@ -26,12 +26,6 @@ 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")
ErrCodeAccessDenied = errors.MustNewCode("access_denied")
ErrCodeQueryCanceled = errors.MustNewCode("query_canceled")
ErrCodeQueryTimeout = errors.MustNewCode("query_timeout")
@@ -117,47 +111,11 @@ 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")
},
// A query on a table outside the grants of the ClickHouse user it runs as.
chproto.ErrAccessDenied: func(cause error, ex *clickhouse.Exception) error {
return errors.WrapForbiddenf(cause, ErrCodeAccessDenied, "query not allowed: %s", ex.Message).
WithSuggestions("query only the telemetry tables")
},
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]*provider
cluster string
hooks []telemetrystore.TelemetryStoreHook
}
@@ -171,17 +129,21 @@ func NewFactory(hookFactories ...factory.ProviderFactory[telemetrystore.Telemetr
func New(ctx context.Context, providerSettings factory.ProviderSettings, config telemetrystore.Config, hookFactories ...factory.ProviderFactory[telemetrystore.TelemetryStoreHook, telemetrystore.Config]) (telemetrystore.TelemetryStore, error) {
settings := factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/telemetrystore/clickhousetelemetrystore")
chConn, err := open(config.Clickhouse.DSN, config.Connection)
options, err := clickhouse.ParseDSN(config.Clickhouse.DSN)
if err != nil {
return nil, err
}
options.MaxIdleConns = config.Connection.MaxIdleConns
options.MaxOpenConns = config.Connection.MaxOpenConns
options.DialTimeout = config.Connection.DialTimeout
// Read JSON columns natively: the driver mis-decodes JSON(max_dynamic_paths=0) columns without
// the flattened serialization, and reading them as a string collapses a key stored at both a
// scalar and an object path.
options.Settings["output_format_native_use_flattened_dynamic_and_json_serialization"] = 1
profileConns := make(map[string]clickhouse.Conn, len(config.Clickhouse.Profiles))
for profile, profileConfig := range config.Clickhouse.Profiles {
profileConns[profile], err = open(profileConfig.DSN, config.Connection)
if err != nil {
return nil, err
}
chConn, err := clickhouse.Open(options)
if err != nil {
return nil, err
}
hooks := make([]telemetrystore.TelemetryStoreHook, len(hookFactories))
@@ -216,96 +178,49 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
cluster: config.Clickhouse.Cluster,
hooks: hooks,
}
withProfiles(p, profileConns)
return p, nil
}
func open(dsn string, connection telemetrystore.ConnectionConfig) (clickhouse.Conn, error) {
options, err := clickhouse.ParseDSN(dsn)
if err != nil {
return nil, err
}
options.MaxIdleConns = connection.MaxIdleConns
options.MaxOpenConns = connection.MaxOpenConns
options.DialTimeout = connection.DialTimeout
// Read JSON columns natively: the driver mis-decodes JSON(max_dynamic_paths=0) columns without
// the flattened serialization, and reading them as a string collapses a key stored at both a
// scalar and an object path.
options.Settings["output_format_native_use_flattened_dynamic_and_json_serialization"] = 1
return clickhouse.Open(options)
func (p *provider) ClickhouseDB() clickhouse.Conn {
return p
}
// withProfiles gives every profile a copy of main that runs its queries over the profile's pool.
func withProfiles(main *provider, conns map[string]clickhouse.Conn) {
main.profiles = make(map[string]*provider, len(conns))
for profile, conn := range conns {
profiled := *main
profiled.profile = profile
profiled.clickHouseConn = conn
main.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 closes the profile pools with the main one. A profile store leaves its pool to the main store.
func (provider *provider) Close() error {
if provider.profile != "" {
return nil
}
errs := make([]error, 0, len(provider.profiles)+1)
for _, profiled := range provider.profiles {
errs = append(errs, profiled.clickHouseConn.Close())
}
errs = append(errs, provider.clickHouseConn.Close())
return errors.Join(errs...)
}
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) {
func (p *provider) Query(ctx context.Context, query string, args ...interface{}) (driver.Rows, error) {
event := telemetrystore.NewQueryEvent(query, args)
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
rows, err := provider.clickHouseConn.Query(ctx, query, args...)
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
rows, err := p.clickHouseConn.Query(ctx, query, args...)
if err != nil {
event.Err = err
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
return nil, castError(err)
}
@@ -313,71 +228,71 @@ func (provider *provider) Query(ctx context.Context, query string, args ...inter
Rows: telemetrystore.WrapRows(rows),
ctx: ctx,
event: event,
onClose: func() { telemetrystore.WrapAfterQuery(provider.hooks, ctx, event) },
onClose: func() { telemetrystore.WrapAfterQuery(p.hooks, ctx, event) },
}, nil
}
func (provider *provider) QueryRow(ctx context.Context, query string, args ...interface{}) driver.Row {
func (p *provider) QueryRow(ctx context.Context, query string, args ...interface{}) driver.Row {
event := telemetrystore.NewQueryEvent(query, args)
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
row := provider.clickHouseConn.QueryRow(ctx, query, args...)
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
row := p.clickHouseConn.QueryRow(ctx, query, args...)
if row == nil {
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
return nil
}
event.Err = row.Err()
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
return &rowWithCastError{Row: row}
}
func (provider *provider) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
func (p *provider) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
event := telemetrystore.NewQueryEvent(query, args)
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
err := provider.clickHouseConn.Select(ctx, dest, query, args...)
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
err := p.clickHouseConn.Select(ctx, dest, query, args...)
event.Err = err
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
return castError(err)
}
func (provider *provider) Exec(ctx context.Context, query string, args ...interface{}) error {
func (p *provider) Exec(ctx context.Context, query string, args ...interface{}) error {
event := telemetrystore.NewQueryEvent(query, args)
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
err := provider.clickHouseConn.Exec(ctx, query, args...)
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
err := p.clickHouseConn.Exec(ctx, query, args...)
event.Err = err
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
return castError(err)
}
func (provider *provider) AsyncInsert(ctx context.Context, query string, wait bool, args ...interface{}) error {
func (p *provider) AsyncInsert(ctx context.Context, query string, wait bool, args ...interface{}) error {
event := telemetrystore.NewQueryEvent(query, args)
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
err := provider.clickHouseConn.Exec(clickhouse.Context(ctx, clickhouse.WithAsync(wait)), query, args...)
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
err := p.clickHouseConn.Exec(clickhouse.Context(ctx, clickhouse.WithAsync(wait)), query, args...)
event.Err = err
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
return castError(err)
}
func (provider *provider) PrepareBatch(ctx context.Context, query string, opts ...driver.PrepareBatchOption) (driver.Batch, error) {
func (p *provider) PrepareBatch(ctx context.Context, query string, opts ...driver.PrepareBatchOption) (driver.Batch, error) {
event := telemetrystore.NewQueryEvent(query, nil)
ctx = telemetrystore.WrapBeforeQuery(provider.hooks, ctx, event)
batch, err := provider.clickHouseConn.PrepareBatch(ctx, query, opts...)
ctx = telemetrystore.WrapBeforeQuery(p.hooks, ctx, event)
batch, err := p.clickHouseConn.PrepareBatch(ctx, query, opts...)
event.Err = err
telemetrystore.WrapAfterQuery(provider.hooks, ctx, event)
telemetrystore.WrapAfterQuery(p.hooks, ctx, event)
if batch == nil {
return nil, castError(err)
@@ -385,12 +300,12 @@ func (provider *provider) PrepareBatch(ctx context.Context, query string, opts .
return &batchWithCastError{Batch: batch}, castError(err)
}
func (provider *provider) ServerVersion() (*driver.ServerVersion, error) {
return provider.clickHouseConn.ServerVersion()
func (p *provider) ServerVersion() (*driver.ServerVersion, error) {
return p.clickHouseConn.ServerVersion()
}
func (provider *provider) Contributors() []string {
return provider.clickHouseConn.Contributors()
func (p *provider) Contributors() []string {
return p.clickHouseConn.Contributors()
}
func castError(err error) error {

View File

@@ -1,74 +0,0 @@
package clickhousetelemetrystore
import (
"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"
)
func TestCastError_ProfileErrors(t *testing.T) {
testCases := []struct {
name string
code chproto.Error
want errors.Code
notFound bool
forbidden 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: "AccessDenied_AccessDenied", code: chproto.ErrAccessDenied, want: ErrCodeAccessDenied, forbidden: true},
{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.forbidden, errors.Ast(err, errors.TypeForbidden), "got %v", err)
assert.Equal(t, !testCase.notFound && !testCase.forbidden, 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()
profileConn, err := cmock.NewClickHouseWithQueryMatcher(&clickhouse.Options{}, sqlmock.QueryMatcherEqual)
require.NoError(t, err)
profileConn.ExpectClose()
p := &provider{clickHouseConn: conn, cluster: "cluster"}
withProfiles(p, map[string]clickhouse.Conn{"clickhouse_query": profileConn})
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, profileConn, profiled.clickHouseConn)
assert.Same(t, conn, p.clickHouseConn)
assert.Equal(t, "cluster", profiled.Cluster())
assert.Empty(t, p.profile)
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())
assert.NoError(t, profileConn.ExpectationsWereMet())
}

View File

@@ -37,15 +37,6 @@ type ClickhouseConfig struct {
// QuerySettings is the query settings for clickhouse.
QuerySettings QuerySettings `mapstructure:"settings"`
// Profiles are the stores TelemetryStore.WithProfile returns, by name. Each runs its queries as the
// ClickHouse user in its DSN, under that user's settings profile and grants.
Profiles map[string]ProfileConfig `mapstructure:"profiles"`
}
type ProfileConfig struct {
// DSN is the database source name of the ClickHouse user the profile's queries run as.
DSN string `mapstructure:"dsn"`
}
type QuerySettings struct {

View File

@@ -11,10 +11,6 @@ type TelemetryStore interface {
// ClickhouseDB returns the clickhouse connection, which can also EXPLAIN.
ClickhouseDB() clickhouse.Conn
// WithProfile returns a store that runs every query as the profile's ClickHouse user, or this store when
// the profile is not in the config.
WithProfile(profile string) TelemetryStore
// Cluster returns the cluster name.
Cluster() string

View File

@@ -31,36 +31,31 @@ func New(_ telemetrystore.Config, matcher sqlmock.QueryMatcher) *Provider {
}
// ClickhouseDB returns the mock Clickhouse connection.
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
func (p *Provider) ClickhouseDB() clickhouse.Conn {
return p.clickhouseDB.(clickhouse.Conn)
}
// Cluster returns the cluster name.
func (provider *Provider) Cluster() string {
func (p *Provider) Cluster() string {
return "cluster"
}
// Estimate runs EXPLAIN ESTIMATE against the mock connection.
func (provider *Provider) Estimate(ctx context.Context, stmt string, args ...any) ([]telemetrystoretypes.EstimateEntry, error) {
return clickhousetelemetrystore.RunExplainEstimate(ctx, provider.clickhouseDB.(clickhouse.Conn), stmt, args...)
func (p *Provider) Estimate(ctx context.Context, stmt string, args ...any) ([]telemetrystoretypes.EstimateEntry, error) {
return clickhousetelemetrystore.RunExplainEstimate(ctx, p.clickhouseDB.(clickhouse.Conn), stmt, args...)
}
// Plan runs EXPLAIN PLAN against the mock connection.
func (provider *Provider) Plan(ctx context.Context, stmt string, args ...any) error {
return clickhousetelemetrystore.RunExplainPlan(ctx, provider.clickhouseDB.(clickhouse.Conn), stmt, args...)
func (p *Provider) Plan(ctx context.Context, stmt string, args ...any) error {
return clickhousetelemetrystore.RunExplainPlan(ctx, p.clickhouseDB.(clickhouse.Conn), stmt, args...)
}
// Indexes runs EXPLAIN indexes against the mock connection.
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...)
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...)
}
// Mock returns the underlying Clickhouse mock instance for setting expectations.
func (provider *Provider) Mock() cmock.ClickConnMockCommon {
return provider.clickhouseDB
func (p *Provider) Mock() cmock.ClickConnMockCommon {
return p.clickhouseDB
}

View File

@@ -71,7 +71,7 @@ type PostableRule struct {
RuleType RuleType `json:"ruleType" required:"true" description:"The evaluation strategy: threshold_rule compares against thresholds, promql_rule evaluates a PromQL expression, anomaly_rule scores deviation from seasonal history. Defaults to threshold_rule for builder and clickhouse_sql queries and is forced to promql_rule for promql queries."`
RuleCondition *RuleCondition `json:"condition" required:"true" description:"The query to evaluate and the thresholds applied to its result."`
Labels map[string]string `json:"labels,omitempty" description:"Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. severity is the conventional routing label."`
Labels map[string]string `json:"labels,omitempty" description:"Labels attached to every alert the rule fires. Keys match [a-zA-Z_.][a-zA-Z0-9_.]*; values may use Go templates such as {{$labels.service_name}}. A label named after one the engine writes, such as severity or threshold.name, is ignored."`
Annotations map[string]string `json:"annotations,omitempty" description:"Free text attached to every alert, conventionally summary and description. Values may use Go templates with $value, $threshold and $labels."`
Disabled bool `json:"disabled" description:"Whether the rule is paused. A disabled rule is neither evaluated nor notifies. Defaults to false."`

View File

@@ -31,6 +31,23 @@ const (
AlertDescriptionLabel = "description"
)
// reservedLabelNames are written by the rule engine; a rule label with one of
// these names never reaches the alert.
var reservedLabelNames = map[string]struct{}{
MetricNameLabel: {},
TemporalityLabel: {},
AlertNameLabel: {},
AlertRuleIDLabel: {},
RuleSourceLabel: {},
NoDataLabel: {},
LabelThresholdName: {},
LabelSeverityName: {},
LabelLastSeen: {},
// TODO: the template layer reads is_recovering but nothing writes it;
// set it from Alert.IsRecovering.
LabelIsRecovering: {},
}
// Label is a key/value pair of strings.
// TODO(srikanthccv): https://github.com/SigNoz/signoz/issues/9232?
type Label struct {
@@ -285,6 +302,20 @@ func (b *Builder) Set(n, v string) *Builder {
return b
}
// SetRuleLabels adds the rule's labels, each expanded, skipping the names the
// engine writes.
// TODO: a rule label still overwrites a query result label of the same name;
// decide whether those are locked too.
func (b *Builder) SetRuleLabels(ruleLabels map[string]string, expand func(string) string) *Builder {
for name, value := range ruleLabels {
if _, reserved := reservedLabelNames[name]; reserved {
continue
}
b.Set(name, expand(value))
}
return b
}
// Labels returns the labels from the builder. If no modifications
// were made, the original labels are returned.
func (b *Builder) Labels() Labels {

View File

@@ -131,14 +131,23 @@ export interface Settings {
/** The member's table row. Search for the email first: the list pages at 20. */
row(email: string): Locator;
inviteButton(): Locator;
searchInput(): Locator;
resetLinkButton(): Locator;
/** The "is not authorized" message a denied list or drawer renders. */
deniedMessage(): Locator;
openInvite(): Promise<void>;
inviteDialog(): Locator;
invite(rows: { email: string; role: string }[]): Promise<void>;
openMember(email: string): Promise<void>;
drawer(): Locator;
nameInput(): Locator;
roleSelect(): Locator;
deleteButton(): Locator;
setRoles(roles: string[]): Promise<void>;
/** Add a role to the open member without clearing the others. */
addRole(role: string): Promise<void>;
/** Remove one role from the open member, leaving the others. */
removeRole(role: string): Promise<void>;
saveMember(): Promise<void>;
/** Mint a reset or invite link from the drawer and return it. */
generateLink(): Promise<string>;
@@ -414,6 +423,9 @@ export function settingsDriver(page: Page): Settings {
// antd keys each row by the member id; the email is what callers hold.
row: (email) => page.locator('tr[data-row-key]').filter({ hasText: email }),
inviteButton: () => page.getByTestId('invite-member-btn'),
searchInput: () => page.getByTestId('members-search-input'),
resetLinkButton: () => page.getByTestId('member-reset-link-btn'),
deniedMessage: () => page.getByText(/is not authorized/).first(),
openInvite: async () => {
await settings.members.inviteButton().click();
await expect(settings.members.inviteDialog()).toBeVisible();
@@ -449,6 +461,7 @@ export function settingsDriver(page: Page): Settings {
await expect(settings.members.drawer().getByText(email)).toBeVisible();
},
drawer: () => page.getByRole('dialog', { name: 'Member Details' }),
nameInput: () => page.getByPlaceholder('Enter name'),
roleSelect: () => selectById('member-role'),
deleteButton: () => page.getByTestId('member-delete-btn'),
setRoles: async (roles) => {
@@ -458,6 +471,21 @@ export function settingsDriver(page: Page): Settings {
await pickInDialog(settings.members.drawer(), select, role);
}
},
addRole: async (role) => {
await pickInDialog(
settings.members.drawer(),
settings.members.roleSelect(),
role,
);
},
removeRole: async (role) => {
await settings.members
.roleSelect()
.locator('.ant-select-selection-item')
.filter({ hasText: role })
.locator('.ant-select-selection-item-remove')
.click();
},
saveMember: async () => {
await page.getByTestId('member-save-btn').click();
await expect(

View File

@@ -1,7 +1,7 @@
import { withAuthFlows } from '../auth-flows';
import { kit } from '../kit';
import { guestDriver, kit, type Layer } from '../kit';
import { withSettings } from './driver';
import { settingsDriver, withSettings, type Settings } from './driver';
import {
withActiveEditor,
withCustomRoleSeed,
@@ -9,6 +9,12 @@ import {
withServiceAccountSeed,
} from './seeds';
/** The settings pages bound to the logged-out `guestPage`, for a signed-in member. */
const withGuestSettings: Layer<{ guestSettings: Settings }> = guestDriver(
'guestSettings',
({ page }) => settingsDriver(page),
);
/**
* The settings `test`. Seeds are test-scoped and lazy: a spec pays only for the
* ones it destructures. `authFlows` drives a guest context, for the flows that
@@ -16,8 +22,10 @@ import {
*/
export const test = withAuthFlows(
withSettings(
withActiveEditor(
withPendingMember(withServiceAccountSeed(withCustomRoleSeed(kit))),
withGuestSettings(
withActiveEditor(
withPendingMember(withServiceAccountSeed(withCustomRoleSeed(kit))),
),
),
),
);

View File

@@ -1,9 +1,11 @@
import type { Browser, TestType } from '@playwright/test';
import type { User } from '../auth';
import type { AuthtypesTransactionGroupDTO } from '../schemas';
import { onAdminPage, type Layer } from '../kit';
import { usersApi, type CreatedUser } from '../users';
import { grantAll, roleName, settingsDriver } from './driver';
import { grantAll, roleName, settingsDriver, type Settings } from './driver';
/**
* Test-scoped seeds. Each test that asks for one gets its own copy, created on
@@ -123,3 +125,40 @@ export const withActiveEditor = perTest(
({ browser, value }) =>
onAdminPage(browser, (page) => usersApi(page).remove(value.id)),
);
export interface GrantedUser {
user: User;
role: string;
remove(): Promise<void>;
}
/**
* An active member whose only role is a fresh custom role holding
* `transactionGroups`. Call `remove` in a `finally`.
*/
export async function createGrantedUser(
settings: Settings,
workerIndex: number,
kind: string,
transactionGroups: AuthtypesTransactionGroupDTO[],
): Promise<GrantedUser> {
const name = roleName(kind, workerIndex);
const roleId = await settings.roles.createApi({ name, transactionGroups });
const label = `${kind}-w${workerIndex}-${Date.now()}`;
let member: CreatedUser;
try {
member = await settings.users.createActive(name, label);
} catch (error) {
await settings.users.removeByEmail(`e2e-${label}@signoz.test`);
await settings.roles.removeApi(roleId);
throw error;
}
return {
user: member.user,
role: name,
remove: async () => {
await settings.users.remove(member.id);
await settings.roles.removeApi(roleId);
},
};
}

View File

@@ -1,4 +1,9 @@
import { expect, test } from '../../drivers/settings';
import {
createGrantedUser,
expect,
grantAll,
test,
} from '../../drivers/settings';
test.describe('settings — members', () => {
test('TC-01 an invite of two rows creates two pending members', async ({
@@ -62,4 +67,163 @@ test.describe('settings — members', () => {
await settings.members.search(email);
await expect(settings.members.row(email)).toContainText('DELETED');
});
test('TC-04 a user without create:user cannot invite members', async ({
settings,
authFlows,
guestSettings,
}, testInfo) => {
const member = await createGrantedUser(
settings,
testInfo.workerIndex,
'list-users',
[grantAll('list', 'user', 'user')],
);
try {
await authFlows.login(member.user);
await guestSettings.members.goto();
await expect(guestSettings.members.inviteButton()).toHaveAttribute(
'data-denied-permissions',
/create/,
);
} finally {
await member.remove();
}
});
test('TC-05 a user without list:user cannot list members', async ({
settings,
authFlows,
guestSettings,
}, testInfo) => {
const member = await createGrantedUser(
settings,
testInfo.workerIndex,
'no-list',
[grantAll('read', 'dashboard')],
);
try {
await authFlows.login(member.user);
await guestSettings.members.goto();
await expect(guestSettings.members.deniedMessage()).toBeVisible();
await expect(guestSettings.members.searchInput()).toHaveAttribute(
'data-denied-permissions',
/list/,
);
} finally {
await member.remove();
}
});
test('TC-06 an invite link is unavailable without factor-password:list', async ({
settings,
authFlows,
guestSettings,
pendingMember,
}, testInfo) => {
const member = await createGrantedUser(
settings,
testInfo.workerIndex,
'no-pwd',
[grantAll('list', 'user', 'user'), grantAll('read', 'user', 'user')],
);
try {
await authFlows.login(member.user);
await guestSettings.members.goto();
await guestSettings.members.openMember(pendingMember.email);
await expect(guestSettings.members.resetLinkButton()).toHaveAttribute(
'data-denied-permissions',
/factor-password/,
);
await expect(guestSettings.members.resetLinkButton()).toContainText(
'Invite Link Unavailable',
);
} finally {
await member.remove();
}
});
test('TC-07 a member can add and remove a role on themselves', async ({
settings,
authFlows,
guestSettings,
}, testInfo) => {
const member = await createGrantedUser(
settings,
testInfo.workerIndex,
'self-edit',
[
grantAll('list', 'user', 'user'),
grantAll('read', 'user', 'user'),
grantAll('attach', 'user', 'user'),
grantAll('detach', 'user', 'user'),
grantAll('list', 'role', 'role'),
grantAll('read', 'role', 'role'),
grantAll('attach', 'role', 'role'),
grantAll('detach', 'role', 'role'),
],
);
try {
await authFlows.login(member.user);
await guestSettings.members.goto();
await guestSettings.members.openMember(member.user.email);
await guestSettings.members.addRole('signoz-viewer');
await guestSettings.members.saveMember();
await guestSettings.reload();
await guestSettings.members.openMember(member.user.email);
await expect(guestSettings.members.roleSelect()).toContainText(
'signoz-viewer',
);
const added = await settings.users.byEmail(member.user.email);
expect(
(added?.userRoles ?? []).map((entry) => entry.role.name).sort(),
).toEqual([member.role, 'signoz-viewer'].sort());
await guestSettings.members.removeRole('signoz-viewer');
await guestSettings.members.saveMember();
await guestSettings.reload();
await guestSettings.members.openMember(member.user.email);
await expect(guestSettings.members.roleSelect()).not.toContainText(
'signoz-viewer',
);
const removed = await settings.users.byEmail(member.user.email);
expect((removed?.userRoles ?? []).map((entry) => entry.role.name)).toEqual([
member.role,
]);
} finally {
await member.remove();
}
});
test('TC-08 a user without update:user cannot rename another member', async ({
settings,
authFlows,
guestSettings,
activeEditor,
}, testInfo) => {
const member = await createGrantedUser(
settings,
testInfo.workerIndex,
'no-update',
[grantAll('list', 'user', 'user'), grantAll('read', 'user', 'user')],
);
try {
await authFlows.login(member.user);
await guestSettings.members.goto();
await guestSettings.members.openMember(activeEditor.user.email);
await expect(guestSettings.members.nameInput()).toHaveAttribute(
'data-denied-permissions',
/update/,
);
} finally {
await member.remove();
}
});
});

View File

@@ -1,7 +1,4 @@
import io
import os
import tarfile
import time
from collections.abc import Callable, Generator
from datetime import datetime
from typing import Any
@@ -598,113 +595,3 @@ 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_user", scope="package")
def clickhouse_user(
clickhouse: types.TestContainerClickhouse,
) -> Generator[Callable[[str, str, list[str]], str], Any]:
"""
Yields create(name, profile, grants) that defines a user whose default settings profile is
profile, and returns its DSN on the docker network. Users are users.d files, like settings
profiles. They are removed on teardown.
"""
container = docker.from_env().containers.get(clickhouse.container.id)
password = "password"
names: list[str] = []
def wait_for_user(name: str, exists: bool) -> None:
deadline = time.monotonic() + 30
while time.monotonic() < deadline:
count = clickhouse.conn.query(
"SELECT count() FROM system.users WHERE name = {name:String}",
parameters={"name": name},
).result_rows[0][0]
if (count == 1) == exists:
return
time.sleep(0.5)
raise TimeoutError(f"user {name} did not {'appear' if exists else 'disappear'}")
def create(name: str, profile: str, grants: list[str]) -> str:
queries = "".join(f"<query>{grant}</query>" for grant in grants)
body = (f"<clickhouse><users><{name}><password>{password}</password><profile>{profile}</profile><networks><ip>::/0</ip></networks><grants>{queries}</grants></{name}></users></clickhouse>").encode()
archive = io.BytesIO()
with tarfile.open(fileobj=archive, mode="w") as tar:
info = tarfile.TarInfo(f"user-{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_user(name, exists=True)
return f"tcp://{name}:{password}@{clickhouse.container.container_configs['9000'].address}:9000"
yield create
for name in names:
container.exec_run(["rm", "-f", f"/etc/clickhouse-server/users.d/user-{name}.xml"])
for name in names:
wait_for_user(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}")

View File

@@ -0,0 +1,12 @@
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:01:00+00:00","value":1,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:02:00+00:00","value":2,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:03:00+00:00","value":4,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:04:00+00:00","value":4,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:05:00+00:00","value":15,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:06:00+00:00","value":10,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:07:00+00:00","value":36,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:08:00+00:00","value":25,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:09:00+00:00","value":37,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:10:00+00:00","value":35,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:11:00+00:00","value":39,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}
{"metric_name":"request_total_reserved_labels_from_engine","labels":{"service":"api","endpoint":"/health","status_code":"200"},"timestamp":"2026-01-29T10:12:00+00:00","value":25,"temporality":"Cumulative","type_":"Sum","is_monotonic":false,"flags":0,"description":"","unit":"","env":"default","resource_attrs":{},"scope_attrs":{}}

View File

@@ -0,0 +1,57 @@
{
"alert": "reserved_labels_from_engine",
"ruleType": "threshold_rule",
"alertType": "METRIC_BASED_ALERT",
"condition": {
"thresholds": {
"kind": "basic",
"spec": [
{
"name": "critical",
"target": 10,
"matchType": "at_least_once",
"op": "above",
"channels": [
"test channel"
]
}
]
},
"compositeQuery": {
"queryType": "clickhouse_sql",
"panelType": "graph",
"queries": [
{
"type": "clickhouse_sql",
"spec": {
"name": "A",
"query": "WITH __temporal_aggregation_cte AS (\n SELECT \n fingerprint, \n toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), toIntervalSecond(60)) AS ts, \n avg(value) AS per_series_value \n FROM signoz_metrics.distributed_samples_v4 AS points \n INNER JOIN (\n SELECT fingerprint \n FROM signoz_metrics.time_series_v4 \n WHERE metric_name IN ('request_total_reserved_labels_from_engine') \n AND LOWER(temporality) LIKE LOWER('cumulative') \n GROUP BY fingerprint\n ) AS filtered_time_series ON points.fingerprint = filtered_time_series.fingerprint \n WHERE metric_name IN ('request_total_reserved_labels_from_engine') \n AND unix_milli >= $start_timestamp_ms \n AND unix_milli < $end_timestamp_ms \n GROUP BY fingerprint, ts \n ORDER BY fingerprint, ts\n), \n__spatial_aggregation_cte AS (\n SELECT \n ts, \n sum(per_series_value) AS value \n FROM __temporal_aggregation_cte \n WHERE isNaN(per_series_value) = 0 \n GROUP BY ts\n) \nSELECT * FROM __spatial_aggregation_cte \nORDER BY ts"
}
}
]
},
"selectedQueryName": "A"
},
"evaluation": {
"kind": "rolling",
"spec": {
"evalWindow": "5m0s",
"frequency": "15s"
}
},
"labels": {"threshold.name": "spoofed", "severity": "spoofed", "team": "payments"},
"annotations": {
"description": "This alert is fired when the defined metric (current value: {{$value}}) crosses the threshold ({{$threshold}})",
"summary": "This alert is fired when the defined metric (current value: {{$value}}) crosses the threshold ({{$threshold}})"
},
"notificationSettings": {
"groupBy": [],
"usePolicy": false,
"renotify": {
"enabled": false,
"interval": "30m",
"alertStates": []
}
},
"schemaVersion": "v2alpha1"
}

View File

@@ -1,246 +0,0 @@
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(currentUser() = 'signoz_query') AS user, 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] == [1, 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
def test_clickhouse_sql_switching_profile_keeps_limits(
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 SETTINGS profile = 'default'", "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]
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",
],
ids=["readonly_stricter", "readonly_lifted"],
)
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
@pytest.mark.parametrize(
"query",
[
"SELECT toFloat64(count()) AS value FROM system.query_log",
"CREATE TABLE default.integration_readonly_probe (x UInt8) ENGINE = Memory",
],
ids=["read_outside_grants", "write"],
)
def test_clickhouse_sql_outside_grants_is_denied(
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.FORBIDDEN, response.text
error = response.json()["error"]
assert (error["code"], error["suggestions"]) == ("access_denied", ["query only the telemetry tables"]), error

View File

@@ -1,56 +0,0 @@
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],
clickhouse_user: Callable[[str, str, list[str]], str],
request: pytest.FixtureRequest,
pytestconfig: pytest.Config,
) -> types.SigNoz:
"""
SigNoz whose clickhouse_sql queries run as signoz_query, a user with the clickhouse_query
profile that can only read the default database.
"""
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>",
)
dsn = clickhouse_user("signoz_query", "clickhouse_query", ["GRANT SELECT ON default.*"])
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_DSN": dsn,
},
)

View File

@@ -543,6 +543,32 @@ TEST_RULES_MISCELLANEOUS = [
],
),
),
# the rule carries threshold.name and severity labels of its own; the alert
# keeps the values the engine wrote and only the plain label comes through
types.AlertTestCase(
name="test_rule_labels_do_not_override_engine_labels",
rule_path="ruler/test_scenarios/reserved_labels_from_engine/rule.json",
alert_data=[
types.AlertData(
type="metrics",
data_path="ruler/test_scenarios/reserved_labels_from_engine/alert_data.jsonl",
),
],
alert_expectation=types.AlertExpectation(
should_alert=True,
wait_time_seconds=30,
expected_alerts=[
types.FiringAlert(
labels={
"alertname": "reserved_labels_from_engine",
"threshold.name": "critical",
"severity": "critical",
"team": "payments",
}
),
],
),
),
# TODO: @abhishekhugetech enable the test for multi threshold rule, pylint: disable=W0511
# after the [issue](https://github.com/SigNoz/engineering-pod/issues/3934) with alertManager is resolved
# types.AlertTestCase(