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

[receiver/filestats] add file.count metric #30191

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/filestats_filecount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: filestatsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add a file.count metric to filestatsreceiver that reports the number of files matched by the receiver

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24651]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
8 changes: 8 additions & 0 deletions receiver/filestatsreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Elapsed time since last access of the file or folder, in seconds since Epoch.
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | false |

### file.count

The number of files matched

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {file} | Gauge | Int |

### file.ctime

Elapsed time since the last change of the file or folder, in seconds since Epoch. In addition to `file.mtime`, this metric tracks metadata changes such as permissions or renaming the file.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ all_set:
metrics:
file.atime:
enabled: true
file.count:
enabled: true
file.ctime:
enabled: true
file.mtime:
Expand All @@ -18,6 +20,8 @@ none_set:
metrics:
file.atime:
enabled: false
file.count:
enabled: false
file.ctime:
enabled: false
file.mtime:
Expand Down
6 changes: 6 additions & 0 deletions receiver/filestatsreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ metrics:
gauge:
value_type: int
unit: "b"
file.count:
description: The number of files matched
enabled: false
gauge:
value_type: int
unit: "{file}"
tests:
config:
3 changes: 3 additions & 0 deletions receiver/filestatsreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) {
s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}

s.mb.RecordFileCountDataPoint(now, int64(len(matches)))
s.mb.EmitForResource()

if len(scrapeErrors) > 0 {
return s.mb.Emit(), scrapererror.NewPartialScrapeError(multierr.Combine(scrapeErrors...), len(scrapeErrors))
}
Expand Down
12 changes: 10 additions & 2 deletions receiver/filestatsreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ func Test_Scrape_All(t *testing.T) {
cfg.Include = filepath.Join(tmpDir, "*.log")
cfg.Metrics.FileAtime.Enabled = true
cfg.Metrics.FileCtime.Enabled = true
cfg.Metrics.FileCount.Enabled = true

s := newScraper(cfg, receivertest.NewNopCreateSettings())
metrics, err := s.scrape(context.Background())
require.NoError(t, err)
require.Equal(t, 0, metrics.ResourceMetrics().Len())
require.Equal(t, 1, metrics.ResourceMetrics().Len())
fileCount := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
require.Equal(t, int64(0), fileCount.Gauge().DataPoints().At(0).IntValue())
require.Equal(t, "file.count", fileCount.Name())
logFile := filepath.Join(tmpDir, "my.log")
err = os.WriteFile(logFile, []byte("something"), 0600)
t.Cleanup(func() {
Expand All @@ -71,8 +75,9 @@ func Test_Scrape_All(t *testing.T) {
require.Equal(t, []string{logFile}, matches)
metrics, err = s.scrape(context.Background())
require.NoError(t, err)
require.Equal(t, 1, metrics.ResourceMetrics().Len())
require.Equal(t, 2, metrics.ResourceMetrics().Len())
require.Equal(t, 4, metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().Len())
require.Equal(t, 1, metrics.ResourceMetrics().At(1).ScopeMetrics().At(0).Metrics().Len())
aTimeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
require.Equal(t, "file.atime", aTimeMetric.Name())
require.Equal(t, fileinfo.ModTime().Unix(), aTimeMetric.Sum().DataPoints().At(0).IntValue())
Expand All @@ -85,4 +90,7 @@ func Test_Scrape_All(t *testing.T) {
sizeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(3)
require.Equal(t, "file.size", sizeMetric.Name())
require.Equal(t, int64(9), sizeMetric.Gauge().DataPoints().At(0).IntValue())
fileCount = metrics.ResourceMetrics().At(1).ScopeMetrics().At(0).Metrics().At(0)
require.Equal(t, "file.count", fileCount.Name())
require.Equal(t, int64(1), fileCount.Gauge().DataPoints().At(0).IntValue())
}