Skip to content

Commit 37283f0

Browse files
committed
Update file watcher to only monitor directories referenced in the nginx configuration
1 parent 9f24720 commit 37283f0

File tree

17 files changed

+426
-158
lines changed

17 files changed

+426
-158
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/google/uuid v1.6.0
1717
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
1818
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1
19+
github.com/hashicorp/go-multierror v1.1.1
1920
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c
2021
github.com/nginxinc/nginx-plus-go-client/v2 v2.0.1
2122
github.com/nginxinc/nginx-prometheus-exporter v1.3.0
@@ -132,6 +133,7 @@ require (
132133
github.com/gorilla/mux v1.8.1 // indirect
133134
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
134135
github.com/hashicorp/consul/api v1.30.0 // indirect
136+
github.com/hashicorp/errwrap v1.1.0 // indirect
135137
github.com/hashicorp/go-version v1.7.0 // indirect
136138
github.com/hashicorp/golang-lru v1.0.2 // indirect
137139
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ github.com/hashicorp/consul/api v1.30.0 h1:ArHVMMILb1nQv8vZSGIwwQd2gtc+oSQZ6Caly
278278
github.com/hashicorp/consul/api v1.30.0/go.mod h1:B2uGchvaXVW2JhFoS8nqTxMD5PBykr4ebY4JWHTTeLM=
279279
github.com/hashicorp/cronexpr v1.1.2 h1:wG/ZYIKT+RT3QkOdgYc+xsKWVRgnxJ1OJtjjy84fJ9A=
280280
github.com/hashicorp/cronexpr v1.1.2/go.mod h1:P4wA0KBl9C5q2hABiMO7cp6jcIg96CDh1Efb3g1PWA4=
281+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
281282
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
282283
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
283284
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=

internal/collector/containermetricsreceiver/internal/scraper/cpuscraper/internal/cgroup/cpu_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
package cgroup
77

88
import (
9+
"errors"
910
"os"
1011
"path"
1112
"runtime"
1213
"strconv"
1314
"testing"
1415

16+
"github.com/hashicorp/go-multierror"
17+
1518
"github.com/stretchr/testify/assert"
1619
)
1720

@@ -76,7 +79,16 @@ func TestCollectCPUStats(t *testing.T) {
7679
cpuStat, err := cgroupCPUSource.collectCPUStats()
7780

7881
// Assert error
79-
assert.IsType(tt, test.errorType, err)
82+
if err != nil {
83+
var multiError *multierror.Error
84+
if errors.As(err, &multiError) {
85+
assert.IsType(tt, test.errorType, multiError.Errors[0])
86+
} else {
87+
assert.IsType(tt, test.errorType, err)
88+
}
89+
} else {
90+
assert.IsType(tt, test.errorType, err)
91+
}
8092

8193
// Assert result
8294
assert.Equal(tt, test.cpuStat, cpuStat)

internal/collector/containermetricsreceiver/internal/scraper/memoryscraper/internal/cgroup/memory_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ package cgroup
77

88
import (
99
"context"
10+
"errors"
1011
"os"
1112
"path"
1213
"runtime"
1314
"strconv"
1415
"testing"
1516

17+
"github.com/hashicorp/go-multierror"
18+
1619
"github.com/shirou/gopsutil/v4/mem"
1720
"github.com/stretchr/testify/assert"
1821
)
@@ -115,7 +118,16 @@ func TestVirtualMemoryStat(t *testing.T) {
115118
virtualMemoryStat, err := cgroupCPUSource.VirtualMemoryStat()
116119

117120
// Assert error
118-
assert.IsType(tt, test.errorType, err)
121+
if err != nil {
122+
var multiError *multierror.Error
123+
if errors.As(err, &multiError) {
124+
assert.IsType(tt, test.errorType, multiError.Errors[0])
125+
} else {
126+
assert.IsType(tt, test.errorType, err)
127+
}
128+
} else {
129+
assert.IsType(tt, test.errorType, err)
130+
}
119131

120132
// Assert result
121133
assert.Equal(tt, test.virtualMemoryStat, *virtualMemoryStat)

internal/collector/containermetricsreceiver/internal/utils.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"os"
1212
"strconv"
1313
"strings"
14+
15+
"github.com/hashicorp/go-multierror"
1416
)
1517

1618
type Source interface {
@@ -22,12 +24,18 @@ func ReadLines(filename string) ([]string, error) {
2224
}
2325

2426
// nolint: revive
25-
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
27+
func ReadLinesOffsetN(filename string, offset uint, n int) (lines []string, err error) {
2628
f, err := os.Open(filename)
29+
defer func() {
30+
closeErr := f.Close()
31+
if closeErr != nil {
32+
err = multierror.Append(err, closeErr)
33+
}
34+
}()
35+
2736
if err != nil {
2837
return []string{}, err
2938
}
30-
defer f.Close()
3139

3240
var ret []string
3341

internal/config/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ func (c *Config) AreReceiversConfigured() bool {
412412
}
413413

414414
func isAllowedDir(dir string, allowedDirs []string) bool {
415+
if !strings.HasSuffix(dir, "/") && filepath.Ext(dir) == "" {
416+
dir += "/"
417+
}
418+
415419
for _, allowedDirectory := range allowedDirs {
416420
if strings.HasPrefix(dir, allowedDirectory) {
417421
return true

internal/config/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ func TestTypes_IsDirectoryAllowed(t *testing.T) {
2525
allowed: true,
2626
allowedDirs: []string{
2727
AgentDirName,
28-
"/etc/nginx",
28+
"/etc/nginx/",
2929
"/var/log/nginx/",
3030
},
31-
fileDir: "/etc/nginx/nginx.conf",
31+
fileDir: "/etc/nginx",
3232
},
3333
{
3434
name: "Test 2: directory not allowed",

internal/datasource/config/nginx_config_parser.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (ncp *NginxConfigParser) Parse(ctx context.Context, instance *mpi.Instance)
100100
return ncp.createNginxConfigContext(ctx, instance, payload)
101101
}
102102

103-
// nolint: cyclop,revive,gocognit
103+
// nolint: cyclop,revive,gocognit,gocyclo
104104
func (ncp *NginxConfigParser) createNginxConfigContext(
105105
ctx context.Context,
106106
instance *mpi.Instance,
@@ -138,6 +138,10 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
138138
err := ncp.crossplaneConfigTraverse(ctx, &conf,
139139
func(ctx context.Context, parent, directive *crossplane.Directive) error {
140140
switch directive.Directive {
141+
case "include":
142+
include := ncp.parseIncludeDirective(directive)
143+
144+
nginxConfigContext.Includes = append(nginxConfigContext.Includes, include)
141145
case "log_format":
142146
formatMap = ncp.formatMap(directive)
143147
case "access_log":
@@ -214,6 +218,17 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
214218
return nginxConfigContext, nil
215219
}
216220

221+
func (ncp *NginxConfigParser) parseIncludeDirective(directive *crossplane.Directive) string {
222+
var include string
223+
if filepath.IsAbs(directive.Args[0]) {
224+
include = directive.Args[0]
225+
} else {
226+
include = filepath.Join(filepath.Dir(directive.File), directive.Args[0])
227+
}
228+
229+
return include
230+
}
231+
217232
func (ncp *NginxConfigParser) addAccessLog(accessLog *model.AccessLog,
218233
accessLogs []*model.AccessLog,
219234
) []*model.AccessLog {

internal/datasource/host/info.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,17 @@ func (i *Info) containerID() string {
205205
// mountInfo is the path: "/proc/self/mountinfo"
206206
func containerIDFromMountInfo(mountInfo string) (string, error) {
207207
mInfoFile, err := os.Open(mountInfo)
208-
if err != nil {
209-
return "", fmt.Errorf("could not read %s: %w", mountInfo, err)
210-
}
211208
defer func(f *os.File, fileName string) {
212209
closeErr := f.Close()
213210
if closeErr != nil {
214211
slog.Error("Unable to close file", "file", fileName, "error", closeErr)
215212
}
216213
}(mInfoFile, mountInfo)
217214

215+
if err != nil {
216+
return "", fmt.Errorf("could not read %s: %w", mountInfo, err)
217+
}
218+
218219
fileScanner := bufio.NewScanner(mInfoFile)
219220
fileScanner.Split(bufio.ScanLines)
220221

@@ -308,15 +309,15 @@ func (i *Info) releaseInfo(ctx context.Context, osReleaseLocation string) (relea
308309

309310
func readOsRelease(path string) (map[string]string, error) {
310311
f, err := os.Open(path)
311-
if err != nil {
312-
return nil, fmt.Errorf("release file %s is unreadable: %w", path, err)
313-
}
314312
defer func(f *os.File, fileName string) {
315313
closeErr := f.Close()
316314
if closeErr != nil {
317315
slog.Error("Unable to close file", "file", fileName, "error", closeErr)
318316
}
319317
}(f, path)
318+
if err != nil {
319+
return nil, fmt.Errorf("release file %s is unreadable: %w", path, err)
320+
}
320321

321322
info, err := parseOsReleaseFile(f)
322323
if err != nil {

internal/file/file_manager_service.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,15 +854,16 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.
854854

855855
// 0600 ensures only root can read/write
856856
newFile, err := os.OpenFile(fms.manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
857-
if err != nil {
858-
return fmt.Errorf("failed to read manifest file: %w", err)
859-
}
860857
defer func() {
861858
if closeErr := newFile.Close(); closeErr != nil {
862859
writeError = closeErr
863860
}
864861
}()
865862

863+
if err != nil {
864+
return fmt.Errorf("failed to read manifest file: %w", err)
865+
}
866+
866867
_, err = newFile.Write(manifestJSON)
867868
if err != nil {
868869
return fmt.Errorf("failed to write manifest file: %w", err)

0 commit comments

Comments
 (0)