-
Notifications
You must be signed in to change notification settings - Fork 487
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
[Flow] Add new discovery.file component #4404
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a90102
Add component for prometheus-based file discovery, temporarily named …
spartan0x117 6e82d27
Add docs for new discovery.file component
spartan0x117 d2cb04a
Update CHANGELOG
spartan0x117 6ec2835
Merge main branch and rename file2 -> file
spartan0x117 8651cb2
Add note in docs covering discovery.file rename
spartan0x117 9df22b3
Fix all.go comment
spartan0x117 2e7a970
Merge branch 'main' into spartan0x117/flow-discovery-file
spartan0x117 a40e2ae
Add more detail to discovery.file.md note
spartan0x117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package file | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/discovery" | ||
"github.com/prometheus/common/model" | ||
prom_discovery "github.com/prometheus/prometheus/discovery/file" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "discovery.file", | ||
Args: Arguments{}, | ||
Exports: discovery.Exports{}, | ||
|
||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return New(opts, args.(Arguments)) | ||
}, | ||
}) | ||
} | ||
|
||
type Arguments struct { | ||
Files []string `river:"files,attr"` | ||
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"` | ||
} | ||
|
||
var DefaultArguments = Arguments{ | ||
RefreshInterval: 5 * time.Minute, | ||
} | ||
|
||
// SetToDefault implements river.Defaulter. | ||
func (a *Arguments) SetToDefault() { | ||
*a = DefaultArguments | ||
} | ||
|
||
func (a *Arguments) Convert() *prom_discovery.SDConfig { | ||
return &prom_discovery.SDConfig{ | ||
Files: a.Files, | ||
RefreshInterval: model.Duration(a.RefreshInterval), | ||
} | ||
} | ||
|
||
func New(opts component.Options, args Arguments) (*discovery.Component, error) { | ||
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) { | ||
newArgs := args.(Arguments) | ||
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger), 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,43 @@ | ||
package file | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/agent/pkg/river" | ||
"github.com/prometheus/common/model" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
cfg := ` | ||
refresh_interval = "10m" | ||
files = ["file1", "file2"]` | ||
|
||
var args Arguments | ||
err := river.Unmarshal([]byte(cfg), &args) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, len(args.Files)) | ||
require.Equal(t, 10*time.Minute, args.RefreshInterval) | ||
} | ||
|
||
func TestUnmarshal_Defaults(t *testing.T) { | ||
cfg := `files = ["file1"]` | ||
|
||
var args Arguments | ||
err := river.Unmarshal([]byte(cfg), &args) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(args.Files)) | ||
require.Equal(t, 5*time.Minute, args.RefreshInterval) | ||
} | ||
|
||
func TestConvert(t *testing.T) { | ||
args := Arguments{ | ||
Files: []string{"file1", "file2"}, | ||
RefreshInterval: 10 * time.Minute, | ||
} | ||
|
||
promSDConfig := args.Convert() | ||
require.Equal(t, 2, len(promSDConfig.Files)) | ||
require.Equal(t, model.Duration(10*time.Minute), promSDConfig.RefreshInterval) | ||
} |
163 changes: 163 additions & 0 deletions
163
docs/sources/flow/reference/components/discovery.file.md
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,163 @@ | ||
--- | ||
title: discovery.file | ||
--- | ||
|
||
# discovery.file | ||
|
||
> **NOTE:** In `v0.35.0` of the Grafana Agent, the `discovery.file` component was renamed to [local.file_match][]. From `v0.35.0` onwards, the `discovery.file` | ||
> component behaves as documented here. | ||
|
||
[local.file_match]: {{< relref "./local.file_match.md" >}} | ||
|
||
`discovery.file` discovers targets from a set of files, similar to the [Prometheus file_sd_config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config). | ||
|
||
## Usage | ||
|
||
```river | ||
discovery.file "LABEL" { | ||
files = [FILE_PATH_1, FILE_PATH_2, ...] | ||
} | ||
``` | ||
|
||
## Arguments | ||
|
||
The following arguments are supported: | ||
|
||
Name | Type | Description | Default | Required | ||
------------------ | ------------------- | ------------------------------------------ |---------| -------- | ||
`files` | `list(string)` | Files to read and discover targets from. | | yes | ||
`refresh_interval` | `duration` | How often to sync targets. | "5m" | no | ||
|
||
The last path segment of each element in `files` may contain a single * that matches any character sequence, e.g. `my/path/tg_*.json`. | ||
|
||
## Exported fields | ||
|
||
The following fields are exported and can be referenced by other components: | ||
|
||
Name | Type | Description | ||
--------- | ------------------- | ----------- | ||
`targets` | `list(map(string))` | The set of targets discovered from the filesystem. | ||
|
||
Each target includes the following labels: | ||
|
||
* `__meta_filepath`: The absolute path to the file the target was discovered from. | ||
|
||
## Component health | ||
|
||
`discovery.file` is only reported as unhealthy when given an invalid | ||
configuration. In those cases, exported fields retain their last healthy | ||
values. | ||
|
||
## Debug information | ||
|
||
`discovery.file` does not expose any component-specific debug information. | ||
|
||
### Debug metrics | ||
|
||
`discovery.file` does not expose any component-specific debug metrics. | ||
|
||
## Examples | ||
|
||
### Example target files | ||
```json | ||
[ | ||
{ | ||
"targets": [ "127.0.0.1:9091", "127.0.0.1:9092" ], | ||
"labels": { | ||
"environment": "dev" | ||
} | ||
}, | ||
{ | ||
"targets": [ "127.0.0.1:9093" ], | ||
"labels": { | ||
"environment": "prod" | ||
} | ||
} | ||
] | ||
``` | ||
|
||
```yaml | ||
- targets: | ||
- 127.0.0.1:9999 | ||
- 127.0.0.1:10101 | ||
labels: | ||
job: worker | ||
- targets: | ||
- 127.0.0.1:9090 | ||
labels: | ||
job: prometheus | ||
``` | ||
|
||
### Basic file discovery | ||
|
||
This example discovers targets from a single file, scrapes them, and writes metrics | ||
to a Prometheus remote write endpoint. | ||
|
||
```river | ||
discovery.file "example" { | ||
files = ["/tmp/example.json"] | ||
} | ||
|
||
prometheus.scrape "default" { | ||
targets = discovery.file.example.targets | ||
forward_to = [prometheus.remote_write.demo.receiver] | ||
} | ||
|
||
prometheus.remote_write "demo" { | ||
endpoint { | ||
url = PROMETHEUS_REMOTE_WRITE_URL | ||
|
||
basic_auth { | ||
username = USERNAME | ||
password = PASSWORD | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Replace the following: | ||
- `PROMETHEUS_REMOTE_WRITE_URL`: The URL of the Prometheus remote_write-compatible server to send metrics to. | ||
- `USERNAME`: The username to use for authentication to the remote_write API. | ||
- `PASSWORD`: The password to use for authentication to the remote_write API. | ||
|
||
### File discovery with retained file path label | ||
|
||
This example discovers targets from a wildcard file path, scrapes them, and writes metrics | ||
to a Prometheus remote write endpoint. | ||
|
||
It also uses a relabeling rule to retain the file path as a label on each target. | ||
|
||
```river | ||
discovery.file "example" { | ||
files = ["/tmp/example_*.yaml"] | ||
} | ||
|
||
discovery.relabel "keep_filepath" { | ||
targets = discovery.file.example.targets | ||
rule { | ||
source_labels = ["__meta_filepath"] | ||
target_label = "filepath" | ||
} | ||
} | ||
|
||
prometheus.scrape "default" { | ||
targets = discovery.relabel.keep_filepath.output | ||
forward_to = [prometheus.remote_write.demo.receiver] | ||
} | ||
|
||
prometheus.remote_write "demo" { | ||
endpoint { | ||
url = PROMETHEUS_REMOTE_WRITE_URL | ||
|
||
basic_auth { | ||
username = USERNAME | ||
password = PASSWORD | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Replace the following: | ||
- `PROMETHEUS_REMOTE_WRITE_URL`: The URL of the Prometheus remote_write-compatible server to send metrics to. | ||
- `USERNAME`: The username to use for authentication to the remote_write API. | ||
- `PASSWORD`: The password to use for authentication to the remote_write API. |
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.
Maybe specify here briefly what local.file_match does, and why they may want to go there instead.