Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--dry-run Promtail. #1652

Merged
merged 10 commits into from
Feb 13, 2020
3 changes: 2 additions & 1 deletion cmd/promtail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {

func main() {
printVersion := flag.Bool("version", false, "Print this builds version information")
dryRun := flag.Bool("dry-run", false, "Start Promtail but print entries instead of sending them to Loki.")

// Load config, merging config file and CLI flags
var config config.Config
Expand Down Expand Up @@ -57,7 +58,7 @@ func main() {
stages.Debug = true
}

p, err := promtail.New(config)
p, err := promtail.New(config, *dryRun)
if err != nil {
level.Error(util.Logger).Log("msg", "error creating promtail", "error", err)
os.Exit(1)
Expand Down
56 changes: 56 additions & 0 deletions pkg/promtail/client/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package client

import (
"fmt"
"os"
"sync"
"text/tabwriter"
"time"

"github.com/cortexproject/cortex/pkg/util"
"github.com/fatih/color"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v2"
)

type logger struct {
*tabwriter.Writer
sync.Mutex
}

// NewLogger creates a new client logger that logs entries instead of sending them.
func NewLogger(cfgs ...Config) (Client, error) {
// make sure the clients config is valid
c, err := NewMulti(util.Logger, cfgs...)
if err != nil {
return nil, err
}
c.Stop()
fmt.Println(color.YellowString("Clients configured:"))
for _, cfg := range cfgs {
yaml, err := yaml.Marshal(cfg)
if err != nil {
return nil, err
}
fmt.Println("----------------------")
fmt.Println(string(yaml))
}
return &logger{
Writer: tabwriter.NewWriter(os.Stdout, 0, 4, 0, '\t', 0),
}, nil
}

func (*logger) Stop() {}

func (l *logger) Handle(labels model.LabelSet, time time.Time, entry string) error {
l.Lock()
defer l.Unlock()
fmt.Fprint(l.Writer, color.BlueString(time.Format("2006-01-02T15:04:05")))
fmt.Fprint(l.Writer, "\t")
fmt.Fprint(l.Writer, color.YellowString(labels.String()))
fmt.Fprint(l.Writer, "\t")
fmt.Fprint(l.Writer, entry)
fmt.Fprint(l.Writer, "\n")
l.Flush()
return nil
}
21 changes: 21 additions & 0 deletions pkg/promtail/client/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import (
"net/url"
"testing"
"time"

"github.com/cortexproject/cortex/pkg/util/flagext"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
)

func TestNewLogger(t *testing.T) {
_, err := NewLogger([]Config{}...)
require.Error(t, err)

l, err := NewLogger([]Config{{URL: flagext.URLValue{URL: &url.URL{Host: "string"}}}}...)
require.NoError(t, err)
err = l.Handle(model.LabelSet{"foo": "bar"}, time.Now(), "entry")
require.NoError(t, err)
}
20 changes: 14 additions & 6 deletions pkg/promtail/promtail.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Promtail struct {
}

// New makes a new Promtail.
func New(cfg config.Config) (*Promtail, error) {
func New(cfg config.Config, dryRun bool) (*Promtail, error) {
positions, err := positions.New(util.Logger, cfg.PositionsConfig)
if err != nil {
return nil, err
Expand All @@ -30,12 +30,20 @@ func New(cfg config.Config) (*Promtail, error) {
cfg.ClientConfigs = append(cfg.ClientConfigs, cfg.ClientConfig)
}

client, err := client.NewMulti(util.Logger, cfg.ClientConfigs...)
if err != nil {
return nil, err
var cl client.Client
if dryRun {
cl, err = client.NewLogger(cfg.ClientConfigs...)
if err != nil {
return nil, err
}
} else {
cl, err = client.NewMulti(util.Logger, cfg.ClientConfigs...)
if err != nil {
return nil, err
}
}

tms, err := targets.NewTargetManagers(util.Logger, positions, client, cfg.ScrapeConfig, &cfg.TargetConfig)
tms, err := targets.NewTargetManagers(util.Logger, positions, cl, cfg.ScrapeConfig, &cfg.TargetConfig)
if err != nil {
return nil, err
}
Expand All @@ -46,7 +54,7 @@ func New(cfg config.Config) (*Promtail, error) {
}

return &Promtail{
client: client,
client: cl,
positions: positions,
targetManagers: tms,
server: server,
Expand Down
29 changes: 28 additions & 1 deletion pkg/promtail/promtail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
Expand All @@ -19,18 +20,22 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse"
"github.com/prometheus/prometheus/promql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafana/loki/pkg/logentry/stages"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/promtail/api"
"github.com/grafana/loki/pkg/promtail/client"
"github.com/grafana/loki/pkg/promtail/config"
"github.com/grafana/loki/pkg/promtail/positions"
"github.com/grafana/loki/pkg/promtail/scrape"
"github.com/grafana/loki/pkg/promtail/targets"
)
Expand Down Expand Up @@ -84,7 +89,7 @@ func TestPromtail(t *testing.T) {

// Run.

p, err := New(buildTestConfig(t, positionsFileName, testDir))
p, err := New(buildTestConfig(t, positionsFileName, testDir), false)
if err != nil {
t.Error("error creating promtail", err)
return
Expand Down Expand Up @@ -628,3 +633,25 @@ func randName() string {
}
return string(b)
}

func Test_DryRun(t *testing.T) {

f, err := ioutil.TempFile("/tmp", "Test_DryRun")
owen-d marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
defer os.Remove(f.Name())

_, err = New(config.Config{}, true)
require.Error(t, err)

prometheus.DefaultRegisterer = prometheus.NewRegistry() // reset registry, otherwise you can't create 2 weavework server.

p, err := New(config.Config{
ClientConfig: client.Config{URL: flagext.URLValue{URL: &url.URL{Host: "string"}}},
PositionsConfig: positions.Config{
PositionsFile: f.Name(),
SyncPeriod: time.Second,
},
}, false)
require.NoError(t, err)
require.IsType(t, client.MultiClient{}, p.client)
}