Skip to content

Commit

Permalink
Good practices code
Browse files Browse the repository at this point in the history
Signed-off-by: Kushal Shukla <kushalshukla110@gmail.com>
  • Loading branch information
kushalShukla-web committed Oct 4, 2024
1 parent c9d5111 commit 5840472
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM quay.io/prometheus/busybox:latest
LABEL maintainer="The Prometheus Authors <prometheus-developers@googlegroups.com>"

COPY blockSyncer /bin/blocksyncer
COPY block-sync /bin/blocksyncer

ENTRYPOINT ["/bin/blocksyncer"]
8 changes: 4 additions & 4 deletions tools/blockSyncer/Readme.md → tools/block-sync/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# blockSynch - TSDB Data Synchronization Tool
# block-sync - TSDB Data Synchronization Tool


The `block-sync` command is a CLI tool designed to synchronize TSDB data with an object storage system.
Expand All @@ -23,7 +23,7 @@ The `upload` command allows you to upload TSDB data from a specified path to an
### Usage

```bash
./blockSynch upload --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
./block-sync upload --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>


```
Expand All @@ -34,7 +34,7 @@ The `download` command allows you to retrieve TSDB data from an object storage b
### Usage

```bash
./blockSynch download --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
./block-sync download --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
```
## Config File

Expand All @@ -49,5 +49,5 @@ config:
secret_key: your-secret-key
insecure: false # Set to true if using HTTP instead of HTTPS
```
For more follow this link [Storage.md](https://thanos.io/tip/thanos/storage.md/)
You can customize the config file , follow this link [Storage.md](https://thanos.io/tip/thanos/storage.md/)
36 changes: 25 additions & 11 deletions tools/blockSyncer/obj.go → tools/block-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"

Expand All @@ -24,27 +27,38 @@ func main() {
app := kingpin.New(filepath.Base(os.Args[0]), "Tool for storing TSDB data to object storage")
app.HelpFlag.Short('h')

var tsdbPath, objectConfig, objectKey string
var s *Store

objstore := app.Command("block-sync", `Using an object storage to store the data`)
var (
s *Store
tsdbPath string
objectConfig string
objectKey string
logger *slog.Logger
)
objstore := app.Command("blocksync", `Using an object storage to store the data`)
objstore.Flag("tsdb-path", "Path for The TSDB data in prometheus").Required().StringVar(&tsdbPath)
objstore.Flag("objstore.config-file", "Path for The Config file").Required().StringVar(&objectConfig)
objstore.Flag("key", "Path for the Key where to store block data").Required().StringVar(&objectKey)

objstore.Action(func(c *kingpin.ParseContext) error {
s = newstore(tsdbPath, objectConfig, objectKey)
var err error
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
s, err = newStore(tsdbPath, objectConfig, objectKey, logger)
if err != nil {
logger.Error("Failed to create store", "error", err)
return fmt.Errorf("fail to create store :%w", err)
}
return nil
})

uploadCmd := objstore.Command("upload", "Uploading data")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
uploadCmd := objstore.Command("upload", "Uploading data to objstore")
uploadCmd.Action(func(c *kingpin.ParseContext) error {
return s.upload(c)
return s.upload(ctx)
})

downloadCmd := objstore.Command("download", "Downloading data")
downloadCmd := objstore.Command("download", "Downloading data from objstore")
downloadCmd.Action(func(c *kingpin.ParseContext) error {
return s.download(c)
return s.download(ctx)
})
kingpin.MustParse(app.Parse(os.Args[1:]))

}
87 changes: 87 additions & 0 deletions tools/block-sync/upload_download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 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 main

import (
"context"
"fmt"
"log/slog"
"os"

"github.com/go-kit/log"
"github.com/thanos-io/objstore"
"github.com/thanos-io/objstore/client"
)

type Store struct {
bucket objstore.Bucket
tsdbpath string
objectkey string
objectconfig string
bucketlogger *slog.Logger
}

func newStore(tsdbPath, objectConfig, objectKey string, logger *slog.Logger) (*Store, error) {
configBytes, err := os.ReadFile(objectConfig)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}

bucket, err := client.NewBucket(log.NewNopLogger(), configBytes, "block-sync")
if err != nil {
return nil, fmt.Errorf("failed to create bucket existence:%w", err)
}
return &Store{
bucket: bucket,
tsdbpath: tsdbPath,
objectkey: objectKey,
objectconfig: objectConfig,
bucketlogger: logger,
}, nil

}

func (c *Store) upload(ctx context.Context) error {
exists, err := c.bucket.Exists(ctx, c.objectkey)
if err != nil {
return fmt.Errorf("failed to check new bucket:%w", err)
}
c.bucketlogger.Info("Bucket checked Successfully", "Bucket name", exists)

err = objstore.UploadDir(ctx, log.NewNopLogger(), c.bucket, c.tsdbpath, c.objectkey)
if err != nil {
c.bucketlogger.Error("Failed to upload directory", "path", c.tsdbpath, "error", err)
return fmt.Errorf("failed to upload directory from path %s to bucket: %v", c.tsdbpath, err)
}

c.bucketlogger.Info("Successfully uploaded directory", "path", c.tsdbpath, "bucket", c.bucket.Name())
return nil
}

func (c *Store) download(ctx context.Context) error {
exists, err := c.bucket.Exists(ctx, c.objectkey)
if err != nil {
return fmt.Errorf("failed to check new bucket:%w", err)
}
c.bucketlogger.Info("Bucket checked Successfully", "Bucket name", exists)

err = objstore.DownloadDir(ctx, log.NewNopLogger(), c.bucket, "dir/", c.objectkey, c.tsdbpath)
if err != nil {
c.bucketlogger.Error("Failed to download directory", "path", c.tsdbpath, "error", err)
return fmt.Errorf("failed to download directory from path %s to bucket: %v", c.tsdbpath, err)
}

c.bucketlogger.Info("Successfully downloaded directory", "path", c.tsdbpath, "bucket", c.bucket.Name())
return nil
}
90 changes: 0 additions & 90 deletions tools/blockSyncer/upload_download.go

This file was deleted.

0 comments on commit 5840472

Please sign in to comment.