Skip to content

Commit 2644953

Browse files
feat: add config option to set service explorer attributes
1 parent 82181fe commit 2644953

37 files changed

+617
-94
lines changed

integration/scripts/test_configure.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22
import utils as u
33

44

5-
@u.print_test_decorator
6-
def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
7-
init_command = r'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent init-config --token {} --observe_url {} --cloud_resource_detectors ec2'.format(
5+
def init_config_command(env_vars: dict) -> str:
6+
return "init-config --token {} --observe_url {} --cloud_resource_detectors ec2 --resource_attributes deployment.environment=test".format(
87
env_vars["observe_token"], env_vars["observe_url"]
98
)
109

10+
11+
def run_init_config_common(remote_host: u.Host, init_command: str) -> None:
12+
# Print the config to be used first
13+
result = remote_host.run_command(init_command + " --print")
14+
print("Setting agent config:\n{}\n".format("=" * 21))
15+
u.print_remote_result(result)
16+
if result.exited != 0 or result.stderr:
17+
raise ValueError("❌ Error in init-config print")
18+
1119
# Set up correct config with observe url and token
1220
result = remote_host.run_command(init_command)
1321
if result.exited != 0 or result.stderr:
@@ -16,30 +24,25 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
1624

1725

1826
@u.print_test_decorator
19-
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
20-
docker_prefix = u.get_docker_prefix(remote_host, False)
21-
init_command = "{} init-config --token {} --observe_url {} --cloud_resource_detectors ec2".format(
22-
docker_prefix, env_vars["observe_token"], env_vars["observe_url"]
27+
def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
28+
init_command = (
29+
r'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent '
30+
+ init_config_command(env_vars)
2331
)
32+
run_init_config_common(remote_host, init_command)
2433

25-
# Set up correct config with observe url and token
26-
result = remote_host.run_command(init_command)
27-
if result.exited != 0 or result.stderr:
28-
u.print_remote_result(result)
29-
raise ValueError("❌ Error in init-config")
34+
35+
@u.print_test_decorator
36+
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
37+
docker_prefix = u.get_docker_prefix(remote_host, False)
38+
init_command = docker_prefix + " " + init_config_command(env_vars)
39+
run_init_config_common(remote_host, init_command)
3040

3141

3242
@u.print_test_decorator
3343
def run_test_linux(remote_host: u.Host, env_vars: dict) -> None:
34-
init_command = "sudo observe-agent init-config --token {} --observe_url {} --cloud_resource_detectors ec2".format(
35-
env_vars["observe_token"], env_vars["observe_url"]
36-
)
37-
38-
# Set up correct config with observe url and token
39-
result = remote_host.run_command(init_command)
40-
if result.exited != 0 or result.stderr:
41-
u.print_remote_result(result)
42-
raise ValueError("❌ Error in init-config")
44+
init_command = "sudo observe-agent " + init_config_command(env_vars)
45+
run_init_config_common(remote_host, init_command)
4346

4447

4548
if __name__ == "__main__":

integration/scripts/test_start.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ def _check_status_loop(
2121
Returns:
2222
bool: agent_status
2323
"""
24-
24+
sleep_seconds = 1.5
2525
agent_status = False
26+
time.sleep(sleep_seconds)
2627
for _ in range(start_timeout):
2728
metrics_dict = defaultdict(list)
2829
try:
2930
result = remote_host.run_command(status_command)
3031
except Exception as e:
3132
print("Ignoring exception: ", e)
32-
time.sleep(1)
33+
time.sleep(sleep_seconds)
3334
continue
3435
for line in result.stdout.splitlines():
3536
if ":" in line:
@@ -47,7 +48,7 @@ def _check_status_loop(
4748
_ + 1, start_timeout
4849
)
4950
)
50-
time.sleep(1)
51+
time.sleep(sleep_seconds)
5152
return agent_status
5253

5354

@@ -65,7 +66,7 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
6566
# status
6667
start_command = r".\start_agent_windows.ps1"
6768
status_command = r'Get-Service ObserveAgent;Set-Location "${Env:Programfiles}\Observe\observe-agent"; ./observe-agent status'
68-
start_timeout = 30 # how long to wait for observe-agent to start
69+
start_timeout = 10 # how many times to check for the running agent
6970

7071
# Get windows home dir paths for consistency
7172
home_dir = r"/C:/Users/{}".format(env_vars["user"]) # for user in sftp
@@ -82,7 +83,7 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
8283
) # Eg: sftp to /C:/Users/Adminstrator/install_windows.ps1
8384
# Run start_agent script
8485
result = remote_host.run_command(start_command)
85-
print(result)
86+
u.print_remote_result(result)
8687

8788
if (
8889
result.stderr
@@ -99,11 +100,12 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
99100
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
100101
docker_prefix = u.get_docker_prefix(remote_host, True)
101102
start_command = "start"
102-
start_timeout = 30 # how long to wait for observe-agent to start
103+
start_timeout = 10 # how many times to check for the running agent
103104

104105
# Start Observe Agent
105106
result = remote_host.run_command(docker_prefix + " " + start_command)
106107
if result.stderr:
108+
u.print_remote_result(result)
107109
u.die("❌ Error starting observe-agent container")
108110
else:
109111
print("✅ Observe Agent started successfully: " + result.stdout)
@@ -131,14 +133,19 @@ def run_test_linux(remote_host: u.Host, env_vars: dict) -> None:
131133

132134
start_command = "sudo systemctl enable --now observe-agent"
133135
status_command = "observe-agent status"
134-
start_timeout = 30 # how long to wait for observe-agent to start
136+
start_timeout = 10 # how many times to check for the running agent
135137

136138
# Start Observe Agent
137-
remote_host.run_command(start_command)
139+
result = remote_host.run_command(start_command)
140+
u.print_remote_result(result)
138141

139142
# Check Agent Status
140143
agent_status = _check_status_loop(remote_host, start_timeout, status_command)
141144
if not agent_status:
145+
# If the agent never started up, try running start to see what the error is. Use unsafe because we expect a non-zero exit code.
146+
u.print_remote_result(
147+
remote_host.run_command_unsafe("timeout 10s sudo observe-agent start")
148+
)
142149
u.die("❌ Error in Observe Agent Status Test ")
143150

144151

integration/scripts/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,7 @@ def _get_connection(self) -> Connection:
168168
)
169169

170170
def run_command(self, command) -> Result:
171-
try:
172-
with self._get_connection() as connection:
173-
print("Running `{0}` on {1}".format(command, self.host_ip))
174-
result = connection.run(command, warn=True, hide=True)
175-
except (socket_error, AuthenticationException) as exc:
176-
self._raise_authentication_err(exc)
171+
result = self.run_command_unsafe(command)
177172

178173
if result.failed:
179174
raise ExampleException(
@@ -188,6 +183,16 @@ def run_command(self, command) -> Result:
188183

189184
return result
190185

186+
def run_command_unsafe(self, command) -> Result:
187+
try:
188+
with self._get_connection() as connection:
189+
print("Running `{0}` on {1}".format(command, self.host_ip))
190+
result = connection.run(command, warn=True, hide=True)
191+
except (socket_error, AuthenticationException) as exc:
192+
self._raise_authentication_err(exc)
193+
194+
return result
195+
191196
def put_file(self, local_path, remote_path) -> None:
192197
try:
193198
with self._get_connection() as connection:

internal/commands/initconfig/initconfig.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
token string
1919
observe_url string
2020
cloud_resource_detectors []string
21+
resource_attributes map[string]string
2122
self_monitoring_enabled bool
2223
host_monitoring_enabled bool
2324
host_monitoring_logs_enabled bool
@@ -83,6 +84,9 @@ func RegisterConfigFlags(cmd *cobra.Command, v *viper.Viper) {
8384
cmd.PersistentFlags().StringSliceVar(&cloud_resource_detectors, "cloud_resource_detectors", []string{}, "The cloud environments from which to detect resources")
8485
v.BindPFlag("cloud_resource_detectors", cmd.PersistentFlags().Lookup("cloud_resource_detectors"))
8586

87+
cmd.PersistentFlags().StringToStringVar(&resource_attributes, "resource_attributes", map[string]string{}, "The cloud environments from which to detect resources")
88+
v.BindPFlag("resource_attributes", cmd.PersistentFlags().Lookup("resource_attributes"))
89+
8690
cmd.PersistentFlags().BoolVar(&self_monitoring_enabled, "self_monitoring::enabled", true, "Enable self monitoring")
8791
v.BindPFlag("self_monitoring::enabled", cmd.PersistentFlags().Lookup("self_monitoring::enabled"))
8892
v.SetDefault("self_monitoring::enabled", true)

internal/config/configschema.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type AgentConfig struct {
7373
ObserveURL string `yaml:"observe_url" mapstructure:"observe_url"`
7474
CloudResourceDetectors []string `yaml:"cloud_resource_detectors,omitempty" mapstructure:"cloud_resource_detectors"`
7575
Debug bool `yaml:"debug,omitempty" mapstructure:"debug"`
76+
Attributes map[string]string `yaml:"attributes,omitempty" mapstructure:"attributes"`
77+
ResourceAttributes map[string]string `yaml:"resource_attributes,omitempty" mapstructure:"resource_attributes"`
7678
HealthCheck HealthCheckConfig `yaml:"health_check" mapstructure:"health_check"`
7779
Forwarding ForwardingConfig `yaml:"forwarding" mapstructure:"forwarding"`
7880
InternalTelemetry InternalTelemetryConfig `yaml:"internal_telemetry" mapstructure:"internal_telemetry"`
@@ -81,6 +83,14 @@ type AgentConfig struct {
8183
OtelConfigOverrides map[string]any `yaml:"otel_config_overrides,omitempty" mapstructure:"otel_config_overrides"`
8284
}
8385

86+
func (config *AgentConfig) HasAttributes() bool {
87+
return len(config.Attributes) > 0
88+
}
89+
90+
func (config *AgentConfig) HasResourceAttributes() bool {
91+
return len(config.ResourceAttributes) > 0
92+
}
93+
8494
func SetViperDefaults(v *viper.Viper, separator string) {
8595
var config AgentConfig
8696
defaults.SetDefaults(&config)

internal/connections/allconnectiontypes.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ var CommonConnectionType = MakeConnectionType(
2323
},
2424
colConfigFilePath: "base.yaml.tmpl",
2525
},
26+
{
27+
enabledCheck: func(agentConfig *config.AgentConfig) bool {
28+
return agentConfig.HasAttributes() || agentConfig.HasResourceAttributes()
29+
},
30+
colConfigFilePath: "attributes.yaml.tmpl",
31+
},
2632
{
2733
enabledCheck: func(agentConfig *config.AgentConfig) bool {
2834
return agentConfig.Forwarding.Enabled
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
processors:
2+
{{- if .HasAttributes }}
3+
attributes/observe_global_attributes:
4+
actions:
5+
{{- range $key, $value := .Attributes }}
6+
- key: {{ $key }}
7+
value: {{ $value }}
8+
action: insert
9+
{{- end }}
10+
{{- end }}
11+
{{- if .HasResourceAttributes }}
12+
resource/observe_global_resource_attributes:
13+
attributes:
14+
{{- range $key, $value := .ResourceAttributes }}
15+
- key: {{ $key }}
16+
value: {{ $value }}
17+
action: insert
18+
{{- end }}
19+
{{- end }}

packaging/docker/observe-agent/connections/common/base.yaml.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ processors:
3838
host.id:
3939
enabled: false
4040
os.type:
41-
enabled: true
41+
enabled: true
4242
host.arch:
4343
enabled: true
4444
host.name:

packaging/docker/observe-agent/connections/common/forward.yaml.tmpl

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,41 @@ service:
2323
pipelines:
2424
metrics/forward:
2525
receivers: [otlp]
26-
processors: [resourcedetection, resourcedetection/cloud, deltatocumulative, batch]
26+
processors:
27+
- resourcedetection
28+
- resourcedetection/cloud
29+
- deltatocumulative
30+
{{- if .HasAttributes }}
31+
- attributes/observe_global_attributes
32+
{{- end }}
33+
{{- if .HasResourceAttributes }}
34+
- resource/observe_global_resource_attributes
35+
{{- end }}
36+
- batch
2737
exporters: [prometheusremotewrite/observe]
2838

2939
logs/forward:
3040
receivers: [otlp]
31-
processors: [resourcedetection, resourcedetection/cloud]
41+
processors:
42+
- resourcedetection
43+
- resourcedetection/cloud
44+
{{- if .HasAttributes }}
45+
- attributes/observe_global_attributes
46+
{{- end }}
47+
{{- if .HasResourceAttributes }}
48+
- resource/observe_global_resource_attributes
49+
{{- end }}
3250
exporters: [otlphttp/observe, count]
3351

3452
traces/forward:
3553
receivers: [otlp]
36-
processors: [resourcedetection, resourcedetection/cloud]
54+
processors:
55+
- resourcedetection
56+
- resourcedetection/cloud
57+
{{- if .HasAttributes }}
58+
- attributes/observe_global_attributes
59+
{{- end }}
60+
{{- if .HasResourceAttributes }}
61+
- resource/observe_global_resource_attributes
62+
{{- end }}
3763
exporters: [otlphttp/observetracing]

packaging/docker/observe-agent/connections/host_monitoring/host.yaml.tmpl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ receivers:
88
service:
99
pipelines:
1010
metrics/agent-filestats:
11-
receivers: [filestats/agent]
12-
processors: [resourcedetection, resourcedetection/cloud]
13-
exporters: [prometheusremotewrite/observe]
11+
receivers: [filestats/agent]
12+
processors:
13+
- memory_limiter
14+
- resourcedetection
15+
- resourcedetection/cloud
16+
{{- if .HasAttributes }}
17+
- attributes/observe_global_attributes
18+
{{- end }}
19+
{{- if .HasResourceAttributes }}
20+
- resource/observe_global_resource_attributes
21+
{{- end }}
22+
- batch
23+
exporters: [prometheusremotewrite/observe]

0 commit comments

Comments
 (0)