From a4ade03c93cade42d2f2b2730ca6bc510b191ac4 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Fri, 2 Aug 2024 20:35:10 +0200 Subject: [PATCH 01/23] move KymaInputAllowed field into reconciler config --- controllers/telemetry/metricpipeline_controller.go | 1 - internal/reconciler/metricpipeline/reconciler.go | 9 +++++---- main.go | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/controllers/telemetry/metricpipeline_controller.go b/controllers/telemetry/metricpipeline_controller.go index 204f0d6a0..737e0e0d5 100644 --- a/controllers/telemetry/metricpipeline_controller.go +++ b/controllers/telemetry/metricpipeline_controller.go @@ -64,7 +64,6 @@ type MetricPipelineControllerConfig struct { SelfMonitorName string TelemetryNamespace string - KymaInputAllowed bool } func NewMetricPipelineController(client client.Client, reconcileTriggerChan <-chan event.GenericEvent, config MetricPipelineControllerConfig) (*MetricPipelineController, error) { diff --git a/internal/reconciler/metricpipeline/reconciler.go b/internal/reconciler/metricpipeline/reconciler.go index 50a497b2c..b4c8f2867 100644 --- a/internal/reconciler/metricpipeline/reconciler.go +++ b/internal/reconciler/metricpipeline/reconciler.go @@ -29,10 +29,11 @@ import ( const defaultReplicaCount int32 = 2 type Config struct { - Agent otelcollector.AgentConfig - Gateway otelcollector.GatewayConfig - MaxPipelines int - ModuleVersion string + Agent otelcollector.AgentConfig + Gateway otelcollector.GatewayConfig + MaxPipelines int + ModuleVersion string + KymaInputAllowed bool } type AgentConfigBuilder interface { diff --git a/main.go b/main.go index 4862c0867..1ca5ae1c0 100644 --- a/main.go +++ b/main.go @@ -528,12 +528,12 @@ func enableMetricsController(mgr manager.Manager, reconcileTriggerChan <-chan ev }, OTLPServiceName: metricOTLPServiceName, }, - MaxPipelines: maxMetricPipelines, - ModuleVersion: version, + MaxPipelines: maxMetricPipelines, + ModuleVersion: version, + KymaInputAllowed: kymaInputAllowed, }, TelemetryNamespace: telemetryNamespace, SelfMonitorName: selfMonitorName, - KymaInputAllowed: kymaInputAllowed, }, ) if err != nil { From a28e54dedb4b8fa7a7d3da8ea5b80eb19d8067a3 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Sun, 4 Aug 2024 22:44:36 +0200 Subject: [PATCH 02/23] add config for singleton_receiver_creator/kymastats --- .../config/metric/gateway/config.go | 30 ++++++++- .../config/metric/gateway/config_builder.go | 61 ++++++++++++------- .../config/metric/gateway/receivers.go | 45 ++++++++++++++ .../reconciler/metricpipeline/reconciler.go | 4 +- 4 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 internal/otelcollector/config/metric/gateway/receivers.go diff --git a/internal/otelcollector/config/metric/gateway/config.go b/internal/otelcollector/config/metric/gateway/config.go index 4b4dfdc57..122348a9b 100644 --- a/internal/otelcollector/config/metric/gateway/config.go +++ b/internal/otelcollector/config/metric/gateway/config.go @@ -13,7 +13,35 @@ type Config struct { } type Receivers struct { - OTLP config.OTLPReceiver `yaml:"otlp"` + OTLP config.OTLPReceiver `yaml:"otlp"` + SingletonKymaStatsReceiverCreator *SingletonKymaStatsReceiverCreator `yaml:"singleton_receiver_creator/kymastats,omitempty"` +} + +type SingletonKymaStatsReceiverCreator struct { + AuthType string `yaml:"auth_type"` + LeaderElection LeaderElection `yaml:"leader_election"` + SingletonKymaStatsReceiver SingletonKymaStatsReceiver `yaml:"receiver"` +} + +type LeaderElection struct { + LeaseName string `yaml:"lease_name"` + LeaseNamespace string `yaml:"lease_namespace"` +} + +type SingletonKymaStatsReceiver struct { + KymaStatsReceiver KymaStatsReceiver `yaml:"kymastats"` +} + +type KymaStatsReceiver struct { + AuthType string `yaml:"auth_type"` + CollectionInterval string `yaml:"collection_interval"` + Modules []ModuleGVR `yaml:"modules"` +} + +type ModuleGVR struct { + Group string `yaml:"group"` + Version string `yaml:"version"` + Resource string `yaml:"resource"` } type Processors struct { diff --git a/internal/otelcollector/config/metric/gateway/config_builder.go b/internal/otelcollector/config/metric/gateway/config_builder.go index 2a29d7fd4..b9516268c 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder.go +++ b/internal/otelcollector/config/metric/gateway/config_builder.go @@ -11,14 +11,13 @@ import ( "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/otlpexporter" - "github.com/kyma-project/telemetry-manager/internal/otelcollector/ports" ) type Builder struct { Reader client.Reader } -func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline) (*Config, otlpexporter.EnvVars, error) { +func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool) (*Config, otlpexporter.EnvVars, error) { cfg := &Config{ Base: config.Base{ Service: config.DefaultService(make(config.Pipelines)), @@ -45,34 +44,28 @@ func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.Metri queueSize, otlpexporter.SignalTypeMetric, ) - if err := declareComponentsForMetricPipeline(ctx, otlpExporterBuilder, &pipeline, cfg, envVars); err != nil { + if err := declareComponentsForMetricPipeline(ctx, otlpExporterBuilder, &pipeline, cfg, envVars, gatewayNamespace, kymaInputAllowed); err != nil { return nil, nil, err } pipelineID := fmt.Sprintf("metrics/%s", pipeline.Name) - cfg.Service.Pipelines[pipelineID] = makeServicePipelineConfig(&pipeline) + cfg.Service.Pipelines[pipelineID] = makeServicePipelineConfig(&pipeline, kymaInputAllowed) } return cfg, envVars, nil } -func makeReceiversConfig() Receivers { - return Receivers{ - OTLP: config.OTLPReceiver{ - Protocols: config.ReceiverProtocols{ - HTTP: config.Endpoint{ - Endpoint: fmt.Sprintf("${%s}:%d", config.EnvVarCurrentPodIP, ports.OTLPHTTP), - }, - GRPC: config.Endpoint{ - Endpoint: fmt.Sprintf("${%s}:%d", config.EnvVarCurrentPodIP, ports.OTLPGRPC), - }, - }, - }, - } -} - -// declareComponentsForMetricPipeline enriches a Config (exporters, processors, etc.) with components for a given telemetryv1alpha1.MetricPipeline. -func declareComponentsForMetricPipeline(ctx context.Context, otlpExporterBuilder *otlpexporter.ConfigBuilder, pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, envVars otlpexporter.EnvVars) error { +// declareComponentsForMetricPipeline enriches a Config (receivers, processors, exporters etc.) with components for a given telemetryv1alpha1.MetricPipeline. +func declareComponentsForMetricPipeline( + ctx context.Context, + otlpExporterBuilder *otlpexporter.ConfigBuilder, + pipeline *telemetryv1alpha1.MetricPipeline, + cfg *Config, + envVars otlpexporter.EnvVars, + gatewayNamespace string, + kymaInputAllowed bool, +) error { + declareSingletonKymaStatsReceiverCreator(pipeline, cfg, gatewayNamespace, kymaInputAllowed) declareDiagnosticMetricsDropFilters(pipeline, cfg) declareInputSourceFilters(pipeline, cfg) declareRuntimeResourcesFilters(pipeline, cfg) @@ -80,6 +73,12 @@ func declareComponentsForMetricPipeline(ctx context.Context, otlpExporterBuilder return declareOTLPExporter(ctx, otlpExporterBuilder, pipeline, cfg, envVars) } +func declareSingletonKymaStatsReceiverCreator(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, gatewayNamespace string, kymaInputAllowed bool) { + if isKymaInputEnabled(pipeline.Annotations, kymaInputAllowed) { + cfg.Receivers.SingletonKymaStatsReceiverCreator = makeSingletonKymaStatsReceiverCreatorConfig(gatewayNamespace) + } +} + func declareDiagnosticMetricsDropFilters(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config) { input := pipeline.Spec.Input @@ -157,7 +156,7 @@ func declareOTLPExporter(ctx context.Context, otlpExporterBuilder *otlpexporter. return nil } -func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline) config.Pipeline { +func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline, kymaInputAllowed bool) config.Pipeline { processors := []string{"memory_limiter", "k8sattributes"} input := pipeline.Spec.Input @@ -170,7 +169,7 @@ func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline) confi processors = append(processors, "resource/insert-cluster-name", "transform/resolve-service-name", "batch") return config.Pipeline{ - Receivers: []string{"otlp"}, + Receivers: makeReceiversIDs(pipeline.Annotations, kymaInputAllowed), Processors: processors, Exporters: []string{makeOTLPExporterID(pipeline)}, } @@ -248,6 +247,18 @@ func formatNamespaceFilterID(pipelineName string, inputSourceType metric.InputSo return fmt.Sprintf("filter/%s-filter-by-namespace-%s-input", pipelineName, inputSourceType) } +func makeReceiversIDs(annotations map[string]string, kymaInputAllowed bool) []string { + var receivers []string + + receivers = append(receivers, "otlp") + + if isKymaInputEnabled(annotations, kymaInputAllowed) { + receivers = append(receivers, "singleton_receiver_creator/kymastats") + } + + return receivers +} + func makeOTLPExporterID(pipeline *telemetryv1alpha1.MetricPipeline) string { return otlpexporter.ExporterID(pipeline.Spec.Output.Otlp.Protocol, pipeline.Name) } @@ -297,3 +308,7 @@ func isRuntimeContainerMetricsEnabled(input telemetryv1alpha1.MetricPipelineInpu return !isRuntimeContainerMetricsDisabled } + +func isKymaInputEnabled(annotations map[string]string, kymaInputAllowed bool) bool { + return kymaInputAllowed && annotations["experimental-kyma-input"] == "true" +} diff --git a/internal/otelcollector/config/metric/gateway/receivers.go b/internal/otelcollector/config/metric/gateway/receivers.go new file mode 100644 index 000000000..fc5379d90 --- /dev/null +++ b/internal/otelcollector/config/metric/gateway/receivers.go @@ -0,0 +1,45 @@ +package gateway + +import ( + "fmt" + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" + "github.com/kyma-project/telemetry-manager/internal/otelcollector/ports" +) + +func makeReceiversConfig() Receivers { + return Receivers{ + OTLP: config.OTLPReceiver{ + Protocols: config.ReceiverProtocols{ + HTTP: config.Endpoint{ + Endpoint: fmt.Sprintf("${%s}:%d", config.EnvVarCurrentPodIP, ports.OTLPHTTP), + }, + GRPC: config.Endpoint{ + Endpoint: fmt.Sprintf("${%s}:%d", config.EnvVarCurrentPodIP, ports.OTLPGRPC), + }, + }, + }, + } +} + +func makeSingletonKymaStatsReceiverCreatorConfig(gatewayNamespace string) *SingletonKymaStatsReceiverCreator { + return &SingletonKymaStatsReceiverCreator{ + AuthType: "serviceAccount", + LeaderElection: LeaderElection{ + LeaseName: "telemetry-metric-gateway-kymastats", + LeaseNamespace: gatewayNamespace, + }, + SingletonKymaStatsReceiver: SingletonKymaStatsReceiver{ + KymaStatsReceiver: KymaStatsReceiver{ + AuthType: "serviceAccount", + CollectionInterval: "30s", + Modules: []ModuleGVR{ + { + Group: "operator.kyma-project.io", + Version: "v1alpha1", + Resource: "telemetries", + }, + }, + }, + }, + } +} diff --git a/internal/reconciler/metricpipeline/reconciler.go b/internal/reconciler/metricpipeline/reconciler.go index b4c8f2867..529ccd106 100644 --- a/internal/reconciler/metricpipeline/reconciler.go +++ b/internal/reconciler/metricpipeline/reconciler.go @@ -41,7 +41,7 @@ type AgentConfigBuilder interface { } type GatewayConfigBuilder interface { - Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline) (*gateway.Config, otlpexporter.EnvVars, error) + Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool) (*gateway.Config, otlpexporter.EnvVars, error) } type AgentApplierDeleter interface { @@ -243,7 +243,7 @@ func isMetricAgentRequired(pipeline *telemetryv1alpha1.MetricPipeline) bool { } func (r *Reconciler) reconcileMetricGateway(ctx context.Context, pipeline *telemetryv1alpha1.MetricPipeline, allPipelines []telemetryv1alpha1.MetricPipeline) error { - collectorConfig, collectorEnvVars, err := r.gatewayConfigBuilder.Build(ctx, allPipelines) + collectorConfig, collectorEnvVars, err := r.gatewayConfigBuilder.Build(ctx, allPipelines, r.config.Gateway.Namespace, r.config.KymaInputAllowed) if err != nil { return fmt.Errorf("failed to create collector config: %w", err) } From 66b473fb6d2d88165150698b64bacf28206478aa Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 00:29:22 +0200 Subject: [PATCH 03/23] fix existing unit tests --- .../metric/gateway/config_builder_test.go | 313 +++++++++++++----- .../config/metric/gateway/processors_test.go | 150 ++++++--- .../mocks/gateway_config_builder.go | 22 +- .../metricpipeline/reconciler_test.go | 30 +- 4 files changed, 365 insertions(+), 150 deletions(-) diff --git a/internal/otelcollector/config/metric/gateway/config_builder_test.go b/internal/otelcollector/config/metric/gateway/config_builder_test.go index 31ec169c9..5d360c5dc 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder_test.go +++ b/internal/otelcollector/config/metric/gateway/config_builder_test.go @@ -21,11 +21,17 @@ func TestMakeConfig(t *testing.T) { ctx := context.Background() fakeClient := fake.NewClientBuilder().Build() sut := Builder{Reader: fakeClient} + gatewayNamespace := "test-namespace" t.Run("otlp exporter endpoint", func(t *testing.T) { - collectorConfig, envVars, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPOutput(testutils.OTLPEndpoint("http://localhost")).Build(), - }) + collectorConfig, envVars, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPOutput(testutils.OTLPEndpoint("http://localhost")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) expectedEndpoint := fmt.Sprintf("${%s}", "OTLP_ENDPOINT_TEST") @@ -39,8 +45,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("secure", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder(). - WithName("test").WithOTLPOutput(testutils.OTLPEndpoint("https://localhost")).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPOutput(testutils.OTLPEndpoint("https://localhost")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -49,8 +61,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("insecure", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test-insecure").WithOTLPOutput(testutils.OTLPEndpoint("http://localhost")).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test-insecure").WithOTLPOutput(testutils.OTLPEndpoint("http://localhost")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-insecure") @@ -59,9 +77,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("basic auth", func(t *testing.T) { - collectorConfig, envVars, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test-basic-auth").WithOTLPOutput(testutils.OTLPBasicAuth("user", "password")).Build(), - }) + collectorConfig, envVars, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test-basic-auth").WithOTLPOutput(testutils.OTLPBasicAuth("user", "password")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-basic-auth") @@ -77,9 +100,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("custom header", func(t *testing.T) { - collectorConfig, envVars, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test-custom-header").WithOTLPOutput(testutils.OTLPCustomHeader("Authorization", "TOKEN_VALUE", "Api-Token")).Build(), - }) + collectorConfig, envVars, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test-custom-header").WithOTLPOutput(testutils.OTLPCustomHeader("Authorization", "TOKEN_VALUE", "Api-Token")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-custom-header") @@ -94,9 +122,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("mtls", func(t *testing.T) { - collectorConfig, envVars, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test-mtls").WithOTLPOutput(testutils.OTLPClientTLSFromString("ca", "cert", "key")).Build(), - }) + collectorConfig, envVars, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test-mtls").WithOTLPOutput(testutils.OTLPClientTLSFromString("ca", "cert", "key")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-mtls") @@ -112,7 +145,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("extensions", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.NotEmpty(t, collectorConfig.Extensions.HealthCheck.Endpoint) @@ -122,7 +162,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("telemetry", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, "info", collectorConfig.Service.Telemetry.Logs.Level) @@ -131,16 +178,29 @@ func TestMakeConfig(t *testing.T) { }) t.Run("single pipeline queue size", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().WithName("test").Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, 256, collectorConfig.Exporters["otlp/test"].OTLP.SendingQueue.QueueSize, "Pipeline should have the full queue size") }) t.Run("multi pipeline queue size", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test-1").Build(), - testutils.NewMetricPipelineBuilder().WithName("test-2").Build(), - testutils.NewMetricPipelineBuilder().WithName("test-3").Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test-1").Build(), + testutils.NewMetricPipelineBuilder().WithName("test-2").Build(), + testutils.NewMetricPipelineBuilder().WithName("test-3").Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, 85, collectorConfig.Exporters["otlp/test-1"].OTLP.SendingQueue.QueueSize, "Queue size should be divided by the number of pipelines") require.Equal(t, 85, collectorConfig.Exporters["otlp/test-2"].OTLP.SendingQueue.QueueSize, "Queue size should be divided by the number of pipelines") @@ -149,8 +209,14 @@ func TestMakeConfig(t *testing.T) { t.Run("single pipeline topology", func(t *testing.T) { t.Run("with no inputs enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(false).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(false).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -171,8 +237,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with prometheus input enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).WithPrometheusInputDiagnosticMetrics(true).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).WithPrometheusInputDiagnosticMetrics(true).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -191,8 +263,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with prometheus input enabled and diagnostic metrics disabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).WithPrometheusInputDiagnosticMetrics(false).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).WithPrometheusInputDiagnosticMetrics(false).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -212,8 +290,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with prometheus input enabled and diagnostic metrics implicitly disabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -233,15 +317,21 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with runtime input enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - // Simulate the default scenario for runtime input by enabling both pod and container metrics - // NOTE: the pod and container metrics are enabled by default on the CRD level when the runtime input is defined - testutils.NewMetricPipelineBuilder(). - WithName("test"). - WithRuntimeInput(true). - WithRuntimeInputPodMetrics(true). - WithRuntimeInputContainerMetrics(true). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + // Simulate the default scenario for runtime input by enabling both pod and container metrics + // NOTE: the pod and container metrics are enabled by default on the CRD level when the runtime input is defined + testutils.NewMetricPipelineBuilder(). + WithName("test"). + WithRuntimeInput(true). + WithRuntimeInputPodMetrics(true). + WithRuntimeInputContainerMetrics(true). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -260,13 +350,18 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with runtime input enabled and only pod metrics enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder(). - WithName("test"). - WithRuntimeInput(true). - WithRuntimeInputContainerMetrics(false). - Build(), - }) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder(). + WithName("test"). + WithRuntimeInput(true). + WithRuntimeInputContainerMetrics(false). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -286,13 +381,18 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with runtime input enabled and only container metrics enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder(). - WithName("test"). - WithRuntimeInput(true). - WithRuntimeInputPodMetrics(false). - Build(), - }) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder(). + WithName("test"). + WithRuntimeInput(true). + WithRuntimeInputPodMetrics(false). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -312,8 +412,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with istio input enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).WithIstioInputDiagnosticMetrics(true).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).WithIstioInputDiagnosticMetrics(true).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -332,8 +438,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with istio input enabled and diagnostic metrics disabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).WithIstioInputDiagnosticMetrics(false).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).WithIstioInputDiagnosticMetrics(false).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -353,8 +465,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with istio input enabled and diagnostic metrics implicitly disabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -374,8 +492,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with otlp input implicitly enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -395,8 +519,14 @@ func TestMakeConfig(t *testing.T) { }) t.Run("with otlp input explicitly enabled", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(true).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(true).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -417,24 +547,29 @@ func TestMakeConfig(t *testing.T) { }) t.Run("multi pipeline topology", func(t *testing.T) { - collectorConfig, envVars, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - // Simulate the default scenario for runtime input by enabling both pod and container metrics - // NOTE: the pod and container metrics are enabled by default on the CRD level when the runtime input is defined - testutils.NewMetricPipelineBuilder(). - WithName("test-1"). - WithRuntimeInput(true, testutils.ExcludeNamespaces(namespaces.System()...)). - WithRuntimeInputPodMetrics(true). - WithRuntimeInputContainerMetrics(true). - Build(), - testutils.NewMetricPipelineBuilder(). - WithName("test-2"). - WithPrometheusInput(true, testutils.ExcludeNamespaces(namespaces.System()...)). - Build(), - testutils.NewMetricPipelineBuilder(). - WithName("test-3"). - WithIstioInput(true). - Build(), - }) + collectorConfig, envVars, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + // Simulate the default scenario for runtime input by enabling both pod and container metrics + // NOTE: the pod and container metrics are enabled by default on the CRD level when the runtime input is defined + testutils.NewMetricPipelineBuilder(). + WithName("test-1"). + WithRuntimeInput(true, testutils.ExcludeNamespaces(namespaces.System()...)). + WithRuntimeInputPodMetrics(true). + WithRuntimeInputContainerMetrics(true). + Build(), + testutils.NewMetricPipelineBuilder(). + WithName("test-2"). + WithPrometheusInput(true, testutils.ExcludeNamespaces(namespaces.System()...)). + Build(), + testutils.NewMetricPipelineBuilder(). + WithName("test-3"). + WithIstioInput(true). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-1") @@ -506,9 +641,17 @@ func TestMakeConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - config, _, err := sut.Build(context.Background(), []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(tt.withOtlpInput).WithOTLPOutput(testutils.OTLPEndpoint("https://localhost")).Build(), - }) + config, _, err := sut.Build( + context.Background(), + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder(). + WithName("test"). + WithOTLPInput(tt.withOtlpInput). + WithOTLPOutput(testutils.OTLPEndpoint("https://localhost")).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) configYAML, err := yaml.Marshal(config) diff --git a/internal/otelcollector/config/metric/gateway/processors_test.go b/internal/otelcollector/config/metric/gateway/processors_test.go index 77c6b425a..43adbd389 100644 --- a/internal/otelcollector/config/metric/gateway/processors_test.go +++ b/internal/otelcollector/config/metric/gateway/processors_test.go @@ -15,9 +15,17 @@ func TestProcessors(t *testing.T) { ctx := context.Background() fakeClient := fake.NewClientBuilder().Build() sut := Builder{Reader: fakeClient} + gatewayNamespace := "test-namespace" t.Run("insert cluster name processor", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, 1, len(collectorConfig.Processors.InsertClusterName.Attributes)) @@ -27,7 +35,14 @@ func TestProcessors(t *testing.T) { }) t.Run("memory limit processors", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, "1s", collectorConfig.Processors.MemoryLimiter.CheckInterval) @@ -36,7 +51,14 @@ func TestProcessors(t *testing.T) { }) t.Run("batch processors", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, 1024, collectorConfig.Processors.Batch.SendBatchSize) @@ -45,7 +67,14 @@ func TestProcessors(t *testing.T) { }) t.Run("k8s attributes processors", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.Equal(t, "serviceAccount", collectorConfig.Processors.K8sAttributes.AuthType) @@ -73,7 +102,14 @@ func TestProcessors(t *testing.T) { }) t.Run("drop by input source filter", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{testutils.NewMetricPipelineBuilder().WithOTLPInput(false).Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithOTLPInput(false).Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) require.NotNil(t, collectorConfig.Processors.DropIfInputSourceRuntime) @@ -99,13 +135,19 @@ func TestProcessors(t *testing.T) { }) t.Run("namespace filter processor using include", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test"). - WithRuntimeInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). - WithPrometheusInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). - WithIstioInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). - WithOTLPInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test"). + WithRuntimeInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). + WithPrometheusInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). + WithIstioInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). + WithOTLPInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) namespaceFilters := collectorConfig.Processors.NamespaceFilters @@ -136,13 +178,19 @@ func TestProcessors(t *testing.T) { }) t.Run("namespace filter processor using exclude", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test"). - WithRuntimeInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). - WithPrometheusInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). - WithIstioInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). - WithOTLPInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test"). + WithRuntimeInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). + WithPrometheusInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). + WithIstioInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). + WithOTLPInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) namespaceFilters := collectorConfig.Processors.NamespaceFilters @@ -173,11 +221,17 @@ func TestProcessors(t *testing.T) { }) t.Run("prometheus diagnostic metrics filter processor", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test"). - WithPrometheusInput(true). - WithPrometheusInputDiagnosticMetrics(false). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test"). + WithPrometheusInput(true). + WithPrometheusInputDiagnosticMetrics(false). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) prometheusScrapeFilter := collectorConfig.Processors.DropDiagnosticMetricsIfInputSourcePrometheus @@ -189,11 +243,17 @@ func TestProcessors(t *testing.T) { }) t.Run("istio diagnostic metrics filter processor", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test"). - WithIstioInput(true). - WithIstioInputDiagnosticMetrics(false). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test"). + WithIstioInput(true). + WithIstioInputDiagnosticMetrics(false). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) istioScrapeFilter := collectorConfig.Processors.DropDiagnosticMetricsIfInputSourceIstio @@ -207,11 +267,17 @@ func TestProcessors(t *testing.T) { }) t.Run("runtime pod metrics filter processor", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test"). - WithRuntimeInput(true). - WithRuntimeInputPodMetrics(false). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test"). + WithRuntimeInput(true). + WithRuntimeInputPodMetrics(false). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) runtimePodMetricsFilter := collectorConfig.Processors.DropRuntimePodMetrics @@ -222,11 +288,17 @@ func TestProcessors(t *testing.T) { }) t.Run("runtime container metrics filter processor", func(t *testing.T) { - collectorConfig, _, err := sut.Build(ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test"). - WithRuntimeInput(true). - WithRuntimeInputContainerMetrics(false). - Build()}) + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test"). + WithRuntimeInput(true). + WithRuntimeInputContainerMetrics(false). + Build(), + }, + gatewayNamespace, + false, + ) require.NoError(t, err) runtimeContainerMetricsFilter := collectorConfig.Processors.DropRuntimeContainerMetrics diff --git a/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go b/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go index 538103581..651918fc8 100644 --- a/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go +++ b/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go @@ -19,9 +19,9 @@ type GatewayConfigBuilder struct { mock.Mock } -// Build provides a mock function with given fields: ctx, pipelines -func (_m *GatewayConfigBuilder) Build(ctx context.Context, pipelines []v1alpha1.MetricPipeline) (*gateway.Config, otlpexporter.EnvVars, error) { - ret := _m.Called(ctx, pipelines) +// Build provides a mock function with given fields: ctx, pipelines, gatewayNamespace, kymaInputAllowed +func (_m *GatewayConfigBuilder) Build(ctx context.Context, pipelines []v1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool) (*gateway.Config, otlpexporter.EnvVars, error) { + ret := _m.Called(ctx, pipelines, gatewayNamespace, kymaInputAllowed) if len(ret) == 0 { panic("no return value specified for Build") @@ -30,27 +30,27 @@ func (_m *GatewayConfigBuilder) Build(ctx context.Context, pipelines []v1alpha1. var r0 *gateway.Config var r1 otlpexporter.EnvVars var r2 error - if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline) (*gateway.Config, otlpexporter.EnvVars, error)); ok { - return rf(ctx, pipelines) + if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) (*gateway.Config, otlpexporter.EnvVars, error)); ok { + return rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) } - if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline) *gateway.Config); ok { - r0 = rf(ctx, pipelines) + if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) *gateway.Config); ok { + r0 = rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*gateway.Config) } } - if rf, ok := ret.Get(1).(func(context.Context, []v1alpha1.MetricPipeline) otlpexporter.EnvVars); ok { - r1 = rf(ctx, pipelines) + if rf, ok := ret.Get(1).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) otlpexporter.EnvVars); ok { + r1 = rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(otlpexporter.EnvVars) } } - if rf, ok := ret.Get(2).(func(context.Context, []v1alpha1.MetricPipeline) error); ok { - r2 = rf(ctx, pipelines) + if rf, ok := ret.Get(2).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) error); ok { + r2 = rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) } else { r2 = ret.Error(2) } diff --git a/internal/reconciler/metricpipeline/reconciler_test.go b/internal/reconciler/metricpipeline/reconciler_test.go index 9257b55e2..08980df58 100644 --- a/internal/reconciler/metricpipeline/reconciler_test.go +++ b/internal/reconciler/metricpipeline/reconciler_test.go @@ -74,7 +74,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -133,7 +133,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -192,7 +192,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -254,7 +254,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -322,7 +322,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -391,7 +391,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -465,7 +465,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline, secret).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -524,7 +524,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything).Return(nil) @@ -593,7 +593,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) pipelineLockStub := &metricMocks.PipelineLock{} pipelineLockStub.On("TryAcquireLock", mock.Anything, mock.Anything).Return(resourcelock.ErrMaxPipelinesExceeded) @@ -750,7 +750,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -887,7 +887,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -976,7 +976,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline, secret).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -1044,7 +1044,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -1115,7 +1115,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything).Return(nil).Times(1) @@ -1242,7 +1242,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline)).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) From 18757fcbfe9dc112f31801d138136949de7b3764 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 00:30:45 +0200 Subject: [PATCH 04/23] fix linting --- internal/otelcollector/config/metric/gateway/receivers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/otelcollector/config/metric/gateway/receivers.go b/internal/otelcollector/config/metric/gateway/receivers.go index fc5379d90..381c0d481 100644 --- a/internal/otelcollector/config/metric/gateway/receivers.go +++ b/internal/otelcollector/config/metric/gateway/receivers.go @@ -2,6 +2,7 @@ package gateway import ( "fmt" + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" "github.com/kyma-project/telemetry-manager/internal/otelcollector/ports" ) From d346cfa0c7f55dfb2a9ab0d7db2557625db1ffa8 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 04:11:01 +0200 Subject: [PATCH 05/23] add unit tests for singleton_receiver_creator/kymastats --- .../metric/gateway/config_builder_test.go | 59 +++++++++++++++++- .../config/metric/gateway/receivers_test.go | 61 +++++++++++++++++++ internal/testutils/metric_pipeline_builder.go | 15 +++-- 3 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 internal/otelcollector/config/metric/gateway/receivers_test.go diff --git a/internal/otelcollector/config/metric/gateway/config_builder_test.go b/internal/otelcollector/config/metric/gateway/config_builder_test.go index 5d360c5dc..ff66a95db 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder_test.go +++ b/internal/otelcollector/config/metric/gateway/config_builder_test.go @@ -215,7 +215,7 @@ func TestMakeConfig(t *testing.T) { testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(false).Build(), }, gatewayNamespace, - false, + true, ) require.NoError(t, err) @@ -224,6 +224,7 @@ func TestMakeConfig(t *testing.T) { require.Contains(t, collectorConfig.Service.Pipelines, "metrics/test") require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Exporters, "otlp/test") require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Receivers, "otlp") + require.NotContains(t, collectorConfig.Service.Pipelines["metrics/test"].Receivers, "singleton_receiver_creator/kymastats") require.Equal(t, []string{"memory_limiter", "k8sattributes", "filter/drop-if-input-source-runtime", @@ -544,6 +545,62 @@ func TestMakeConfig(t *testing.T) { "batch", }, collectorConfig.Service.Pipelines["metrics/test"].Processors) }) + + t.Run("with kyma input annotation existing and kyma input is allowed", func(t *testing.T) { + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + }, + gatewayNamespace, + true, + ) + require.NoError(t, err) + + require.Contains(t, collectorConfig.Exporters, "otlp/test") + + require.Contains(t, collectorConfig.Service.Pipelines, "metrics/test") + require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Exporters, "otlp/test") + require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Receivers, "otlp") + require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Receivers, "singleton_receiver_creator/kymastats") + require.Equal(t, []string{"memory_limiter", + "k8sattributes", + "filter/drop-if-input-source-runtime", + "filter/drop-if-input-source-prometheus", + "filter/drop-if-input-source-istio", + "resource/insert-cluster-name", + "transform/resolve-service-name", + "batch", + }, collectorConfig.Service.Pipelines["metrics/test"].Processors) + }) + + t.Run("with kyma input annotation existing and kyma input is not allowed", func(t *testing.T) { + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + }, + gatewayNamespace, + false, + ) + require.NoError(t, err) + + require.Contains(t, collectorConfig.Exporters, "otlp/test") + + require.Contains(t, collectorConfig.Service.Pipelines, "metrics/test") + require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Exporters, "otlp/test") + require.Contains(t, collectorConfig.Service.Pipelines["metrics/test"].Receivers, "otlp") + require.NotContains(t, collectorConfig.Service.Pipelines["metrics/test"].Receivers, "singleton_receiver_creator/kymastats") + require.Equal(t, []string{"memory_limiter", + "k8sattributes", + "filter/drop-if-input-source-runtime", + "filter/drop-if-input-source-prometheus", + "filter/drop-if-input-source-istio", + "resource/insert-cluster-name", + "transform/resolve-service-name", + "batch", + }, collectorConfig.Service.Pipelines["metrics/test"].Processors) + }) }) t.Run("multi pipeline topology", func(t *testing.T) { diff --git a/internal/otelcollector/config/metric/gateway/receivers_test.go b/internal/otelcollector/config/metric/gateway/receivers_test.go new file mode 100644 index 000000000..82a2b37df --- /dev/null +++ b/internal/otelcollector/config/metric/gateway/receivers_test.go @@ -0,0 +1,61 @@ +package gateway + +import ( + "context" + "testing" + + telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1" + "github.com/kyma-project/telemetry-manager/internal/testutils" + "github.com/stretchr/testify/require" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestReceivers(t *testing.T) { + ctx := context.Background() + fakeClient := fake.NewClientBuilder().Build() + sut := Builder{Reader: fakeClient} + gatewayNamespace := "test-namespace" + + t.Run("OTLP receiver", func(t *testing.T) { + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").Build(), + }, + gatewayNamespace, + false, + ) + require.NoError(t, err) + + otlpReceiver := collectorConfig.Receivers.OTLP + require.NotNil(t, otlpReceiver) + require.Equal(t, "${MY_POD_IP}:4318", otlpReceiver.Protocols.HTTP.Endpoint) + require.Equal(t, "${MY_POD_IP}:4317", otlpReceiver.Protocols.GRPC.Endpoint) + }) + + t.Run("singleton kyma stats receiver creator", func(t *testing.T) { + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + }, + gatewayNamespace, + true, + ) + require.NoError(t, err) + + singletonKymaStatsReceiverCreator := collectorConfig.Receivers.SingletonKymaStatsReceiverCreator + require.NotNil(t, singletonKymaStatsReceiverCreator) + require.Equal(t, "serviceAccount", singletonKymaStatsReceiverCreator.AuthType) + require.Equal(t, "telemetry-metric-gateway-kymastats", singletonKymaStatsReceiverCreator.LeaderElection.LeaseName) + require.Equal(t, gatewayNamespace, singletonKymaStatsReceiverCreator.LeaderElection.LeaseNamespace) + + kymaStatsReceiver := singletonKymaStatsReceiverCreator.SingletonKymaStatsReceiver.KymaStatsReceiver + require.Equal(t, "serviceAccount", kymaStatsReceiver.AuthType) + require.Equal(t, "30s", kymaStatsReceiver.CollectionInterval) + require.Len(t, kymaStatsReceiver.Modules, 1) + require.Equal(t, "operator.kyma-project.io", kymaStatsReceiver.Modules[0].Group) + require.Equal(t, "v1alpha1", kymaStatsReceiver.Modules[0].Version) + require.Equal(t, "telemetries", kymaStatsReceiver.Modules[0].Resource) + }) +} diff --git a/internal/testutils/metric_pipeline_builder.go b/internal/testutils/metric_pipeline_builder.go index 6a9465125..06f74000c 100644 --- a/internal/testutils/metric_pipeline_builder.go +++ b/internal/testutils/metric_pipeline_builder.go @@ -13,8 +13,9 @@ import ( type MetricPipelineBuilder struct { randSource rand.Source - name string - labels map[string]string + name string + labels map[string]string + annotations map[string]string inRuntime *telemetryv1alpha1.MetricPipelineRuntimeInput inPrometheus *telemetryv1alpha1.MetricPipelinePrometheusInput @@ -45,6 +46,11 @@ func (b *MetricPipelineBuilder) WithLabels(labels map[string]string) *MetricPipe return b } +func (b *MetricPipelineBuilder) WithAnnotations(annotations map[string]string) *MetricPipelineBuilder { + b.annotations = annotations + return b +} + type InputOptions func(selector *telemetryv1alpha1.MetricPipelineInputNamespaceSelector) func IncludeNamespaces(namespaces ...string) InputOptions { @@ -220,8 +226,9 @@ func (b *MetricPipelineBuilder) Build() telemetryv1alpha1.MetricPipeline { pipeline := telemetryv1alpha1.MetricPipeline{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Labels: b.labels, + Name: name, + Labels: b.labels, + Annotations: b.annotations, }, Status: telemetryv1alpha1.MetricPipelineStatus{ Conditions: b.statusConditions, From 80148fa3f430f67334a5d13aecffcc9109f8c641 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 05:02:42 +0200 Subject: [PATCH 06/23] refactor setting instrumentation scope into metric pkg from agent pkg --- .../config/metric/agent/config.go | 16 ++++----- .../config/metric/agent/config_builder.go | 2 +- .../config/metric/agent/processors.go | 8 ++--- .../metric/agent/set_instrumention_scope.go | 33 ------------------- .../otelcollector/config/metric/config.go | 8 +++++ .../set_instrumentation_scope_test.go | 17 ++++------ .../config/metric/set_instrumention_scope.go | 33 +++++++++++++++++++ .../config/metric/shared_attributes.go | 3 ++ 8 files changed, 62 insertions(+), 58 deletions(-) delete mode 100644 internal/otelcollector/config/metric/agent/set_instrumention_scope.go create mode 100644 internal/otelcollector/config/metric/config.go rename internal/otelcollector/config/metric/{agent => }/set_instrumentation_scope_test.go (83%) create mode 100644 internal/otelcollector/config/metric/set_instrumention_scope.go diff --git a/internal/otelcollector/config/metric/agent/config.go b/internal/otelcollector/config/metric/agent/config.go index f5c1fcbce..034fa7577 100644 --- a/internal/otelcollector/config/metric/agent/config.go +++ b/internal/otelcollector/config/metric/agent/config.go @@ -4,6 +4,7 @@ import ( "time" "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" ) type Config struct { @@ -115,11 +116,11 @@ const ( type Processors struct { config.BaseProcessors `yaml:",inline"` - DeleteServiceName *config.ResourceProcessor `yaml:"resource/delete-service-name,omitempty"` - DropInternalCommunication *FilterProcessor `yaml:"filter/drop-internal-communication,omitempty"` - SetInstrumentationScopeRuntime *TransformProcessor `yaml:"transform/set-instrumentation-scope-runtime,omitempty"` - SetInstrumentationScopePrometheus *TransformProcessor `yaml:"transform/set-instrumentation-scope-prometheus,omitempty"` - SetInstrumentationScopeIstio *TransformProcessor `yaml:"transform/set-instrumentation-scope-istio,omitempty"` + DeleteServiceName *config.ResourceProcessor `yaml:"resource/delete-service-name,omitempty"` + DropInternalCommunication *FilterProcessor `yaml:"filter/drop-internal-communication,omitempty"` + SetInstrumentationScopeRuntime *metric.TransformProcessor `yaml:"transform/set-instrumentation-scope-runtime,omitempty"` + SetInstrumentationScopePrometheus *metric.TransformProcessor `yaml:"transform/set-instrumentation-scope-prometheus,omitempty"` + SetInstrumentationScopeIstio *metric.TransformProcessor `yaml:"transform/set-instrumentation-scope-istio,omitempty"` } type Exporters struct { @@ -133,8 +134,3 @@ type FilterProcessor struct { type FilterProcessorMetrics struct { Metric []string `yaml:"metric,omitempty"` } - -type TransformProcessor struct { - ErrorMode string `yaml:"error_mode"` - MetricStatements []config.TransformProcessorStatements `yaml:"metric_statements"` -} diff --git a/internal/otelcollector/config/metric/agent/config_builder.go b/internal/otelcollector/config/metric/agent/config_builder.go index a8c526812..904137996 100644 --- a/internal/otelcollector/config/metric/agent/config_builder.go +++ b/internal/otelcollector/config/metric/agent/config_builder.go @@ -43,7 +43,7 @@ func (b *Builder) Build(pipelines []telemetryv1alpha1.MetricPipeline, opts Build Extensions: config.DefaultExtensions(), }, Receivers: makeReceiversConfig(inputs, opts), - Processors: makeProcessorsConfig(inputs, opts), + Processors: makeProcessorsConfig(inputs, opts.InstrumentationScopeVersion), Exporters: makeExportersConfig(b.Config.GatewayOTLPServiceName), } } diff --git a/internal/otelcollector/config/metric/agent/processors.go b/internal/otelcollector/config/metric/agent/processors.go index 066e3ee7d..4114652d6 100644 --- a/internal/otelcollector/config/metric/agent/processors.go +++ b/internal/otelcollector/config/metric/agent/processors.go @@ -5,7 +5,7 @@ import ( "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" ) -func makeProcessorsConfig(inputs inputSources, opts BuildOptions) Processors { +func makeProcessorsConfig(inputs inputSources, instrumentationScopeVersion string) Processors { processorsConfig := Processors{ BaseProcessors: config.BaseProcessors{ Batch: makeBatchProcessorConfig(), @@ -17,16 +17,16 @@ func makeProcessorsConfig(inputs inputSources, opts BuildOptions) Processors { processorsConfig.DeleteServiceName = makeDeleteServiceNameConfig() if inputs.runtime { - processorsConfig.SetInstrumentationScopeRuntime = makeInstrumentationScopeProcessor(metric.InputSourceRuntime, opts) + processorsConfig.SetInstrumentationScopeRuntime = metric.MakeInstrumentationScopeProcessor(metric.InputSourceRuntime, instrumentationScopeVersion) } if inputs.prometheus { - processorsConfig.SetInstrumentationScopePrometheus = makeInstrumentationScopeProcessor(metric.InputSourcePrometheus, opts) + processorsConfig.SetInstrumentationScopePrometheus = metric.MakeInstrumentationScopeProcessor(metric.InputSourcePrometheus, instrumentationScopeVersion) } if inputs.istio { processorsConfig.DropInternalCommunication = makeFilterToDropMetricsForTelemetryComponents() - processorsConfig.SetInstrumentationScopeIstio = makeInstrumentationScopeProcessor(metric.InputSourceIstio, opts) + processorsConfig.SetInstrumentationScopeIstio = metric.MakeInstrumentationScopeProcessor(metric.InputSourceIstio, instrumentationScopeVersion) } } diff --git a/internal/otelcollector/config/metric/agent/set_instrumention_scope.go b/internal/otelcollector/config/metric/agent/set_instrumention_scope.go deleted file mode 100644 index ccd4bf949..000000000 --- a/internal/otelcollector/config/metric/agent/set_instrumention_scope.go +++ /dev/null @@ -1,33 +0,0 @@ -package agent - -import ( - "fmt" - - "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" - "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" -) - -var upstreamInstrumentationScopeName = map[metric.InputSourceType]string{ - metric.InputSourceRuntime: "otelcol/kubeletstatsreceiver", - metric.InputSourcePrometheus: "otelcol/prometheusreceiver", - metric.InputSourceIstio: "otelcol/prometheusreceiver", -} - -func makeInstrumentationScopeProcessor(inputSource metric.InputSourceType, opts BuildOptions) *TransformProcessor { - return &TransformProcessor{ - ErrorMode: "ignore", - MetricStatements: []config.TransformProcessorStatements{ - { - Context: "scope", - Statements: makeInstrumentationStatement(inputSource, opts), - }, - }, - } -} - -func makeInstrumentationStatement(inputSource metric.InputSourceType, opts BuildOptions) []string { - return []string{ - fmt.Sprintf("set(version, \"%s\") where name == \"%s\"", opts.InstrumentationScopeVersion, upstreamInstrumentationScopeName[inputSource]), - fmt.Sprintf("set(name, \"%s\") where name == \"%s\"", metric.InstrumentationScope[inputSource], upstreamInstrumentationScopeName[inputSource]), - } -} diff --git a/internal/otelcollector/config/metric/config.go b/internal/otelcollector/config/metric/config.go new file mode 100644 index 000000000..a289c3b46 --- /dev/null +++ b/internal/otelcollector/config/metric/config.go @@ -0,0 +1,8 @@ +package metric + +import "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" + +type TransformProcessor struct { + ErrorMode string `yaml:"error_mode"` + MetricStatements []config.TransformProcessorStatements `yaml:"metric_statements"` +} diff --git a/internal/otelcollector/config/metric/agent/set_instrumentation_scope_test.go b/internal/otelcollector/config/metric/set_instrumentation_scope_test.go similarity index 83% rename from internal/otelcollector/config/metric/agent/set_instrumentation_scope_test.go rename to internal/otelcollector/config/metric/set_instrumentation_scope_test.go index 0b116d54e..74d0707ab 100644 --- a/internal/otelcollector/config/metric/agent/set_instrumentation_scope_test.go +++ b/internal/otelcollector/config/metric/set_instrumentation_scope_test.go @@ -1,20 +1,17 @@ -package agent +package metric import ( "testing" "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" - "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" ) func TestTransformedInstrumentationScope(t *testing.T) { - opts := BuildOptions{ - InstrumentationScopeVersion: "main", - } + instrumentationScopeVersion := "main" tests := []struct { name string want *TransformProcessor - inputSource metric.InputSourceType + inputSource InputSourceType }{ { name: "InputSourceRuntime", @@ -28,7 +25,7 @@ func TestTransformedInstrumentationScope(t *testing.T) { }, }}, }, - inputSource: metric.InputSourceRuntime, + inputSource: InputSourceRuntime, }, { name: "InputSourcePrometheus", want: &TransformProcessor{ @@ -41,7 +38,7 @@ func TestTransformedInstrumentationScope(t *testing.T) { }, }}, }, - inputSource: metric.InputSourcePrometheus, + inputSource: InputSourcePrometheus, }, { name: "InputSourceIstio", want: &TransformProcessor{ @@ -54,13 +51,13 @@ func TestTransformedInstrumentationScope(t *testing.T) { }, }}, }, - inputSource: metric.InputSourceIstio, + inputSource: InputSourceIstio, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := makeInstrumentationScopeProcessor(tt.inputSource, opts); !compareTransformProcessor(got, tt.want) { + if got := MakeInstrumentationScopeProcessor(tt.inputSource, instrumentationScopeVersion); !compareTransformProcessor(got, tt.want) { t.Errorf("makeInstrumentationScopeProcessor() = %v, want %v", got, tt.want) } }) diff --git a/internal/otelcollector/config/metric/set_instrumention_scope.go b/internal/otelcollector/config/metric/set_instrumention_scope.go new file mode 100644 index 000000000..ef092cc81 --- /dev/null +++ b/internal/otelcollector/config/metric/set_instrumention_scope.go @@ -0,0 +1,33 @@ +package metric + +import ( + "fmt" + + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" +) + +var upstreamInstrumentationScopeName = map[InputSourceType]string{ + InputSourceRuntime: "otelcol/kubeletstatsreceiver", + InputSourcePrometheus: "otelcol/prometheusreceiver", + InputSourceIstio: "otelcol/prometheusreceiver", + InputSourceKyma: "otelcol/kymastats", +} + +func MakeInstrumentationScopeProcessor(inputSource InputSourceType, instrumentationScopeVersion string) *TransformProcessor { + return &TransformProcessor{ + ErrorMode: "ignore", + MetricStatements: []config.TransformProcessorStatements{ + { + Context: "scope", + Statements: makeInstrumentationStatement(inputSource, instrumentationScopeVersion), + }, + }, + } +} + +func makeInstrumentationStatement(inputSource InputSourceType, instrumentationScopeVersion string) []string { + return []string{ + fmt.Sprintf("set(version, \"%s\") where name == \"%s\"", instrumentationScopeVersion, upstreamInstrumentationScopeName[inputSource]), + fmt.Sprintf("set(name, \"%s\") where name == \"%s\"", InstrumentationScope[inputSource], upstreamInstrumentationScopeName[inputSource]), + } +} diff --git a/internal/otelcollector/config/metric/shared_attributes.go b/internal/otelcollector/config/metric/shared_attributes.go index b85107bba..7efcbca07 100644 --- a/internal/otelcollector/config/metric/shared_attributes.go +++ b/internal/otelcollector/config/metric/shared_attributes.go @@ -7,16 +7,19 @@ const ( InputSourcePrometheus InputSourceType = "prometheus" InputSourceIstio InputSourceType = "istio" InputSourceOtlp InputSourceType = "otlp" + InputSourceKyma InputSourceType = "kyma" ) const ( InstrumentationScopeRuntime = "io.kyma-project.telemetry/runtime" InstrumentationScopePrometheus = "io.kyma-project.telemetry/prometheus" InstrumentationScopeIstio = "io.kyma-project.telemetry/istio" + InstrumentationScopeKyma = "io.kyma-project.telemetry/kyma" ) var InstrumentationScope = map[InputSourceType]string{ InputSourceRuntime: InstrumentationScopeRuntime, InputSourcePrometheus: InstrumentationScopePrometheus, InputSourceIstio: InstrumentationScopeIstio, + InputSourceKyma: InstrumentationScopeKyma, } From 1ac35de903f2332bc3c2760f19bba2f605474def Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 05:52:39 +0200 Subject: [PATCH 07/23] set instrumentationScope for kymastats metrics --- .../config/metric/gateway/config.go | 2 ++ .../config/metric/gateway/config_builder.go | 16 ++++++++++++++-- internal/reconciler/metricpipeline/reconciler.go | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/otelcollector/config/metric/gateway/config.go b/internal/otelcollector/config/metric/gateway/config.go index 122348a9b..854b73876 100644 --- a/internal/otelcollector/config/metric/gateway/config.go +++ b/internal/otelcollector/config/metric/gateway/config.go @@ -2,6 +2,7 @@ package gateway import ( "github.com/kyma-project/telemetry-manager/internal/otelcollector/config" + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" ) type Config struct { @@ -58,6 +59,7 @@ type Processors struct { DropRuntimePodMetrics *FilterProcessor `yaml:"filter/drop-runtime-pod-metrics,omitempty"` DropRuntimeContainerMetrics *FilterProcessor `yaml:"filter/drop-runtime-container-metrics,omitempty"` ResolveServiceName *TransformProcessor `yaml:"transform/resolve-service-name,omitempty"` + SetInstrumentationScopeKyma *metric.TransformProcessor `yaml:"transform/set-instrumentation-scope-kyma,omitempty"` // NamespaceFilters contains filter processors, which need different configurations per pipeline NamespaceFilters NamespaceFilters `yaml:",inline,omitempty"` diff --git a/internal/otelcollector/config/metric/gateway/config_builder.go b/internal/otelcollector/config/metric/gateway/config_builder.go index b9516268c..d5591bfba 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder.go +++ b/internal/otelcollector/config/metric/gateway/config_builder.go @@ -17,7 +17,7 @@ type Builder struct { Reader client.Reader } -func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool) (*Config, otlpexporter.EnvVars, error) { +func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool, instrumentationScopeVersion string) (*Config, otlpexporter.EnvVars, error) { cfg := &Config{ Base: config.Base{ Service: config.DefaultService(make(config.Pipelines)), @@ -44,7 +44,7 @@ func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.Metri queueSize, otlpexporter.SignalTypeMetric, ) - if err := declareComponentsForMetricPipeline(ctx, otlpExporterBuilder, &pipeline, cfg, envVars, gatewayNamespace, kymaInputAllowed); err != nil { + if err := declareComponentsForMetricPipeline(ctx, otlpExporterBuilder, &pipeline, cfg, envVars, gatewayNamespace, kymaInputAllowed, instrumentationScopeVersion); err != nil { return nil, nil, err } @@ -64,12 +64,14 @@ func declareComponentsForMetricPipeline( envVars otlpexporter.EnvVars, gatewayNamespace string, kymaInputAllowed bool, + instrumentationScopeVersion string, ) error { declareSingletonKymaStatsReceiverCreator(pipeline, cfg, gatewayNamespace, kymaInputAllowed) declareDiagnosticMetricsDropFilters(pipeline, cfg) declareInputSourceFilters(pipeline, cfg) declareRuntimeResourcesFilters(pipeline, cfg) declareNamespaceFilters(pipeline, cfg) + declareInstrumentationScopeTransform(pipeline, cfg, instrumentationScopeVersion, kymaInputAllowed) return declareOTLPExporter(ctx, otlpExporterBuilder, pipeline, cfg, envVars) } @@ -142,6 +144,12 @@ func declareNamespaceFilters(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Co } } +func declareInstrumentationScopeTransform(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, instrumentationScopeVersion string, kymaInputAllowed bool) { + if isKymaInputEnabled(pipeline.Annotations, kymaInputAllowed) { + cfg.Processors.SetInstrumentationScopeKyma = metric.MakeInstrumentationScopeProcessor(metric.InputSourceKyma, instrumentationScopeVersion) + } +} + func declareOTLPExporter(ctx context.Context, otlpExporterBuilder *otlpexporter.ConfigBuilder, pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, envVars otlpexporter.EnvVars) error { otlpExporterConfig, otlpExporterEnvVars, err := otlpExporterBuilder.MakeConfig(ctx) if err != nil { @@ -166,6 +174,10 @@ func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline, kymaI processors = append(processors, makeRuntimeResourcesFiltersIDs(input)...) processors = append(processors, makeDiagnosticMetricFiltersIDs(input)...) + if isKymaInputEnabled(pipeline.Annotations, kymaInputAllowed) { + processors = append(processors, "transform/set-instrumentation-scope-kyma") + } + processors = append(processors, "resource/insert-cluster-name", "transform/resolve-service-name", "batch") return config.Pipeline{ diff --git a/internal/reconciler/metricpipeline/reconciler.go b/internal/reconciler/metricpipeline/reconciler.go index 529ccd106..0edd17910 100644 --- a/internal/reconciler/metricpipeline/reconciler.go +++ b/internal/reconciler/metricpipeline/reconciler.go @@ -41,7 +41,7 @@ type AgentConfigBuilder interface { } type GatewayConfigBuilder interface { - Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool) (*gateway.Config, otlpexporter.EnvVars, error) + Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool, instrumentationScopeVersion string) (*gateway.Config, otlpexporter.EnvVars, error) } type AgentApplierDeleter interface { @@ -243,7 +243,7 @@ func isMetricAgentRequired(pipeline *telemetryv1alpha1.MetricPipeline) bool { } func (r *Reconciler) reconcileMetricGateway(ctx context.Context, pipeline *telemetryv1alpha1.MetricPipeline, allPipelines []telemetryv1alpha1.MetricPipeline) error { - collectorConfig, collectorEnvVars, err := r.gatewayConfigBuilder.Build(ctx, allPipelines, r.config.Gateway.Namespace, r.config.KymaInputAllowed) + collectorConfig, collectorEnvVars, err := r.gatewayConfigBuilder.Build(ctx, allPipelines, r.config.Gateway.Namespace, r.config.KymaInputAllowed, r.config.ModuleVersion) if err != nil { return fmt.Errorf("failed to create collector config: %w", err) } From deec1426b078e575bf1416cb2964a1379cc6c701 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 06:07:36 +0200 Subject: [PATCH 08/23] refactor gateway config builder options --- .../config/metric/gateway/config_builder.go | 38 ++++++++++--------- .../reconciler/metricpipeline/reconciler.go | 9 ++++- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/internal/otelcollector/config/metric/gateway/config_builder.go b/internal/otelcollector/config/metric/gateway/config_builder.go index d5591bfba..df4341545 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder.go +++ b/internal/otelcollector/config/metric/gateway/config_builder.go @@ -17,7 +17,13 @@ type Builder struct { Reader client.Reader } -func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool, instrumentationScopeVersion string) (*Config, otlpexporter.EnvVars, error) { +type BuildOptions struct { + GatewayNamespace string + InstrumentationScopeVersion string + KymaInputAllowed bool +} + +func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, opts BuildOptions) (*Config, otlpexporter.EnvVars, error) { cfg := &Config{ Base: config.Base{ Service: config.DefaultService(make(config.Pipelines)), @@ -44,12 +50,12 @@ func (b *Builder) Build(ctx context.Context, pipelines []telemetryv1alpha1.Metri queueSize, otlpexporter.SignalTypeMetric, ) - if err := declareComponentsForMetricPipeline(ctx, otlpExporterBuilder, &pipeline, cfg, envVars, gatewayNamespace, kymaInputAllowed, instrumentationScopeVersion); err != nil { + if err := declareComponentsForMetricPipeline(ctx, otlpExporterBuilder, &pipeline, cfg, envVars, opts); err != nil { return nil, nil, err } pipelineID := fmt.Sprintf("metrics/%s", pipeline.Name) - cfg.Service.Pipelines[pipelineID] = makeServicePipelineConfig(&pipeline, kymaInputAllowed) + cfg.Service.Pipelines[pipelineID] = makeServicePipelineConfig(&pipeline, opts) } return cfg, envVars, nil @@ -62,22 +68,20 @@ func declareComponentsForMetricPipeline( pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, envVars otlpexporter.EnvVars, - gatewayNamespace string, - kymaInputAllowed bool, - instrumentationScopeVersion string, + opts BuildOptions, ) error { - declareSingletonKymaStatsReceiverCreator(pipeline, cfg, gatewayNamespace, kymaInputAllowed) + declareSingletonKymaStatsReceiverCreator(pipeline, cfg, opts) declareDiagnosticMetricsDropFilters(pipeline, cfg) declareInputSourceFilters(pipeline, cfg) declareRuntimeResourcesFilters(pipeline, cfg) declareNamespaceFilters(pipeline, cfg) - declareInstrumentationScopeTransform(pipeline, cfg, instrumentationScopeVersion, kymaInputAllowed) + declareInstrumentationScopeTransform(pipeline, cfg, opts) return declareOTLPExporter(ctx, otlpExporterBuilder, pipeline, cfg, envVars) } -func declareSingletonKymaStatsReceiverCreator(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, gatewayNamespace string, kymaInputAllowed bool) { - if isKymaInputEnabled(pipeline.Annotations, kymaInputAllowed) { - cfg.Receivers.SingletonKymaStatsReceiverCreator = makeSingletonKymaStatsReceiverCreatorConfig(gatewayNamespace) +func declareSingletonKymaStatsReceiverCreator(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, opts BuildOptions) { + if isKymaInputEnabled(pipeline.Annotations, opts.KymaInputAllowed) { + cfg.Receivers.SingletonKymaStatsReceiverCreator = makeSingletonKymaStatsReceiverCreatorConfig(opts.GatewayNamespace) } } @@ -144,9 +148,9 @@ func declareNamespaceFilters(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Co } } -func declareInstrumentationScopeTransform(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, instrumentationScopeVersion string, kymaInputAllowed bool) { - if isKymaInputEnabled(pipeline.Annotations, kymaInputAllowed) { - cfg.Processors.SetInstrumentationScopeKyma = metric.MakeInstrumentationScopeProcessor(metric.InputSourceKyma, instrumentationScopeVersion) +func declareInstrumentationScopeTransform(pipeline *telemetryv1alpha1.MetricPipeline, cfg *Config, opts BuildOptions) { + if isKymaInputEnabled(pipeline.Annotations, opts.KymaInputAllowed) { + cfg.Processors.SetInstrumentationScopeKyma = metric.MakeInstrumentationScopeProcessor(metric.InputSourceKyma, opts.InstrumentationScopeVersion) } } @@ -164,7 +168,7 @@ func declareOTLPExporter(ctx context.Context, otlpExporterBuilder *otlpexporter. return nil } -func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline, kymaInputAllowed bool) config.Pipeline { +func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline, opts BuildOptions) config.Pipeline { processors := []string{"memory_limiter", "k8sattributes"} input := pipeline.Spec.Input @@ -174,14 +178,14 @@ func makeServicePipelineConfig(pipeline *telemetryv1alpha1.MetricPipeline, kymaI processors = append(processors, makeRuntimeResourcesFiltersIDs(input)...) processors = append(processors, makeDiagnosticMetricFiltersIDs(input)...) - if isKymaInputEnabled(pipeline.Annotations, kymaInputAllowed) { + if isKymaInputEnabled(pipeline.Annotations, opts.KymaInputAllowed) { processors = append(processors, "transform/set-instrumentation-scope-kyma") } processors = append(processors, "resource/insert-cluster-name", "transform/resolve-service-name", "batch") return config.Pipeline{ - Receivers: makeReceiversIDs(pipeline.Annotations, kymaInputAllowed), + Receivers: makeReceiversIDs(pipeline.Annotations, opts.KymaInputAllowed), Processors: processors, Exporters: []string{makeOTLPExporterID(pipeline)}, } diff --git a/internal/reconciler/metricpipeline/reconciler.go b/internal/reconciler/metricpipeline/reconciler.go index 0edd17910..05e3951ec 100644 --- a/internal/reconciler/metricpipeline/reconciler.go +++ b/internal/reconciler/metricpipeline/reconciler.go @@ -41,7 +41,7 @@ type AgentConfigBuilder interface { } type GatewayConfigBuilder interface { - Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool, instrumentationScopeVersion string) (*gateway.Config, otlpexporter.EnvVars, error) + Build(ctx context.Context, pipelines []telemetryv1alpha1.MetricPipeline, options gateway.BuildOptions) (*gateway.Config, otlpexporter.EnvVars, error) } type AgentApplierDeleter interface { @@ -243,7 +243,12 @@ func isMetricAgentRequired(pipeline *telemetryv1alpha1.MetricPipeline) bool { } func (r *Reconciler) reconcileMetricGateway(ctx context.Context, pipeline *telemetryv1alpha1.MetricPipeline, allPipelines []telemetryv1alpha1.MetricPipeline) error { - collectorConfig, collectorEnvVars, err := r.gatewayConfigBuilder.Build(ctx, allPipelines, r.config.Gateway.Namespace, r.config.KymaInputAllowed, r.config.ModuleVersion) + collectorConfig, collectorEnvVars, err := r.gatewayConfigBuilder.Build(ctx, allPipelines, gateway.BuildOptions{ + GatewayNamespace: r.config.Gateway.Namespace, + InstrumentationScopeVersion: r.config.ModuleVersion, + KymaInputAllowed: r.config.KymaInputAllowed, + }) + if err != nil { return fmt.Errorf("failed to create collector config: %w", err) } From e26761b8482ada544e0b6054b6a0914fd060057a Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 06:30:10 +0200 Subject: [PATCH 09/23] fix existing unit tests --- .../metric/gateway/config_builder_test.go | 84 +++++++------------ .../config/metric/gateway/processors_test.go | 34 +++----- .../config/metric/gateway/receivers_test.go | 12 +-- .../mocks/gateway_config_builder.go | 22 ++--- .../metricpipeline/reconciler_test.go | 30 +++---- 5 files changed, 75 insertions(+), 107 deletions(-) diff --git a/internal/otelcollector/config/metric/gateway/config_builder_test.go b/internal/otelcollector/config/metric/gateway/config_builder_test.go index ff66a95db..70e7a497a 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder_test.go +++ b/internal/otelcollector/config/metric/gateway/config_builder_test.go @@ -21,7 +21,6 @@ func TestMakeConfig(t *testing.T) { ctx := context.Background() fakeClient := fake.NewClientBuilder().Build() sut := Builder{Reader: fakeClient} - gatewayNamespace := "test-namespace" t.Run("otlp exporter endpoint", func(t *testing.T) { collectorConfig, envVars, err := sut.Build( @@ -29,8 +28,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPOutput(testutils.OTLPEndpoint("http://localhost")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -50,8 +48,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPOutput(testutils.OTLPEndpoint("https://localhost")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test") @@ -66,8 +63,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test-insecure").WithOTLPOutput(testutils.OTLPEndpoint("http://localhost")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-insecure") @@ -82,8 +78,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test-basic-auth").WithOTLPOutput(testutils.OTLPBasicAuth("user", "password")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-basic-auth") @@ -105,8 +100,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test-custom-header").WithOTLPOutput(testutils.OTLPCustomHeader("Authorization", "TOKEN_VALUE", "Api-Token")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-custom-header") @@ -127,8 +121,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test-mtls").WithOTLPOutput(testutils.OTLPClientTLSFromString("ca", "cert", "key")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Contains(t, collectorConfig.Exporters, "otlp/test-mtls") @@ -150,8 +143,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -167,8 +159,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -183,8 +174,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Equal(t, 256, collectorConfig.Exporters["otlp/test"].OTLP.SendingQueue.QueueSize, "Pipeline should have the full queue size") @@ -198,8 +188,7 @@ func TestMakeConfig(t *testing.T) { testutils.NewMetricPipelineBuilder().WithName("test-2").Build(), testutils.NewMetricPipelineBuilder().WithName("test-3").Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) require.Equal(t, 85, collectorConfig.Exporters["otlp/test-1"].OTLP.SendingQueue.QueueSize, "Queue size should be divided by the number of pipelines") @@ -214,8 +203,9 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(false).Build(), }, - gatewayNamespace, - true, + BuildOptions{ + KymaInputAllowed: true, + }, ) require.NoError(t, err) @@ -243,8 +233,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).WithPrometheusInputDiagnosticMetrics(true).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -269,8 +258,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).WithPrometheusInputDiagnosticMetrics(false).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -296,8 +284,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithPrometheusInput(true).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -330,8 +317,7 @@ func TestMakeConfig(t *testing.T) { WithRuntimeInputContainerMetrics(true). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -360,8 +346,7 @@ func TestMakeConfig(t *testing.T) { WithRuntimeInputContainerMetrics(false). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -391,8 +376,7 @@ func TestMakeConfig(t *testing.T) { WithRuntimeInputPodMetrics(false). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -418,8 +402,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).WithIstioInputDiagnosticMetrics(true).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -444,8 +427,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).WithIstioInputDiagnosticMetrics(false).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -471,8 +453,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithIstioInput(true).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -498,8 +479,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -525,8 +505,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithOTLPInput(true).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -552,8 +531,9 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), }, - gatewayNamespace, - true, + BuildOptions{ + KymaInputAllowed: true, + }, ) require.NoError(t, err) @@ -568,6 +548,7 @@ func TestMakeConfig(t *testing.T) { "filter/drop-if-input-source-runtime", "filter/drop-if-input-source-prometheus", "filter/drop-if-input-source-istio", + "transform/set-instrumentation-scope-kyma", "resource/insert-cluster-name", "transform/resolve-service-name", "batch", @@ -580,8 +561,7 @@ func TestMakeConfig(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -624,8 +604,7 @@ func TestMakeConfig(t *testing.T) { WithIstioInput(true). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -706,8 +685,7 @@ func TestMakeConfig(t *testing.T) { WithOTLPInput(tt.withOtlpInput). WithOTLPOutput(testutils.OTLPEndpoint("https://localhost")).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) diff --git a/internal/otelcollector/config/metric/gateway/processors_test.go b/internal/otelcollector/config/metric/gateway/processors_test.go index 43adbd389..d94d430f3 100644 --- a/internal/otelcollector/config/metric/gateway/processors_test.go +++ b/internal/otelcollector/config/metric/gateway/processors_test.go @@ -15,7 +15,6 @@ func TestProcessors(t *testing.T) { ctx := context.Background() fakeClient := fake.NewClientBuilder().Build() sut := Builder{Reader: fakeClient} - gatewayNamespace := "test-namespace" t.Run("insert cluster name processor", func(t *testing.T) { collectorConfig, _, err := sut.Build( @@ -23,8 +22,7 @@ func TestProcessors(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -40,8 +38,7 @@ func TestProcessors(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -56,8 +53,7 @@ func TestProcessors(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -72,8 +68,7 @@ func TestProcessors(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -107,8 +102,7 @@ func TestProcessors(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithOTLPInput(false).Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -145,8 +139,7 @@ func TestProcessors(t *testing.T) { WithOTLPInput(true, testutils.IncludeNamespaces("ns-1", "ns-2")). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -188,8 +181,7 @@ func TestProcessors(t *testing.T) { WithOTLPInput(true, testutils.ExcludeNamespaces("ns-1", "ns-2")). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -229,8 +221,7 @@ func TestProcessors(t *testing.T) { WithPrometheusInputDiagnosticMetrics(false). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -251,8 +242,7 @@ func TestProcessors(t *testing.T) { WithIstioInputDiagnosticMetrics(false). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -275,8 +265,7 @@ func TestProcessors(t *testing.T) { WithRuntimeInputPodMetrics(false). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -296,8 +285,7 @@ func TestProcessors(t *testing.T) { WithRuntimeInputContainerMetrics(false). Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) diff --git a/internal/otelcollector/config/metric/gateway/receivers_test.go b/internal/otelcollector/config/metric/gateway/receivers_test.go index 82a2b37df..95fccba36 100644 --- a/internal/otelcollector/config/metric/gateway/receivers_test.go +++ b/internal/otelcollector/config/metric/gateway/receivers_test.go @@ -14,7 +14,6 @@ func TestReceivers(t *testing.T) { ctx := context.Background() fakeClient := fake.NewClientBuilder().Build() sut := Builder{Reader: fakeClient} - gatewayNamespace := "test-namespace" t.Run("OTLP receiver", func(t *testing.T) { collectorConfig, _, err := sut.Build( @@ -22,8 +21,7 @@ func TestReceivers(t *testing.T) { []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").Build(), }, - gatewayNamespace, - false, + BuildOptions{}, ) require.NoError(t, err) @@ -34,13 +32,17 @@ func TestReceivers(t *testing.T) { }) t.Run("singleton kyma stats receiver creator", func(t *testing.T) { + gatewayNamespace := "test-namespace" + collectorConfig, _, err := sut.Build( ctx, []telemetryv1alpha1.MetricPipeline{ testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), }, - gatewayNamespace, - true, + BuildOptions{ + GatewayNamespace: gatewayNamespace, + KymaInputAllowed: true, + }, ) require.NoError(t, err) diff --git a/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go b/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go index 651918fc8..1657e95c2 100644 --- a/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go +++ b/internal/reconciler/metricpipeline/mocks/gateway_config_builder.go @@ -19,9 +19,9 @@ type GatewayConfigBuilder struct { mock.Mock } -// Build provides a mock function with given fields: ctx, pipelines, gatewayNamespace, kymaInputAllowed -func (_m *GatewayConfigBuilder) Build(ctx context.Context, pipelines []v1alpha1.MetricPipeline, gatewayNamespace string, kymaInputAllowed bool) (*gateway.Config, otlpexporter.EnvVars, error) { - ret := _m.Called(ctx, pipelines, gatewayNamespace, kymaInputAllowed) +// Build provides a mock function with given fields: ctx, pipelines, options +func (_m *GatewayConfigBuilder) Build(ctx context.Context, pipelines []v1alpha1.MetricPipeline, options gateway.BuildOptions) (*gateway.Config, otlpexporter.EnvVars, error) { + ret := _m.Called(ctx, pipelines, options) if len(ret) == 0 { panic("no return value specified for Build") @@ -30,27 +30,27 @@ func (_m *GatewayConfigBuilder) Build(ctx context.Context, pipelines []v1alpha1. var r0 *gateway.Config var r1 otlpexporter.EnvVars var r2 error - if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) (*gateway.Config, otlpexporter.EnvVars, error)); ok { - return rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) + if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline, gateway.BuildOptions) (*gateway.Config, otlpexporter.EnvVars, error)); ok { + return rf(ctx, pipelines, options) } - if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) *gateway.Config); ok { - r0 = rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) + if rf, ok := ret.Get(0).(func(context.Context, []v1alpha1.MetricPipeline, gateway.BuildOptions) *gateway.Config); ok { + r0 = rf(ctx, pipelines, options) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*gateway.Config) } } - if rf, ok := ret.Get(1).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) otlpexporter.EnvVars); ok { - r1 = rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) + if rf, ok := ret.Get(1).(func(context.Context, []v1alpha1.MetricPipeline, gateway.BuildOptions) otlpexporter.EnvVars); ok { + r1 = rf(ctx, pipelines, options) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(otlpexporter.EnvVars) } } - if rf, ok := ret.Get(2).(func(context.Context, []v1alpha1.MetricPipeline, string, bool) error); ok { - r2 = rf(ctx, pipelines, gatewayNamespace, kymaInputAllowed) + if rf, ok := ret.Get(2).(func(context.Context, []v1alpha1.MetricPipeline, gateway.BuildOptions) error); ok { + r2 = rf(ctx, pipelines, options) } else { r2 = ret.Error(2) } diff --git a/internal/reconciler/metricpipeline/reconciler_test.go b/internal/reconciler/metricpipeline/reconciler_test.go index 08980df58..41d166c17 100644 --- a/internal/reconciler/metricpipeline/reconciler_test.go +++ b/internal/reconciler/metricpipeline/reconciler_test.go @@ -74,7 +74,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -133,7 +133,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -192,7 +192,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -254,7 +254,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -322,7 +322,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -391,7 +391,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -465,7 +465,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline, secret).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -524,7 +524,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything).Return(nil) @@ -593,7 +593,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil) pipelineLockStub := &metricMocks.PipelineLock{} pipelineLockStub.On("TryAcquireLock", mock.Anything, mock.Anything).Return(resourcelock.ErrMaxPipelinesExceeded) @@ -750,7 +750,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -887,7 +887,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -976,7 +976,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline, secret).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -1044,7 +1044,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) gatewayApplierDeleterMock := &metricMocks.GatewayApplierDeleter{} gatewayApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) @@ -1115,7 +1115,7 @@ func TestReconcile(t *testing.T) { fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline).WithStatusSubresource(&pipeline).Build() gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("DeleteResources", mock.Anything, mock.Anything).Return(nil).Times(1) @@ -1242,7 +1242,7 @@ func TestReconcile(t *testing.T) { agentConfigBuilderMock.On("Build", containsPipeline(pipeline), mock.Anything).Return(&agent.Config{}).Times(1) gatewayConfigBuilderMock := &metricMocks.GatewayConfigBuilder{} - gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything, mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) + gatewayConfigBuilderMock.On("Build", mock.Anything, containsPipeline(pipeline), mock.Anything).Return(&gateway.Config{}, nil, nil).Times(1) agentApplierDeleterMock := &metricMocks.AgentApplierDeleter{} agentApplierDeleterMock.On("ApplyResources", mock.Anything, mock.Anything, mock.Anything).Return(nil) From 16ba1bf1a2900911e4a716c7056858a5300573db Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 06:41:53 +0200 Subject: [PATCH 10/23] add unit test for setting kyma instrumentation scope --- .../config/metric/gateway/processors_test.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/otelcollector/config/metric/gateway/processors_test.go b/internal/otelcollector/config/metric/gateway/processors_test.go index d94d430f3..becd33ec7 100644 --- a/internal/otelcollector/config/metric/gateway/processors_test.go +++ b/internal/otelcollector/config/metric/gateway/processors_test.go @@ -295,4 +295,27 @@ func TestProcessors(t *testing.T) { expectedCondition := "instrumentation_scope.name == \"io.kyma-project.telemetry/runtime\" and IsMatch(name, \"(^k8s.container.*)|(^container.*)\")" require.Equal(t, expectedCondition, runtimeContainerMetricsFilter.Metrics.Metric[0]) }) + + t.Run("kyma instrumentation scope transform processor", func(t *testing.T) { + collectorConfig, _, err := sut.Build( + ctx, + []telemetryv1alpha1.MetricPipeline{ + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + }, + BuildOptions{ + InstrumentationScopeVersion: "main", + KymaInputAllowed: true, + }, + ) + require.NoError(t, err) + + require.NotNil(t, collectorConfig.Processors.SetInstrumentationScopeKyma) + require.Equal(t, "ignore", collectorConfig.Processors.SetInstrumentationScopeKyma.ErrorMode) + require.Len(t, collectorConfig.Processors.SetInstrumentationScopeKyma.MetricStatements, 1) + require.Equal(t, "scope", collectorConfig.Processors.SetInstrumentationScopeKyma.MetricStatements[0].Context) + require.Len(t, collectorConfig.Processors.SetInstrumentationScopeKyma.MetricStatements[0].Statements, 2) + require.Equal(t, "set(version, \"main\") where name == \"otelcol/kymastats\"", collectorConfig.Processors.SetInstrumentationScopeKyma.MetricStatements[0].Statements[0]) + require.Equal(t, "set(name, \"io.kyma-project.telemetry/kyma\") where name == \"otelcol/kymastats\"", collectorConfig.Processors.SetInstrumentationScopeKyma.MetricStatements[0].Statements[1]) + + }) } From 4d56ba043fd7ba7068387f956504354a04736051 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 06:42:29 +0200 Subject: [PATCH 11/23] fix linting --- .../otelcollector/config/metric/gateway/receivers_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/otelcollector/config/metric/gateway/receivers_test.go b/internal/otelcollector/config/metric/gateway/receivers_test.go index 95fccba36..50eeb9629 100644 --- a/internal/otelcollector/config/metric/gateway/receivers_test.go +++ b/internal/otelcollector/config/metric/gateway/receivers_test.go @@ -4,10 +4,11 @@ import ( "context" "testing" - telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1" - "github.com/kyma-project/telemetry-manager/internal/testutils" "github.com/stretchr/testify/require" "sigs.k8s.io/controller-runtime/pkg/client/fake" + + telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1" + "github.com/kyma-project/telemetry-manager/internal/testutils" ) func TestReceivers(t *testing.T) { From a978ed795e7accddca45ca263b258b5af9e69d33 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 06:53:09 +0200 Subject: [PATCH 12/23] rename kyma input annotation and refactor it into a const --- .../otelcollector/config/metric/gateway/config_builder.go | 4 +++- .../config/metric/gateway/config_builder_test.go | 4 ++-- .../otelcollector/config/metric/gateway/processors_test.go | 2 +- .../otelcollector/config/metric/gateway/receivers_test.go | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/otelcollector/config/metric/gateway/config_builder.go b/internal/otelcollector/config/metric/gateway/config_builder.go index df4341545..30afe68da 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder.go +++ b/internal/otelcollector/config/metric/gateway/config_builder.go @@ -13,6 +13,8 @@ import ( "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/otlpexporter" ) +const KymaInputAnnotation = "telemetry.kyma-project.io/experimental-kyma-input" + type Builder struct { Reader client.Reader } @@ -326,5 +328,5 @@ func isRuntimeContainerMetricsEnabled(input telemetryv1alpha1.MetricPipelineInpu } func isKymaInputEnabled(annotations map[string]string, kymaInputAllowed bool) bool { - return kymaInputAllowed && annotations["experimental-kyma-input"] == "true" + return kymaInputAllowed && annotations[KymaInputAnnotation] == "true" } diff --git a/internal/otelcollector/config/metric/gateway/config_builder_test.go b/internal/otelcollector/config/metric/gateway/config_builder_test.go index 70e7a497a..d1e94582c 100644 --- a/internal/otelcollector/config/metric/gateway/config_builder_test.go +++ b/internal/otelcollector/config/metric/gateway/config_builder_test.go @@ -529,7 +529,7 @@ func TestMakeConfig(t *testing.T) { collectorConfig, _, err := sut.Build( ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{KymaInputAnnotation: "true"}).Build(), }, BuildOptions{ KymaInputAllowed: true, @@ -559,7 +559,7 @@ func TestMakeConfig(t *testing.T) { collectorConfig, _, err := sut.Build( ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{KymaInputAnnotation: "true"}).Build(), }, BuildOptions{}, ) diff --git a/internal/otelcollector/config/metric/gateway/processors_test.go b/internal/otelcollector/config/metric/gateway/processors_test.go index becd33ec7..e729a56d7 100644 --- a/internal/otelcollector/config/metric/gateway/processors_test.go +++ b/internal/otelcollector/config/metric/gateway/processors_test.go @@ -300,7 +300,7 @@ func TestProcessors(t *testing.T) { collectorConfig, _, err := sut.Build( ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{KymaInputAnnotation: "true"}).Build(), }, BuildOptions{ InstrumentationScopeVersion: "main", diff --git a/internal/otelcollector/config/metric/gateway/receivers_test.go b/internal/otelcollector/config/metric/gateway/receivers_test.go index 50eeb9629..3bd87c95a 100644 --- a/internal/otelcollector/config/metric/gateway/receivers_test.go +++ b/internal/otelcollector/config/metric/gateway/receivers_test.go @@ -38,7 +38,7 @@ func TestReceivers(t *testing.T) { collectorConfig, _, err := sut.Build( ctx, []telemetryv1alpha1.MetricPipeline{ - testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{"experimental-kyma-input": "true"}).Build(), + testutils.NewMetricPipelineBuilder().WithName("test").WithAnnotations(map[string]string{KymaInputAnnotation: "true"}).Build(), }, BuildOptions{ GatewayNamespace: gatewayNamespace, From 000962f24e0d617700c0a350a277a5d50cfed710 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Mon, 5 Aug 2024 12:59:53 +0200 Subject: [PATCH 13/23] rename v1beta1 test label to experimental --- .github/workflows/pr-integration.yml | 4 ++-- test/e2e/logs_basic_v1beta1_test.go | 2 +- test/e2e/metrics_basic_v1beta1_test.go | 2 +- test/e2e/traces_basic_v1beta1_test.go | 2 +- test/testkit/suite/suite.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pr-integration.yml b/.github/workflows/pr-integration.yml index b34df9e05..5bba92cbf 100644 --- a/.github/workflows/pr-integration.yml +++ b/.github/workflows/pr-integration.yml @@ -37,7 +37,7 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} - name: Run tests - run: bin/ginkgo run --tags e2e --label-filter="${{ matrix.ginkgo-labels }} && !v1beta1" test/e2e + run: bin/ginkgo run --tags e2e --label-filter="${{ matrix.ginkgo-labels }} && !experimental" test/e2e - name: Finalize test uses: "./.github/template/finalize-test" @@ -64,7 +64,7 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} - name: Run tests - run: bin/ginkgo run --tags e2e --label-filter="${{ matrix.ginkgo-labels }} && v1beta1" test/e2e + run: bin/ginkgo run --tags e2e --label-filter="${{ matrix.ginkgo-labels }} && experimental" test/e2e - name: Finalize test uses: "./.github/template/finalize-test" diff --git a/test/e2e/logs_basic_v1beta1_test.go b/test/e2e/logs_basic_v1beta1_test.go index 83682c938..1ce716437 100644 --- a/test/e2e/logs_basic_v1beta1_test.go +++ b/test/e2e/logs_basic_v1beta1_test.go @@ -19,7 +19,7 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/suite" ) -var _ = Describe(suite.ID(), Label(suite.LabelLogs, suite.LabelV1Beta1), Ordered, func() { +var _ = Describe(suite.ID(), Label(suite.LabelLogs, suite.LabelExperimental), Ordered, func() { var ( mockNs = suite.ID() pipelineName = suite.ID() diff --git a/test/e2e/metrics_basic_v1beta1_test.go b/test/e2e/metrics_basic_v1beta1_test.go index ff581cc26..ec6caa085 100644 --- a/test/e2e/metrics_basic_v1beta1_test.go +++ b/test/e2e/metrics_basic_v1beta1_test.go @@ -26,7 +26,7 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/suite" ) -var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelV1Beta1), Ordered, func() { +var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { var ( mockNs = suite.ID() pipelineName = suite.ID() diff --git a/test/e2e/traces_basic_v1beta1_test.go b/test/e2e/traces_basic_v1beta1_test.go index 3160e50a6..a5f07f590 100644 --- a/test/e2e/traces_basic_v1beta1_test.go +++ b/test/e2e/traces_basic_v1beta1_test.go @@ -26,7 +26,7 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/suite" ) -var _ = Describe(suite.ID(), Label(suite.LabelTraces, suite.LabelV1Beta1), func() { +var _ = Describe(suite.ID(), Label(suite.LabelTraces, suite.LabelExperimental), func() { var ( mockNs = suite.ID() pipelineName = suite.ID() diff --git a/test/testkit/suite/suite.go b/test/testkit/suite/suite.go index c53ec82da..3f3b6aa3f 100644 --- a/test/testkit/suite/suite.go +++ b/test/testkit/suite/suite.go @@ -43,7 +43,7 @@ const ( LabelTraces = "traces" LabelMetrics = "metrics" LabelTelemetry = "telemetry" - LabelV1Beta1 = "v1beta1" + LabelExperimental = "experimental" LabelTelemetryLogAnalysis = "telemetry-log-analysis" LabelMaxPipeline = "max-pipeline" From fd8e54b4fee106cda963b9da73bc0bcdd46cfd73 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Tue, 6 Aug 2024 16:28:02 +0200 Subject: [PATCH 14/23] add e2e for kyma status metrics --- test/e2e/metrics_kyma_input_test.go | 154 ++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 test/e2e/metrics_kyma_input_test.go diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go new file mode 100644 index 000000000..269aef7b1 --- /dev/null +++ b/test/e2e/metrics_kyma_input_test.go @@ -0,0 +1,154 @@ +package e2e + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" + "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric/gateway" + "github.com/kyma-project/telemetry-manager/internal/testutils" + "github.com/kyma-project/telemetry-manager/test/testkit/assert" + kitk8s "github.com/kyma-project/telemetry-manager/test/testkit/k8s" + kitkyma "github.com/kyma-project/telemetry-manager/test/testkit/kyma" + . "github.com/kyma-project/telemetry-manager/test/testkit/matchers/metric" + "github.com/kyma-project/telemetry-manager/test/testkit/mocks/backend" + "github.com/kyma-project/telemetry-manager/test/testkit/periodic" + "github.com/kyma-project/telemetry-manager/test/testkit/suite" + "k8s.io/apimachinery/pkg/types" + "net/http" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { + var ( + mockNs = suite.ID() + pipelineName = suite.ID() + backendExportURL string + ) + + makeResources := func() []client.Object { + var objs []client.Object + objs = append(objs, kitk8s.NewNamespace(mockNs).K8sObject()) + + backend := backend.New(mockNs, backend.SignalTypeMetrics) + objs = append(objs, backend.K8sObjects()...) + backendExportURL = backend.ExportURL(proxyClient) + + metricPipeline := testutils.NewMetricPipelineBuilder(). + WithName(pipelineName). + WithAnnotations(map[string]string{gateway.KymaInputAnnotation: "true"}). + WithOTLPOutput(testutils.OTLPEndpoint(backend.Endpoint())). + Build() + objs = append(objs, &metricPipeline) + + return objs + } + + Context("When a metricpipeline with kyma input annotation exists", Ordered, func() { + BeforeAll(func() { + k8sObjects := makeResources() + + DeferCleanup(func() { + Expect(kitk8s.DeleteObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed()) + }) + + Expect(kitk8s.CreateObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed()) + }) + + It("Ensures the metric gateway deployment is ready", func() { + assert.DeploymentReady(ctx, k8sClient, kitkyma.MetricGatewayName) + }) + + It("Ensures the metrics backend is ready", func() { + assert.DeploymentReady(ctx, k8sClient, types.NamespacedName{Name: backend.DefaultName, Namespace: mockNs}) + }) + + It("Ensures the metricpipeline is healthy", func() { + assert.MetricPipelineHealthy(ctx, k8sClient, pipelineName) + }) + + It("Ensures Telemetry module status metrics are sent to backend", func() { + Eventually(func(g Gomega) { + resp, err := proxyClient.Get(backendExportURL) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(resp).To(HaveHTTPStatus(http.StatusOK)) + g.Expect(resp).To(HaveHTTPBody(SatisfyAll( + ContainMd(SatisfyAll( + ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), + ContainMetric(SatisfyAll( + WithName(Equal("kyma.module.status.state")), + ContainDataPointAttrs(HaveKey("state")), + )), + ContainScope(SatisfyAll( + WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), + WithScopeVersion( + SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ), + ), + )), + )), + ContainMd(SatisfyAll( + ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), + ContainMetric(SatisfyAll( + WithName(Equal("kyma.module.status.conditions")), + ContainDataPointAttrs(HaveKeyWithValue("type", "LogComponentsHealthy")), + ContainDataPointAttrs(HaveKey("status")), + ContainDataPointAttrs(HaveKey("reason")), + )), + ContainScope(SatisfyAll( + WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), + WithScopeVersion( + SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ), + ), + )), + )), + ContainMd(SatisfyAll( + ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), + ContainMetric(SatisfyAll( + WithName(Equal("kyma.module.status.conditions")), + ContainDataPointAttrs(HaveKeyWithValue("type", "TraceComponentsHealthy")), + ContainDataPointAttrs(HaveKey("status")), + ContainDataPointAttrs(HaveKey("reason")), + )), + ContainScope(SatisfyAll( + WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), + WithScopeVersion( + SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ), + ), + )), + )), + ContainMd(SatisfyAll( + ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), + ContainMetric(SatisfyAll( + WithName(Equal("kyma.module.status.conditions")), + ContainDataPointAttrs(HaveKeyWithValue("type", "MetricComponentsHealthy")), + ContainDataPointAttrs(HaveKey("status")), + ContainDataPointAttrs(HaveKey("reason")), + )), + ContainScope(SatisfyAll( + WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), + WithScopeVersion( + SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ), + ), + )), + )), + ))) + }, periodic.TelemetryEventuallyTimeout, periodic.TelemetryInterval).Should(Succeed()) + }) + }) +}) From 8a0c53e61bd1d3ecd01a9f0714eaef534f1acbfa Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Tue, 6 Aug 2024 16:46:20 +0200 Subject: [PATCH 15/23] add go build for the kyma input test --- test/e2e/metrics_kyma_input_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 269aef7b1..70d954313 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -1,3 +1,5 @@ +//go:build e2e + package e2e import ( From 6ecdf6c2c0910eaa11a864aa084a26b5d0061fef Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Tue, 6 Aug 2024 17:28:50 +0200 Subject: [PATCH 16/23] add e2e test for new gateway RBAC --- test/e2e/metrics_resources_test.go | 57 ++++++++++++++++++++++++++++-- test/testkit/kyma/common_names.go | 2 ++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/test/e2e/metrics_resources_test.go b/test/e2e/metrics_resources_test.go index 60bd64f84..4977dd2f9 100644 --- a/test/e2e/metrics_resources_test.go +++ b/test/e2e/metrics_resources_test.go @@ -19,11 +19,11 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/suite" ) -var _ = Describe(suite.ID(), Label(suite.LabelMetrics), Ordered, func() { - var pipelineName = suite.ID() +var _ = Describe(suite.ID(), Ordered, func() { const ownerReferenceKind = "MetricPipeline" - Context("When a MetricPipeline exists", Ordered, func() { + Context("When a MetricPipeline exists", Label(suite.LabelMetrics), Ordered, func() { + var pipelineName = suite.ID() endpointKey := "metrics-endpoint" secret := kitk8s.NewOpaqueSecret("metrics-resources", kitkyma.DefaultNamespaceName, kitk8s.WithStringData(endpointKey, "http://localhost:4317")) metricPipeline := testutils.NewMetricPipelineBuilder(). @@ -136,6 +136,57 @@ var _ = Describe(suite.ID(), Label(suite.LabelMetrics), Ordered, func() { }) }) + + //TODO: Move the tests in this Context to the Context above ("When a MetricPipeline exists") when the feature flag --kyma-input-allowed is removed + Context("When a MetricPipeline exists in experimental channel", Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { + var pipelineName = suite.IDWithSuffix("experimental") + endpointKey := "metrics-endpoint" + secret := kitk8s.NewOpaqueSecret("metrics-resources", kitkyma.DefaultNamespaceName, kitk8s.WithStringData(endpointKey, "http://localhost:4317")) + metricPipeline := testutils.NewMetricPipelineBuilder(). + WithName(pipelineName). + WithOTLPOutput(testutils.OTLPEndpointFromSecret(secret.Name(), secret.Namespace(), endpointKey)). + WithRuntimeInput(true). + Build() + + BeforeAll(func() { + DeferCleanup(func() { + Expect(kitk8s.DeleteObjects(ctx, k8sClient, &metricPipeline)).Should(Succeed()) + }) + Expect(kitk8s.CreateObjects(ctx, k8sClient, &metricPipeline, secret.K8sObject())).Should(Succeed()) + }) + + Context("should have experimental gateway resources", Ordered, func() { + It("Should have a gateway Role owned by the MetricPipeline", func() { + var role rbacv1.Role + assert.HasOwnerReference(ctx, k8sClient, &role, kitkyma.MetricGatewayRole, ownerReferenceKind, pipelineName) + }) + + It("Should have a gateway RoleBinding owned by the MetricPipeline", func() { + var roleBinding rbacv1.RoleBinding + assert.HasOwnerReference(ctx, k8sClient, &roleBinding, kitkyma.MetricGatewayRoleBinding, ownerReferenceKind, pipelineName) + }) + }) + + It("Should clean up experimental gateway resources when pipeline becomes non-reconcilable", func() { + By("Deleting referenced secret", func() { + Expect(k8sClient.Delete(ctx, secret.K8sObject())).Should(Succeed()) + }) + + Eventually(func(g Gomega) bool { + var role rbacv1.Role + err := k8sClient.Get(ctx, kitkyma.MetricGatewayRole, &role) + return apierrors.IsNotFound(err) + }, periodic.EventuallyTimeout, periodic.DefaultInterval).Should(BeTrue(), "Role still exists") + + Eventually(func(g Gomega) bool { + var roleBinding rbacv1.ClusterRoleBinding + err := k8sClient.Get(ctx, kitkyma.MetricGatewayRoleBinding, &roleBinding) + return apierrors.IsNotFound(err) + }, periodic.EventuallyTimeout, periodic.DefaultInterval).Should(BeTrue(), "RoleBinding still exists") + + }) + + }) }) func gatewayResourcesAreDeleted() { diff --git a/test/testkit/kyma/common_names.go b/test/testkit/kyma/common_names.go index 544242fa0..d87b31824 100644 --- a/test/testkit/kyma/common_names.go +++ b/test/testkit/kyma/common_names.go @@ -36,6 +36,8 @@ var ( MetricGatewayClusterRole = types.NamespacedName{Name: MetricGatewayBaseName, Namespace: SystemNamespaceName} MetricGatewayClusterRoleBinding = types.NamespacedName{Name: MetricGatewayBaseName, Namespace: SystemNamespaceName} MetricGatewayConfigMap = types.NamespacedName{Name: MetricGatewayBaseName, Namespace: SystemNamespaceName} + MetricGatewayRole = types.NamespacedName{Name: MetricGatewayBaseName, Namespace: SystemNamespaceName} + MetricGatewayRoleBinding = types.NamespacedName{Name: MetricGatewayBaseName, Namespace: SystemNamespaceName} MetricAgentName = types.NamespacedName{Name: MetricAgentBaseName, Namespace: SystemNamespaceName} MetricAgentMetricsService = types.NamespacedName{Name: MetricAgentBaseName + "-metrics", Namespace: SystemNamespaceName} From 9a7557bc056e60065f7c96ecd8f006fe354e68a5 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Tue, 6 Aug 2024 17:31:15 +0200 Subject: [PATCH 17/23] fix linting --- test/e2e/metrics_kyma_input_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 70d954313..5c6fbb53a 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -3,8 +3,12 @@ package e2e import ( + "net/http" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric" "github.com/kyma-project/telemetry-manager/internal/otelcollector/config/metric/gateway" @@ -16,9 +20,6 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/mocks/backend" "github.com/kyma-project/telemetry-manager/test/testkit/periodic" "github.com/kyma-project/telemetry-manager/test/testkit/suite" - "k8s.io/apimachinery/pkg/types" - "net/http" - "sigs.k8s.io/controller-runtime/pkg/client" ) var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { From 4127e88575dfd1095a9957c5281fbd32433ac8c5 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Tue, 6 Aug 2024 18:40:13 +0200 Subject: [PATCH 18/23] add e2e test for kyma status metrics when annotation is absent --- test/e2e/metrics_kyma_input_test.go | 79 ++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 5c6fbb53a..9c3153a36 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -24,55 +24,74 @@ import ( var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { var ( - mockNs = suite.ID() - pipelineName = suite.ID() - backendExportURL string + mockNs = suite.ID() + + pipelineWithAnnotationName = suite.IDWithSuffix("with-annotation") + backendForKymaInputName = suite.IDWithSuffix("for-kyma-input") + backendForKymaInputExportURL string + + pipelineWithoutAnnotationName = suite.IDWithSuffix("without-annotation") + backendForNoKymaInputName = suite.IDWithSuffix("for-no-kyma-input") + backendForNoKymaInputExportURL string ) makeResources := func() []client.Object { var objs []client.Object objs = append(objs, kitk8s.NewNamespace(mockNs).K8sObject()) - backend := backend.New(mockNs, backend.SignalTypeMetrics) - objs = append(objs, backend.K8sObjects()...) - backendExportURL = backend.ExportURL(proxyClient) + backendForKymaInput := backend.New(mockNs, backend.SignalTypeMetrics, backend.WithName(backendForKymaInputName)) + objs = append(objs, backendForKymaInput.K8sObjects()...) + backendForKymaInputExportURL = backendForKymaInput.ExportURL(proxyClient) + + backendForNoKymaInput := backend.New(mockNs, backend.SignalTypeMetrics, backend.WithName(backendForNoKymaInputName)) + objs = append(objs, backendForNoKymaInput.K8sObjects()...) + backendForNoKymaInputExportURL = backendForNoKymaInput.ExportURL(proxyClient) - metricPipeline := testutils.NewMetricPipelineBuilder(). - WithName(pipelineName). + metricPipelineWithAnnotation := testutils.NewMetricPipelineBuilder(). + WithName(pipelineWithAnnotationName). WithAnnotations(map[string]string{gateway.KymaInputAnnotation: "true"}). - WithOTLPOutput(testutils.OTLPEndpoint(backend.Endpoint())). + WithOTLPOutput(testutils.OTLPEndpoint(backendForKymaInput.Endpoint())). + Build() + objs = append(objs, &metricPipelineWithAnnotation) + + metricPipelineWithoutAnnotation := testutils.NewMetricPipelineBuilder(). + WithName(pipelineWithoutAnnotationName). + WithOTLPOutput(testutils.OTLPEndpoint(backendForNoKymaInput.Endpoint())). Build() - objs = append(objs, &metricPipeline) + objs = append(objs, &metricPipelineWithoutAnnotation) return objs } - Context("When a metricpipeline with kyma input annotation exists", Ordered, func() { - BeforeAll(func() { - k8sObjects := makeResources() - - DeferCleanup(func() { - Expect(kitk8s.DeleteObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed()) - }) + BeforeAll(func() { + k8sObjects := makeResources() - Expect(kitk8s.CreateObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed()) + DeferCleanup(func() { + Expect(kitk8s.DeleteObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed()) }) + Expect(kitk8s.CreateObjects(ctx, k8sClient, k8sObjects...)).Should(Succeed()) + }) + + Context("When a metricpipeline with kyma input annotation exists", Ordered, func() { + It("Ensures the metric gateway deployment is ready", func() { assert.DeploymentReady(ctx, k8sClient, kitkyma.MetricGatewayName) }) - It("Ensures the metrics backend is ready", func() { - assert.DeploymentReady(ctx, k8sClient, types.NamespacedName{Name: backend.DefaultName, Namespace: mockNs}) + It("Ensures the metrics backends are ready", func() { + assert.DeploymentReady(ctx, k8sClient, types.NamespacedName{Name: backendForKymaInputName, Namespace: mockNs}) + assert.DeploymentReady(ctx, k8sClient, types.NamespacedName{Name: backendForNoKymaInputName, Namespace: mockNs}) }) - It("Ensures the metricpipeline is healthy", func() { - assert.MetricPipelineHealthy(ctx, k8sClient, pipelineName) + It("Ensures the metric pipelines are healthy", func() { + assert.MetricPipelineHealthy(ctx, k8sClient, pipelineWithAnnotationName) + assert.MetricPipelineHealthy(ctx, k8sClient, pipelineWithoutAnnotationName) }) - It("Ensures Telemetry module status metrics are sent to backend", func() { + It("Ensures Telemetry module status metrics are sent to the backend which is receiving metrics from the pipeline with annotation", func() { Eventually(func(g Gomega) { - resp, err := proxyClient.Get(backendExportURL) + resp, err := proxyClient.Get(backendForKymaInputExportURL) g.Expect(err).NotTo(HaveOccurred()) g.Expect(resp).To(HaveHTTPStatus(http.StatusOK)) g.Expect(resp).To(HaveHTTPBody(SatisfyAll( @@ -153,5 +172,17 @@ var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), ))) }, periodic.TelemetryEventuallyTimeout, periodic.TelemetryInterval).Should(Succeed()) }) + + It("Ensures Telemetry module status metrics are not sent to the backend which is receiving metrics from the pipeline without annotation", func() { + Consistently(func(g Gomega) { + resp, err := proxyClient.Get(backendForNoKymaInputExportURL) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(resp).To(HaveHTTPStatus(http.StatusOK)) + g.Expect(resp).To(HaveHTTPBody(SatisfyAll( + Not(ContainMd(ContainMetric(WithName(Equal("kyma.module.status.state"))))), + Not(ContainMd(ContainMetric(WithName(Equal("kyma.module.status.conditions"))))), + ))) + }, periodic.TelemetryConsistentlyTimeout, periodic.TelemetryInterval).Should(Succeed()) + }) }) }) From 511bb8951a78296e716ef43129bec7ab47d02496 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Wed, 7 Aug 2024 16:50:58 +0200 Subject: [PATCH 19/23] create a flatMetrics struct for each metric data point --- test/testkit/matchers/metric/pmetric_utils.go | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/test/testkit/matchers/metric/pmetric_utils.go b/test/testkit/matchers/metric/pmetric_utils.go index b625f9eeb..a7033b30f 100644 --- a/test/testkit/matchers/metric/pmetric_utils.go +++ b/test/testkit/matchers/metric/pmetric_utils.go @@ -47,15 +47,18 @@ func flattenMetrics(md pmetric.Metrics) []FlatMetric { scopeMetrics := resourceMetrics.ScopeMetrics().At(j) for k := 0; k < scopeMetrics.Metrics().Len(); k++ { metric := scopeMetrics.Metrics().At(k) - flatMetrics = append(flatMetrics, FlatMetric{ - Name: metric.Name(), - Description: metric.Description(), - ScopeAndVersion: ScopeVersion{scopeMetrics.Scope().Name(), scopeMetrics.Scope().Version()}, - ResourceAttributes: attributeToMap(resourceMetrics.Resource().Attributes()), - ScopeAttributes: attributeToMap(scopeMetrics.Scope().Attributes()), - MetricAttributes: attributeToMap(getAttributesPerDataPoint(metric)[0]), - Type: metric.Type(), - }) + dataPointsAttributes := getAttributesPerDataPoint(metric) + for l := 0; l < len(dataPointsAttributes); l++ { + flatMetrics = append(flatMetrics, FlatMetric{ + Name: metric.Name(), + Description: metric.Description(), + ScopeAndVersion: ScopeVersion{scopeMetrics.Scope().Name(), scopeMetrics.Scope().Version()}, + ResourceAttributes: attributeToMap(resourceMetrics.Resource().Attributes()), + ScopeAttributes: attributeToMap(scopeMetrics.Scope().Attributes()), + MetricAttributes: attributeToMap(dataPointsAttributes[l]), + Type: metric.Type(), + }) + } } } } From 1c5209c4ad205a0969e145f394460c8a06685c42 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Wed, 7 Aug 2024 16:59:21 +0200 Subject: [PATCH 20/23] adjust kyma input e2e test for the new metrics matcher --- test/e2e/metrics_kyma_input_test.go | 136 ++++++++++++---------------- 1 file changed, 60 insertions(+), 76 deletions(-) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 9c3153a36..9a4daa859 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -22,7 +22,7 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/suite" ) -var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { +var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental, "test"), Ordered, func() { var ( mockNs = suite.ID() @@ -94,82 +94,64 @@ var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), resp, err := proxyClient.Get(backendForKymaInputExportURL) g.Expect(err).NotTo(HaveOccurred()) g.Expect(resp).To(HaveHTTPStatus(http.StatusOK)) - g.Expect(resp).To(HaveHTTPBody(SatisfyAll( - ContainMd(SatisfyAll( - ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), - ContainMetric(SatisfyAll( - WithName(Equal("kyma.module.status.state")), - ContainDataPointAttrs(HaveKey("state")), + g.Expect(resp).To(HaveHTTPBody( + WithFlatMetrics(SatisfyAll( + ContainElement(SatisfyAll( + // Check the "kyma.module.status.state" metric + HaveField("Name", "kyma.module.status.state"), + HaveField("MetricAttributes", HaveKey("state")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), )), - ContainScope(SatisfyAll( - WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), - WithScopeVersion( - SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ), - ), + ContainElement(SatisfyAll( + // Check the "kyma.module.status.conditions" metric for the "LogComponentsHealthy" condition type + HaveField("Name", "kyma.module.status.conditions"), + HaveField("MetricAttributes", HaveKeyWithValue("type", "LogComponentsHealthy")), + HaveField("MetricAttributes", HaveKey("status")), + HaveField("MetricAttributes", HaveKey("reason")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), )), - )), - ContainMd(SatisfyAll( - ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), - ContainMetric(SatisfyAll( - WithName(Equal("kyma.module.status.conditions")), - ContainDataPointAttrs(HaveKeyWithValue("type", "LogComponentsHealthy")), - ContainDataPointAttrs(HaveKey("status")), - ContainDataPointAttrs(HaveKey("reason")), - )), - ContainScope(SatisfyAll( - WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), - WithScopeVersion( - SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ), - ), - )), - )), - ContainMd(SatisfyAll( - ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), - ContainMetric(SatisfyAll( - WithName(Equal("kyma.module.status.conditions")), - ContainDataPointAttrs(HaveKeyWithValue("type", "TraceComponentsHealthy")), - ContainDataPointAttrs(HaveKey("status")), - ContainDataPointAttrs(HaveKey("reason")), + ContainElement(SatisfyAll( + // Check the "kyma.module.status.conditions" metric for the "MetricComponentsHealthy" condition type + HaveField("Name", "kyma.module.status.conditions"), + HaveField("MetricAttributes", HaveKeyWithValue("type", "MetricComponentsHealthy")), + HaveField("MetricAttributes", HaveKey("status")), + HaveField("MetricAttributes", HaveKey("reason")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), )), - ContainScope(SatisfyAll( - WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), - WithScopeVersion( - SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ), - ), + ContainElement(SatisfyAll( + // Check the "kyma.module.status.conditions" metric for the "TraceComponentsHealthy" condition type + HaveField("Name", "kyma.module.status.conditions"), + HaveField("MetricAttributes", HaveKeyWithValue("type", "TraceComponentsHealthy")), + HaveField("MetricAttributes", HaveKey("status")), + HaveField("MetricAttributes", HaveKey("reason")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), )), )), - ContainMd(SatisfyAll( - ContainResourceAttrs(HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - ContainResourceAttrs(HaveKeyWithValue("kyma.module.name", "Telemetry")), - ContainMetric(SatisfyAll( - WithName(Equal("kyma.module.status.conditions")), - ContainDataPointAttrs(HaveKeyWithValue("type", "MetricComponentsHealthy")), - ContainDataPointAttrs(HaveKey("status")), - ContainDataPointAttrs(HaveKey("reason")), - )), - ContainScope(SatisfyAll( - WithScopeName(ContainSubstring(metric.InstrumentationScopeKyma)), - WithScopeVersion( - SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ), - ), - )), - )), - ))) + )) }, periodic.TelemetryEventuallyTimeout, periodic.TelemetryInterval).Should(Succeed()) }) @@ -178,10 +160,12 @@ var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), resp, err := proxyClient.Get(backendForNoKymaInputExportURL) g.Expect(err).NotTo(HaveOccurred()) g.Expect(resp).To(HaveHTTPStatus(http.StatusOK)) - g.Expect(resp).To(HaveHTTPBody(SatisfyAll( - Not(ContainMd(ContainMetric(WithName(Equal("kyma.module.status.state"))))), - Not(ContainMd(ContainMetric(WithName(Equal("kyma.module.status.conditions"))))), - ))) + g.Expect(resp).To(HaveHTTPBody( + WithFlatMetrics(SatisfyAll( + Not(ContainElement(HaveField("Name", "kyma.module.status.state"))), + Not(ContainElement(HaveField("Name", "kyma.module.status.conditions"))), + )), + )) }, periodic.TelemetryConsistentlyTimeout, periodic.TelemetryInterval).Should(Succeed()) }) }) From e3632d3790a64344e1959beb27d698f457211c4c Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Wed, 7 Aug 2024 17:00:14 +0200 Subject: [PATCH 21/23] remove the dummy test label --- test/e2e/metrics_kyma_input_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 9a4daa859..044eb58e4 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -22,7 +22,7 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/suite" ) -var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental, "test"), Ordered, func() { +var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { var ( mockNs = suite.ID() From eab7aab9ee17de3fa1da8faf19148dc2de7cbab7 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Thu, 8 Aug 2024 14:27:16 +0200 Subject: [PATCH 22/23] split the checks for the kyma input e2e test --- test/e2e/metrics_kyma_input_test.go | 124 +++++++++++++++------------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 044eb58e4..1d3bcfd0d 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -20,6 +20,7 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/mocks/backend" "github.com/kyma-project/telemetry-manager/test/testkit/periodic" "github.com/kyma-project/telemetry-manager/test/testkit/suite" + "io" ) var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() { @@ -94,62 +95,73 @@ var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), resp, err := proxyClient.Get(backendForKymaInputExportURL) g.Expect(err).NotTo(HaveOccurred()) g.Expect(resp).To(HaveHTTPStatus(http.StatusOK)) - g.Expect(resp).To(HaveHTTPBody( - WithFlatMetrics(SatisfyAll( - ContainElement(SatisfyAll( - // Check the "kyma.module.status.state" metric - HaveField("Name", "kyma.module.status.state"), - HaveField("MetricAttributes", HaveKey("state")), - HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), - HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), - HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ))), - )), - ContainElement(SatisfyAll( - // Check the "kyma.module.status.conditions" metric for the "LogComponentsHealthy" condition type - HaveField("Name", "kyma.module.status.conditions"), - HaveField("MetricAttributes", HaveKeyWithValue("type", "LogComponentsHealthy")), - HaveField("MetricAttributes", HaveKey("status")), - HaveField("MetricAttributes", HaveKey("reason")), - HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), - HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), - HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ))), - )), - ContainElement(SatisfyAll( - // Check the "kyma.module.status.conditions" metric for the "MetricComponentsHealthy" condition type - HaveField("Name", "kyma.module.status.conditions"), - HaveField("MetricAttributes", HaveKeyWithValue("type", "MetricComponentsHealthy")), - HaveField("MetricAttributes", HaveKey("status")), - HaveField("MetricAttributes", HaveKey("reason")), - HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), - HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), - HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ))), - )), - ContainElement(SatisfyAll( - // Check the "kyma.module.status.conditions" metric for the "TraceComponentsHealthy" condition type - HaveField("Name", "kyma.module.status.conditions"), - HaveField("MetricAttributes", HaveKeyWithValue("type", "TraceComponentsHealthy")), - HaveField("MetricAttributes", HaveKey("status")), - HaveField("MetricAttributes", HaveKey("reason")), - HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), - HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), - HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), - HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( - Equal("main"), - MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), - ))), - )), + bodyContent, err := io.ReadAll(resp.Body) + defer resp.Body.Close() + g.Expect(err).NotTo(HaveOccurred()) + + // Check the "kyma.module.status.state" metric + g.Expect(bodyContent).To(WithFlatMetrics( + ContainElement(SatisfyAll( + HaveField("Name", "kyma.module.status.state"), + HaveField("MetricAttributes", HaveKey("state")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), + )), + )) + + // Check the "kyma.module.status.conditions" metric for the "LogComponentsHealthy" condition type + g.Expect(bodyContent).To(WithFlatMetrics( + ContainElement(SatisfyAll( + HaveField("Name", "kyma.module.status.conditions"), + HaveField("MetricAttributes", HaveKeyWithValue("type", "LogComponentsHealthy")), + HaveField("MetricAttributes", HaveKey("status")), + HaveField("MetricAttributes", HaveKey("reason")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), + )), + )) + + // Check the "kyma.module.status.conditions" metric for the "MetricComponentsHealthy" condition type + g.Expect(bodyContent).To(WithFlatMetrics( + ContainElement(SatisfyAll( + HaveField("Name", "kyma.module.status.conditions"), + HaveField("MetricAttributes", HaveKeyWithValue("type", "MetricComponentsHealthy")), + HaveField("MetricAttributes", HaveKey("status")), + HaveField("MetricAttributes", HaveKey("reason")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), + )), + )) + + // Check the "kyma.module.status.conditions" metric for the "TraceComponentsHealthy" condition type + g.Expect(bodyContent).To(WithFlatMetrics( + ContainElement(SatisfyAll( + HaveField("Name", "kyma.module.status.conditions"), + HaveField("MetricAttributes", HaveKeyWithValue("type", "TraceComponentsHealthy")), + HaveField("MetricAttributes", HaveKey("status")), + HaveField("MetricAttributes", HaveKey("reason")), + HaveField("ResourceAttributes", HaveKeyWithValue("k8s.namespace.name", kitkyma.SystemNamespaceName)), + HaveField("ResourceAttributes", HaveKeyWithValue("kyma.module.name", "Telemetry")), + HaveField("ScopeAndVersion", HaveField("Name", metric.InstrumentationScopeKyma)), + HaveField("ScopeAndVersion", HaveField("Version", SatisfyAny( + Equal("main"), + MatchRegexp("[0-9]+.[0-9]+.[0-9]+"), + ))), )), )) }, periodic.TelemetryEventuallyTimeout, periodic.TelemetryInterval).Should(Succeed()) From f8d3e18408add75ba44626c64d0be68db71b51a6 Mon Sep 17 00:00:00 2001 From: Mostafa Shorim Date: Thu, 8 Aug 2024 14:44:38 +0200 Subject: [PATCH 23/23] fix linting --- test/e2e/metrics_kyma_input_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/metrics_kyma_input_test.go b/test/e2e/metrics_kyma_input_test.go index 1d3bcfd0d..bbcb7991a 100644 --- a/test/e2e/metrics_kyma_input_test.go +++ b/test/e2e/metrics_kyma_input_test.go @@ -3,6 +3,7 @@ package e2e import ( + "io" "net/http" . "github.com/onsi/ginkgo/v2" @@ -20,7 +21,6 @@ import ( "github.com/kyma-project/telemetry-manager/test/testkit/mocks/backend" "github.com/kyma-project/telemetry-manager/test/testkit/periodic" "github.com/kyma-project/telemetry-manager/test/testkit/suite" - "io" ) var _ = Describe(suite.ID(), Label(suite.LabelMetrics, suite.LabelExperimental), Ordered, func() {