Skip to content

Commit

Permalink
Addressing PR review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed Jan 15, 2025
1 parent 2e663ce commit 5578777
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 42 deletions.
29 changes: 0 additions & 29 deletions internal/runtime/foreach_metrics_test.go

This file was deleted.

33 changes: 25 additions & 8 deletions internal/runtime/foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ func TestForeach(t *testing.T) {
}
}

func TestForeachMetrics(t *testing.T) {
directory := "./testdata/foreach_metrics"
for _, file := range getTestFiles(directory, t) {
tc := buildTestForEach(t, filepath.Join(directory, file.Name()))
t.Run(tc.description, func(t *testing.T) {
if tc.module != "" {
defer os.Remove("module.alloy")
require.NoError(t, os.WriteFile("module.alloy", []byte(tc.module), 0664))
}
if tc.update != nil {
testConfigForEach(t, tc.main, tc.reloadConfig, func() {
require.NoError(t, os.WriteFile(tc.update.name, []byte(tc.update.updateConfig), 0664))
}, tc.expectedMetrics, tc.expectedDurationMetrics)
} else {
testConfigForEach(t, tc.main, tc.reloadConfig, nil, tc.expectedMetrics, tc.expectedDurationMetrics)
}
})
}
}

type testForEachFile struct {
description string // description at the top of the txtar file
main string // root config that the controller should load
Expand Down Expand Up @@ -115,11 +135,9 @@ func testConfigForEach(t *testing.T, config string, reloadConfig string, update
"alloy_component_evaluation_seconds",
}

if countedMetrics, err := testutil.GatherAndCount(reg, metricsToCheck...); err != nil {
require.NoError(t, err)
} else {
require.Equal(t, *expectedDurationMetrics, countedMetrics)
}
countedMetrics, err := testutil.GatherAndCount(reg, metricsToCheck...)
require.NoError(t, err)
require.Equal(t, *expectedDurationMetrics, countedMetrics)
}

if expectedMetrics != nil {
Expand All @@ -132,9 +150,8 @@ func testConfigForEach(t *testing.T, config string, reloadConfig string, update
"pulse_count",
}

if err := testutil.GatherAndCompare(reg, strings.NewReader(*expectedMetrics), metricsToCheck...); err != nil {
require.NoError(t, err)
}
err := testutil.GatherAndCompare(reg, strings.NewReader(*expectedMetrics), metricsToCheck...)
require.NoError(t, err)
}

if update != nil {
Expand Down
10 changes: 7 additions & 3 deletions internal/runtime/internal/controller/node_config_foreach.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ func (fn *ForeachConfigNode) ID() ComponentID {
// Foreach doesn't have the ability to export values.
// This is something we could implement in the future if there is a need for it.
type Arguments struct {
Collection []any `alloy:"collection,attr"`
Var string `alloy:"var,attr"`
EnableMetrics bool `alloy:"enable_metrics,attr,optional"`
Collection []any `alloy:"collection,attr"`
Var string `alloy:"var,attr"`

// enable_metrics should be false by default.
// That way users are protected from an explosion of debug metrics
// if there are many items inside "collection".
EnableMetrics bool `alloy:"enable_metrics,attr,optional"`
}

func (fn *ForeachConfigNode) Evaluate(evalScope *vm.Scope) error {
Expand Down
1 change: 1 addition & 0 deletions internal/runtime/internal/testcomponents/pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (p *Pulse) Run(ctx context.Context) error {
for _, r := range p.cfg.ForwardTo {
r.ReceiveInt(1)
}
p.pulseCount.Inc()
p.count++
}
p.cfgMut.Unlock()
Expand Down
3 changes: 2 additions & 1 deletion internal/runtime/testdata/foreach_metrics/foreach_1.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Foreach with only one item. The pulse will send "1" to the receiver of the summation component until it reaches 10.
Test to make sure debug metrics are disabled by default for foreach.
Use a foreach with only one item. The pulse will send "1" to the receiver of the summation component until it reaches 10.

-- main.alloy --
foreach "testForeach" {
Expand Down
3 changes: 2 additions & 1 deletion internal/runtime/testdata/foreach_metrics/foreach_2.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Test to make sure debug metrics for foreach are shown when they are enabled explicitly.
Foreach with only one item. The pulse will send "1" to the receiver of the summation component until it reaches 10.

-- main.alloy --
Expand Down Expand Up @@ -35,7 +36,7 @@ alloy_component_evaluation_queue_size{controller_id="",controller_path="/"} 1
alloy_component_evaluation_queue_size{controller_id="foreach_10_1",controller_path="/foreach.testForeach"} 0
# HELP pulse_count
# TYPE pulse_count counter
pulse_count{component_id="testcomponents.pulse.pt",component_path="/foreach.testForeach/foreach_10_1"} 0
pulse_count{component_id="testcomponents.pulse.pt",component_path="/foreach.testForeach/foreach_10_1"} 10

-- expected_duration_metrics.prom --

Expand Down
46 changes: 46 additions & 0 deletions internal/runtime/testdata/foreach_metrics/foreach_3.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
This test uses two different items in the collection.

-- main.alloy --
foreach "testForeach" {
collection = [6,4]
var = "num"
enable_metrics = true

template {
testcomponents.pulse "pt" {
max = num
frequency = "10ms"
forward_to = [testcomponents.summation_receiver.sum.receiver]
}
}
}

// Similar to testcomponents.summation, but with a "receiver" export
testcomponents.summation_receiver "sum" {
}

-- expected_metrics.prom --

# HELP alloy_component_controller_evaluating Tracks if the controller is currently in the middle of a graph evaluation
# TYPE alloy_component_controller_evaluating gauge
alloy_component_controller_evaluating{controller_id="",controller_path="/"} 0
alloy_component_controller_evaluating{controller_id="foreach_4_1",controller_path="/foreach.testForeach"} 0
alloy_component_controller_evaluating{controller_id="foreach_6_1",controller_path="/foreach.testForeach"} 0
# HELP alloy_component_controller_running_components Total number of running components.
# TYPE alloy_component_controller_running_components gauge
alloy_component_controller_running_components{controller_id="",controller_path="/",health_type="healthy"} 2
alloy_component_controller_running_components{controller_id="foreach_4_1",controller_path="/foreach.testForeach",health_type="healthy"} 1
alloy_component_controller_running_components{controller_id="foreach_6_1",controller_path="/foreach.testForeach",health_type="healthy"} 1
# HELP alloy_component_evaluation_queue_size Tracks the number of components waiting to be evaluated in the worker pool
# TYPE alloy_component_evaluation_queue_size gauge
alloy_component_evaluation_queue_size{controller_id="",controller_path="/"} 1
alloy_component_evaluation_queue_size{controller_id="foreach_4_1",controller_path="/foreach.testForeach"} 0
alloy_component_evaluation_queue_size{controller_id="foreach_6_1",controller_path="/foreach.testForeach"} 0
# HELP pulse_count
# TYPE pulse_count counter
pulse_count{component_id="testcomponents.pulse.pt",component_path="/foreach.testForeach/foreach_4_1"} 4
pulse_count{component_id="testcomponents.pulse.pt",component_path="/foreach.testForeach/foreach_6_1"} 6

-- expected_duration_metrics.prom --

6

0 comments on commit 5578777

Please sign in to comment.