Skip to content

Commit

Permalink
feat: add send_hostname configuration option
Browse files Browse the repository at this point in the history
And its HOST_METERING_SEND_HOSTNAME env var couterpart.

Possible values and meaning:
- yes: display_name label should be sent
- no: display_name label should not be sent
- undefined or unexpected value: display_name label should be sent

https://issues.redhat.com/browse/HMS-3264

Signed-off-by: Petr Vobornik <[email protected]>
  • Loading branch information
pvoborni committed Dec 18, 2023
1 parent 698ff05 commit 5c2ce20
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"time"
)

const (
SendHostnameYes = "yes"
SendHostnameNo = "no"
)

const (
DefaultConfigPath = "/etc/host-metering.conf"
DefaultWriteUrl = "http://localhost:9090/api/v1/write"
Expand All @@ -17,6 +22,7 @@ const (
DefaultKeyPath = "/etc/pki/consumer/key.pem"
DefaultCollectInterval = 0 * time.Second
DefaultLabelRefreshInterval = 86400 * time.Second
DefaultSendHostname = SendHostnameYes
DefaultWriteRetryAttempts = 8
DefaultWriteRetryMinInt = 1 * time.Second
DefaultWriteRetryMaxInt = 10 * time.Second
Expand All @@ -33,6 +39,7 @@ type Config struct {
WriteInterval time.Duration
CollectInterval time.Duration
LabelRefreshInterval time.Duration
SendHostname string
HostCertPath string
HostCertKeyPath string
WriteRetryAttempts uint
Expand All @@ -54,6 +61,7 @@ func NewConfig() *Config {
HostCertKeyPath: DefaultKeyPath,
CollectInterval: DefaultCollectInterval,
LabelRefreshInterval: DefaultLabelRefreshInterval,
SendHostname: DefaultSendHostname,
WriteRetryAttempts: DefaultWriteRetryAttempts,
WriteRetryMinInt: DefaultWriteRetryMinInt,
WriteRetryMaxInt: DefaultWriteRetryMaxInt,
Expand All @@ -76,6 +84,7 @@ func (c *Config) String() string {
fmt.Sprintf("| HostCertKeyPath: %s", c.HostCertKeyPath),
fmt.Sprintf("| CollectIntervalSec: %.0f", c.CollectInterval.Seconds()),
fmt.Sprintf("| LabelRefreshIntervalSec: %.0f", c.LabelRefreshInterval.Seconds()),
fmt.Sprintf("| SendHostname: %s", c.SendHostname),
fmt.Sprintf("| WriteRetryAttempts: %d", c.WriteRetryAttempts),
fmt.Sprintf("| WriteRetryMinIntSec: %.0f", c.WriteRetryMinInt.Seconds()),
fmt.Sprintf("| WriteRetryMaxIntSec: %.0f", c.WriteRetryMaxInt.Seconds()),
Expand Down Expand Up @@ -113,6 +122,9 @@ func (c *Config) UpdateFromEnvVars() error {
c.LabelRefreshInterval, err = parseSeconds("HOST_METERING_LABEL_REFRESH_INTERVAL_SEC", v, c.LabelRefreshInterval)
multiError.Add(err)
}
if v := os.Getenv("HOST_METERING_SEND_HOSTNAME"); v != "" {
c.SendHostname = v
}
if v := os.Getenv("HOST_METERING_WRITE_RETRY_ATTEMPTS"); v != "" {
c.WriteRetryAttempts, err = parseUint("HOST_METERING_WRITE_RETRY_ATTEMPTS", v, c.WriteRetryAttempts)
multiError.Add(err)
Expand Down Expand Up @@ -230,6 +242,9 @@ func (c *Config) UpdateFromConfigFile(path string) error {
c.LabelRefreshInterval, err = parseSeconds("label_refresh_interval_sec", v, c.LabelRefreshInterval)
multiError.Add(err)
}
if v, ok := config[section]["send_hostname"]; ok {
c.SendHostname = v
}
if v, ok := config[section]["write_retry_attempts"]; ok {
c.WriteRetryAttempts, err = parseUint("write_retry_attempts", v, c.WriteRetryAttempts)
multiError.Add(err)
Expand Down
6 changes: 6 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestDefaultConfig(t *testing.T) {
"| HostCertKeyPath: /etc/pki/consumer/key.pem\n" +
"| CollectIntervalSec: 0\n" +
"| LabelRefreshIntervalSec: 86400\n" +
"| SendHostname: yes\n" +
"| WriteRetryAttempts: 8\n" +
"| WriteRetryMinIntSec: 1\n" +
"| WriteRetryMaxIntSec: 10\n" +
Expand Down Expand Up @@ -65,6 +66,7 @@ func TestConfigFile(t *testing.T) {
"| HostCertKeyPath: /tmp/key.pem\n" +
"| CollectIntervalSec: 20\n" +
"| LabelRefreshIntervalSec: 300\n" +
"| SendHostname: no\n" +
"| WriteRetryAttempts: 4\n" +
"| WriteRetryMinIntSec: 5\n" +
"| WriteRetryMaxIntSec: 6\n" +
Expand All @@ -85,6 +87,7 @@ func TestConfigFile(t *testing.T) {
"collect_interval_sec = 20\n" +
"; And also these comments.\n" +
"label_refresh_interval_sec = 300\n" +
"send_hostname = no\n" +
"write_retry_attempts = 4\n" +
"write_retry_min_int_sec = 5\n" +
"write_retry_max_int_sec = 6\n" +
Expand Down Expand Up @@ -144,6 +147,7 @@ func TestEnvVariables(t *testing.T) {
"| HostCertKeyPath: /tmp/key.pem\n" +
"| CollectIntervalSec: 20\n" +
"| LabelRefreshIntervalSec: 300\n" +
"| SendHostname: no\n" +
"| WriteRetryAttempts: 4\n" +
"| WriteRetryMinIntSec: 5\n" +
"| WriteRetryMaxIntSec: 6\n" +
Expand All @@ -160,6 +164,7 @@ func TestEnvVariables(t *testing.T) {
t.Setenv("HOST_METERING_HOST_CERT_PATH", "/tmp/cert.pem")
t.Setenv("HOST_METERING_HOST_CERT_KEY_PATH", "/tmp/key.pem")
t.Setenv("HOST_METERING_COLLECT_INTERVAL_SEC", "20")
t.Setenv("HOST_METERING_SEND_HOSTNAME", "no")
t.Setenv("HOST_METERING_LABEL_REFRESH_INTERVAL_SEC", "300")
t.Setenv("HOST_METERING_WRITE_RETRY_ATTEMPTS", "4")
t.Setenv("HOST_METERING_WRITE_RETRY_MIN_INT_SEC", "5")
Expand Down Expand Up @@ -214,6 +219,7 @@ func clearEnvironment() {
_ = os.Unsetenv("HOST_METERING_HOST_CERT_PATH")
_ = os.Unsetenv("HOST_METERING_HOST_CERT_KEY_PATH")
_ = os.Unsetenv("HOST_METERING_COLLECT_INTERVAL_SEC")
_ = os.Unsetenv("HOST_METERING_SEND_HOSTNAME")
_ = os.Unsetenv("HOST_METERING_LABEL_REFRESH_INTERVAL_SEC")
_ = os.Unsetenv("HOST_METERING_WRITE_RETRY_ATTEMPTS")
_ = os.Unsetenv("HOST_METERING_WRITE_RETRY_MIN_INT_SEC")
Expand Down
3 changes: 3 additions & 0 deletions contrib/man/host-metering.1
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Interval between collecting host metrics in seconds.
\fBHOST_METERING_LABEL_REFRESH_INTERVAL_SEC\fR
Interval between refreshing host labels in seconds.

\fBHOST_METERING_SEND_HOSTNAME\fR
Send hostname to remote server. By default \fByes\fR set to \fBno\fR to disable.

\fBHOST_METERING_WRITE_RETRY_ATTEMPTS\fR
Number of write attempts to remote server.

Expand Down
6 changes: 6 additions & 0 deletions contrib/man/host-metering.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ label_refresh_interval_sec (integer)
Interval between refreshing host labels in seconds.
.RE

.PP
send_hostname (string)
.RS 4
Send hostname to remote server. By default \fByes\fR set to \fBno\fR to disable.
.RE

.PP
write_retry_attempts (integer)
.RS 4
Expand Down

0 comments on commit 5c2ce20

Please sign in to comment.