forked from parca-dev/parca
/
storage.go
126 lines (105 loc) · 3.03 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2018 The conprof 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 storage
import (
"encoding/base64"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/prometheus/pkg/labels"
)
type Storage interface {
Get(series, timestamp string, read func(io.Reader) error) error
List() (map[string][]string, error)
Appender() Appender
}
type diskStorage struct {
logger log.Logger
path string
}
func NewDiskStorage(logger log.Logger, path string) Storage {
return &diskStorage{
logger: logger,
path: path,
}
}
func (s *diskStorage) Get(series, timestamp string, read func(io.Reader) error) error {
f, err := os.Open(path.Join(s.path, base64.URLEncoding.EncodeToString([]byte(series)), timestamp))
if err != nil {
return err
}
defer f.Close()
return read(f)
}
func (s *diskStorage) List() (map[string][]string, error) {
seriesDirs, err := ioutil.ReadDir(s.path)
if err != nil {
return nil, err
}
series := make(map[string][]string, len(seriesDirs))
for _, seriesDir := range seriesDirs {
seriesDirName := seriesDir.Name()
if !seriesDir.IsDir() || seriesDirName == "." || seriesDirName == ".." {
continue
}
decodedSeriesName, err := base64.URLEncoding.DecodeString(seriesDirName)
if err != nil {
return nil, err
}
seriesName := string(decodedSeriesName)
files, err := ioutil.ReadDir(filepath.Join(s.path, seriesDirName))
if err != nil {
return nil, err
}
series[seriesName] = make([]string, len(files))
for _, file := range files {
if file.IsDir() {
continue
}
series[seriesName] = append(series[seriesName], file.Name())
}
}
return series, nil
}
func (s *diskStorage) Appender() Appender {
return &storageAppender{path: s.path, logger: s.logger}
}
type Appender interface {
Add(l labels.Labels, timestamp int64, profile []byte) error
}
type storageAppender struct {
logger log.Logger
path string
}
func (s *storageAppender) Add(labels labels.Labels, timestamp int64, profile []byte) error {
sort.Sort(labels)
seriesString := labels.String()
seriesString = base64.URLEncoding.EncodeToString([]byte(seriesString))
p := filepath.Join(s.path, seriesString)
err := os.MkdirAll(p, os.ModePerm)
if err != nil {
return err
}
fullpath := filepath.Join(p, intToString(timestamp))
level.Debug(s.logger).Log("msg", "writing profile", "path", fullpath)
return ioutil.WriteFile(fullpath, profile, 0644)
}
func intToString(i int64) string {
return strconv.FormatInt(i, 10)
}