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
19 changes: 19 additions & 0 deletions internal/manifests/collector/adapters/config_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package adapters
import (
"encoding/json"
"errors"
"fmt"

"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -101,8 +102,26 @@ type AppSignals struct {
type emf struct {
}

// jmx marks the presence of the jmx section in the agent configuration. The
// CloudWatch agent accepts this section as either a single object or an array
// of objects (one entry per JMX target), so unmarshalling must tolerate both.
// The operator only cares about presence, not contents.
type jmx struct{}

// UnmarshalJSON accepts both the object and array forms of the jmx section.
func (j *jmx) UnmarshalJSON(data []byte) error {
var value any
if err := json.Unmarshal(data, &value); err != nil {
return err
}
switch value.(type) {
case map[string]any, []any:
return nil
default:
return fmt.Errorf("invalid jmx configuration: expected object or array, got %T", value)
}
}

type kubernetes struct {
EnhancedContainerInsights bool `json:"enhanced_container_insights,omitempty"`
AcceleratedComputeMetrics bool `json:"accelerated_compute_metrics,omitempty"`
Expand Down
17 changes: 17 additions & 0 deletions internal/manifests/collector/adapters/config_from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ func TestEmptyString(t *testing.T) {
assert.NoError(t, err)
assert.Empty(t, res, 0)
}

func TestConfigStructFromJSONStringJMXObject(t *testing.T) {
config, err := adapters.ConfigStructFromJSONString(`{"metrics":{"metrics_collected":{"jmx":{"jvm":{"measurement":["jvm.memory.heap.used"]}}}}}`)
assert.NoError(t, err)
assert.NotNil(t, config.Metrics.MetricsCollected.JMX)
}

func TestConfigStructFromJSONStringJMXArray(t *testing.T) {
config, err := adapters.ConfigStructFromJSONString(`{"metrics":{"metrics_collected":{"jmx":[{"jvm":{}},{"kafka-consumer":{}}]}}}`)
assert.NoError(t, err)
assert.NotNil(t, config.Metrics.MetricsCollected.JMX)
}

func TestConfigStructFromJSONStringJMXInvalidType(t *testing.T) {
_, err := adapters.ConfigStructFromJSONString(`{"metrics":{"metrics_collected":{"jmx":"invalid"}}}`)
assert.Error(t, err)
}
9 changes: 9 additions & 0 deletions internal/manifests/collector/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,15 @@ func TestJMXGetContainerPorts(t *testing.T) {
assert.Equal(t, corev1.ProtocolTCP, containerPorts[JmxHttp].Protocol)
}

func TestJMXArrayGetContainerPorts(t *testing.T) {
cfg := getJSONStringFromFile("./test-resources/jmxAgentArrayConfig.json")
containerPorts := getContainerPorts(logger, cfg, "", []corev1.ServicePort{})
assert.Equal(t, 1, len(containerPorts))
assert.Equal(t, int32(4314), containerPorts[JmxHttp].ContainerPort)
assert.Equal(t, JmxHttp, containerPorts[JmxHttp].Name)
assert.Equal(t, corev1.ProtocolTCP, containerPorts[JmxHttp].Protocol)
}

func TestJMXContainerInsightsGetContainerPorts(t *testing.T) {
cfg := getJSONStringFromFile("./test-resources/jmxContainerInsightsConfig.json")
containerPorts := getContainerPorts(logger, cfg, "", []corev1.ServicePort{})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"metrics": {
"metrics_collected": {
"jmx": [
{
"jvm": {
"measurement": [
"jvm.memory.heap.used"
]
}
},
{
"kafka-consumer": {
"measurement": [
"kafka.consumer.fetch-rate"
]
}
}
]
}
}
}
Loading