2021-04-08 19:58:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/opentsdb"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/vm"
|
2022-09-21 08:08:33 +00:00
|
|
|
"github.com/cheggaaa/pb/v3"
|
2021-04-08 19:58:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type otsdbProcessor struct {
|
2023-06-02 12:57:08 +00:00
|
|
|
oc *opentsdb.Client
|
|
|
|
im *vm.Importer
|
|
|
|
otsdbcc int
|
|
|
|
isVerbose bool
|
2021-04-08 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type queryObj struct {
|
|
|
|
Series opentsdb.Meta
|
|
|
|
Rt opentsdb.RetentionMeta
|
|
|
|
Tr opentsdb.TimeRange
|
|
|
|
StartTime int64
|
|
|
|
}
|
|
|
|
|
2024-06-10 10:20:52 +00:00
|
|
|
func newOtsdbProcessor(oc *opentsdb.Client, im *vm.Importer, otsdbcc int, verbose bool) *otsdbProcessor {
|
2021-04-08 19:58:06 +00:00
|
|
|
if otsdbcc < 1 {
|
|
|
|
otsdbcc = 1
|
|
|
|
}
|
|
|
|
return &otsdbProcessor{
|
2023-06-02 12:57:08 +00:00
|
|
|
oc: oc,
|
|
|
|
im: im,
|
|
|
|
otsdbcc: otsdbcc,
|
|
|
|
isVerbose: verbose,
|
2021-04-08 19:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 12:57:08 +00:00
|
|
|
func (op *otsdbProcessor) run() error {
|
2021-04-08 19:58:06 +00:00
|
|
|
log.Println("Loading all metrics from OpenTSDB for filters: ", op.oc.Filters)
|
|
|
|
var metrics []string
|
|
|
|
for _, filter := range op.oc.Filters {
|
|
|
|
q := fmt.Sprintf("%s/api/suggest?type=metrics&q=%s&max=%d", op.oc.Addr, filter, op.oc.Limit)
|
|
|
|
m, err := op.oc.FindMetrics(q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("metric discovery failed for %q: %s", q, err)
|
|
|
|
}
|
|
|
|
metrics = append(metrics, m...)
|
|
|
|
}
|
|
|
|
if len(metrics) < 1 {
|
|
|
|
return fmt.Errorf("found no timeseries to import with filters %q", op.oc.Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
question := fmt.Sprintf("Found %d metrics to import. Continue?", len(metrics))
|
2024-06-10 10:20:52 +00:00
|
|
|
if !prompt(question) {
|
2021-04-08 19:58:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
op.im.ResetStats()
|
2021-04-10 12:24:18 +00:00
|
|
|
var startTime int64
|
|
|
|
if op.oc.HardTS != 0 {
|
|
|
|
startTime = op.oc.HardTS
|
|
|
|
} else {
|
|
|
|
startTime = time.Now().Unix()
|
|
|
|
}
|
2021-04-08 19:58:06 +00:00
|
|
|
queryRanges := 0
|
|
|
|
// pre-calculate the number of query ranges we'll be processing
|
|
|
|
for _, rt := range op.oc.Retentions {
|
|
|
|
queryRanges += len(rt.QueryRanges)
|
|
|
|
}
|
|
|
|
for _, metric := range metrics {
|
2022-05-13 07:55:27 +00:00
|
|
|
log.Printf("Starting work on %s", metric)
|
2021-04-08 19:58:06 +00:00
|
|
|
serieslist, err := op.oc.FindSeries(metric)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't retrieve series list for %s : %s", metric, err)
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Create channels for collecting/processing series and errors
|
|
|
|
We'll create them per metric to reduce pressure against OpenTSDB
|
|
|
|
|
|
|
|
Limit the size of seriesCh so we can't get too far ahead of actual processing
|
|
|
|
*/
|
|
|
|
seriesCh := make(chan queryObj, op.otsdbcc)
|
|
|
|
errCh := make(chan error)
|
|
|
|
// we're going to make serieslist * queryRanges queries, so we should represent that in the progress bar
|
|
|
|
bar := pb.StartNew(len(serieslist) * queryRanges)
|
2022-08-26 16:20:27 +00:00
|
|
|
defer func(bar *pb.ProgressBar) {
|
|
|
|
bar.Finish()
|
|
|
|
}(bar)
|
2021-04-08 19:58:06 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(op.otsdbcc)
|
|
|
|
for i := 0; i < op.otsdbcc; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for s := range seriesCh {
|
|
|
|
if err := op.do(s); err != nil {
|
|
|
|
errCh <- fmt.Errorf("couldn't retrieve series for %s : %s", metric, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bar.Increment()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Loop through all series for this metric, processing all retentions and time ranges
|
|
|
|
requested. This loop is our primary "collect data from OpenTSDB loop" and should
|
|
|
|
be async, sending data to VictoriaMetrics over time.
|
|
|
|
|
|
|
|
The idea with having the select at the inner-most loop is to ensure quick
|
|
|
|
short-circuiting on error.
|
|
|
|
*/
|
|
|
|
for _, series := range serieslist {
|
|
|
|
for _, rt := range op.oc.Retentions {
|
|
|
|
for _, tr := range rt.QueryRanges {
|
|
|
|
select {
|
|
|
|
case otsdbErr := <-errCh:
|
|
|
|
return fmt.Errorf("opentsdb error: %s", otsdbErr)
|
|
|
|
case vmErr := <-op.im.Errors():
|
2023-06-02 12:57:08 +00:00
|
|
|
return fmt.Errorf("import process failed: %s", wrapErr(vmErr, op.isVerbose))
|
2021-04-08 19:58:06 +00:00
|
|
|
case seriesCh <- queryObj{
|
|
|
|
Tr: tr, StartTime: startTime,
|
|
|
|
Series: series, Rt: opentsdb.RetentionMeta{
|
|
|
|
FirstOrder: rt.FirstOrder, SecondOrder: rt.SecondOrder, AggTime: rt.AggTime}}:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 05:03:41 +00:00
|
|
|
|
2021-04-08 19:58:06 +00:00
|
|
|
// Drain channels per metric
|
|
|
|
close(seriesCh)
|
|
|
|
wg.Wait()
|
|
|
|
close(errCh)
|
|
|
|
// check for any lingering errors on the query side
|
|
|
|
for otsdbErr := range errCh {
|
|
|
|
return fmt.Errorf("Import process failed: \n%s", otsdbErr)
|
|
|
|
}
|
|
|
|
bar.Finish()
|
|
|
|
log.Print(op.im.Stats())
|
|
|
|
}
|
|
|
|
op.im.Close()
|
|
|
|
for vmErr := range op.im.Errors() {
|
2022-01-03 19:12:01 +00:00
|
|
|
if vmErr.Err != nil {
|
2023-06-02 12:57:08 +00:00
|
|
|
return fmt.Errorf("import process failed: %s", wrapErr(vmErr, op.isVerbose))
|
2022-01-03 19:12:01 +00:00
|
|
|
}
|
2021-04-08 19:58:06 +00:00
|
|
|
}
|
|
|
|
log.Println("Import finished!")
|
|
|
|
log.Print(op.im.Stats())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *otsdbProcessor) do(s queryObj) error {
|
|
|
|
start := s.StartTime - s.Tr.Start
|
|
|
|
end := s.StartTime - s.Tr.End
|
2022-01-04 06:51:23 +00:00
|
|
|
data, err := op.oc.GetData(s.Series, s.Rt, start, end, op.oc.MsecsTime)
|
2021-04-08 19:58:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to collect data for %v in %v:%v :: %v", s.Series, s.Rt, s.Tr, err)
|
|
|
|
}
|
|
|
|
if len(data.Timestamps) < 1 || len(data.Values) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-16 08:35:17 +00:00
|
|
|
labels := make([]vm.LabelPair, 0, len(data.Tags))
|
2021-04-08 19:58:06 +00:00
|
|
|
for k, v := range data.Tags {
|
|
|
|
labels = append(labels, vm.LabelPair{Name: k, Value: v})
|
|
|
|
}
|
2022-05-03 05:03:41 +00:00
|
|
|
ts := vm.TimeSeries{
|
2021-04-08 19:58:06 +00:00
|
|
|
Name: data.Metric,
|
|
|
|
LabelPairs: labels,
|
|
|
|
Timestamps: data.Timestamps,
|
|
|
|
Values: data.Values,
|
|
|
|
}
|
2023-09-01 07:34:16 +00:00
|
|
|
return op.im.Input(&ts)
|
2021-04-08 19:58:06 +00:00
|
|
|
}
|