-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
factory.go
122 lines (106 loc) · 3.22 KB
/
factory.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
/*
* Copyright (c) 2021 The Clymene 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 opentsdb
import (
"flag"
"github.com/Clymene-project/Clymene/plugin/storage/opentsdb/dryrun"
"github.com/Clymene-project/Clymene/plugin/storage/opentsdb/http"
"github.com/Clymene-project/Clymene/plugin/storage/opentsdb/metricstore/dbmodel"
"github.com/Clymene-project/Clymene/plugin/storage/opentsdb/socket"
"github.com/Clymene-project/Clymene/storage/metricstore"
"github.com/spf13/viper"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"strconv"
"strings"
)
// The opentsdb factory was developed based on opentsdb's tcollector.
type Factory struct {
options Options
metricsFactory metrics.Factory
logger *zap.Logger
client Client
}
func NewFactory() *Factory {
return &Factory{}
}
// AddFlags implements plugin.Configurable
func (f *Factory) AddFlags(flagSet *flag.FlagSet) {
f.options.AddFlags(flagSet)
}
// InitFromViper implements plugin.Configurable
func (f *Factory) InitFromViper(v *viper.Viper) {
f.options.InitFromViper(v)
}
// InitFromOptions initializes factory from options.
func (f *Factory) InitFromOptions(o Options) {
f.options = o
}
func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) error {
f.metricsFactory, f.logger = metricsFactory, logger
logger.Info("Factory Initialize", zap.String("type", "opentsdb"))
// hosts init
var hosts []http.Hosts
if f.options.hosts != "" {
for _, host := range strings.Split(f.options.hosts, ",") {
hostsplit := strings.Split(host, ":")
var h http.Hosts
var err error
h.Host = hostsplit[0]
h.Port, err = strconv.Atoi(hostsplit[1])
// check host:port format
if err != nil {
f.logger.Panic("failed to host init", zap.Error(err))
}
hosts = append(hosts, h)
}
} else {
hosts = append(hosts, http.Hosts{Host: f.options.host, Port: f.options.port})
}
// prompb.TimeSeries to json
converter := &dbmodel.Converter{
MaxTags: f.options.maxTags,
}
if f.options.http {
// http, https
f.client = http.NewClient(&http.Options{
Hosts: hosts,
HttpPassword: f.options.httpPassword,
HttpUsername: f.options.httpUsername,
SSL: f.options.ssl,
HttpApiPath: f.options.httpApiPath,
Factory: f.metricsFactory,
Timeout: f.options.timeout,
MaxChunk: f.options.maxChunk,
}, converter, f.logger)
} else if f.options.dryRun {
// dryrun
f.client = dryrun.NewClient(
f.options.maxChunk,
converter,
f.logger,
)
} else {
// socket
f.client = socket.NewClient(&socket.Options{
Hosts: hosts,
}, converter, f.logger)
}
return nil
}
func (f Factory) CreateWriter() (metricstore.Writer, error) {
return NewMetricWriter(f.logger, f.client), nil
}