Skip to content

refactor: split main otel config into multiple connections, add tests #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/generate-changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser-pro
# 'latest', 'nightly', or a semver
version: 2.7.0
version: 2.8.2
args: changelog
env:
GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
2 changes: 1 addition & 1 deletion .github/workflows/release-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser-pro
version: 2.7.0
version: 2.8.2
args: release --prepare --clean --snapshot --verbose
env:
GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
2 changes: 1 addition & 1 deletion .github/workflows/release-nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser-pro
version: 2.7.0
version: 2.8.2
args: release --clean --skip=validate --verbose --nightly --parallelism 6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
with:
distribution: goreleaser-pro
# 'latest', 'nightly', or a semver
version: 2.7.0
version: 2.8.2
args: release --clean --skip=validate --verbose
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser-pro
version: 2.7.0
version: 2.8.2
args: release --prepare --clean --snapshot --verbose --parallelism 6
env:
GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/vuln-check-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser-pro
version: 2.7.0
version: 2.8.2
args: build --snapshot --id=default --skip=validate --single-target
env:
GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/vuln-check-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser-pro
version: 2.7.0
version: 2.8.2
args: build --snapshot --id=default --skip=validate --single-target
env:
GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
Expand Down
32 changes: 13 additions & 19 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ before:
- go generate ./...

builds:
- id: default
- id: linux_build
binary: observe-agent
env:
- CGO_ENABLED=0
Expand Down Expand Up @@ -44,10 +44,9 @@ builds:
ldflags: -s -w -X github.com/observeinc/observe-agent/build.Version={{.Version}}

archives:
- id: default
- id: linux
formats: ["tar.gz"]
builds:
- default
ids: [linux_build]
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
Expand All @@ -57,15 +56,15 @@ archives:
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
files:
- src: "packaging/linux/etc/observe-agent/*"
dst: "."
# The default agent config file and the service file live in config.
- src: "packaging/linux/config/*"
dst: "."
- src: "packaging/linux/connections/*"
dst: "connections"

- id: windows
formats: ["zip"]
builds:
- windows_build
ids: [windows_build]
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
Expand All @@ -74,19 +73,14 @@ archives:
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
files:
- src: "packaging/windows/config/otel-collector.yaml"
dst: "otel-collector.yaml"
- src: "packaging/windows/observe-agent.yaml"
dst: "observe-agent.yaml"
- src: "packaging/windows/connections/host_monitoring/*"
dst: "connections/host_monitoring"
- src: "packaging/windows/connections/self_monitoring/*"
dst: "connections/self_monitoring"
- src: "packaging/windows/connections/*"
dst: "connections"

- id: macos
formats: ["zip"]
builds:
- mac_build
ids: [mac_build]
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
Expand Down Expand Up @@ -124,7 +118,7 @@ changelog:
order: 999

nfpms:
- id: default
- id: linux

package_name: observe-agent

Expand Down Expand Up @@ -168,8 +162,8 @@ nfpms:
# Contents to add to the package.
# GoReleaser will automatically add the binaries.
contents:
- src: packaging/linux/etc/
dst: /etc
- src: packaging/linux/connections
dst: /etc/observe-agent/connections
type: tree

- src: packaging/linux/config/observe-agent.yaml
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.7
require (
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/jarcoal/httpmock v1.3.1
github.com/mcuadros/go-defaults v1.2.0
github.com/observeinc/observe-agent/observecol v0.0.0-00010101000000-000000000000
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.62.0
Expand Down
8 changes: 3 additions & 5 deletions integration/scripts/install_windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $temp_dir="C:\temp"
#Create directories for temp & observe-agent installation ls
New-Item -ItemType Directory -Force -Path $temp_dir
New-Item -ItemType Directory -Force -Path $observeagent_install_dir
New-Item -ItemType Directory -Force -Path $observeagent_install_dir\config
New-Item -ItemType Directory -Force -Path $observeagent_install_dir\connections
New-Item -ItemType Directory -Force -Path $program_data_filestorage

# Stop the observe agent if its running so that we can copy the new .exe
Expand All @@ -36,7 +36,5 @@ Expand-Archive -Force -LiteralPath $local_installer -DestinationPath "$temp_dir
Write-Output "Copying files from $temp_dir\observe-agent_extract to $observeagent_install_dir"
Copy-Item -Force -Path $temp_dir\observe-agent_extract\observe-agent.exe -Destination $observeagent_install_dir
Copy-Item -Force -Path $temp_dir\observe-agent_extract\observe-agent.yaml -Destination $observeagent_install_dir
Copy-Item -Force -Path $temp_dir\observe-agent_extract\otel-collector.yaml -Destination $observeagent_install_dir\config\otel-collector.yaml
Copy-Item -Force -Path $temp_dir\observe-agent_extract\connections\ -Destination $observeagent_install_dir\connections -Recurse


Copy-Item -Force -Path $temp_dir\observe-agent_extract\connections\* -Destination $observeagent_install_dir\connections -Recurse
Get-ChildItem -Path $observeagent_install_dir -Recurse
13 changes: 10 additions & 3 deletions integration/scripts/start_agent_windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ if(-not (Get-Service ObserveAgent -ErrorAction SilentlyContinue)){
DisplayName = "Observe Agent"
StartupType = "Automatic"
Description = "Observe Agent based on OpenTelemetry collector"
}
}
$params | Select-Object *
New-Service @params
Write-Output "Starting ObserveAgent Service..."
Start-Service ObserveAgent
try {
Start-Service ObserveAgent -ErrorAction Stop
} catch {
Write-Output "Error starting ObserveAgent service!"
$_ | Select-Object *
# Print the agent config to help debug
&"${observeagent_install_dir}\observe-agent.exe" --observe-config "${observeagent_install_dir}\observe-agent.yaml" config
}
}
else{
Write-Output "ObserveAgent Service already exists, restarting service..."
Stop-Service ObserveAgent
Expand Down
6 changes: 6 additions & 0 deletions internal/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func printAllConfigsIndividually(configFilePaths []string) error {
}

func printShortOtelConfig(ctx context.Context, configFilePaths []string) error {
if len(configFilePaths) == 0 {
return nil
}
settings := observecol.ConfigProviderSettings(configFilePaths)
resolver, err := confmap.NewResolver(settings.ResolverSettings)
if err != nil {
Expand All @@ -107,6 +110,9 @@ func printShortOtelConfig(ctx context.Context, configFilePaths []string) error {
}

func printFullOtelConfig(configFilePaths []string) error {
if len(configFilePaths) == 0 {
return nil
}
colSettings := observecol.GenerateCollectorSettingsWithConfigFiles(configFilePaths)
factories, err := colSettings.Factories()
if err != nil {
Expand Down
37 changes: 26 additions & 11 deletions internal/commands/initconfig/initconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"os"
"testing"

"github.com/go-viper/mapstructure/v2"
"github.com/observeinc/observe-agent/internal/config"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

func Test_InitConfigCommand(t *testing.T) {
Expand Down Expand Up @@ -77,17 +78,31 @@ func Test_InitConfigCommand(t *testing.T) {
if err != nil {
if tc.expectErr == "" {
t.Errorf("Expected no error, got %v", err)
} else {
assert.ErrorContains(t, err, tc.expectErr)
}
}
var config config.AgentConfig
configFile, err := os.ReadFile("./test-config.yaml")
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
assert.Equal(t, tc.expectedConfig, config)

var configYaml, configMapstructure config.AgentConfig

// Decode via mapstructure (which is how viper does it)
configFileContents, err := os.ReadFile("./test-config.yaml")
assert.NoError(t, err)
var yamlMap map[string]any
err = yaml.Unmarshal(configFileContents, &yamlMap)
assert.NoError(t, err)
err = mapstructure.Decode(yamlMap, &configMapstructure)
assert.NoError(t, err)
assert.Equal(t, tc.expectedConfig, configMapstructure)

// Decode via yaml in order to do strict field checks
configFile, err := os.Open("./test-config.yaml")
assert.NoError(t, err)
decoder := yaml.NewDecoder(configFile)
// Ensure that all fields in the output yaml are present in our struct
decoder.KnownFields(true)
err = decoder.Decode(&configYaml)
assert.NoError(t, err)
assert.Equal(t, tc.expectedConfig, configYaml)
}
}
13 changes: 13 additions & 0 deletions internal/config/configschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"strings"

"github.com/mcuadros/go-defaults"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -38,17 +39,29 @@ type SelfMonitoringConfig struct {
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
}

type HealthCheckConfig struct {
Enabled bool `yaml:"enabled" mapstructure:"enabled" default:"true"`
}

type ForwardingConfig struct {
Enabled bool `yaml:"enabled" mapstructure:"enabled" default:"true"`
}

type AgentConfig struct {
Token string `yaml:"token" mapstructure:"token"`
ObserveURL string `yaml:"observe_url" mapstructure:"observe_url"`
CloudResourceDetectors []string `yaml:"cloud_resource_detectors,omitempty" mapstructure:"cloud_resource_detectors"`
Debug bool `yaml:"debug,omitempty" mapstructure:"debug"`
HealthCheck HealthCheckConfig `yaml:"health_check" mapstructure:"health_check"`
Forwarding ForwardingConfig `yaml:"forwarding" mapstructure:"forwarding"`
SelfMonitoring SelfMonitoringConfig `yaml:"self_monitoring,omitempty" mapstructure:"self_monitoring"`
HostMonitoring HostMonitoringConfig `yaml:"host_monitoring,omitempty" mapstructure:"host_monitoring"`
OtelConfigOverrides map[string]any `yaml:"otel_config_overrides,omitempty" mapstructure:"otel_config_overrides"`
}

func AgentConfigFromViper(v *viper.Viper) (*AgentConfig, error) {
var config AgentConfig
defaults.SetDefaults(&config)
err := v.Unmarshal(&config)
if err != nil {
return nil, err
Expand Down
24 changes: 24 additions & 0 deletions internal/config/configschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -43,3 +44,26 @@ func TestAgentConfigValidate(t *testing.T) {
}
assert.ErrorContains(t, invalidTokenConfig.Validate(), "invalid Token")
}

func TestAgentConfigFromViper(t *testing.T) {
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
v.Set("token", "some:token")
v.Set("observe_url", "https://observeinc.com")
v.Set("host_monitoring::enabled", true)
config, err := AgentConfigFromViper(v)
assert.NoError(t, err)
assert.Equal(t, "some:token", config.Token)
assert.Equal(t, "https://observeinc.com", config.ObserveURL)
assert.Equal(t, true, config.HostMonitoring.Enabled)

// Validate that defaults are set when the value is not in the viper config
assert.Equal(t, true, config.HealthCheck.Enabled)
assert.Equal(t, true, config.Forwarding.Enabled)

// Validate that defaults are overridden by present values
v.Set("health_check::enabled", false)
config, err = AgentConfigFromViper(v)
assert.NoError(t, err)
assert.Equal(t, false, config.HealthCheck.Enabled)
assert.Equal(t, true, config.Forwarding.Enabled)
}
Loading
Loading