2023-10-05 12:39:51 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-10-15 22:25:23 +00:00
|
|
|
"sync"
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2023-10-05 12:39:51 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2023-10-15 22:25:23 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2023-10-05 12:39:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/newrelic"
|
2025-03-18 15:24:48 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/protoparserutil"
|
2023-10-05 12:39:51 +00:00
|
|
|
)
|
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
var (
|
|
|
|
maxInsertRequestSize = flagutil.NewBytes("newrelic.maxInsertRequestSize", 64*1024*1024, "The maximum size in bytes of a single NewRelic request "+
|
|
|
|
"to /newrelic/infra/v2/metrics/events/bulk")
|
|
|
|
)
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
// Parse parses NewRelic POST request for /newrelic/infra/v2/metrics/events/bulk from r and calls callback for the parsed request.
|
2023-10-05 12:39:51 +00:00
|
|
|
//
|
2023-10-15 22:25:23 +00:00
|
|
|
// callback shouldn't hold rows after returning.
|
2025-03-14 17:10:37 +00:00
|
|
|
func Parse(r io.Reader, encoding string, callback func(rows []newrelic.Row) error) error {
|
|
|
|
readCalls.Inc()
|
2025-03-18 15:24:48 +00:00
|
|
|
err := protoparserutil.ReadUncompressedData(r, encoding, maxInsertRequestSize, func(data []byte) error {
|
2025-03-14 17:10:37 +00:00
|
|
|
return parseData(data, callback)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
readErrors.Inc()
|
|
|
|
return fmt.Errorf("cannot decode NewRelic data: %w", err)
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
2025-03-14 17:10:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2025-03-14 17:10:37 +00:00
|
|
|
func parseData(data []byte, callback func(rows []newrelic.Row) error) error {
|
2023-10-15 22:25:23 +00:00
|
|
|
rows := getRows()
|
|
|
|
defer putRows(rows)
|
2023-10-05 12:39:51 +00:00
|
|
|
|
2025-03-14 17:10:37 +00:00
|
|
|
if err := rows.Unmarshal(data); err != nil {
|
2023-10-05 12:39:51 +00:00
|
|
|
unmarshalErrors.Inc()
|
2023-10-15 22:25:23 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal NewRelic request: %w", err)
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in missing timestamps
|
|
|
|
currentTimestamp := int64(fasttime.UnixTimestamp())
|
2023-10-15 22:25:23 +00:00
|
|
|
for i := range rows.Rows {
|
|
|
|
r := &rows.Rows[i]
|
|
|
|
if r.Timestamp == 0 {
|
|
|
|
r.Timestamp = currentTimestamp * 1e3
|
2023-10-05 12:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 22:25:23 +00:00
|
|
|
if err := callback(rows.Rows); err != nil {
|
2023-10-05 12:39:51 +00:00
|
|
|
return fmt.Errorf("error when processing imported data: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-15 22:25:23 +00:00
|
|
|
|
|
|
|
func getRows() *newrelic.Rows {
|
|
|
|
v := rowsPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &newrelic.Rows{}
|
|
|
|
}
|
|
|
|
return v.(*newrelic.Rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putRows(rows *newrelic.Rows) {
|
|
|
|
rows.Reset()
|
|
|
|
rowsPool.Put(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rowsPool sync.Pool
|
|
|
|
|
|
|
|
var (
|
|
|
|
readCalls = metrics.NewCounter(`vm_protoparser_read_calls_total{type="newrelic"}`)
|
|
|
|
readErrors = metrics.NewCounter(`vm_protoparser_read_errors_total{type="newrelic"}`)
|
|
|
|
unmarshalErrors = metrics.NewCounter(`vm_protoparser_unmarshal_errors_total{type="newrelic"}`)
|
|
|
|
)
|