@@ -987,8 +987,6 @@ func TestOTLPLogAttributesAsIndexLabels(t *testing.T) {
987
987
func TestOTLPStructuredMetadataCalculation (t * testing.T ) {
988
988
now := time .Unix (0 , time .Now ().UnixNano ())
989
989
990
- // Create test with resource, scope, and entry metadata that would previously cause negative calculations
991
- // when subtracting resource/scope attribute sizes from the total structured metadata size
992
990
generateLogs := func () plog.Logs {
993
991
ld := plog .NewLogs ()
994
992
@@ -1069,6 +1067,88 @@ func TestOTLPStructuredMetadataCalculation(t *testing.T) {
1069
1067
require .True (t , entryMetadataFound , "Entry metadata should be present in the entry" )
1070
1068
}
1071
1069
1070
+ func TestNegativeMetadataScenarioExplicit (t * testing.T ) {
1071
+ // This test explicitly demonstrates how negative structured metadata size values
1072
+ // could occur when subtracting resource/scope attributes from total structured metadata size
1073
+
1074
+ // Setup: Create metadata with a label that would be excluded from size calculation
1075
+ resourceMeta := push.LabelsAdapter {
1076
+ {Name : "resource_key" , Value : "resource_value" }, // 27 bytes
1077
+ {Name : "excluded_label" , Value : "value" }, // This would be excluded from size calculation
1078
+ }
1079
+
1080
+ scopeMeta := push.LabelsAdapter {
1081
+ {Name : "scope_key" , Value : "scope_value" }, // 21 bytes
1082
+ }
1083
+
1084
+ entryMeta := push.LabelsAdapter {
1085
+ {Name : "entry_key" , Value : "entry_value" }, // 21 bytes
1086
+ }
1087
+
1088
+ // ExcludedStructuredMetadataLabels would exclude certain labels
1089
+ // from size calculations.
1090
+ calculateSize := func (labels push.LabelsAdapter ) int {
1091
+ size := 0
1092
+ for _ , label := range labels {
1093
+ // Simulate a label being excluded from size calc
1094
+ if label .Name != "excluded_label" {
1095
+ size += len (label .Name ) + len (label .Value )
1096
+ }
1097
+ }
1098
+ return size
1099
+ }
1100
+
1101
+ // Calculate sizes with simulated exclusions
1102
+ resourceSize := calculateSize (resourceMeta ) // 27 bytes (excluded_label not counted)
1103
+ scopeSize := calculateSize (scopeMeta ) // 21 bytes
1104
+ entrySize := calculateSize (entryMeta ) // 21 bytes
1105
+
1106
+ // The original approach:
1107
+ // 1. Add resource and scope attributes to entry metadata
1108
+ combined := make (push.LabelsAdapter , 0 )
1109
+ combined = append (combined , entryMeta ... )
1110
+ combined = append (combined , resourceMeta ... )
1111
+ combined = append (combined , scopeMeta ... )
1112
+
1113
+ // 2. Calculate combined size (with certain labels excluded)
1114
+ combinedSize := calculateSize (combined ) // Should be 27 + 21 + 21 = 69 bytes
1115
+
1116
+ // 3. Calculate entry-specific metadata by subtraction
1117
+ // metadataSize := int64(combinedSize - resourceSize - scopeSize)
1118
+ oldCalculation := combinedSize - resourceSize - scopeSize
1119
+
1120
+ // Should be: 69 - 27 - 21 = 21 bytes, which equals entrySize
1121
+
1122
+ t .Logf ("Resource size: %d bytes" , resourceSize )
1123
+ t .Logf ("Scope size: %d bytes" , scopeSize )
1124
+ t .Logf ("Entry size: %d bytes" , entrySize )
1125
+ t .Logf ("Combined size: %d bytes" , combinedSize )
1126
+ t .Logf ("Old calculation (combined - resource - scope): %d bytes" , oldCalculation )
1127
+
1128
+ // Now, to demonstrate how this could produce negative values:
1129
+ // In reality, due to potential inconsistencies in how labels were excluded/combined/normalized,
1130
+ // the combined size could be LESS than the sum of parts
1131
+ simulatedRealCombinedSize := resourceSize + scopeSize - 5 // 5 bytes less than sum
1132
+
1133
+ // Using the original calculation method:
1134
+ simulatedRealCalculation := simulatedRealCombinedSize - resourceSize - scopeSize
1135
+ // This will be: (27 + 21 - 5) - 27 - 21 = 43 - 48 = -5 bytes
1136
+
1137
+ t .Logf ("Simulated real combined size: %d bytes" , simulatedRealCombinedSize )
1138
+ t .Logf ("Simulated real calculation (old method): %d bytes" , simulatedRealCalculation )
1139
+
1140
+ // This would be a negative value!
1141
+ require .Less (t , simulatedRealCalculation , 0 ,
1142
+ "This demonstrates how the old calculation could produce negative values" )
1143
+
1144
+ // Directly use entry's size before combining
1145
+ t .Logf ("New calculation (direct entry size): %d bytes" , entrySize )
1146
+ require .Equal (t , entrySize , 20 ,
1147
+ "New calculation provides correct entry size" )
1148
+ require .Greater (t , entrySize , 0 ,
1149
+ "New calculation always produces non-negative values" )
1150
+ }
1151
+
1072
1152
func TestOTLPSeverityTextAsLabel (t * testing.T ) {
1073
1153
now := time .Unix (0 , time .Now ().UnixNano ())
1074
1154
0 commit comments