-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send logs to multiple loki instances (#536)
* Adds the ability to provide multiple Loki URL For backward compatibility `client:` still works with flag. * add some tests for multi client * update ksonnet module to support multiple client * fix comment * fix lint issues
- Loading branch information
1 parent
fa4f936
commit 53075db
Showing
13 changed files
with
342 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package client | ||
|
||
import ( | ||
"flag" | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/util" | ||
"github.com/cortexproject/cortex/pkg/util/flagext" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
// Config describes configuration for a HTTP pusher client. | ||
type Config struct { | ||
URL flagext.URLValue | ||
BatchWait time.Duration | ||
BatchSize int | ||
|
||
BackoffConfig util.BackoffConfig `yaml:"backoff_config"` | ||
// The labels to add to any time series or alerts when communicating with loki | ||
ExternalLabels model.LabelSet `yaml:"external_labels,omitempty"` | ||
Timeout time.Duration `yaml:"timeout"` | ||
} | ||
|
||
// RegisterFlags registers flags. | ||
func (c *Config) RegisterFlags(flags *flag.FlagSet) { | ||
flags.Var(&c.URL, "client.url", "URL of log server") | ||
flags.DurationVar(&c.BatchWait, "client.batch-wait", 1*time.Second, "Maximum wait period before sending batch.") | ||
flags.IntVar(&c.BatchSize, "client.batch-size-bytes", 100*1024, "Maximum batch size to accrue before sending. ") | ||
|
||
flag.IntVar(&c.BackoffConfig.MaxRetries, "client.max-retries", 5, "Maximum number of retires when sending batches.") | ||
flag.DurationVar(&c.BackoffConfig.MinBackoff, "client.min-backoff", 100*time.Millisecond, "Initial backoff time between retries.") | ||
flag.DurationVar(&c.BackoffConfig.MaxBackoff, "client.max-backoff", 5*time.Second, "Maximum backoff time between retries.") | ||
flag.DurationVar(&c.Timeout, "client.timeout", 10*time.Second, "Maximum time to wait for server to respond to a request") | ||
|
||
} | ||
|
||
// UnmarshalYAML implement Yaml Unmarshaler | ||
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
type raw Config | ||
// force sane defaults. | ||
cfg := raw{ | ||
BackoffConfig: util.BackoffConfig{ | ||
MaxBackoff: 5 * time.Second, | ||
MaxRetries: 5, | ||
MinBackoff: 100 * time.Millisecond, | ||
}, | ||
BatchSize: 100 * 1024, | ||
BatchWait: 1 * time.Second, | ||
Timeout: 10 * time.Second, | ||
} | ||
if err := unmarshal(&cfg); err != nil { | ||
return err | ||
} | ||
|
||
*c = Config(cfg) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package fake | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/grafana/loki/pkg/promtail/api" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
// Client is a fake client used for testing. | ||
type Client struct { | ||
OnHandleEntry api.EntryHandlerFunc | ||
OnStop func() | ||
} | ||
|
||
// Stop implements client.Client | ||
func (c *Client) Stop() { | ||
c.OnStop() | ||
} | ||
|
||
// Handle implements client.Client | ||
func (c *Client) Handle(labels model.LabelSet, time time.Time, entry string) error { | ||
return c.OnHandleEntry.Handle(labels, time, entry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/grafana/loki/pkg/util" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
// MultiClient is client pushing to one or more loki instances. | ||
type MultiClient []Client | ||
|
||
// NewMulti creates a new client | ||
func NewMulti(logger log.Logger, cfgs ...Config) (Client, error) { | ||
if len(cfgs) == 0 { | ||
return nil, errors.New("at least one client config should be provided") | ||
} | ||
var clients []Client | ||
for _, cfg := range cfgs { | ||
clients = append(clients, New(cfg, logger)) | ||
} | ||
return MultiClient(clients), nil | ||
} | ||
|
||
// Handle Implements api.EntryHandler | ||
func (m MultiClient) Handle(labels model.LabelSet, time time.Time, entry string) error { | ||
var result util.MultiError | ||
for _, client := range m { | ||
if err := client.Handle(labels, time, entry); err != nil { | ||
result.Add(err) | ||
} | ||
} | ||
return result.Err() | ||
} | ||
|
||
// Stop implements Client | ||
func (m MultiClient) Stop() { | ||
for _, c := range m { | ||
c.Stop() | ||
} | ||
} |
Oops, something went wrong.