2024-06-17 12:13:18 +02:00
|
|
|
package insertutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
|
|
)
|
|
|
|
|
2024-06-18 00:23:17 +02:00
|
|
|
// ExtractTimestampRFC3339NanoFromFields extracts RFC3339 timestamp in nanoseconds from the field with the name timeField at fields.
|
2024-06-17 12:13:18 +02:00
|
|
|
//
|
|
|
|
// The value for the timeField is set to empty string after returning from the function,
|
|
|
|
// so it could be ignored during data ingestion.
|
|
|
|
//
|
|
|
|
// The current timestamp is returned if fields do not contain a field with timeField name or if the timeField value is empty.
|
2024-06-18 00:23:17 +02:00
|
|
|
func ExtractTimestampRFC3339NanoFromFields(timeField string, fields []logstorage.Field) (int64, error) {
|
2024-06-17 12:13:18 +02:00
|
|
|
for i := range fields {
|
|
|
|
f := &fields[i]
|
|
|
|
if f.Name != timeField {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-25 14:08:01 +02:00
|
|
|
if f.Value == "" || f.Value == "0" {
|
|
|
|
return time.Now().UnixNano(), nil
|
|
|
|
}
|
2024-06-18 00:23:17 +02:00
|
|
|
nsecs, ok := logstorage.TryParseTimestampRFC3339Nano(f.Value)
|
2024-06-17 12:13:18 +02:00
|
|
|
if !ok {
|
2024-06-25 14:08:01 +02:00
|
|
|
return 0, fmt.Errorf("cannot unmarshal rfc3339 timestamp from %s=%q", timeField, f.Value)
|
2024-06-17 12:13:18 +02:00
|
|
|
}
|
|
|
|
f.Value = ""
|
|
|
|
return nsecs, nil
|
|
|
|
}
|
|
|
|
return time.Now().UnixNano(), nil
|
|
|
|
}
|