/
provider.go
138 lines (126 loc) · 4.37 KB
/
provider.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
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2021 Terminus, Inc.
//
// 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 elasticsearch
import (
"context"
"fmt"
"strconv"
"time"
"github.com/olivere/elastic"
"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/providers/elasticsearch"
"github.com/erda-project/erda/modules/core/monitor/log"
"github.com/erda-project/erda/modules/core/monitor/log/storage"
retention "github.com/erda-project/erda/modules/core/monitor/settings/retention-strategy"
"github.com/erda-project/erda/modules/core/monitor/storekit"
"github.com/erda-project/erda/modules/core/monitor/storekit/elasticsearch/index/creator"
"github.com/erda-project/erda/modules/core/monitor/storekit/elasticsearch/index/loader"
)
type (
config struct {
QueryTimeout time.Duration `file:"query_timeout" default:"1m"`
WriteTimeout time.Duration `file:"write_timeout" default:"1m"`
ReadPageSize int `file:"read_page_size" default:"1024"`
IndexType string `file:"index_type" default:"log"`
}
provider struct {
Cfg *config
Log logs.Logger
ES1 elasticsearch.Interface `autowired:"elasticsearch@log" optional:"true"`
ES2 elasticsearch.Interface `autowired:"elasticsearch" optional:"true"`
Loader loader.Interface `autowired:"elasticsearch.index.loader@log"`
Creator creator.Interface `autowired:"elasticsearch.index.creator@log" optional:"true"`
Retention retention.Interface `autowired:"storage-retention-strategy@log" optional:"true"`
es elasticsearch.Interface
client *elastic.Client
queryTimeout string
}
)
func (p *provider) Init(ctx servicehub.Context) (err error) {
if p.ES1 != nil {
p.es = p.ES1
} else if p.ES2 != nil {
p.es = p.ES2
} else {
return fmt.Errorf("elasticsearch is required")
}
p.client = p.es.Client()
if p.Retention != nil {
ctx.AddTask(func(c context.Context) error {
p.Retention.Loading(ctx)
return nil
})
}
return nil
}
var _ storage.Storage = (*provider)(nil)
func (p *provider) NewWriter(ctx context.Context) (storekit.BatchWriter, error) {
if p.Creator == nil || p.Retention == nil {
return nil, fmt.Errorf("elasticsearch.index.creator@log and storage-retention-strategy@log is required for Writer")
}
w := p.es.NewWriter(&elasticsearch.WriteOptions{
Timeout: p.Cfg.WriteTimeout,
Enc: func(val interface{}) (index, id, typ string, body interface{}, err error) {
data := val.(*log.LabeledLog)
var wait <-chan error
if p.Retention == nil {
wait, index = p.Creator.Ensure(data.Tags["dice_org_name"])
} else {
key := p.Retention.GetConfigKey(data.Source, data.Tags)
if len(key) > 0 {
wait, index = p.Creator.Ensure(data.Tags["dice_org_name"], key)
} else {
wait, index = p.Creator.Ensure(data.Tags["dice_org_name"])
}
}
if wait != nil {
select {
case <-wait:
case <-ctx.Done():
return "", "", "", nil, storekit.ErrExitConsume
}
}
id = data.ID
if len(id) > 12 {
id = id[:12]
}
return index, id + strconv.FormatInt(data.Timestamp, 36) + "-" + strconv.FormatInt(data.Offset, 36), p.Cfg.IndexType, &Document{
Log: &data.Log,
Date: getUnixMillisecond(data.Timestamp),
}, nil
},
})
return w, nil
}
// Document .
type Document struct {
*log.Log
Date int64 `json:"@timestamp"`
}
const maxUnixMillisecond int64 = 9999999999999
func getUnixMillisecond(ts int64) int64 {
if ts > maxUnixMillisecond {
return ts / int64(time.Millisecond)
}
return ts
}
func init() {
servicehub.Register("log-storage-elasticsearch", &servicehub.Spec{
Services: []string{"log-storage-elasticsearch-reader", "log-storage-writer"},
Dependencies: []string{"elasticsearch"},
ConfigFunc: func() interface{} { return &config{} },
Creator: func() servicehub.Provider { return &provider{} },
})
}