-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Pipe data to Promtail #1649
Merged
Merged
Pipe data to Promtail #1649
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b188c23
Adds readerTarget that read from io.Reader
cyriltovena 8d65a11
Updates comment and function names.
cyriltovena 1270cfe
Adds stdin manager
cyriltovena ae8597d
Add more tests.
cyriltovena 7ac9c5c
Adds default config with labels and support for static configs labels.
cyriltovena 2429a2d
Adds a test with pipeline stages.
cyriltovena d22b439
Fixes race on shutdown and adds documentation
cyriltovena 55d7a19
Fixes deadlock on server.run
cyriltovena 9139daa
Update docs/clients/promtail/troubleshooting.md
cyriltovena a9ae6cb
gofmt
cyriltovena 5971974
gofmt
cyriltovena eb76b55
unlock mutex if Promtail.Run is called when Promtail is stopped
rfratto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,168 @@ | ||
package targets | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/util" | ||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/grafana/loki/pkg/logentry/stages" | ||
"github.com/grafana/loki/pkg/promtail/api" | ||
"github.com/grafana/loki/pkg/promtail/scrape" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/prometheus/discovery/config" | ||
"github.com/prometheus/prometheus/discovery/targetgroup" | ||
) | ||
|
||
// bufferSize is the size of the buffered reader | ||
const bufferSize = 8096 | ||
|
||
// file is an interface allowing us to abstract a file. | ||
type file interface { | ||
Stat() (os.FileInfo, error) | ||
io.Reader | ||
} | ||
|
||
var ( | ||
// stdIn is os.Stdin but can be replaced for testing purpose. | ||
stdIn file = os.Stdin | ||
hostName, _ = os.Hostname() | ||
// defaultStdInCfg is the default config for stdin target if none provided. | ||
defaultStdInCfg = scrape.Config{ | ||
JobName: "stdin", | ||
ServiceDiscoveryConfig: config.ServiceDiscoveryConfig{ | ||
StaticConfigs: []*targetgroup.Group{ | ||
{Labels: model.LabelSet{"job": "stdin"}}, | ||
{Labels: model.LabelSet{"hostname": model.LabelValue(hostName)}}, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
func isStdinPipe() bool { | ||
info, err := stdIn.Stat() | ||
if err != nil { | ||
level.Warn(util.Logger).Log("err", err) | ||
return false | ||
} | ||
m := info.Mode() | ||
if m&os.ModeCharDevice != 0 || info.Size() <= 0 { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
type Shutdownable interface { | ||
Shutdown() | ||
} | ||
|
||
type stdinTargetManager struct { | ||
*readerTarget | ||
app Shutdownable | ||
} | ||
|
||
func newStdinTargetManager(app Shutdownable, client api.EntryHandler, configs []scrape.Config) (*stdinTargetManager, error) { | ||
reader, err := newReaderTarget(stdIn, client, getStdinConfig(configs)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stdinManager := &stdinTargetManager{ | ||
readerTarget: reader, | ||
app: app, | ||
} | ||
// when we're done flushing our stdin we can shutdown the app. | ||
go func() { | ||
<-reader.ctx.Done() | ||
app.Shutdown() | ||
}() | ||
return stdinManager, nil | ||
} | ||
|
||
func getStdinConfig(configs []scrape.Config) scrape.Config { | ||
cfg := defaultStdInCfg | ||
// if we receive configs we use the first one. | ||
if len(configs) > 0 { | ||
if len(configs) > 1 { | ||
level.Warn(util.Logger).Log("msg", fmt.Sprintf("too many scrape configs, skipping %d configs.", len(configs)-1)) | ||
} | ||
cfg = configs[0] | ||
} | ||
return cfg | ||
} | ||
|
||
func (t *stdinTargetManager) Ready() bool { | ||
return t.ctx.Err() == nil | ||
} | ||
func (t *stdinTargetManager) Stop() { t.cancel() } | ||
func (t *stdinTargetManager) ActiveTargets() map[string][]Target { return nil } | ||
func (t *stdinTargetManager) AllTargets() map[string][]Target { return nil } | ||
|
||
type readerTarget struct { | ||
in *bufio.Reader | ||
out api.EntryHandler | ||
lbs model.LabelSet | ||
logger log.Logger | ||
|
||
cancel context.CancelFunc | ||
ctx context.Context | ||
} | ||
|
||
func newReaderTarget(in io.Reader, client api.EntryHandler, cfg scrape.Config) (*readerTarget, error) { | ||
pipeline, err := stages.NewPipeline(log.With(util.Logger, "component", "pipeline"), cfg.PipelineStages, &cfg.JobName, prometheus.DefaultRegisterer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
lbs := model.LabelSet{} | ||
for _, static := range cfg.ServiceDiscoveryConfig.StaticConfigs { | ||
if static != nil && static.Labels != nil { | ||
lbs = lbs.Merge(static.Labels) | ||
} | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t := &readerTarget{ | ||
in: bufio.NewReaderSize(in, bufferSize), | ||
out: pipeline.Wrap(client), | ||
cancel: cancel, | ||
ctx: ctx, | ||
lbs: lbs, | ||
logger: log.With(util.Logger, "component", "reader"), | ||
} | ||
go t.read() | ||
|
||
return t, nil | ||
} | ||
|
||
func (t *readerTarget) read() { | ||
defer t.cancel() | ||
|
||
for { | ||
if t.ctx.Err() != nil { | ||
return | ||
} | ||
line, err := t.in.ReadString('\n') | ||
if err != nil && err != io.EOF { | ||
level.Warn(t.logger).Log("msg", "error reading buffer", "err", err) | ||
return | ||
} | ||
line = strings.TrimRight(line, "\r\n") | ||
if line == "" { | ||
if err == io.EOF { | ||
return | ||
} | ||
continue | ||
} | ||
if err := t.out.Handle(t.lbs, time.Now(), line); err != nil { | ||
level.Error(t.logger).Log("msg", "error sending line", "err", err) | ||
} | ||
if err == io.EOF { | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slim-bean Feel free to reword here. Also should this gets its own page ? I think for now having it in troubleshooting is fine.