2019-11-21 07:10:25 -05:00
// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tsdb
import (
"context"
"fmt"
"os"
"path/filepath"
2020-01-30 07:12:43 +00:00
"github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/pkg/labels"
2019-11-21 07:10:25 -05:00
)
var InvalidTimesError = fmt . Errorf ( "max time is lesser than min time" )
type MetricSample struct {
TimestampMs int64
Value float64
Labels labels . Labels
}
// CreateHead creates a TSDB writer head to write the sample data to.
func CreateHead ( samples [ ] * MetricSample , chunkRange int64 , logger log . Logger ) ( * Head , error ) {
2020-01-30 07:12:43 +00:00
head , err := NewHead ( nil , logger , nil , chunkRange , DefaultStripeSize )
2019-11-21 07:10:25 -05:00
if err != nil {
return nil , err
}
app := head . Appender ( )
for _ , sample := range samples {
_ , err = app . Add ( sample . Labels , sample . TimestampMs , sample . Value )
if err != nil {
return nil , err
}
}
err = app . Commit ( )
if err != nil {
return nil , err
}
return head , nil
}
// CreateBlock creates a chunkrange block from the samples passed to it, and writes it to disk.
func CreateBlock ( samples [ ] * MetricSample , dir string , mint , maxt int64 , logger log . Logger ) ( string , error ) {
chunkRange := maxt - mint
if chunkRange == 0 {
chunkRange = DefaultBlockDuration
}
if chunkRange < 0 {
return "" , InvalidTimesError
}
head , err := CreateHead ( samples , chunkRange , logger )
if err != nil {
return "" , err
}
2020-02-06 15:58:38 +00:00
compactor , err := NewLeveledCompactor ( context . Background ( ) , nil , logger , ExponentialBlockRanges ( DefaultBlockDuration , 3 , 5 ) , nil )
2019-11-21 07:10:25 -05:00
if err != nil {
return "" , err
}
err = os . MkdirAll ( dir , 0777 )
if err != nil {
return "" , err
}
ulid , err := compactor . Write ( dir , head , mint , maxt , nil )
if err != nil {
return "" , err
}
return filepath . Join ( dir , ulid . String ( ) ) , nil
}