Skip to content

Commit c9d5111

Browse files
Update README file and refactor code for improved readability
Signed-off-by: Kushal Shukla <[email protected]>
1 parent bbf98f7 commit c9d5111

File tree

6 files changed

+195
-125
lines changed

6 files changed

+195
-125
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM quay.io/prometheus/busybox:latest
22
LABEL maintainer="The Prometheus Authors <[email protected]>"
33

4-
COPY object-storage /bin/object-storage
4+
COPY blockSyncer /bin/blocksyncer
55

6-
ENTRYPOINT ["/bin/object-storage"]
6+
ENTRYPOINT ["/bin/blocksyncer"]

tools/blockSyncer/Readme.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# blockSynch - TSDB Data Synchronization Tool
3+
4+
5+
The `block-sync` command is a CLI tool designed to synchronize TSDB data with an object storage system.
6+
7+
## Table of Contents
8+
9+
1. [Upload](#upload)
10+
2. [Download](#download)
11+
12+
## Command Flags
13+
14+
- ``` -h , --help```:Displays context-sensitive help
15+
- ``` - tsdb-path```: Path for The TSDB data in prometheus
16+
- ```- objstore.config-file```: Path for The Config file
17+
- ```- Key```: Path for the Key where to store block data , i.e a Directory.
18+
19+
## Upload
20+
21+
The `upload` command allows you to upload TSDB data from a specified path to an object storage bucket. This command is essential for backing up your TSDB data or migrating it to an object storage solution for future use.
22+
23+
### Usage
24+
25+
```bash
26+
./blockSynch upload --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
27+
28+
29+
```
30+
## Download
31+
32+
The `download` command allows you to retrieve TSDB data from an object storage bucket to a specified local path. This command is essential for restoring your TSDB data or retrieving it for local analysis and processing.
33+
34+
### Usage
35+
36+
```bash
37+
./blockSynch download --tsdb-path=<path-to-tsdb> --objstore.config-file=<path-to-config> --key=<object-key>
38+
```
39+
## Config File
40+
41+
The configuration file is essential for connecting to your object storage solution. Below are basic templates for different object storage systems.
42+
43+
```yaml
44+
type: s3
45+
config:
46+
bucket: your-bucket-name
47+
endpoint: https://your-minio-endpoint.com
48+
access_key: your-access-key
49+
secret_key: your-secret-key
50+
insecure: false # Set to true if using HTTP instead of HTTPS
51+
```
52+
For more follow this link [Storage.md](https://thanos.io/tip/thanos/storage.md/)
53+

tools/blockSyncer/obj.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package main
15+
16+
import (
17+
"os"
18+
"path/filepath"
19+
20+
"gopkg.in/alecthomas/kingpin.v2"
21+
)
22+
23+
func main() {
24+
app := kingpin.New(filepath.Base(os.Args[0]), "Tool for storing TSDB data to object storage")
25+
app.HelpFlag.Short('h')
26+
27+
var tsdbPath, objectConfig, objectKey string
28+
var s *Store
29+
30+
objstore := app.Command("block-sync", `Using an object storage to store the data`)
31+
objstore.Flag("tsdb-path", "Path for The TSDB data in prometheus").Required().StringVar(&tsdbPath)
32+
objstore.Flag("objstore.config-file", "Path for The Config file").Required().StringVar(&objectConfig)
33+
objstore.Flag("key", "Path for the Key where to store block data").Required().StringVar(&objectKey)
34+
35+
objstore.Action(func(c *kingpin.ParseContext) error {
36+
s = newstore(tsdbPath, objectConfig, objectKey)
37+
return nil
38+
})
39+
40+
uploadCmd := objstore.Command("upload", "Uploading data")
41+
uploadCmd.Action(func(c *kingpin.ParseContext) error {
42+
return s.upload(c)
43+
})
44+
45+
downloadCmd := objstore.Command("download", "Downloading data")
46+
downloadCmd.Action(func(c *kingpin.ParseContext) error {
47+
return s.download(c)
48+
})
49+
kingpin.MustParse(app.Parse(os.Args[1:]))
50+
}

tools/blockSyncer/upload_download.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package main
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"log/slog"
20+
"os"
21+
22+
"github.com/go-kit/log"
23+
"github.com/thanos-io/objstore"
24+
"github.com/thanos-io/objstore/client"
25+
"gopkg.in/alecthomas/kingpin.v2"
26+
)
27+
28+
type Store struct {
29+
Bucket objstore.Bucket
30+
TsdbPath string
31+
ObjectKey string
32+
ObjectConfig string
33+
}
34+
35+
func newstore(tsdbPath, objectConfig, objectKey string) *Store {
36+
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
37+
configBytes, err := os.ReadFile(objectConfig)
38+
if err != nil {
39+
logger.Error("failed to read config file", "error", err)
40+
return nil
41+
}
42+
43+
bucket, err := client.NewBucket(log.NewNopLogger(), configBytes, "block-sync")
44+
if err != nil {
45+
logger.Error("Failed to create bucket existence", "error", err)
46+
return nil
47+
}
48+
49+
exists, err := bucket.Exists(context.Background(), objectKey)
50+
if err != nil {
51+
logger.Error("Failed to create new bucket", "error", err)
52+
return nil
53+
}
54+
logger.Info("Bucket existence check", "bucket_name", objectKey, "exists", exists)
55+
56+
return &Store{
57+
Bucket: bucket,
58+
TsdbPath: tsdbPath,
59+
ObjectConfig: objectConfig,
60+
ObjectKey: objectKey,
61+
}
62+
}
63+
64+
func (c *Store) upload(*kingpin.ParseContext) error {
65+
66+
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
67+
68+
err := objstore.UploadDir(context.Background(), log.NewNopLogger(), c.Bucket, c.TsdbPath, c.ObjectKey)
69+
if err != nil {
70+
logger.Error("Failed to upload directory", "path", c.TsdbPath, "error", err)
71+
return fmt.Errorf("failed to upload directory from path %s to bucket: %v", c.TsdbPath, err)
72+
}
73+
74+
logger.Info("Successfully uploaded directory", "path", c.TsdbPath, "bucket", c.Bucket.Name())
75+
return nil
76+
}
77+
78+
func (c *Store) download(*kingpin.ParseContext) error {
79+
80+
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
81+
82+
err := objstore.DownloadDir(context.Background(), log.NewNopLogger(), c.Bucket, "dir/", c.ObjectKey, c.TsdbPath)
83+
if err != nil {
84+
logger.Error("Failed to download directory", "path", c.TsdbPath, "error", err)
85+
return fmt.Errorf("failed to download directory from path %s to bucket: %v", c.TsdbPath, err)
86+
}
87+
88+
logger.Info("Successfully downloaded directory", "path", c.TsdbPath, "bucket", c.Bucket.Name())
89+
return nil
90+
}

tools/object-storage/obj.go

-35
This file was deleted.

tools/object-storage/upload_download.go

-88
This file was deleted.

0 commit comments

Comments
 (0)