Skip to content

Commit 31d051e

Browse files
authored
Merge pull request #12 from infrawatch/public-loki
Split loki connector constructor.
2 parents 028cc6a + 9f6eb50 commit 31d051e

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

connector/loki.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,42 @@ func (client *LokiConnector) IsReady() bool {
8282
return err == nil && response.StatusCode == 200
8383
}
8484

85-
//ConnectLoki creates a new loki connector
86-
func ConnectLoki(cfg config.Config, logger *logging.Logger) (*LokiConnector, error) {
85+
func CreateLokiConnector(logger *logging.Logger, address string, maxWaitTime time.Duration, batchSize int64) (*LokiConnector, error) {
8786
client := LokiConnector{
88-
quit: make(chan struct{}),
89-
streams: make(chan *LokiStream),
90-
logger: logger,
91-
endpoints: endpoints{
87+
url: address,
88+
maxBatch: batchSize,
89+
maxWaitTime: maxWaitTime,
90+
quit: make(chan struct{}),
91+
streams: make(chan *LokiStream),
92+
logger: logger,
93+
endpoints: endpoints{
9294
push: "/loki/api/v1/push",
9395
query: "/loki/api/v1/query_range",
9496
ready: "/ready",
9597
},
9698
}
99+
err := client.Connect()
100+
return &client, err
101+
}
97102

103+
//ConnectLoki creates a new loki connector
104+
func ConnectLoki(cfg config.Config, logger *logging.Logger) (*LokiConnector, error) {
98105
var err error
106+
var url string
107+
var maxBatch int64
108+
var maxWaitTime time.Duration
109+
99110
var addr *config.Option
100111
switch conf := cfg.(type) {
101112
case *config.INIConfig:
102113
addr, err = conf.GetOption("loki/connection")
103114
case *config.JSONConfig:
104115
addr, err = conf.GetOption("Loki.Connection.Address")
105116
default:
106-
return &client, fmt.Errorf("Unknown Config type")
117+
return nil, fmt.Errorf("Unknown Config type")
107118
}
108119
if err == nil && addr != nil {
109-
client.url = addr.GetString()
120+
url = addr.GetString()
110121
} else {
111122
return nil, fmt.Errorf("Failed to get connection URL from configuration file")
112123
}
@@ -119,7 +130,7 @@ func ConnectLoki(cfg config.Config, logger *logging.Logger) (*LokiConnector, err
119130
batchSize, err = conf.GetOption("Loki.Connection.BatchSize")
120131
}
121132
if err == nil && batchSize != nil {
122-
client.maxBatch = batchSize.GetInt()
133+
maxBatch = batchSize.GetInt()
123134
} else {
124135
return nil, fmt.Errorf("Failed to get connection max batch size from configuration file")
125136
}
@@ -132,13 +143,12 @@ func ConnectLoki(cfg config.Config, logger *logging.Logger) (*LokiConnector, err
132143
waitTime, err = conf.GetOption("Loki.Connection.MaxWaitTime")
133144
}
134145
if err == nil && waitTime != nil {
135-
client.maxWaitTime = time.Duration(waitTime.GetInt()) * time.Millisecond
146+
maxWaitTime = time.Duration(waitTime.GetInt()) * time.Millisecond
136147
} else {
137148
return nil, fmt.Errorf("Failed to get connection max wait time from configuration file")
138149
}
139150

140-
err = client.Connect()
141-
return &client, err
151+
return CreateLokiConnector(logger, url, maxWaitTime, maxBatch)
142152
}
143153

144154
//Connect just checks the Loki availability

0 commit comments

Comments
 (0)