Skip to content
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test:

.PHONY: proto
proto:
test -z $(protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative api/api.proto >/dev/null 2>&1 || echo 1) || (echo "[WARN] Fix proto generation issues" && exit 1)
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative api/api.proto

.PHONY: dist
dist:
Expand Down
30 changes: 24 additions & 6 deletions api/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ message RegisterAgentRequest {
bool metrics = 4;
repeated string metrics_targets = 5;
bool profiles = 6;
string node = 7;
}

message RegisterAgentResponse {
Expand All @@ -53,6 +54,7 @@ message GetAgentResponse {
repeated string metrics_targets = 6;
bool profiles = 7;
string created_at = 8;
string node = 9;
}

message ListAgentsRequest {}
Expand Down
1 change: 1 addition & 0 deletions internal/controller/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Agent struct {
Metrics bool `json:"metrics"`
MetricsTargets []string `json:"metrics_targets"`
Profiles bool `json:"profiles"`
Node string `json:"node"`
}

func (c *Controller) RegisterAgent(data *Agent) (string, error) {
Expand Down
30 changes: 30 additions & 0 deletions internal/controller/agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ loki.source.file "file" {
}
{{ end -}}

{{ if .LogSources.Events }}
{{ range $index, $source := .LogSources.Events }}
loki.source.windowsevent "event_{{ $index }}" {
eventlog_name = "{{ $source }}"
forward_to = [loki.write.default.receiver]
}
{{ end -}}
{{ end -}}

{{ if .Metrics }}

prometheus.remote_write "default" {
Expand All @@ -151,6 +160,7 @@ prometheus.remote_write "default" {
}
}

{{ if eq .Node "unix" }}
prometheus.exporter.unix "node" {
include_exporter_metrics = true
enable_collectors = [
Expand All @@ -163,6 +173,18 @@ prometheus.scrape "node" {
forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s"
}
{{ end -}}

{{ if eq .Node "windows" }}
prometheus.exporter.windows "node" {}

prometheus.scrape "node" {
targets = prometheus.exporter.windows.node.targets
forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s"
}
{{ end -}}


prometheus.receive_http "default" {
http {
Expand Down Expand Up @@ -220,13 +242,15 @@ pyroscope.write "backend" {
type alloyConfigData struct {
ServiceName string
Hostname string
Node string
Token string
TokenExpiry string
ResourceId string
LogSources struct {
Journal bool
Docker bool
Files []string
Events []string
}
Metrics bool
MetricsTargets []struct {
Expand Down Expand Up @@ -255,6 +279,7 @@ func (c *Controller) generateAlloyConfig(agent *model.Agent) (*alloyConfigData,

data := &alloyConfigData{
Hostname: agent.Hostname,
Node: agent.Node,
ServiceName: c.config.Hostname(),
Token: token,
TokenExpiry: expiresAt.Format("2006-01-02 15:04:05 MST"),
Expand All @@ -263,10 +288,12 @@ func (c *Controller) generateAlloyConfig(agent *model.Agent) (*alloyConfigData,
Journal bool
Docker bool
Files []string
Events []string
}{
Journal: false,
Docker: false,
Files: make([]string, 0),
Events: make([]string, 0),
},
Metrics: agent.Metrics,
MetricsTargets: make([]struct {
Expand All @@ -291,6 +318,9 @@ func (c *Controller) generateAlloyConfig(agent *model.Agent) (*alloyConfigData,
case "file":
files = append(files, fmt.Sprintf("{__path__ = \"%s\"}", uri.Path))
data.LogSources.Files = files
case "event":
events := append(data.LogSources.Events, uri.Host)
data.LogSources.Events = events
default:
continue
}
Expand Down
7 changes: 6 additions & 1 deletion internal/controller/agent_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (c *Controller) marshalNewAgent(data *Agent) (*model.Agent, error) {
return nil, fmt.Errorf("hostname must not be empty")
}

if data.Node == "" || !slices.Contains([]string{"windows", "unix"}, data.Node) {
return nil, fmt.Errorf("node must be either 'windows' or 'unix'")
}

effectiveLogSources, err := c.__parseLogSources(data)
if err != nil {
return nil, err
Expand All @@ -27,6 +31,7 @@ func (c *Controller) marshalNewAgent(data *Agent) (*model.Agent, error) {

agent := &model.Agent{
Hostname: data.Hostname,
Node: data.Node,
LogSources: effectiveLogSources,
Metrics: data.Metrics,
MetricsTargets: effectiveMetricsTargets,
Expand Down Expand Up @@ -66,7 +71,7 @@ func (c *Controller) __parseLogSources(data *Agent) ([]string, error) {
if err != nil {
continue
}
if !slices.Contains([]string{"journal", "docker", "file"}, uri.Scheme) {
if !slices.Contains([]string{"journal", "docker", "file", "event"}, uri.Scheme) {
continue
}

Expand Down
17 changes: 16 additions & 1 deletion internal/controller/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Test_RegisterAgentReturnsError_InvalidParameters(t *testing.T) {

data := Agent{
Hostname: "",
Node: "unix",
Labels: nil,
LogSources: nil,
Metrics: false,
Expand All @@ -64,6 +65,11 @@ func Test_RegisterAgentReturnsError_InvalidParameters(t *testing.T) {
_, err = ctrl.RegisterAgent(&data)
expected = "no valid log source specified"
assert.EqualError(t, err, expected, "register agent with invalid log source")

data.Node = "invalid"
_, err = ctrl.RegisterAgent(&data)
expected = "node must be either 'windows' or 'unix'"
assert.EqualError(t, err, expected, "register agent with invalid node type")
}

func Test_RegisterAgentReturnsResourceId(t *testing.T) {
Expand All @@ -74,6 +80,7 @@ func Test_RegisterAgentReturnsResourceId(t *testing.T) {

data := Agent{
Hostname: "test-host",
Node: "unix",
Labels: []string{"key=value", "env=prod"},
LogSources: []string{"file:///var/log/syslog"},
Metrics: false,
Expand Down Expand Up @@ -114,6 +121,7 @@ func Test_DeregisterAgentSucceeds(t *testing.T) {

data := Agent{
Hostname: "test-host",
Node: "unix",
Labels: []string{"key=value"},
LogSources: []string{"file:///var/log/syslog"},
Metrics: false,
Expand Down Expand Up @@ -147,6 +155,7 @@ func Test_CreateAgentConfigReturnsConfig(t *testing.T) {

data := Agent{
Hostname: "test-host",
Node: "unix",
Labels: []string{"key=value", "statement"},
LogSources: []string{"file:///var/log/syslog", "journal://", "docker://"},
Metrics: false,
Expand Down Expand Up @@ -181,6 +190,7 @@ func Test_GetAgentReturnsAgent(t *testing.T) {

data := Agent{
Hostname: "test-host",
Node: "unix",
Labels: []string{"key=value"},
LogSources: []string{"file:///var/log/syslog"},
Metrics: false,
Expand Down Expand Up @@ -215,6 +225,7 @@ func Test_ListAgentsReturnsAgents(t *testing.T) {

data := Agent{
Hostname: "test-host-1",
Node: "unix",
Labels: []string{"key=value"},
LogSources: []string{"file:///var/log/syslog"},
Metrics: false,
Expand All @@ -227,8 +238,9 @@ func Test_ListAgentsReturnsAgents(t *testing.T) {

data = Agent{
Hostname: "test-host-2",
Node: "windows",
Labels: []string{"env=dev"},
LogSources: []string{"file:///var/log/syslog"},
LogSources: []string{"event://System"},
Metrics: false,
MetricsTargets: nil,
Profiles: false,
Expand All @@ -252,6 +264,7 @@ func Test_UpdateAgentReturnsError_AgentNotFound(t *testing.T) {

data := Agent{
Hostname: "non-existent-rid",
Node: "unix",
Labels: []string{"key=value"},
LogSources: []string{"file:///var/log/syslog"},
Metrics: false,
Expand All @@ -272,6 +285,7 @@ func Test_UpdateAgentSucceeds(t *testing.T) {

data := Agent{
Hostname: "test-host-update",
Node: "unix",
Labels: []string{"key=value"},
LogSources: []string{"file:///var/log/syslog"},
Metrics: false,
Expand All @@ -284,6 +298,7 @@ func Test_UpdateAgentSucceeds(t *testing.T) {

updatedData := Agent{
Labels: []string{"env=staging"},
Node: "unix",
LogSources: []string{"journal://"},
Metrics: true,
MetricsTargets: []string{"http://localhost:9100/metrics"},
Expand Down
1 change: 1 addition & 0 deletions internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Test_MigrateSucceeds(t *testing.T) {
"registered_at",
"resource_id",
"updated_at",
"node",
}

assert.Equal(t, len(results), len(columns), "agents table should have correct number of columns")
Expand Down
2 changes: 2 additions & 0 deletions internal/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *AgentServer) RegisterAgent(ctx context.Context, req *api.RegisterAgentR
Metrics: req.Metrics,
MetricsTargets: req.MetricsTargets,
Profiles: req.Profiles,
Node: req.Node,
}

rid, err := s.controller.RegisterAgent(agent)
Expand Down Expand Up @@ -122,6 +123,7 @@ func (s *AgentServer) GetAgent(ctx context.Context, req *api.GetAgentRequest) (*
MetricsTargets: agent.MetricsTargets,
Profiles: agent.Profiles,
CreatedAt: agent.CreatedAt.Format(time.RFC3339),
Node: agent.Node,
}, nil
}

Expand Down
Loading