2023-03-23 19:45:15 +05:30
package v3
import (
2023-05-12 16:25:22 +05:30
"fmt"
2023-03-23 19:45:15 +05:30
"testing"
"github.com/stretchr/testify/require"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)
func TestBuildQuery ( t * testing . T ) {
t . Run ( "TestSimpleQueryWithName" , func ( t * testing . T ) {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
AggregateOperator : v3 . AggregateOperatorRateMax ,
Expression : "A" ,
} ,
} ,
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeGraph ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
2023-03-23 19:45:15 +05:30
require . NoError ( t , err )
require . Contains ( t , query , "WHERE metric_name = 'name'" )
} )
}
func TestBuildQueryWithFilters ( t * testing . T ) {
t . Run ( "TestBuildQueryWithFilters" , func ( t * testing . T ) {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
2023-04-07 09:46:21 +05:30
{ Key : v3 . AttributeKey { Key : "a" } , Value : "b" , Operator : v3 . FilterOperatorNotEqual } ,
{ Key : v3 . AttributeKey { Key : "code" } , Value : "ERROR_*" , Operator : v3 . FilterOperatorNotRegex } ,
2023-03-23 19:45:15 +05:30
} } ,
AggregateOperator : v3 . AggregateOperatorRateMax ,
Expression : "A" ,
2024-05-21 12:01:21 +05:30
Temporality : v3 . Cumulative ,
2023-03-23 19:45:15 +05:30
} ,
} ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
2023-03-23 19:45:15 +05:30
require . NoError ( t , err )
2024-05-21 12:01:21 +05:30
require . Contains ( t , query , "WHERE metric_name = 'name' AND temporality = 'Cumulative' AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000 AND JSONExtractString(labels, 'a') != 'b'" )
2023-03-23 19:45:15 +05:30
require . Contains ( t , query , rateWithoutNegative )
require . Contains ( t , query , "not match(JSONExtractString(labels, 'code'), 'ERROR_*')" )
} )
}
func TestBuildQueryWithMultipleQueries ( t * testing . T ) {
t . Run ( "TestBuildQueryWithFilters" , func ( t * testing . T ) {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
2023-04-07 09:46:21 +05:30
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
2023-03-23 19:45:15 +05:30
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
2024-05-21 12:01:21 +05:30
Temporality : v3 . Cumulative ,
2023-03-23 19:45:15 +05:30
Expression : "A" ,
} ,
"B" : {
QueryName : "B" ,
2023-09-17 10:40:45 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name2" } ,
AggregateOperator : v3 . AggregateOperatorRateMax ,
2024-05-21 12:01:21 +05:30
Temporality : v3 . Cumulative ,
2023-03-23 19:45:15 +05:30
Expression : "B" ,
} ,
} ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
2023-03-23 19:45:15 +05:30
require . NoError ( t , err )
2024-05-21 12:01:21 +05:30
require . Contains ( t , query , "WHERE metric_name = 'name' AND temporality = 'Cumulative' AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000 AND JSONExtractString(labels, 'in') IN ['a','b','c']" )
2023-03-23 19:45:15 +05:30
require . Contains ( t , query , rateWithoutNegative )
} )
}
2023-05-12 16:25:22 +05:30
2023-07-03 13:30:37 +05:30
func TestBuildQueryXRate ( t * testing . T ) {
t . Run ( "TestBuildQueryXRate" , func ( t * testing . T ) {
2024-05-21 12:01:21 +05:30
tmpl := ` SELECT ts, %s(rate_value) as value FROM (SELECT ts, If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) as rate_value FROM(SELECT fingerprint, toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), INTERVAL 60 SECOND) as ts, max(value) as value FROM signoz_metrics.distributed_samples_v4 INNER JOIN (SELECT DISTINCT fingerprint FROM signoz_metrics.time_series_v4_1day WHERE metric_name = 'name' AND temporality = '' AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000) as filtered_time_series USING fingerprint WHERE metric_name = 'name' AND unix_milli >= 1650991920000 AND unix_milli < 1651078380000 GROUP BY fingerprint, ts ORDER BY fingerprint, ts) WINDOW rate_window as (PARTITION BY fingerprint ORDER BY fingerprint, ts) ) WHERE isNaN(rate_value) = 0 GROUP BY ts ORDER BY ts `
2023-07-03 13:30:37 +05:30
cases := [ ] struct {
aggregateOperator v3 . AggregateOperator
expectedQuery string
} {
{
aggregateOperator : v3 . AggregateOperatorAvgRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorAvgRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMaxRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMaxRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMinRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMinRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorSumRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorSumRate ] ) ,
} ,
}
for _ , c := range cases {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-07-03 13:30:37 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
AggregateOperator : c . aggregateOperator ,
Expression : "A" ,
} ,
} ,
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeGraph ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
require . NoError ( t , err )
require . Equal ( t , query , c . expectedQuery )
}
} )
}
func TestBuildQueryRPM ( t * testing . T ) {
t . Run ( "TestBuildQueryXRate" , func ( t * testing . T ) {
2024-05-21 12:01:21 +05:30
tmpl := ` SELECT ts, ceil(value * 60) as value FROM (SELECT ts, %s(rate_value) as value FROM (SELECT ts, If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) as rate_value FROM(SELECT fingerprint, toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), INTERVAL 60 SECOND) as ts, max(value) as value FROM signoz_metrics.distributed_samples_v4 INNER JOIN (SELECT DISTINCT fingerprint FROM signoz_metrics.time_series_v4_1day WHERE metric_name = 'name' AND temporality = '' AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000) as filtered_time_series USING fingerprint WHERE metric_name = 'name' AND unix_milli >= 1650991920000 AND unix_milli < 1651078380000 GROUP BY fingerprint, ts ORDER BY fingerprint, ts) WINDOW rate_window as (PARTITION BY fingerprint ORDER BY fingerprint, ts) ) WHERE isNaN(rate_value) = 0 GROUP BY ts ORDER BY ts) `
2023-08-18 07:32:05 +05:30
cases := [ ] struct {
aggregateOperator v3 . AggregateOperator
expectedQuery string
} {
{
aggregateOperator : v3 . AggregateOperatorAvgRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorAvgRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMaxRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMaxRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMinRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMinRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorSumRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorSumRate ] ) ,
} ,
}
for _ , c := range cases {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-08-18 07:32:05 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
AggregateOperator : c . aggregateOperator ,
Expression : "A" ,
} ,
} ,
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeGraph ,
} ,
}
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : true } )
2023-07-03 13:30:37 +05:30
require . NoError ( t , err )
require . Equal ( t , query , c . expectedQuery )
}
} )
}
2023-08-22 17:09:58 +05:30
func TestBuildQueryAdjustedTimes ( t * testing . T ) {
cases := [ ] struct {
name string
params * v3 . QueryRangeParamsV3
expected string
} {
{
name : "TestBuildQueryAdjustedTimes start close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:29
Start : 1686082289000 ,
// 20:41:00
End : 1686084060000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 60 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:10:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082200000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 50 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:52
Start : 1686082312000 ,
// 20:41:00
End : 1686084060000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 60 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:10:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082200000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:00
End : 1686084060000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 30 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:11:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082260000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 30 seconds and end close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:29
End : 1686084089000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 30 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:11:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082260000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 300 seconds and end close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:29
End : 1686084089000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 300 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:05:00 - 20:41:00
// 20:10:00 is the nearest 5 minute interval, but we round down to 20:05:00
// as this is a rate query and we want to include the previous value for the first interval
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686081900000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 180 seconds and end close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:29
End : 1686084089000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 180 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:06:00 - 20:39:00
// 20:09:00 is the nearest 3 minute interval, but we round down to 20:06:00
// as this is a rate query and we want to include the previous value for the first interval
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686081960000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
}
for _ , testCase := range cases {
t . Run ( testCase . name , func ( t * testing . T ) {
q := testCase . params
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
require . NoError ( t , err )
require . Contains ( t , query , testCase . expected )
} )
}
}