Skip to content

Commit

Permalink
unwrap: add relabel for meta.json
Browse files Browse the repository at this point in the history
  • Loading branch information
sepich committed May 18, 2024
1 parent 287eb21 commit 691177e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func main() {

unwrapCmd := app.Command("unwrap", "Split TSDB block to multiple blocks by Label")
unwrapRelabel := extkingpin.RegisterPathOrContent(unwrapCmd, "relabel-config", fmt.Sprintf("YAML file that contains relabeling configuration. Set %s=name1;name2;... to split separate blocks for each uniq label combination.", metaExtLabels), extkingpin.WithEnvSubstitution(), extkingpin.WithRequired())
unwrapMetaRelabel := extkingpin.RegisterPathOrContent(unwrapCmd, "meta-relabel", "YAML file that contains relabeling configuration for block labels (meta.json)", extkingpin.WithEnvSubstitution())
unwrapRecursive := unwrapCmd.Flag("recursive", "Recursive search for blocks in the bucket (Mimir has blocks nested to tenants folders)").Short('r').Default("false").Bool()
unwrapDir := unwrapCmd.Flag("data-dir", "Data directory in which to cache blocks and process tsdb.").Default("./data").String()
unwrapWait := unwrapCmd.Flag("wait-interval", "Wait interval between consecutive runs and bucket refreshes. Run once if 0.").Default("5m").Short('w').Duration()
Expand Down Expand Up @@ -100,7 +101,7 @@ func main() {
case importCmd.FullCommand():
exitCode(importMetrics(bkt, importFromFile, importBlockSize, importDir, importLabels, *importUpload, logger))
case unwrapCmd.FullCommand():
exitCode(unwrap(bkt, *unwrapRelabel, *unwrapRecursive, unwrapDir, unwrapWait, *unwrapDry, unwrapDst, unwrapMaxTime, unwrapSrc, logger))
exitCode(unwrap(bkt, *unwrapRelabel, *unwrapMetaRelabel, *unwrapRecursive, unwrapDir, unwrapWait, *unwrapDry, unwrapDst, unwrapMaxTime, unwrapSrc, logger))
}
}

Expand Down
19 changes: 16 additions & 3 deletions unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

const metaExtLabels = "__meta_ext_labels"

func unwrap(bkt objstore.Bucket, unwrapRelabel extkingpin.PathOrContent, recursive bool, dir *string, wait *time.Duration, unwrapDry bool, outConfig *extkingpin.PathOrContent, maxTime *model.TimeOrDurationValue, unwrapSrc *string, logger log.Logger) (err error) {
func unwrap(bkt objstore.Bucket, unwrapRelabel extkingpin.PathOrContent, unwrapMetaRelabel extkingpin.PathOrContent, recursive bool, dir *string, wait *time.Duration, unwrapDry bool, outConfig *extkingpin.PathOrContent, maxTime *model.TimeOrDurationValue, unwrapSrc *string, logger log.Logger) (err error) {
relabelContentYaml, err := unwrapRelabel.Content()
if err != nil {
return fmt.Errorf("get content of relabel configuration: %w", err)
Expand All @@ -41,6 +41,14 @@ func unwrap(bkt objstore.Bucket, unwrapRelabel extkingpin.PathOrContent, recursi
if err := yaml.Unmarshal(relabelContentYaml, &relabelConfig); err != nil {
return fmt.Errorf("parse relabel configuration: %w", err)
}
metaRelabelContentYaml, err := unwrapMetaRelabel.Content()
if err != nil {
return fmt.Errorf("get content of meta-relabel configuration: %w", err)
}
var metaRelabel []*relabel.Config
if err := yaml.Unmarshal(metaRelabelContentYaml, &metaRelabel); err != nil {
return fmt.Errorf("parse relabel configuration: %w", err)
}

objStoreYaml, err := outConfig.Content()
if err != nil {
Expand Down Expand Up @@ -72,7 +80,7 @@ func unwrap(bkt objstore.Bucket, unwrapRelabel extkingpin.PathOrContent, recursi
continue
}
}
if err := unwrapBlock(bkt, b, relabelConfig, *dir, unwrapDry, dst, logger); err != nil {
if err := unwrapBlock(bkt, b, relabelConfig, metaRelabel, *dir, unwrapDry, dst, logger); err != nil {
return err
}
}
Expand All @@ -90,7 +98,7 @@ func unwrap(bkt objstore.Bucket, unwrapRelabel extkingpin.PathOrContent, recursi
})
}

func unwrapBlock(bkt objstore.Bucket, b Block, relabelConfig []*relabel.Config, dir string, unwrapDry bool, dst objstore.Bucket, logger log.Logger) error {
func unwrapBlock(bkt objstore.Bucket, b Block, relabelConfig []*relabel.Config, metaRelabel []*relabel.Config, dir string, unwrapDry bool, dst objstore.Bucket, logger log.Logger) error {
if err := runutil.DeleteAll(dir); err != nil {
return fmt.Errorf("unable to cleanup cache folder %s: %w", dir, err)
}
Expand All @@ -114,6 +122,11 @@ func unwrapBlock(bkt objstore.Bucket, b Block, relabelConfig []*relabel.Config,
if err != nil {
return fmt.Errorf("fail to read meta.json for %s: %w", b.Id.String(), err)
}
lbls, keep := relabel.Process(labels.FromMap(origMeta.Thanos.Labels), metaRelabel...)
if !keep {
return nil
}
origMeta.Thanos.Labels = lbls.Map()
db, err := tsdb.OpenDBReadOnly(inDir, logger)
if err != nil {
return err
Expand Down

0 comments on commit 691177e

Please sign in to comment.