scraper_test.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package filestatsreceiver
  4. import (
  5. "context"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/bmatcuk/doublestar/v4"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/receiver/receivertest"
  12. )
  13. func Test_Scrape(t *testing.T) {
  14. tmpDir := t.TempDir()
  15. cfg := newDefaultConfig().(*Config)
  16. cfg.Include = filepath.Join(tmpDir, "*.log")
  17. s := newScraper(cfg, receivertest.NewNopCreateSettings())
  18. metrics, err := s.scrape(context.Background())
  19. require.NoError(t, err)
  20. require.Equal(t, 0, metrics.ResourceMetrics().Len())
  21. logFile := filepath.Join(tmpDir, "my.log")
  22. err = os.WriteFile(logFile, []byte("something"), 0600)
  23. t.Cleanup(func() {
  24. _ = os.Remove(tmpDir)
  25. })
  26. require.NoError(t, err)
  27. fileinfo, err := os.Stat(logFile)
  28. require.NoError(t, err)
  29. require.Equal(t, int64(9), fileinfo.Size())
  30. matches, err := doublestar.FilepathGlob(cfg.Include)
  31. require.NoError(t, err)
  32. require.Equal(t, []string{logFile}, matches)
  33. metrics, err = s.scrape(context.Background())
  34. require.NoError(t, err)
  35. require.Equal(t, 1, metrics.ResourceMetrics().Len())
  36. require.Equal(t, 2, metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().Len())
  37. mTimeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
  38. require.Equal(t, "file.mtime", mTimeMetric.Name())
  39. require.Equal(t, fileinfo.ModTime().Unix(), mTimeMetric.Sum().DataPoints().At(0).IntValue())
  40. sizeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(1)
  41. require.Equal(t, "file.size", sizeMetric.Name())
  42. require.Equal(t, int64(9), sizeMetric.Gauge().DataPoints().At(0).IntValue())
  43. }
  44. func Test_Scrape_All(t *testing.T) {
  45. tmpDir := t.TempDir()
  46. cfg := newDefaultConfig().(*Config)
  47. cfg.Include = filepath.Join(tmpDir, "*.log")
  48. cfg.Metrics.FileAtime.Enabled = true
  49. cfg.Metrics.FileCtime.Enabled = true
  50. s := newScraper(cfg, receivertest.NewNopCreateSettings())
  51. metrics, err := s.scrape(context.Background())
  52. require.NoError(t, err)
  53. require.Equal(t, 0, metrics.ResourceMetrics().Len())
  54. logFile := filepath.Join(tmpDir, "my.log")
  55. err = os.WriteFile(logFile, []byte("something"), 0600)
  56. t.Cleanup(func() {
  57. _ = os.Remove(tmpDir)
  58. })
  59. require.NoError(t, err)
  60. fileinfo, err := os.Stat(logFile)
  61. require.NoError(t, err)
  62. require.Equal(t, int64(9), fileinfo.Size())
  63. matches, err := doublestar.FilepathGlob(cfg.Include)
  64. require.NoError(t, err)
  65. require.Equal(t, []string{logFile}, matches)
  66. metrics, err = s.scrape(context.Background())
  67. require.NoError(t, err)
  68. require.Equal(t, 1, metrics.ResourceMetrics().Len())
  69. require.Equal(t, 4, metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().Len())
  70. aTimeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
  71. require.Equal(t, "file.atime", aTimeMetric.Name())
  72. require.Equal(t, fileinfo.ModTime().Unix(), aTimeMetric.Sum().DataPoints().At(0).IntValue())
  73. cTimeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(1)
  74. require.Equal(t, "file.ctime", cTimeMetric.Name())
  75. require.Equal(t, fileinfo.ModTime().Unix(), cTimeMetric.Sum().DataPoints().At(0).IntValue())
  76. mTimeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(2)
  77. require.Equal(t, "file.mtime", mTimeMetric.Name())
  78. require.Equal(t, fileinfo.ModTime().Unix(), mTimeMetric.Sum().DataPoints().At(0).IntValue())
  79. sizeMetric := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(3)
  80. require.Equal(t, "file.size", sizeMetric.Name())
  81. require.Equal(t, int64(9), sizeMetric.Gauge().DataPoints().At(0).IntValue())
  82. }