Compare commits

...

2 Commits

Author SHA1 Message Date
Naman Verma
5a400fb44a fix(rules): keep rule labels from overriding engine labels 2026-11-16 21:01:45 +05:30
Swapnil Nakade
1efbbcfc5e feat: adding integration tests for audit querier (#13550)
<!--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
- adding integration tests for audit querier
- adds tests for querying ingested audit logs

<!--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/13
2026-11-16 14:21:04 +00:00
21 changed files with 941 additions and 477 deletions

View File

@@ -40,7 +40,6 @@ jobs:
suite:
- alertmanager
- alertmanagerrotation
- auditquerier
- basepath
- callbackauthn
- cloudintegrations
@@ -58,6 +57,7 @@ jobs:
- querierscalar
- queriercommon
- querierai
- querieraudit
- rawexportdata
- promqlconformance
- promapiconformance

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:

View File

@@ -124,11 +124,11 @@ tests/
│ ├── alerts/
│ │ ├── 01_*.py # numbered suite files
│ │ └── conftest.py # optional suite-local fixtures
│ ├── auditquerier/
│ ├── cloudintegrations/
│ ├── dashboard/
│ ├── passwordauthn/
│ ├── querier/
│ ├── querieraudit/
│ └── ...
└── e2e/ # Playwright suite (see docs/contributing/tests/e2e.md)
```

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

@@ -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

@@ -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

@@ -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

@@ -11,6 +11,12 @@ from ksuid import KsuidMs
from fixtures import types
from fixtures.fingerprint import LogsOrTracesFingerprint
ALICE_CREATED_DASHBOARD = "alice@acme.com (user-001) created dashboard (dash-001)"
ALICE_UPDATED_DASHBOARD = "alice@acme.com (user-001) updated dashboard (dash-001)"
VIEWER_FAILED_DELETE_DASHBOARD = "viewer@acme.com (user-002) failed to delete dashboard (dash-001): forbidden"
SA_CREATED_ALERT_RULE = "sa-001 created alert-rule (alert-001)"
ALICE_UPDATED_USER = "alice@acme.com (user-001) updated user (user-020)"
class AuditResource(ABC):
labels: str
@@ -180,3 +186,83 @@ def insert_audit_logs(
cluster = clickhouse.env["SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_CLUSTER"]
for table in ["logs", "logs_resource"]:
clickhouse.conn.query(f"TRUNCATE TABLE signoz_audit.{table} ON CLUSTER '{cluster}' SYNC")
def sample_audit_logs(now: datetime.datetime) -> list[AuditLog]:
return [
AuditLog(
timestamp=now - datetime.timedelta(seconds=5),
resources={"service.name": "signoz", "signoz.resource.kind": "dashboard", "signoz.resource.id": "dash-001"},
attributes={
"signoz.principal.id": "user-001",
"signoz.principal.email": "alice@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "create",
"signoz.audit.outcome": "success",
"signoz.audit.fields_changed": 3,
},
body=ALICE_CREATED_DASHBOARD,
event_name="signoz.audit.create",
scope_version="1.0.0",
),
AuditLog(
timestamp=now - datetime.timedelta(seconds=4),
resources={"service.name": "signoz", "signoz.resource.kind": "dashboard", "signoz.resource.id": "dash-001"},
attributes={
"signoz.principal.id": "user-001",
"signoz.principal.email": "alice@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
"signoz.audit.fields_changed": 8,
},
body=ALICE_UPDATED_DASHBOARD,
event_name="signoz.audit.update",
scope_version="1.0.0",
),
AuditLog(
timestamp=now - datetime.timedelta(seconds=3),
resources={"service.name": "signoz", "signoz.resource.kind": "dashboard", "signoz.resource.id": "dash-001"},
attributes={
"signoz.principal.id": "user-002",
"signoz.principal.email": "viewer@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "delete",
"signoz.audit.outcome": "failure",
"exception.type": "forbidden",
},
body=VIEWER_FAILED_DELETE_DASHBOARD,
event_name="signoz.audit.delete",
severity_text="ERROR",
scope_version="1.0.0",
),
AuditLog(
timestamp=now - datetime.timedelta(seconds=2),
resources={"service.name": "signoz", "signoz.resource.kind": "alert-rule", "signoz.resource.id": "alert-001"},
attributes={
"signoz.principal.id": "sa-001",
"signoz.principal.type": "service_account",
"signoz.audit.verb": "create",
"signoz.audit.outcome": "success",
"signoz.audit.fields_changed": 12,
},
body=SA_CREATED_ALERT_RULE,
event_name="signoz.audit.create",
scope_version="1.0.0",
),
AuditLog(
timestamp=now - datetime.timedelta(seconds=1),
resources={"service.name": "signoz", "signoz.resource.kind": "user", "signoz.resource.id": "user-020"},
attributes={
"signoz.principal.id": "user-001",
"signoz.principal.email": "alice@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.action_category": "access_control",
"signoz.audit.outcome": "success",
},
body=ALICE_UPDATED_USER,
event_name="signoz.audit.update",
scope_version="2.0.0",
),
]

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,457 +0,0 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
import pytest
from fixtures import types
from fixtures.audit import AuditLog
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import (
BuilderQuery,
build_aggregation,
build_order_by,
build_scalar_query,
make_query_request,
)
def test_audit_list_all(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""List audit events across multiple resource types — verify count, ordering, and fields."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=3),
resources={
"service.name": "signoz",
"signoz.resource.kind": "alert-rule",
"signoz.resource.id": "alert-001",
},
attributes={
"signoz.principal.id": "user-010",
"signoz.principal.email": "ops@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "create",
"signoz.audit.outcome": "success",
},
body="ops@acme.com (user-010) created alert-rule (alert-001)",
event_name="signoz.audit.create",
severity_text="INFO",
),
AuditLog(
timestamp=now - timedelta(seconds=2),
resources={
"service.name": "signoz",
"signoz.resource.kind": "saved-view",
"signoz.resource.id": "view-001",
},
attributes={
"signoz.principal.id": "user-010",
"signoz.principal.email": "ops@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
},
body="ops@acme.com (user-010) updated saved-view (view-001)",
event_name="signoz.audit.update",
severity_text="INFO",
),
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "user",
"signoz.resource.id": "user-020",
},
attributes={
"signoz.principal.id": "user-010",
"signoz.principal.email": "ops@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.action_category": "access_control",
"signoz.audit.outcome": "success",
},
body="ops@acme.com (user-010) updated user (user-020)",
event_name="signoz.audit.update",
severity_text="INFO",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK
assert response.json()["status"] == "success"
rows = response.json()["data"]["data"]["results"][0]["rows"]
assert len(rows) == 3
# Most recent first
assert [row["data"]["event_name"] for row in rows] == ["signoz.audit.update", "signoz.audit.update", "signoz.audit.create"]
assert [row["data"]["body"]["message"] for row in rows] == [
"ops@acme.com (user-010) updated user (user-020)",
"ops@acme.com (user-010) updated saved-view (view-001)",
"ops@acme.com (user-010) created alert-rule (alert-001)",
]
# Verify event_name and body are present
assert rows[0]["data"]["body"] == {"message": "ops@acme.com (user-010) updated user (user-020)"}
assert rows[0]["data"]["severity_text"] == "INFO"
assert rows[0]["data"]["resource"] == {
"attributes": {
"service.name": "signoz",
"signoz.resource.kind": "user",
"signoz.resource.id": "user-020",
}
}
assert rows[0]["data"]["attributes"]["signoz.audit.action_category"] == "access_control"
assert rows[0]["data"]["scope"]["name"] == "signoz.audit"
@pytest.mark.parametrize(
"filter_expression,expected_count,expected_event_names",
[
pytest.param(
"signoz.principal.id = 'user-001'",
3,
{"signoz.audit.update", "signoz.audit.create"},
id="filter_by_principal_id",
),
pytest.param(
"signoz.audit.outcome = 'failure'",
1,
{"signoz.audit.delete"},
id="filter_by_outcome_failure",
),
pytest.param(
"signoz.resource.kind = 'dashboard' AND signoz.resource.id = 'dash-001'",
3,
{"signoz.audit.delete", "signoz.audit.update", "signoz.audit.create"},
id="filter_by_resource_kind_and_id",
),
pytest.param(
"signoz.principal.type = 'service_account'",
1,
{"signoz.audit.create"},
id="filter_by_principal_type",
),
pytest.param(
"signoz.resource.kind = 'dashboard' AND signoz.audit.verb = 'delete'",
1,
{"signoz.audit.delete"},
id="filter_by_resource_kind_and_action",
),
pytest.param(
"resource.signoz.resource.id = 'sa-001'",
1,
{"signoz.audit.create"},
id="filter_by_resource_prefix",
),
pytest.param(
"body CONTAINS 'failed'",
1,
{"signoz.audit.delete"},
id="filter_by_body_contains",
),
],
)
def test_audit_filter(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
filter_expression: str,
expected_count: int,
expected_event_names: set,
) -> None:
"""Parametrized audit filter tests covering the documented query patterns."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=5),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-001",
},
attributes={
"signoz.principal.id": "user-001",
"signoz.principal.email": "alice@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "create",
"signoz.audit.action_category": "configuration_changes",
"signoz.audit.outcome": "success",
},
body="alice@acme.com created dashboard",
event_name="signoz.audit.create",
),
AuditLog(
timestamp=now - timedelta(seconds=4),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-001",
},
attributes={
"signoz.principal.id": "user-001",
"signoz.principal.email": "alice@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.action_category": "configuration_changes",
"signoz.audit.outcome": "success",
},
body="alice@acme.com updated dashboard",
event_name="signoz.audit.update",
),
AuditLog(
timestamp=now - timedelta(seconds=3),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-001",
},
attributes={
"signoz.principal.id": "user-002",
"signoz.principal.email": "viewer@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "delete",
"signoz.audit.action_category": "configuration_changes",
"signoz.audit.outcome": "failure",
"exception.type": "forbidden",
},
body="viewer@acme.com failed to delete dashboard",
event_name="signoz.audit.delete",
severity_text="ERROR",
),
AuditLog(
timestamp=now - timedelta(seconds=2),
resources={
"service.name": "signoz",
"signoz.resource.kind": "serviceaccount",
"signoz.resource.id": "sa-001",
},
attributes={
"signoz.principal.id": "sa-001",
"signoz.principal.type": "service_account",
"signoz.audit.verb": "create",
"signoz.audit.action_category": "access_control",
"signoz.audit.outcome": "success",
},
body="sa-001 created serviceaccount",
event_name="signoz.audit.create",
),
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "session",
"signoz.resource.id": "*",
},
attributes={
"signoz.principal.id": "user-001",
"signoz.principal.email": "alice@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "create",
"signoz.audit.action_category": "access_control",
"signoz.audit.outcome": "success",
},
body="alice@acme.com created session",
event_name="signoz.audit.create",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
filter_expression=filter_expression,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK
rows = response.json()["data"]["data"]["results"][0]["rows"]
assert len(rows) == expected_count
actual_event_names = {row["data"]["event_name"] for row in rows}
assert actual_event_names == expected_event_names
def test_audit_scalar_count_failures(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""Alert query — count multiple failures from different principals."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=3),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-100",
},
attributes={
"signoz.principal.id": "user-050",
"signoz.principal.type": "user",
"signoz.audit.verb": "delete",
"signoz.audit.outcome": "failure",
},
body="user-050 failed to delete dashboard",
event_name="signoz.audit.delete",
severity_text="ERROR",
),
AuditLog(
timestamp=now - timedelta(seconds=2),
resources={
"service.name": "signoz",
"signoz.resource.kind": "alert-rule",
"signoz.resource.id": "alert-200",
},
attributes={
"signoz.principal.id": "user-060",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "failure",
},
body="user-060 failed to update alert-rule",
event_name="signoz.audit.update",
severity_text="ERROR",
),
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-100",
},
attributes={
"signoz.principal.id": "user-050",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
},
body="user-050 updated dashboard",
event_name="signoz.audit.update",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
build_scalar_query(
name="A",
signal="logs",
source="audit",
aggregations=[build_aggregation("count()")],
filter_expression="signoz.audit.outcome = 'failure'",
)
],
request_type="scalar",
)
assert response.status_code == HTTPStatus.OK
assert response.json()["status"] == "success"
scalar_data = response.json()["data"]["data"]["results"][0].get("data", [])
assert len(scalar_data) == 1
assert scalar_data[0][0] == 2
def test_audit_does_not_leak_into_logs(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""A single audit event in signoz_audit must not appear in regular log queries."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "organization",
"signoz.resource.id": "org-999",
},
attributes={
"signoz.principal.id": "user-admin",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
},
body="user-admin updated organization (org-999)",
event_name="signoz.audit.update",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
limit=100,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK
rows = response.json()["data"]["data"]["results"][0].get("rows") or []
audit_bodies = [row["data"]["body"] for row in rows if "signoz.audit" in row["data"].get("attributes_string", {}).get("signoz.audit.verb", "")]
assert len(audit_bodies) == 0

View File

@@ -0,0 +1,177 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
from fixtures import types
from fixtures.audit import AuditLog
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import BuilderQuery, build_order_by, make_query_request
def test_audit_list_all(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""List audit events across multiple resource types — verify count, ordering, and fields."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=3),
resources={
"service.name": "signoz",
"signoz.resource.kind": "alert-rule",
"signoz.resource.id": "alert-001",
},
attributes={
"signoz.principal.id": "user-010",
"signoz.principal.email": "ops@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "create",
"signoz.audit.outcome": "success",
},
body="ops@acme.com (user-010) created alert-rule (alert-001)",
event_name="signoz.audit.create",
severity_text="INFO",
),
AuditLog(
timestamp=now - timedelta(seconds=2),
resources={
"service.name": "signoz",
"signoz.resource.kind": "saved-view",
"signoz.resource.id": "view-001",
},
attributes={
"signoz.principal.id": "user-010",
"signoz.principal.email": "ops@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
},
body="ops@acme.com (user-010) updated saved-view (view-001)",
event_name="signoz.audit.update",
severity_text="INFO",
),
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "user",
"signoz.resource.id": "user-020",
},
attributes={
"signoz.principal.id": "user-010",
"signoz.principal.email": "ops@acme.com",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.action_category": "access_control",
"signoz.audit.outcome": "success",
},
body="ops@acme.com (user-010) updated user (user-020)",
event_name="signoz.audit.update",
severity_text="INFO",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK
assert response.json()["status"] == "success"
rows = response.json()["data"]["data"]["results"][0]["rows"]
assert len(rows) == 3
# Most recent first
assert [row["data"]["event_name"] for row in rows] == ["signoz.audit.update", "signoz.audit.update", "signoz.audit.create"]
assert [row["data"]["body"]["message"] for row in rows] == [
"ops@acme.com (user-010) updated user (user-020)",
"ops@acme.com (user-010) updated saved-view (view-001)",
"ops@acme.com (user-010) created alert-rule (alert-001)",
]
# Verify event_name and body are present
assert rows[0]["data"]["body"] == {"message": "ops@acme.com (user-010) updated user (user-020)"}
assert rows[0]["data"]["severity_text"] == "INFO"
assert rows[0]["data"]["resource"] == {
"attributes": {
"service.name": "signoz",
"signoz.resource.kind": "user",
"signoz.resource.id": "user-020",
}
}
assert rows[0]["data"]["attributes"]["signoz.audit.action_category"] == "access_control"
assert rows[0]["data"]["scope"]["name"] == "signoz.audit"
def test_audit_does_not_leak_into_logs(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""A single audit event in signoz_audit must not appear in regular log queries."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "organization",
"signoz.resource.id": "org-999",
},
attributes={
"signoz.principal.id": "user-admin",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
},
body="user-admin updated organization (org-999)",
event_name="signoz.audit.update",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
limit=100,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK
rows = response.json()["data"]["data"]["results"][0].get("rows") or []
audit_bodies = [row["data"]["body"] for row in rows if "signoz.audit" in row["data"].get("attributes_string", {}).get("signoz.audit.verb", "")]
assert len(audit_bodies) == 0

View File

@@ -0,0 +1,212 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
from fixtures import types
from fixtures.audit import AuditLog
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import (
RequestType,
build_aggregation,
build_group_by_field,
build_scalar_query,
get_all_series,
index_series_by_label,
make_query_request,
)
def test_audit_scalar_count_failures(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""Alert query — count multiple failures from different principals."""
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=3),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-100",
},
attributes={
"signoz.principal.id": "user-050",
"signoz.principal.type": "user",
"signoz.audit.verb": "delete",
"signoz.audit.outcome": "failure",
},
body="user-050 failed to delete dashboard",
event_name="signoz.audit.delete",
severity_text="ERROR",
),
AuditLog(
timestamp=now - timedelta(seconds=2),
resources={
"service.name": "signoz",
"signoz.resource.kind": "alert-rule",
"signoz.resource.id": "alert-200",
},
attributes={
"signoz.principal.id": "user-060",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "failure",
},
body="user-060 failed to update alert-rule",
event_name="signoz.audit.update",
severity_text="ERROR",
),
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={
"service.name": "signoz",
"signoz.resource.kind": "dashboard",
"signoz.resource.id": "dash-100",
},
attributes={
"signoz.principal.id": "user-050",
"signoz.principal.type": "user",
"signoz.audit.verb": "update",
"signoz.audit.outcome": "success",
},
body="user-050 updated dashboard",
event_name="signoz.audit.update",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
now = datetime.now(tz=UTC)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
build_scalar_query(
name="A",
signal="logs",
source="audit",
aggregations=[build_aggregation("count()")],
filter_expression="signoz.audit.outcome = 'failure'",
)
],
request_type="scalar",
)
assert response.status_code == HTTPStatus.OK
assert response.json()["status"] == "success"
scalar_data = response.json()["data"]["data"]["results"][0].get("data", [])
assert len(scalar_data) == 1
assert scalar_data[0][0] == 2
def test_audit_scalar_distinct_principals_by_resource_kind(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
now = datetime.now(tz=UTC)
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=seconds_ago),
resources={"service.name": "signoz", "signoz.resource.kind": kind, "signoz.resource.id": resource_id},
attributes={"signoz.principal.id": principal, "signoz.principal.type": "user", "signoz.audit.verb": verb, "signoz.audit.outcome": "success"},
body=f"{principal} {verb} {kind} ({resource_id})",
event_name=f"signoz.audit.{verb}",
)
for seconds_ago, kind, resource_id, principal, verb in [
(5, "dashboard", "dash-001", "user-001", "update"),
(4, "dashboard", "dash-001", "user-001", "update"),
(3, "dashboard", "dash-002", "user-002", "delete"),
(2, "alert-rule", "alert-001", "user-001", "create"),
(1, "alert-rule", "alert-002", "user-001", "update"),
]
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
build_scalar_query(
name="A",
signal="logs",
source="audit",
aggregations=[build_aggregation("count_distinct(signoz.principal.id)")],
group_by=[build_group_by_field("signoz.resource.kind", "string", "resource")],
)
],
request_type=RequestType.SCALAR,
)
assert response.status_code == HTTPStatus.OK, response.text
rows = response.json()["data"]["data"]["results"][0]["data"]
assert {row[0]: row[-1] for row in rows} == {"dashboard": 2, "alert-rule": 1}
def test_audit_time_series_count_by_verb(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
one_minute_ago = now - timedelta(minutes=1)
two_minutes_ago = now - timedelta(minutes=2)
insert_audit_logs(
[
AuditLog(
timestamp=timestamp,
resources={"service.name": "signoz", "signoz.resource.kind": "dashboard", "signoz.resource.id": resource_id},
attributes={"signoz.principal.id": principal, "signoz.principal.type": "user", "signoz.audit.verb": verb, "signoz.audit.outcome": "success"},
body=f"{principal} {verb} dashboard ({resource_id})",
event_name=f"signoz.audit.{verb}",
)
for timestamp, resource_id, principal, verb in [
(two_minutes_ago + timedelta(seconds=10), "dash-001", "user-001", "update"),
(two_minutes_ago + timedelta(seconds=20), "dash-002", "user-001", "update"),
(one_minute_ago + timedelta(seconds=10), "dash-001", "user-002", "update"),
(one_minute_ago + timedelta(seconds=30), "dash-002", "user-002", "delete"),
]
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(minutes=5)).timestamp() * 1000),
end_ms=int((now + timedelta(seconds=5)).timestamp() * 1000),
queries=[
build_scalar_query(
name="A",
signal="logs",
source="audit",
aggregations=[build_aggregation("count()")],
group_by=[build_group_by_field("signoz.audit.verb", "string", "attribute")],
)
],
request_type=RequestType.TIME_SERIES,
)
assert response.status_code == HTTPStatus.OK, response.text
series_by_verb = index_series_by_label(get_all_series(response.json(), "A"), "signoz.audit.verb")
counts_by_verb = {verb: {point["timestamp"]: point["value"] for point in series["values"] if point["value"]} for verb, series in series_by_verb.items()}
two_minutes_ago_ms = int(two_minutes_ago.timestamp() * 1000)
one_minute_ago_ms = int(one_minute_ago.timestamp() * 1000)
assert counts_by_verb == {
"update": {two_minutes_ago_ms: 2, one_minute_ago_ms: 1},
"delete": {one_minute_ago_ms: 1},
}

View File

@@ -0,0 +1,74 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
import pytest
from fixtures import types
from fixtures.audit import (
ALICE_CREATED_DASHBOARD,
ALICE_UPDATED_DASHBOARD,
ALICE_UPDATED_USER,
SA_CREATED_ALERT_RULE,
VIEWER_FAILED_DELETE_DASHBOARD,
AuditLog,
sample_audit_logs,
)
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import BuilderQuery, build_order_by, make_query_request
@pytest.mark.parametrize(
"filter_expression,expected_bodies",
[
pytest.param("signoz.principal.id = 'user-001'", {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD, ALICE_UPDATED_USER}, id="equal"),
pytest.param("signoz.audit.verb != 'update'", {ALICE_CREATED_DASHBOARD, VIEWER_FAILED_DELETE_DASHBOARD, SA_CREATED_ALERT_RULE}, id="not_equal"),
pytest.param("signoz.audit.verb IN ['create', 'delete']", {ALICE_CREATED_DASHBOARD, VIEWER_FAILED_DELETE_DASHBOARD, SA_CREATED_ALERT_RULE}, id="in_list"),
pytest.param("signoz.resource.kind NOT IN ['dashboard']", {SA_CREATED_ALERT_RULE, ALICE_UPDATED_USER}, id="not_in_list"),
pytest.param("signoz.resource.kind = 'dashboard' AND signoz.resource.id = 'dash-001'", {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD, VIEWER_FAILED_DELETE_DASHBOARD}, id="and"),
pytest.param("signoz.audit.outcome = 'failure' OR signoz.principal.type = 'service_account'", {VIEWER_FAILED_DELETE_DASHBOARD, SA_CREATED_ALERT_RULE}, id="or"),
pytest.param("NOT signoz.resource.kind = 'dashboard'", {SA_CREATED_ALERT_RULE, ALICE_UPDATED_USER}, id="not"),
pytest.param("signoz.resource.kind = 'dashboard' AND (signoz.audit.verb = 'create' OR signoz.audit.verb = 'delete')", {ALICE_CREATED_DASHBOARD, VIEWER_FAILED_DELETE_DASHBOARD}, id="and_with_grouped_or"),
pytest.param("exception.type EXISTS", {VIEWER_FAILED_DELETE_DASHBOARD}, id="exists"),
pytest.param("exception.type NOT EXISTS", {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD, SA_CREATED_ALERT_RULE, ALICE_UPDATED_USER}, id="not_exists"),
pytest.param("body CONTAINS 'failed'", {VIEWER_FAILED_DELETE_DASHBOARD}, id="contains"),
pytest.param("body NOT CONTAINS 'dashboard'", {SA_CREATED_ALERT_RULE, ALICE_UPDATED_USER}, id="not_contains"),
pytest.param("body LIKE '%failed%'", {VIEWER_FAILED_DELETE_DASHBOARD}, id="like"),
pytest.param("body ILIKE '%ALERT-RULE%'", {SA_CREATED_ALERT_RULE}, id="ilike"),
pytest.param("body REGEXP '^sa-001'", {SA_CREATED_ALERT_RULE}, id="regexp"),
pytest.param("signoz.audit.fields_changed > 5", {ALICE_UPDATED_DASHBOARD, SA_CREATED_ALERT_RULE}, id="gt_number"),
pytest.param("signoz.audit.fields_changed <= 8", {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD}, id="lte_number"),
],
)
def test_audit_filter_operators(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
filter_expression: str,
expected_bodies: set[str],
) -> None:
now = datetime.now(tz=UTC)
insert_audit_logs(sample_audit_logs(now))
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
filter_expression=filter_expression,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK, response.text
rows = response.json()["data"]["data"]["results"][0].get("rows") or []
assert {row["data"]["body"]["message"] for row in rows} == expected_bodies

View File

@@ -0,0 +1,78 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
from fixtures import types
from fixtures.audit import AuditLog
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import (
BuilderQuery,
RequestType,
build_aggregation,
build_order_by,
build_scalar_query,
make_query_request,
)
def test_audit_time_range_is_start_inclusive_end_exclusive(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
) -> None:
"""Events at start, inside, and at end minus one second are returned; one second before start, at end, and two hours earlier are not. The same window bounds a scalar count."""
end = datetime.now(tz=UTC).replace(microsecond=0)
start = end - timedelta(minutes=1)
insert_audit_logs(
[
AuditLog(
timestamp=timestamp,
resources={"service.name": "signoz", "signoz.resource.kind": "dashboard", "signoz.resource.id": "dash-001"},
attributes={"signoz.principal.id": "user-001", "signoz.principal.type": "user", "signoz.audit.verb": "update", "signoz.audit.outcome": "success"},
body=body,
event_name="signoz.audit.update",
)
for timestamp, body in [
(start - timedelta(seconds=1), "one second before start"),
(start, "at start"),
(start + timedelta(seconds=30), "inside"),
(end - timedelta(seconds=1), "one second before end"),
(end, "at end"),
(end - timedelta(hours=2), "two hours earlier"),
]
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
start_ms = int(start.timestamp() * 1000)
end_ms = int(end.timestamp() * 1000)
response = make_query_request(
signoz,
token,
start_ms=start_ms,
end_ms=end_ms,
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type=RequestType.RAW,
)
assert response.status_code == HTTPStatus.OK, response.text
rows = response.json()["data"]["data"]["results"][0].get("rows") or []
assert [row["data"]["body"]["message"] for row in rows] == ["one second before end", "inside", "at start"]
response = make_query_request(
signoz,
token,
start_ms=start_ms,
end_ms=end_ms,
queries=[build_scalar_query(name="A", signal="logs", source="audit", aggregations=[build_aggregation("count()")])],
request_type=RequestType.SCALAR,
)
assert response.status_code == HTTPStatus.OK, response.text
assert response.json()["data"]["data"]["results"][0]["data"][0][0] == 3

View File

@@ -0,0 +1,99 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
import pytest
from fixtures import types
from fixtures.audit import AuditLog
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import BuilderQuery, build_order_by, make_query_request
ATTACHED_ROLE_001_TO_USER_020 = "ops@acme.com (user-010) attached role (role-001) to user (user-020)"
DETACHED_ROLE_001_FROM_USER_021 = "ops@acme.com (user-010) detached role (role-001) from user (user-021)"
ATTACHED_ROLE_002_TO_USER_020 = "ops@acme.com (user-010) attached role (role-002) to user (user-020)"
UPDATED_ROLE_001 = "ops@acme.com (user-010) updated role (role-001)"
@pytest.mark.parametrize(
"filter_expression,expected_bodies",
[
pytest.param("event_name = 'signoz.audit.attach'", {ATTACHED_ROLE_001_TO_USER_020, ATTACHED_ROLE_002_TO_USER_020}, id="attach_events"),
pytest.param("event_name = 'signoz.audit.detach'", {DETACHED_ROLE_001_FROM_USER_021}, id="detach_events"),
pytest.param("signoz.resource.target.id = 'user-020'", {ATTACHED_ROLE_001_TO_USER_020, ATTACHED_ROLE_002_TO_USER_020}, id="by_target_id"),
pytest.param("signoz.resource.id = 'role-001' AND signoz.resource.target.kind = 'user'", {ATTACHED_ROLE_001_TO_USER_020, DETACHED_ROLE_001_FROM_USER_021}, id="by_resource_and_target_kind"),
pytest.param("signoz.resource.target.kind EXISTS", {ATTACHED_ROLE_001_TO_USER_020, DETACHED_ROLE_001_FROM_USER_021, ATTACHED_ROLE_002_TO_USER_020}, id="with_target"),
pytest.param("signoz.resource.target.kind NOT EXISTS", {UPDATED_ROLE_001}, id="without_target"),
],
)
def test_audit_relation_filters(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
filter_expression: str,
expected_bodies: set[str],
) -> None:
"""Attach and detach events carry the counterpart as target attributes on the record; a plain update on the same resource carries none."""
now = datetime.now(tz=UTC)
principal = {"signoz.principal.id": "user-010", "signoz.principal.email": "ops@acme.com", "signoz.principal.type": "user", "signoz.audit.outcome": "success"}
insert_audit_logs(
[
AuditLog(
timestamp=now - timedelta(seconds=4),
resources={"service.name": "signoz", "signoz.resource.kind": "role", "signoz.resource.id": "role-001"},
attributes={**principal, "signoz.audit.verb": "attach", "signoz.resource.target.kind": "user", "signoz.resource.target.id": "user-020"},
body=ATTACHED_ROLE_001_TO_USER_020,
event_name="signoz.audit.attach",
),
AuditLog(
timestamp=now - timedelta(seconds=3),
resources={"service.name": "signoz", "signoz.resource.kind": "role", "signoz.resource.id": "role-001"},
attributes={**principal, "signoz.audit.verb": "detach", "signoz.resource.target.kind": "user", "signoz.resource.target.id": "user-021"},
body=DETACHED_ROLE_001_FROM_USER_021,
event_name="signoz.audit.detach",
),
AuditLog(
timestamp=now - timedelta(seconds=2),
resources={"service.name": "signoz", "signoz.resource.kind": "role", "signoz.resource.id": "role-002"},
attributes={**principal, "signoz.audit.verb": "attach", "signoz.resource.target.kind": "user", "signoz.resource.target.id": "user-020"},
body=ATTACHED_ROLE_002_TO_USER_020,
event_name="signoz.audit.attach",
),
AuditLog(
timestamp=now - timedelta(seconds=1),
resources={"service.name": "signoz", "signoz.resource.kind": "role", "signoz.resource.id": "role-001"},
attributes={**principal, "signoz.audit.verb": "update"},
body=UPDATED_ROLE_001,
event_name="signoz.audit.update",
),
]
)
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
filter_expression=filter_expression,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK, response.text
rows = response.json()["data"]["data"]["results"][0].get("rows") or []
assert {row["data"]["body"]["message"] for row in rows} == expected_bodies
for row in rows:
if row["data"]["event_name"] == "signoz.audit.update":
assert "signoz.resource.target.kind" not in row["data"]["attributes"]
else:
assert row["data"]["attributes"]["signoz.resource.target.kind"] == "user"
assert row["data"]["resource"]["attributes"]["signoz.resource.kind"] == "role"

View File

@@ -0,0 +1,73 @@
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
import pytest
from fixtures import types
from fixtures.audit import (
ALICE_CREATED_DASHBOARD,
ALICE_UPDATED_DASHBOARD,
ALICE_UPDATED_USER,
SA_CREATED_ALERT_RULE,
VIEWER_FAILED_DELETE_DASHBOARD,
AuditLog,
sample_audit_logs,
)
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
from fixtures.querier import BuilderQuery, build_order_by, make_query_request
ALL_SAMPLE_BODIES = {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD, VIEWER_FAILED_DELETE_DASHBOARD, SA_CREATED_ALERT_RULE, ALICE_UPDATED_USER}
BARE_SCOPE_KEYS_SKIP = pytest.mark.skip(reason="bare scope_name and scope_version resolve to attributes in the audit schema; only the scope. prefix reaches the scope column")
@pytest.mark.parametrize(
"filter_expression,expected_bodies",
[
pytest.param("signoz.resource.id = 'alert-001'", {SA_CREATED_ALERT_RULE}, id="resource_key_bare"),
pytest.param("resource.signoz.resource.id = 'alert-001'", {SA_CREATED_ALERT_RULE}, id="resource_key_prefixed"),
pytest.param("signoz.principal.email = 'alice@acme.com'", {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD, ALICE_UPDATED_USER}, id="attribute_key_bare"),
pytest.param("attribute.signoz.principal.email = 'alice@acme.com'", {ALICE_CREATED_DASHBOARD, ALICE_UPDATED_DASHBOARD, ALICE_UPDATED_USER}, id="attribute_key_prefixed"),
pytest.param("severity_text = 'ERROR'", {VIEWER_FAILED_DELETE_DASHBOARD}, id="intrinsic_key_bare"),
pytest.param("log.severity_text = 'ERROR'", {VIEWER_FAILED_DELETE_DASHBOARD}, id="intrinsic_key_prefixed"),
pytest.param("event_name = 'signoz.audit.create'", {ALICE_CREATED_DASHBOARD, SA_CREATED_ALERT_RULE}, id="intrinsic_event_name"),
pytest.param("scope.name = 'signoz.audit'", ALL_SAMPLE_BODIES, id="scope_name_prefixed"),
pytest.param("scope.name = 'other'", set(), id="scope_name_prefixed_no_match"),
pytest.param("scope.version = '2.0.0'", {ALICE_UPDATED_USER}, id="scope_version_prefixed"),
pytest.param("scope_name = 'signoz.audit'", ALL_SAMPLE_BODIES, marks=BARE_SCOPE_KEYS_SKIP, id="scope_name_bare"),
pytest.param("scope_version = '2.0.0'", {ALICE_UPDATED_USER}, marks=BARE_SCOPE_KEYS_SKIP, id="scope_version_bare"),
],
)
def test_audit_filter_by_field_context(
signoz: types.SigNoz,
create_user_admin: None, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
insert_audit_logs: Callable[[list[AuditLog]], None],
filter_expression: str,
expected_bodies: set[str],
) -> None:
"""A bare key resolves to the resource column for the keys the audit writer emits, to a built-in column for intrinsics, and to attributes otherwise; a context prefix pins it."""
now = datetime.now(tz=UTC)
insert_audit_logs(sample_audit_logs(now))
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
response = make_query_request(
signoz,
token,
start_ms=int((now - timedelta(seconds=30)).timestamp() * 1000),
end_ms=int(now.timestamp() * 1000),
queries=[
BuilderQuery(
signal="logs",
source="audit",
limit=100,
filter_expression=filter_expression,
order=[build_order_by("timestamp"), build_order_by("id")],
).to_dict()
],
request_type="raw",
)
assert response.status_code == HTTPStatus.OK, response.text
rows = response.json()["data"]["data"]["results"][0].get("rows") or []
assert {row["data"]["body"]["message"] for row in rows} == expected_bodies

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(