55 "strconv"
66 "time"
77
8+ "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
9+
810 "github.com/VictoriaMetrics/VictoriaLogs/lib/logstorage"
911 "github.com/VictoriaMetrics/easyproto"
1012)
@@ -57,12 +59,12 @@ func decodeResourceLogs(src []byte, pushLogs pushLogsHandler) (err error) {
5759 defer logstorage .PutFields (fs )
5860
5961 // Decode resource
60- data , ok , err := easyproto .GetMessageData (src , 1 )
62+ resourceData , ok , err := easyproto .GetMessageData (src , 1 )
6163 if err != nil {
6264 return fmt .Errorf ("cannot find Resource: %w" , err )
6365 }
6466 if ok {
65- if err = decodeResource (data , fs , fb ); err != nil {
67+ if err = decodeResource (resourceData , fs , fb ); err != nil {
6668 return fmt .Errorf ("cannot decode Resource: %w" , err )
6769 }
6870 }
@@ -120,6 +122,7 @@ func decodeResource(src []byte, fs *logstorage.Fields, fb *fmtBuffer) (err error
120122
121123func decodeScopeLogs (src []byte , fs * logstorage.Fields , pushLogs pushLogsHandler ) (err error ) {
122124 // message ScopeLogs {
125+ // InstrumentationScope scope = 1;
123126 // repeated LogRecord log_records = 2;
124127 // }
125128
@@ -128,6 +131,18 @@ func decodeScopeLogs(src []byte, fs *logstorage.Fields, pushLogs pushLogsHandler
128131
129132 streamFieldsLen := len (fs .Fields )
130133
134+ scopeData , ok , err := easyproto .GetMessageData (src , 1 )
135+ if err != nil {
136+ return fmt .Errorf ("cannot read InstrumentationScope: %w" , err )
137+ }
138+ if ok {
139+ if err := decodeInstrumentationScope (scopeData , fs , fb ); err != nil {
140+ return fmt .Errorf ("cannot decode InstrumentationScope: %w" , err )
141+ }
142+ }
143+
144+ commonFieldsLen := len (fs .Fields )
145+
131146 var fc easyproto.FieldContext
132147 for len (src ) > 0 {
133148 src , err = fc .NextField (src )
@@ -142,7 +157,7 @@ func decodeScopeLogs(src []byte, fs *logstorage.Fields, pushLogs pushLogsHandler
142157 }
143158
144159 fb .reset ()
145- fs .Fields = fs .Fields [:streamFieldsLen ]
160+ fs .Fields = fs .Fields [:commonFieldsLen ]
146161
147162 eventName , timestamp , err := decodeLogRecord (data , fs , fb )
148163 if err != nil {
@@ -159,6 +174,9 @@ func decodeScopeLogs(src []byte, fs *logstorage.Fields, pushLogs pushLogsHandler
159174 f .Value = eventName
160175
161176 pushLogs (timestamp , fs .Fields , streamFieldsLen + 1 )
177+
178+ // Return back common fields to their places before the next iteration
179+ fs .Fields = append (fs .Fields [:streamFieldsLen ], fs .Fields [streamFieldsLen + 1 :commonFieldsLen + 1 ]... )
162180 } else {
163181 pushLogs (timestamp , fs .Fields , streamFieldsLen )
164182 }
@@ -167,6 +185,56 @@ func decodeScopeLogs(src []byte, fs *logstorage.Fields, pushLogs pushLogsHandler
167185 return nil
168186}
169187
188+ func decodeInstrumentationScope (src []byte , fs * logstorage.Fields , fb * fmtBuffer ) error {
189+ // See https://github.com/open-telemetry/opentelemetry-proto/blob/a5f0eac5b802f7ae51dfe41e5116fe5548955e64/opentelemetry/proto/common/v1/common.proto#L76
190+ //
191+ // message InstrumentationScope {
192+ // string name = 1;
193+ // string version = 2;
194+ // repeated KeyValue attributes = 3;
195+ // }
196+
197+ nameData , ok , err := easyproto .GetMessageData (src , 1 )
198+ if err != nil {
199+ return fmt .Errorf ("cannot read name: %w" , err )
200+ }
201+ name := "unknown"
202+ if ok {
203+ name = bytesutil .ToUnsafeString (nameData )
204+ }
205+ fs .Add ("scope.name" , name )
206+
207+ versionData , ok , err := easyproto .GetMessageData (src , 2 )
208+ if err != nil {
209+ return fmt .Errorf ("cannot read version: %w" , err )
210+ }
211+ version := "unknown"
212+ if ok {
213+ version = bytesutil .ToUnsafeString (versionData )
214+ }
215+ fs .Add ("scope.version" , version )
216+
217+ var fc easyproto.FieldContext
218+ for len (src ) > 0 {
219+ src , err = fc .NextField (src )
220+ if err != nil {
221+ return fmt .Errorf ("cannot read the next field: %w" , err )
222+ }
223+ switch fc .FieldNum {
224+ case 3 :
225+ attributesData , ok := fc .MessageData ()
226+ if ! ok {
227+ return fmt .Errorf ("cannot read Attributes data" )
228+ }
229+ if err := decodeKeyValue (attributesData , fs , fb , "scope.attributes" ); err != nil {
230+ return fmt .Errorf ("cannot decode Attributes: %w" , err )
231+ }
232+ }
233+ }
234+
235+ return nil
236+ }
237+
170238func decodeLogRecord (src []byte , fs * logstorage.Fields , fb * fmtBuffer ) (string , int64 , error ) {
171239 // See https://github.com/open-telemetry/opentelemetry-proto/blob/a5f0eac5b802f7ae51dfe41e5116fe5548955e64/opentelemetry/proto/logs/v1/logs.proto#L136
172240 //
@@ -228,11 +296,11 @@ func decodeLogRecord(src []byte, fs *logstorage.Fields, fb *fmtBuffer) (string,
228296 return "" , 0 , fmt .Errorf ("cannot decode Body: %w" , err )
229297 }
230298 case 6 :
231- data , ok := fc .MessageData ()
299+ attributesData , ok := fc .MessageData ()
232300 if ! ok {
233301 return "" , 0 , fmt .Errorf ("cannot read Attributes data" )
234302 }
235- if err := decodeKeyValue (data , fs , fb , "" ); err != nil {
303+ if err := decodeKeyValue (attributesData , fs , fb , "" ); err != nil {
236304 return "" , 0 , fmt .Errorf ("cannot decode Attributes: %w" , err )
237305 }
238306 case 9 :
@@ -282,17 +350,17 @@ func decodeKeyValue(src []byte, fs *logstorage.Fields, fb *fmtBuffer, fieldNameP
282350 // }
283351
284352 // Decode key
285- data , ok , err := easyproto .GetMessageData (src , 1 )
353+ keyData , ok , err := easyproto .GetMessageData (src , 1 )
286354 if err != nil {
287355 return fmt .Errorf ("cannot find Key in KeyValue: %w" , err )
288356 }
289357 if ! ok {
290358 return fmt .Errorf ("key is missing in KeyValue" )
291359 }
292- fieldName := fb .formatSubFieldName (fieldNamePrefix , data )
360+ fieldName := fb .formatSubFieldName (fieldNamePrefix , keyData )
293361
294362 // Decode value
295- data , ok , err = easyproto .GetMessageData (src , 2 )
363+ valueData , ok , err : = easyproto .GetMessageData (src , 2 )
296364 if err != nil {
297365 return fmt .Errorf ("cannot find Value in KeyValue: %w" , err )
298366 }
@@ -301,7 +369,7 @@ func decodeKeyValue(src []byte, fs *logstorage.Fields, fb *fmtBuffer, fieldNameP
301369 return nil
302370 }
303371
304- if err := decodeAnyValue (data , fs , fb , fieldName ); err != nil {
372+ if err := decodeAnyValue (valueData , fs , fb , fieldName ); err != nil {
305373 return fmt .Errorf ("cannot decode AnyValue: %w" , err )
306374 }
307375
0 commit comments