Skip to content

Commit 2d63f6c

Browse files
committed
PR feedback
1 parent 4a3b815 commit 2d63f6c

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

internal/config/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ func ResolveConfig() (*Config, error) {
129129
}
130130

131131
func checkCollectorConfiguration(collector *Collector, config *Config) {
132-
if len(collector.Exporters.OtlpExporters) == 0 && collector.Exporters.PrometheusExporter == nil &&
133-
collector.Exporters.Debug == nil && config.IsGrpcClientConfigured() && config.IsAuthConfigured() &&
132+
if isOTelExporterConfigured(collector) && config.IsGrpcClientConfigured() && config.IsAuthConfigured() &&
134133
config.IsTLSConfigured() {
135134
slog.Info("No collector configuration found in NGINX Agent config, command server configuration found." +
136135
"Using default collector configuration")
@@ -141,7 +140,8 @@ func checkCollectorConfiguration(collector *Collector, config *Config) {
141140
func defaultCollector(collector *Collector, config *Config) {
142141
token := config.Command.Auth.Token
143142
if config.Command.Auth.TokenPath != "" {
144-
pathToken, err := file.RetrieveTokenFromFile(config.Command.Auth.TokenPath)
143+
slog.Debug("Reading token from file", "path", config.Command.Auth.TokenPath)
144+
pathToken, err := file.ReadFromFile(config.Command.Auth.TokenPath)
145145
if err != nil {
146146
slog.Error("Error adding token to default collector, "+
147147
"default collector configuration not started", "error", err)
@@ -970,3 +970,8 @@ func resolveMapStructure(key string, object any) error {
970970

971971
return nil
972972
}
973+
974+
func isOTelExporterConfigured(collector *Collector) bool {
975+
return len(collector.Exporters.OtlpExporters) == 0 && collector.Exporters.PrometheusExporter == nil &&
976+
collector.Exporters.Debug == nil
977+
}

internal/datasource/file/file.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ import (
99
"bytes"
1010
"errors"
1111
"fmt"
12-
"log/slog"
1312
"os"
1413
)
1514

16-
func RetrieveTokenFromFile(path string) (string, error) {
15+
// ReadFromFile reads the contents from a file, trims the white space, trims newlines
16+
// then returns the contents as a string
17+
func ReadFromFile(path string) (string, error) {
1718
if path == "" {
18-
return "", errors.New("token file path is empty")
19+
return "", errors.New("failed to read file since file path is empty")
1920
}
2021

21-
slog.Debug("Reading token from file", "path", path)
22-
var keyVal string
23-
keyBytes, err := os.ReadFile(path)
22+
var content string
23+
contentBytes, err := os.ReadFile(path)
2424
if err != nil {
25-
return "", fmt.Errorf("unable to read token from file: %w", err)
25+
return "", fmt.Errorf("unable to read from file: %w", err)
2626
}
2727

28-
keyBytes = bytes.TrimSpace(keyBytes)
29-
keyBytes = bytes.TrimRight(keyBytes, "\n")
30-
keyVal = string(keyBytes)
28+
contentBytes = bytes.TrimSpace(contentBytes)
29+
contentBytes = bytes.TrimRight(contentBytes, "\n")
30+
content = string(contentBytes)
3131

32-
if keyVal == "" {
33-
return "", errors.New("failed to load token, token file is empty")
32+
if content == "" {
33+
return "", errors.New("failed to read from file, file is empty")
3434
}
3535

36-
return keyVal, nil
36+
return content, nil
3737
}

internal/datasource/file/file_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func Test_RetrieveTokenFromFile(t *testing.T) {
4444
createToken: false,
4545
path: "",
4646
expected: "",
47-
expectedErrMsg: "token file path is empty",
47+
expectedErrMsg: "failed to read file since file path is empty",
4848
},
4949
}
5050
for _, tt := range tests {
@@ -54,7 +54,7 @@ func Test_RetrieveTokenFromFile(t *testing.T) {
5454
require.NoError(t, writeErr)
5555
}
5656

57-
token, err := RetrieveTokenFromFile(tt.path)
57+
token, err := ReadFromFile(tt.path)
5858
if err != nil {
5959
assert.Equal(t, tt.expectedErrMsg, err.Error())
6060
}

internal/grpc/grpc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ func addPerRPCCredentials(agentConfig *config.Config, resourceID string, opts []
243243
token := agentConfig.Command.Auth.Token
244244

245245
if agentConfig.Command.Auth.TokenPath != "" {
246-
tk, err := file.RetrieveTokenFromFile(agentConfig.Command.Auth.TokenPath)
246+
slog.Debug("Reading token from file", "path", agentConfig.Command.Auth.TokenPath)
247+
tk, err := file.ReadFromFile(agentConfig.Command.Auth.TokenPath)
247248
if err == nil {
248249
token = tk
249250
} else {

0 commit comments

Comments
 (0)