2025-05-16 20:09:57 +05:30
package telemetrylogs
import (
"context"
"testing"
schema "github.com/SigNoz/signoz-otel-collector/cmd/signozschemamigrator/schema_migrator"
qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetColumn ( t * testing . T ) {
ctx := context . Background ( )
testCases := [ ] struct {
name string
key telemetrytypes . TelemetryFieldKey
expectedCol * schema . Column
expectedError error
} {
{
name : "Resource field" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "service.name" ,
FieldContext : telemetrytypes . FieldContextResource ,
} ,
2025-09-14 18:18:39 +05:30
expectedCol : logsV2Columns [ "resource" ] ,
2025-05-16 20:09:57 +05:30
expectedError : nil ,
} ,
{
name : "Scope field - scope name" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "name" ,
FieldContext : telemetrytypes . FieldContextScope ,
} ,
expectedCol : logsV2Columns [ "scope_name" ] ,
expectedError : nil ,
} ,
{
name : "Scope field - scope.name" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "scope.name" ,
FieldContext : telemetrytypes . FieldContextScope ,
} ,
expectedCol : logsV2Columns [ "scope_name" ] ,
expectedError : nil ,
} ,
{
name : "Scope field - scope_name" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "scope_name" ,
FieldContext : telemetrytypes . FieldContextScope ,
} ,
expectedCol : logsV2Columns [ "scope_name" ] ,
expectedError : nil ,
} ,
{
name : "Scope field - version" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "version" ,
FieldContext : telemetrytypes . FieldContextScope ,
} ,
expectedCol : logsV2Columns [ "scope_version" ] ,
expectedError : nil ,
} ,
{
name : "Scope field - other scope field" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "custom.scope.field" ,
FieldContext : telemetrytypes . FieldContextScope ,
} ,
expectedCol : logsV2Columns [ "scope_string" ] ,
expectedError : nil ,
} ,
{
name : "Attribute field - string type" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "user.id" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeString ,
} ,
expectedCol : logsV2Columns [ "attributes_string" ] ,
expectedError : nil ,
} ,
{
name : "Attribute field - number type" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.size" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeNumber ,
} ,
expectedCol : logsV2Columns [ "attributes_number" ] ,
expectedError : nil ,
} ,
{
name : "Attribute field - int64 type" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.duration" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeInt64 ,
} ,
expectedCol : logsV2Columns [ "attributes_number" ] ,
expectedError : nil ,
} ,
{
name : "Attribute field - float64 type" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "cpu.utilization" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeFloat64 ,
} ,
expectedCol : logsV2Columns [ "attributes_number" ] ,
expectedError : nil ,
} ,
{
name : "Attribute field - bool type" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.success" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeBool ,
} ,
expectedCol : logsV2Columns [ "attributes_bool" ] ,
expectedError : nil ,
} ,
{
name : "Log field - timestamp" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "timestamp" ,
FieldContext : telemetrytypes . FieldContextLog ,
} ,
expectedCol : logsV2Columns [ "timestamp" ] ,
expectedError : nil ,
} ,
{
name : "Log field - body" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "body" ,
FieldContext : telemetrytypes . FieldContextLog ,
} ,
expectedCol : logsV2Columns [ "body" ] ,
expectedError : nil ,
} ,
{
name : "Log field - nonexistent" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "nonexistent_field" ,
FieldContext : telemetrytypes . FieldContextLog ,
} ,
expectedCol : nil ,
expectedError : qbtypes . ErrColumnNotFound ,
} ,
{
name : "did_user_login" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "did_user_login" ,
Signal : telemetrytypes . SignalLogs ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeBool ,
} ,
expectedCol : logsV2Columns [ "attributes_bool" ] ,
expectedError : nil ,
} ,
}
fm := NewFieldMapper ( )
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
col , err := fm . ColumnFor ( ctx , & tc . key )
if tc . expectedError != nil {
assert . Equal ( t , tc . expectedError , err )
} else {
require . NoError ( t , err )
assert . Equal ( t , tc . expectedCol , col )
}
} )
}
}
func TestGetFieldKeyName ( t * testing . T ) {
ctx := context . Background ( )
testCases := [ ] struct {
name string
key telemetrytypes . TelemetryFieldKey
expectedResult string
expectedError error
} {
{
name : "Simple column type - timestamp" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "timestamp" ,
FieldContext : telemetrytypes . FieldContextLog ,
} ,
expectedResult : "timestamp" ,
expectedError : nil ,
} ,
{
name : "Map column type - string attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "user.id" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeString ,
} ,
expectedResult : "attributes_string['user.id']" ,
expectedError : nil ,
} ,
{
name : "Map column type - number attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.size" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeNumber ,
} ,
expectedResult : "attributes_number['request.size']" ,
expectedError : nil ,
} ,
{
name : "Map column type - bool attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.success" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeBool ,
} ,
expectedResult : "attributes_bool['request.success']" ,
expectedError : nil ,
} ,
{
name : "Map column type - resource attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "service.name" ,
FieldContext : telemetrytypes . FieldContextResource ,
} ,
2025-09-14 18:18:39 +05:30
expectedResult : "multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL)" ,
expectedError : nil ,
} ,
{
name : "Map column type - resource attribute - Materialized" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "service.name" ,
FieldContext : telemetrytypes . FieldContextResource ,
FieldDataType : telemetrytypes . FieldDataTypeString ,
Materialized : true ,
} ,
expectedResult : "multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, `resource_string_service$$name_exists`==true, `resource_string_service$$name`, NULL)" ,
2025-05-16 20:09:57 +05:30
expectedError : nil ,
} ,
{
name : "Non-existent column" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "nonexistent_field" ,
FieldContext : telemetrytypes . FieldContextLog ,
} ,
expectedResult : "" ,
expectedError : qbtypes . ErrColumnNotFound ,
} ,
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
2025-09-14 18:18:39 +05:30
fm := NewFieldMapper ( )
2025-05-16 20:09:57 +05:30
result , err := fm . FieldFor ( ctx , & tc . key )
if tc . expectedError != nil {
assert . Equal ( t , tc . expectedError , err )
} else {
require . NoError ( t , err )
assert . Equal ( t , tc . expectedResult , result )
}
} )
}
}