From 6db5d1aedcc6da3577c417f03e67db755cfbeb90 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 14 Nov 2024 00:40:59 +0100 Subject: [PATCH] [chore]: enable gofumpt linter for processor (#36347) #### Description [gofumpt](https://golangci-lint.run/usage/linters/#gofumpt) enforces a stricter format than gofmt Signed-off-by: Matthieu MOREL --- .../attributes_log_test.go | 2 +- .../attributes_metric_test.go | 1 + .../internal/tracking/identity.go | 8 +- .../internal/data/expo_test.go | 2 +- .../internal/data/histo_test.go | 2 +- .../internal/streams/data_test.go | 6 +- .../deltatocumulativeprocessor/linear.go | 1 - processor/deltatorateprocessor/config.go | 1 - .../deltatorateprocessor/processor_test.go | 154 +- processor/filterprocessor/expr_test.go | 6 +- processor/groupbyattrsprocessor/config.go | 1 - processor/groupbyattrsprocessor/factory.go | 13 +- .../groupbyattrsprocessor/processor_test.go | 4 +- processor/groupbytraceprocessor/config.go | 1 - processor/groupbytraceprocessor/event.go | 12 +- processor/groupbytraceprocessor/factory.go | 3 +- .../groupbytraceprocessor/processor_test.go | 8 + .../groupbytraceprocessor/storage_memory.go | 1 + processor/intervalprocessor/config.go | 4 +- processor/k8sattributesprocessor/e2e_test.go | 17 +- processor/k8sattributesprocessor/factory.go | 11 +- .../internal/kube/client_test.go | 935 +++-- .../internal/kube/fake_informer.go | 3 + processor/logdedupprocessor/factory_test.go | 2 +- processor/logstransformprocessor/factory.go | 3 +- .../logstransformprocessor/processor_test.go | 42 +- .../metricsgenerationprocessor/config.go | 1 - .../processor_test.go | 416 +- processor/metricstransformprocessor/config.go | 2 - .../metrics_testcase_builder_test.go | 6 +- .../metrics_transform_processor_group_test.go | 48 +- ...rics_transform_processor_testcases_test.go | 3618 +++++++++-------- .../operation_scale_value.go | 4 +- .../probabilisticsamplerprocessor/config.go | 1 - .../logsprocessor_test.go | 2 +- .../sampler_mode.go | 28 +- .../tracesprocessor_test.go | 6 +- processor/redactionprocessor/config.go | 1 - processor/redactionprocessor/factory.go | 6 +- .../redactionprocessor/processor_test.go | 15 +- processor/remotetapprocessor/processor.go | 8 +- .../resourcedetectionprocessor/config.go | 1 - .../internal/aws/ec2/ec2.go | 3 +- .../internal/aws/ec2/ec2_test.go | 21 +- .../internal/consul/config.go | 1 - .../internal/resourcedetection.go | 3 +- .../internal/system/system.go | 16 +- processor/resourceprocessor/config.go | 1 - processor/resourceprocessor/factory.go | 9 +- .../resource_processor_test.go | 16 +- processor/routingprocessor/config.go | 1 - processor/routingprocessor/router.go | 1 - processor/spanprocessor/span.go | 2 +- processor/spanprocessor/span_test.go | 12 +- .../sumologicprocessor/processor_test.go | 4 +- .../internal/sampling/always_sample_test.go | 6 +- .../sampling/boolean_tag_filter_test.go | 4 +- .../sampling/numeric_tag_filter_test.go | 4 +- .../sampling/span_count_sampler_test.go | 2 +- .../processor_benchmarks_test.go | 2 +- .../tailsamplingprocessor/processor_test.go | 20 +- .../internal/common/processor.go | 23 +- ...nvert_exponential_hist_to_explicit_hist.go | 13 +- ..._exponential_hist_to_explicit_hist_test.go | 5 - 64 files changed, 2842 insertions(+), 2733 deletions(-) diff --git a/processor/attributesprocessor/attributes_log_test.go b/processor/attributesprocessor/attributes_log_test.go index 5614f0668411..5d1a1d14f562 100644 --- a/processor/attributesprocessor/attributes_log_test.go +++ b/processor/attributesprocessor/attributes_log_test.go @@ -136,7 +136,7 @@ func TestAttributes_FilterLogs(t *testing.T) { } oCfg.Include = &filterconfig.MatchProperties{ Resources: []filterconfig.Attribute{{Key: "name", Value: "^[^i].*"}}, - //Libraries: []filterconfig.InstrumentationLibrary{{Name: "^[^i].*"}}, + // Libraries: []filterconfig.InstrumentationLibrary{{Name: "^[^i].*"}}, Config: *createConfig(filterset.Regexp), } oCfg.Exclude = &filterconfig.MatchProperties{ diff --git a/processor/attributesprocessor/attributes_metric_test.go b/processor/attributesprocessor/attributes_metric_test.go index c9b596e65558..c8b0f49414f4 100644 --- a/processor/attributesprocessor/attributes_metric_test.go +++ b/processor/attributesprocessor/attributes_metric_test.go @@ -347,6 +347,7 @@ func TestMetricAttributes_Hash(t *testing.T) { runIndividualMetricTestCase(t, tc, mp) } } + func TestMetricAttributes_Convert(t *testing.T) { testCases := []metricTestCase{ { diff --git a/processor/cumulativetodeltaprocessor/internal/tracking/identity.go b/processor/cumulativetodeltaprocessor/internal/tracking/identity.go index 2b7400358f3b..c89675512dc9 100644 --- a/processor/cumulativetodeltaprocessor/internal/tracking/identity.go +++ b/processor/cumulativetodeltaprocessor/internal/tracking/identity.go @@ -25,9 +25,11 @@ type MetricIdentity struct { MetricValueType pmetric.NumberDataPointValueType } -const A = int32('A') -const SEP = byte(0x1E) -const SEPSTR = string(SEP) +const ( + A = int32('A') + SEP = byte(0x1E) + SEPSTR = string(SEP) +) func (mi *MetricIdentity) Write(b *bytes.Buffer) { b.WriteRune(A + int32(mi.MetricType)) diff --git a/processor/deltatocumulativeprocessor/internal/data/expo_test.go b/processor/deltatocumulativeprocessor/internal/data/expo_test.go index bbc88dc9c5eb..190dd848c213 100644 --- a/processor/deltatocumulativeprocessor/internal/data/expo_test.go +++ b/processor/deltatocumulativeprocessor/internal/data/expo_test.go @@ -20,7 +20,7 @@ const ΓΈ = math.MaxUint64 func TestExpoAdd(t *testing.T) { type expdp = expotest.Histogram type bins = expotest.Bins - var obs0 = expotest.Observe0 + obs0 := expotest.Observe0 cases := []struct { name string diff --git a/processor/deltatocumulativeprocessor/internal/data/histo_test.go b/processor/deltatocumulativeprocessor/internal/data/histo_test.go index 5305225c16e8..86542bda4bfe 100644 --- a/processor/deltatocumulativeprocessor/internal/data/histo_test.go +++ b/processor/deltatocumulativeprocessor/internal/data/histo_test.go @@ -13,7 +13,7 @@ import ( func TestHistoAdd(t *testing.T) { type histdp = histotest.Histogram - var obs = histotest.Bounds(histo.DefaultBounds).Observe + obs := histotest.Bounds(histo.DefaultBounds).Observe cases := []struct { name string diff --git a/processor/deltatocumulativeprocessor/internal/streams/data_test.go b/processor/deltatocumulativeprocessor/internal/streams/data_test.go index 76ae72ee1ec5..2c42abb4b334 100644 --- a/processor/deltatocumulativeprocessor/internal/streams/data_test.go +++ b/processor/deltatocumulativeprocessor/internal/streams/data_test.go @@ -16,8 +16,10 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/testdata/random" ) -var rdp data.Number -var rid streams.Ident +var ( + rdp data.Number + rid streams.Ident +) func BenchmarkSamples(b *testing.B) { b.Run("iterfn", func(b *testing.B) { diff --git a/processor/deltatocumulativeprocessor/linear.go b/processor/deltatocumulativeprocessor/linear.go index 34edcd377eb2..0b547fe5145d 100644 --- a/processor/deltatocumulativeprocessor/linear.go +++ b/processor/deltatocumulativeprocessor/linear.go @@ -115,7 +115,6 @@ func (p *Linear) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error { // tracked stream: add incoming delta dp to existing cumulative aggregation return acc, delta.AccumulateInto(acc, dp) }() - // aggregation failed, record as metric and drop datapoint if err != nil { p.tel.Datapoints().Inc(ctx, telemetry.Cause(err)) diff --git a/processor/deltatorateprocessor/config.go b/processor/deltatorateprocessor/config.go index 15d6e5cd7971..99efcfd45391 100644 --- a/processor/deltatorateprocessor/config.go +++ b/processor/deltatorateprocessor/config.go @@ -9,7 +9,6 @@ import ( // Config defines the configuration for the processor. type Config struct { - // List of delta sum metrics to convert to rates Metrics []string `mapstructure:"metrics"` } diff --git a/processor/deltatorateprocessor/processor_test.go b/processor/deltatorateprocessor/processor_test.go index 3b859d490e0c..d05bdc78774d 100644 --- a/processor/deltatorateprocessor/processor_test.go +++ b/processor/deltatorateprocessor/processor_test.go @@ -31,84 +31,82 @@ type deltaToRateTest struct { outMetrics pmetric.Metrics } -var ( - testCases = []deltaToRateTest{ - { - name: "delta_to_rate_expect_same", - metrics: nil, - inMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{100}, {4}}, - isDelta: []bool{true, true}, - deltaSecond: 120, - }), - outMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{100}, {4}}, - isDelta: []bool{true, true}, - deltaSecond: 120, - }), - }, - { - name: "delta_to_rate_one_positive", - metrics: []string{"metric_1", "metric_2"}, - inMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{120, 240, 360}, {360}}, - isDelta: []bool{true, true}, - deltaSecond: 120, - }), - outMetrics: generateGaugeMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{1, 2, 3}, {3}}, - }), - }, - { - name: "delta_to_rate_with_cumulative", - metrics: []string{"metric_1", "metric_2"}, - inMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{100}, {4}}, - isDelta: []bool{false, false}, - deltaSecond: 120, - }), - outMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{100}, {4}}, - isDelta: []bool{false, false}, - deltaSecond: 120, - }), - }, - { - name: "delta_to_rate_expect_zero", - metrics: []string{"metric_1", "metric_2"}, - inMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{120, 240, 360}, {360}}, - isDelta: []bool{true, true}, - deltaSecond: 0, - }), - outMetrics: generateGaugeMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{0, 0, 0}, {0}}, - }), - }, - { - name: "int64-delta_to_rate_one_positive", - metrics: []string{"metric_1", "metric_2"}, - inMetrics: generateSumMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricIntValues: [][]int64{{120, 240, 360}, {360}}, - isDelta: []bool{true, true}, - deltaSecond: 120, - }), - outMetrics: generateGaugeMetrics(testMetric{ - metricNames: []string{"metric_1", "metric_2"}, - metricValues: [][]float64{{1, 2, 3}, {3}}, - }), - }, - } -) +var testCases = []deltaToRateTest{ + { + name: "delta_to_rate_expect_same", + metrics: nil, + inMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{100}, {4}}, + isDelta: []bool{true, true}, + deltaSecond: 120, + }), + outMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{100}, {4}}, + isDelta: []bool{true, true}, + deltaSecond: 120, + }), + }, + { + name: "delta_to_rate_one_positive", + metrics: []string{"metric_1", "metric_2"}, + inMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{120, 240, 360}, {360}}, + isDelta: []bool{true, true}, + deltaSecond: 120, + }), + outMetrics: generateGaugeMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{1, 2, 3}, {3}}, + }), + }, + { + name: "delta_to_rate_with_cumulative", + metrics: []string{"metric_1", "metric_2"}, + inMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{100}, {4}}, + isDelta: []bool{false, false}, + deltaSecond: 120, + }), + outMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{100}, {4}}, + isDelta: []bool{false, false}, + deltaSecond: 120, + }), + }, + { + name: "delta_to_rate_expect_zero", + metrics: []string{"metric_1", "metric_2"}, + inMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{120, 240, 360}, {360}}, + isDelta: []bool{true, true}, + deltaSecond: 0, + }), + outMetrics: generateGaugeMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{0, 0, 0}, {0}}, + }), + }, + { + name: "int64-delta_to_rate_one_positive", + metrics: []string{"metric_1", "metric_2"}, + inMetrics: generateSumMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricIntValues: [][]int64{{120, 240, 360}, {360}}, + isDelta: []bool{true, true}, + deltaSecond: 120, + }), + outMetrics: generateGaugeMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{1, 2, 3}, {3}}, + }), + }, +} func TestCumulativeToDeltaProcessor(t *testing.T) { for _, test := range testCases { diff --git a/processor/filterprocessor/expr_test.go b/processor/filterprocessor/expr_test.go index 5921b323f5f9..259328827c85 100644 --- a/processor/filterprocessor/expr_test.go +++ b/processor/filterprocessor/expr_test.go @@ -21,8 +21,10 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig" ) -const filteredMetric = "p0_metric_1" -const filteredAttrKey = "pt-label-key-1" +const ( + filteredMetric = "p0_metric_1" + filteredAttrKey = "pt-label-key-1" +) var filteredAttrVal = pcommon.NewValueStr("pt-label-val-1") diff --git a/processor/groupbyattrsprocessor/config.go b/processor/groupbyattrsprocessor/config.go index ccd078dbeddf..c02f29995b10 100644 --- a/processor/groupbyattrsprocessor/config.go +++ b/processor/groupbyattrsprocessor/config.go @@ -5,7 +5,6 @@ package groupbyattrsprocessor // import "github.com/open-telemetry/opentelemetry // Config is the configuration for the processor. type Config struct { - // GroupByKeys describes the attribute names that are going to be used for grouping. // Empty value is allowed, since processor in such case can compact data GroupByKeys []string `mapstructure:"keys"` diff --git a/processor/groupbyattrsprocessor/factory.go b/processor/groupbyattrsprocessor/factory.go index 666e15e87ce5..66ddf0f98d57 100644 --- a/processor/groupbyattrsprocessor/factory.go +++ b/processor/groupbyattrsprocessor/factory.go @@ -15,9 +15,7 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor/internal/metadata" ) -var ( - consumerCapabilities = consumer.Capabilities{MutatesData: true} -) +var consumerCapabilities = consumer.Capabilities{MutatesData: true} // NewFactory returns a new factory for the Filter processor. func NewFactory() processor.Factory { @@ -64,7 +62,8 @@ func createTracesProcessor( ctx context.Context, set processor.Settings, cfg component.Config, - nextConsumer consumer.Traces) (processor.Traces, error) { + nextConsumer consumer.Traces, +) (processor.Traces, error) { oCfg := cfg.(*Config) gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys) if err != nil { @@ -85,7 +84,8 @@ func createLogsProcessor( ctx context.Context, set processor.Settings, cfg component.Config, - nextConsumer consumer.Logs) (processor.Logs, error) { + nextConsumer consumer.Logs, +) (processor.Logs, error) { oCfg := cfg.(*Config) gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys) if err != nil { @@ -106,7 +106,8 @@ func createMetricsProcessor( ctx context.Context, set processor.Settings, cfg component.Config, - nextConsumer consumer.Metrics) (processor.Metrics, error) { + nextConsumer consumer.Metrics, +) (processor.Metrics, error) { oCfg := cfg.(*Config) gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys) if err != nil { diff --git a/processor/groupbyattrsprocessor/processor_test.go b/processor/groupbyattrsprocessor/processor_test.go index ea32b35d4291..cb0465955ffc 100644 --- a/processor/groupbyattrsprocessor/processor_test.go +++ b/processor/groupbyattrsprocessor/processor_test.go @@ -22,9 +22,7 @@ import ( "go.opentelemetry.io/otel/sdk/metric/metricdata" ) -var ( - attrMap = prepareAttributeMap() -) +var attrMap = prepareAttributeMap() func prepareAttributeMap() pcommon.Map { am := pcommon.NewMap() diff --git a/processor/groupbytraceprocessor/config.go b/processor/groupbytraceprocessor/config.go index 6dcc6286ae1b..050512433f37 100644 --- a/processor/groupbytraceprocessor/config.go +++ b/processor/groupbytraceprocessor/config.go @@ -9,7 +9,6 @@ import ( // Config is the configuration for the processor. type Config struct { - // NumTraces is the max number of traces to keep in memory waiting for the duration. // Default: 1_000_000. NumTraces int `mapstructure:"num_traces"` diff --git a/processor/groupbytraceprocessor/event.go b/processor/groupbytraceprocessor/event.go index be8434f8c6d8..58519db2652e 100644 --- a/processor/groupbytraceprocessor/event.go +++ b/processor/groupbytraceprocessor/event.go @@ -48,11 +48,13 @@ var ( } ) -type eventType int -type event struct { - typ eventType - payload any -} +type ( + eventType int + event struct { + typ eventType + payload any + } +) type tracesWithID struct { id pcommon.TraceID diff --git a/processor/groupbytraceprocessor/factory.go b/processor/groupbytraceprocessor/factory.go index 61ee23aa7a70..91b1cfc6abbb 100644 --- a/processor/groupbytraceprocessor/factory.go +++ b/processor/groupbytraceprocessor/factory.go @@ -54,7 +54,8 @@ func createTracesProcessor( _ context.Context, params processor.Settings, cfg component.Config, - nextConsumer consumer.Traces) (processor.Traces, error) { + nextConsumer consumer.Traces, +) (processor.Traces, error) { oCfg := cfg.(*Config) var st storage diff --git a/processor/groupbytraceprocessor/processor_test.go b/processor/groupbytraceprocessor/processor_test.go index 77864d1bc4c4..162738d8b8f6 100644 --- a/processor/groupbytraceprocessor/processor_test.go +++ b/processor/groupbytraceprocessor/processor_test.go @@ -599,12 +599,15 @@ func (m *mockProcessor) ConsumeTraces(ctx context.Context, td ptrace.Traces) err } return nil } + func (m *mockProcessor) Capabilities() consumer.Capabilities { return consumer.Capabilities{MutatesData: true} } + func (m *mockProcessor) Shutdown(context.Context) error { return nil } + func (m *mockProcessor) Start(_ context.Context, _ component.Host) error { return nil } @@ -625,24 +628,28 @@ func (st *mockStorage) createOrAppend(traceID pcommon.TraceID, trace ptrace.Trac } return nil } + func (st *mockStorage) get(traceID pcommon.TraceID) ([]ptrace.ResourceSpans, error) { if st.onGet != nil { return st.onGet(traceID) } return nil, nil } + func (st *mockStorage) delete(traceID pcommon.TraceID) ([]ptrace.ResourceSpans, error) { if st.onDelete != nil { return st.onDelete(traceID) } return nil, nil } + func (st *mockStorage) start() error { if st.onStart != nil { return st.onStart() } return nil } + func (st *mockStorage) shutdown() error { if st.onShutdown != nil { return st.onShutdown() @@ -659,6 +666,7 @@ var _ consumer.Traces = (*blockingConsumer)(nil) func (b *blockingConsumer) Capabilities() consumer.Capabilities { return consumer.Capabilities{MutatesData: false} } + func (b *blockingConsumer) ConsumeTraces(context.Context, ptrace.Traces) error { <-b.blockCh return nil diff --git a/processor/groupbytraceprocessor/storage_memory.go b/processor/groupbytraceprocessor/storage_memory.go index 4cd97fd51451..9de15d877c94 100644 --- a/processor/groupbytraceprocessor/storage_memory.go +++ b/processor/groupbytraceprocessor/storage_memory.go @@ -49,6 +49,7 @@ func (st *memoryStorage) createOrAppend(traceID pcommon.TraceID, td ptrace.Trace return nil } + func (st *memoryStorage) get(traceID pcommon.TraceID) ([]ptrace.ResourceSpans, error) { st.RLock() rss, ok := st.content[traceID] diff --git a/processor/intervalprocessor/config.go b/processor/intervalprocessor/config.go index ee98305cc3b5..9a9e7d56b87d 100644 --- a/processor/intervalprocessor/config.go +++ b/processor/intervalprocessor/config.go @@ -10,9 +10,7 @@ import ( "go.opentelemetry.io/collector/component" ) -var ( - ErrInvalidIntervalValue = errors.New("invalid interval value") -) +var ErrInvalidIntervalValue = errors.New("invalid interval value") var _ component.Config = (*Config)(nil) diff --git a/processor/k8sattributesprocessor/e2e_test.go b/processor/k8sattributesprocessor/e2e_test.go index 553737a894fa..ce1387846719 100644 --- a/processor/k8sattributesprocessor/e2e_test.go +++ b/processor/k8sattributesprocessor/e2e_test.go @@ -62,7 +62,6 @@ func newExpectedValue(mode int, value string) *expectedValue { // make docker-otelcontribcol // KUBECONFIG=/tmp/kube-config-otelcol-e2e-testing kind load docker-image otelcontribcol:latest func TestE2E_ClusterRBAC(t *testing.T) { - testDir := filepath.Join("testdata", "e2e", "clusterrbac") k8sClient, err := k8stest.NewK8sClient(testKubeConfig) @@ -543,7 +542,6 @@ func TestE2E_ClusterRBAC(t *testing.T) { // Test with `filter::namespace` set and only role binding to collector's SA. We can't get node and namespace labels/annotations. func TestE2E_NamespacedRBAC(t *testing.T) { - testDir := filepath.Join("testdata", "e2e", "namespacedrbac") k8sClient, err := k8stest.NewK8sClient(testKubeConfig) @@ -716,7 +714,6 @@ func TestE2E_NamespacedRBAC(t *testing.T) { // Test with `filter::namespace` set, role binding for namespace-scoped objects (pod, replicaset) and clusterrole // binding for node and namespace objects. func TestE2E_MixRBAC(t *testing.T) { - testDir := filepath.Join("testdata", "e2e", "mixrbac") k8sClient, err := k8stest.NewK8sClient(testKubeConfig) @@ -1089,7 +1086,8 @@ func TestE2E_NamespacedRBACNoPodIP(t *testing.T) { } func scanTracesForAttributes(t *testing.T, ts *consumertest.TracesSink, expectedService string, - kvs map[string]*expectedValue) { + kvs map[string]*expectedValue, +) { // Iterate over the received set of traces starting from the most recent entries due to a bug in the processor: // https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18892 // TODO: Remove the reverse loop once it's fixed. All the metrics should be properly annotated. @@ -1110,7 +1108,8 @@ func scanTracesForAttributes(t *testing.T, ts *consumertest.TracesSink, expected } func scanMetricsForAttributes(t *testing.T, ms *consumertest.MetricsSink, expectedService string, - kvs map[string]*expectedValue) { + kvs map[string]*expectedValue, +) { // Iterate over the received set of metrics starting from the most recent entries due to a bug in the processor: // https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18892 // TODO: Remove the reverse loop once it's fixed. All the metrics should be properly annotated. @@ -1131,7 +1130,8 @@ func scanMetricsForAttributes(t *testing.T, ms *consumertest.MetricsSink, expect } func scanLogsForAttributes(t *testing.T, ls *consumertest.LogsSink, expectedService string, - kvs map[string]*expectedValue) { + kvs map[string]*expectedValue, +) { // Iterate over the received set of logs starting from the most recent entries due to a bug in the processor: // https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18892 // TODO: Remove the reverse loop once it's fixed. All the metrics should be properly annotated. @@ -1152,8 +1152,8 @@ func scanLogsForAttributes(t *testing.T, ls *consumertest.LogsSink, expectedServ } func scanProfilesForAttributes(t *testing.T, ps *consumertest.ProfilesSink, expectedService string, - kvs map[string]*expectedValue) { - + kvs map[string]*expectedValue, +) { // `telemetrygen` doesn't support profiles // https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/36127 // TODO: Remove `t.Skip()` once #36127 is resolved @@ -1207,7 +1207,6 @@ func resourceHasAttributes(resource pcommon.Resource, kvs map[string]*expectedVa case shouldnotexist: shouldNotFoundAttrs[k] = true } - } return true }, diff --git a/processor/k8sattributesprocessor/factory.go b/processor/k8sattributesprocessor/factory.go index f2f05e8bf4ea..2c3eb39b2914 100644 --- a/processor/k8sattributesprocessor/factory.go +++ b/processor/k8sattributesprocessor/factory.go @@ -19,9 +19,11 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor/internal/metadata" ) -var kubeClientProvider = kube.ClientProvider(nil) -var consumerCapabilities = consumer.Capabilities{MutatesData: true} -var defaultExcludes = ExcludeConfig{Pods: []ExcludePodConfig{{Name: "jaeger-agent"}, {Name: "jaeger-collector"}}} +var ( + kubeClientProvider = kube.ClientProvider(nil) + consumerCapabilities = consumer.Capabilities{MutatesData: true} + defaultExcludes = ExcludeConfig{Pods: []ExcludePodConfig{{Name: "jaeger-agent"}, {Name: "jaeger-collector"}}} +) // NewFactory returns a new factory for the k8s processor. func NewFactory() processor.Factory { @@ -167,7 +169,8 @@ func createKubernetesProcessor( cfg component.Config, options ...option, ) *kubernetesprocessor { - kp := &kubernetesprocessor{logger: params.Logger, + kp := &kubernetesprocessor{ + logger: params.Logger, cfg: cfg, options: options, telemetrySettings: params.TelemetrySettings, diff --git a/processor/k8sattributesprocessor/internal/kube/client_test.go b/processor/k8sattributesprocessor/internal/kube/client_test.go index c904a6291f79..7f6b85d195c7 100644 --- a/processor/k8sattributesprocessor/internal/kube/client_test.go +++ b/processor/k8sattributesprocessor/internal/kube/client_test.go @@ -717,178 +717,197 @@ func TestExtractionRules(t *testing.T) { name string rules ExtractionRules attributes map[string]string - }{{ - name: "no-rules", - rules: ExtractionRules{}, - attributes: nil, - }, { - name: "deployment", - rules: ExtractionRules{ - DeploymentName: true, - DeploymentUID: true, - }, - attributes: map[string]string{ - "k8s.deployment.name": "auth-service", - "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - }, - }, { - name: "replicasetId", - rules: ExtractionRules{ - ReplicaSetID: true, - }, - attributes: map[string]string{ - "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", - }, - }, { - name: "replicasetName", - rules: ExtractionRules{ - ReplicaSetName: true, - }, - attributes: map[string]string{ - "k8s.replicaset.name": "auth-service-66f5996c7c", - }, - }, { - name: "daemonsetUID", - rules: ExtractionRules{ - DaemonSetUID: true, - }, - attributes: map[string]string{ - "k8s.daemonset.uid": "c94d3814-2253-427a-ab13-2cf609e4dafa", - }, - }, { - name: "daemonsetName", - rules: ExtractionRules{ - DaemonSetName: true, - }, - attributes: map[string]string{ - "k8s.daemonset.name": "auth-daemonset", - }, - }, { - name: "jobUID", - rules: ExtractionRules{ - JobUID: true, - }, - attributes: map[string]string{ - "k8s.job.uid": "59f27ac1-5c71-42e5-abe9-2c499d603706", - }, - }, { - name: "jobName", - rules: ExtractionRules{ - JobName: true, - }, - attributes: map[string]string{ - "k8s.job.name": "auth-cronjob-27667920", + }{ + { + name: "no-rules", + rules: ExtractionRules{}, + attributes: nil, }, - }, { - name: "cronJob", - rules: ExtractionRules{ - CronJobName: true, + { + name: "deployment", + rules: ExtractionRules{ + DeploymentName: true, + DeploymentUID: true, + }, + attributes: map[string]string{ + "k8s.deployment.name": "auth-service", + "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", + }, }, - attributes: map[string]string{ - "k8s.cronjob.name": "auth-cronjob", + { + name: "replicasetId", + rules: ExtractionRules{ + ReplicaSetID: true, + }, + attributes: map[string]string{ + "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", + }, }, - }, { - name: "statefulsetUID", - rules: ExtractionRules{ - StatefulSetUID: true, + { + name: "replicasetName", + rules: ExtractionRules{ + ReplicaSetName: true, + }, + attributes: map[string]string{ + "k8s.replicaset.name": "auth-service-66f5996c7c", + }, }, - attributes: map[string]string{ - "k8s.statefulset.uid": "03755eb1-6175-47d5-afd5-05cfc30244d7", + { + name: "daemonsetUID", + rules: ExtractionRules{ + DaemonSetUID: true, + }, + attributes: map[string]string{ + "k8s.daemonset.uid": "c94d3814-2253-427a-ab13-2cf609e4dafa", + }, }, - }, { - name: "jobName", - rules: ExtractionRules{ - StatefulSetName: true, + { + name: "daemonsetName", + rules: ExtractionRules{ + DaemonSetName: true, + }, + attributes: map[string]string{ + "k8s.daemonset.name": "auth-daemonset", + }, }, - attributes: map[string]string{ - "k8s.statefulset.name": "pi-statefulset", + { + name: "jobUID", + rules: ExtractionRules{ + JobUID: true, + }, + attributes: map[string]string{ + "k8s.job.uid": "59f27ac1-5c71-42e5-abe9-2c499d603706", + }, }, - }, { - name: "metadata", - rules: ExtractionRules{ - DeploymentName: true, - DeploymentUID: true, - Namespace: true, - PodName: true, - PodUID: true, - PodHostName: true, - PodIP: true, - Node: true, - StartTime: true, + { + name: "jobName", + rules: ExtractionRules{ + JobName: true, + }, + attributes: map[string]string{ + "k8s.job.name": "auth-cronjob-27667920", + }, }, - attributes: map[string]string{ - "k8s.deployment.name": "auth-service", - "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - "k8s.namespace.name": "ns1", - "k8s.node.name": "node1", - "k8s.pod.name": "auth-service-abc12-xyz3", - "k8s.pod.hostname": "host1", - "k8s.pod.uid": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", - "k8s.pod.ip": "1.1.1.1", - "k8s.pod.start_time": func() string { - b, err := pod.GetCreationTimestamp().MarshalText() - require.NoError(t, err) - return string(b) - }(), + { + name: "cronJob", + rules: ExtractionRules{ + CronJobName: true, + }, + attributes: map[string]string{ + "k8s.cronjob.name": "auth-cronjob", + }, }, - }, { - name: "labels", - rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "a1", - Key: "annotation1", - From: MetadataFromPod, - }, - }, - Labels: []FieldExtractionRule{{ - Name: "l1", - Key: "label1", - From: MetadataFromPod, - }, { - Name: "l2", - Key: "label2", - Regex: regexp.MustCompile(`k5=(?P[^\s]+)`), - From: MetadataFromPod, + { + name: "statefulsetUID", + rules: ExtractionRules{ + StatefulSetUID: true, }, + attributes: map[string]string{ + "k8s.statefulset.uid": "03755eb1-6175-47d5-afd5-05cfc30244d7", }, }, - attributes: map[string]string{ - "l1": "lv1", - "l2": "v5", - "a1": "av1", + { + name: "jobName", + rules: ExtractionRules{ + StatefulSetName: true, + }, + attributes: map[string]string{ + "k8s.statefulset.name": "pi-statefulset", + }, }, - }, { - // By default if the From field is not set for labels and annotations we want to extract them from pod - name: "labels-annotations-default-pod", - rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "a1", - Key: "annotation1", + { + name: "metadata", + rules: ExtractionRules{ + DeploymentName: true, + DeploymentUID: true, + Namespace: true, + PodName: true, + PodUID: true, + PodHostName: true, + PodIP: true, + Node: true, + StartTime: true, }, + attributes: map[string]string{ + "k8s.deployment.name": "auth-service", + "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", + "k8s.namespace.name": "ns1", + "k8s.node.name": "node1", + "k8s.pod.name": "auth-service-abc12-xyz3", + "k8s.pod.hostname": "host1", + "k8s.pod.uid": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", + "k8s.pod.ip": "1.1.1.1", + "k8s.pod.start_time": func() string { + b, err := pod.GetCreationTimestamp().MarshalText() + require.NoError(t, err) + return string(b) + }(), }, - Labels: []FieldExtractionRule{{ - Name: "l1", - Key: "label1", - }, { - Name: "l2", - Key: "label2", - Regex: regexp.MustCompile(`k5=(?P[^\s]+)`), + }, + { + name: "labels", + rules: ExtractionRules{ + Annotations: []FieldExtractionRule{ + { + Name: "a1", + Key: "annotation1", + From: MetadataFromPod, + }, + }, + Labels: []FieldExtractionRule{ + { + Name: "l1", + Key: "label1", + From: MetadataFromPod, + }, { + Name: "l2", + Key: "label2", + Regex: regexp.MustCompile(`k5=(?P[^\s]+)`), + From: MetadataFromPod, + }, + }, }, + attributes: map[string]string{ + "l1": "lv1", + "l2": "v5", + "a1": "av1", }, }, - attributes: map[string]string{ - "l1": "lv1", - "l2": "v5", - "a1": "av1", + { + // By default if the From field is not set for labels and annotations we want to extract them from pod + name: "labels-annotations-default-pod", + rules: ExtractionRules{ + Annotations: []FieldExtractionRule{ + { + Name: "a1", + Key: "annotation1", + }, + }, + Labels: []FieldExtractionRule{ + { + Name: "l1", + Key: "label1", + }, { + Name: "l2", + Key: "label2", + Regex: regexp.MustCompile(`k5=(?P[^\s]+)`), + }, + }, + }, + attributes: map[string]string{ + "l1": "lv1", + "l2": "v5", + "a1": "av1", + }, }, - }, { name: "all-labels", rules: ExtractionRules{ - Labels: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:la.*)$"), - From: MetadataFromPod, - }, + Labels: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:la.*)$"), + From: MetadataFromPod, + }, }, }, attributes: map[string]string{ @@ -899,10 +918,11 @@ func TestExtractionRules(t *testing.T) { { name: "all-annotations", rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:an.*)$"), - From: MetadataFromPod, - }, + Annotations: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:an.*)$"), + From: MetadataFromPod, + }, }, }, attributes: map[string]string{ @@ -912,10 +932,11 @@ func TestExtractionRules(t *testing.T) { { name: "all-annotations-not-match", rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:an*)$"), - From: MetadataFromPod, - }, + Annotations: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:an*)$"), + From: MetadataFromPod, + }, }, }, attributes: map[string]string{}, @@ -923,12 +944,13 @@ func TestExtractionRules(t *testing.T) { { name: "captured-groups", rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "$1", - KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`), - HasKeyRegexReference: true, - From: MetadataFromPod, - }, + Annotations: []FieldExtractionRule{ + { + Name: "$1", + KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`), + HasKeyRegexReference: true, + From: MetadataFromPod, + }, }, }, attributes: map[string]string{ @@ -938,12 +960,13 @@ func TestExtractionRules(t *testing.T) { { name: "captured-groups-$0", rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "prefix-$0", - KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`), - HasKeyRegexReference: true, - From: MetadataFromPod, - }, + Annotations: []FieldExtractionRule{ + { + Name: "prefix-$0", + KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`), + HasKeyRegexReference: true, + From: MetadataFromPod, + }, }, }, attributes: map[string]string{ @@ -1024,86 +1047,87 @@ func TestReplicaSetExtractionRules(t *testing.T) { rules ExtractionRules ownerReferences []meta_v1.OwnerReference attributes map[string]string - }{{ - name: "no-rules", - rules: ExtractionRules{}, - attributes: nil, - }, { - name: "one_deployment_is_controller", - ownerReferences: []meta_v1.OwnerReference{ - { - Name: "auth-service", - Kind: "Deployment", - UID: "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - Controller: &isController, + }{ + { + name: "no-rules", + rules: ExtractionRules{}, + attributes: nil, + }, { + name: "one_deployment_is_controller", + ownerReferences: []meta_v1.OwnerReference{ + { + Name: "auth-service", + Kind: "Deployment", + UID: "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", + Controller: &isController, + }, }, - }, - rules: ExtractionRules{ - DeploymentName: true, - DeploymentUID: true, - ReplicaSetID: true, - }, - attributes: map[string]string{ - "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", - "k8s.deployment.name": "auth-service", - "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - }, - }, { - name: "one_deployment_is_controller_another_deployment_is_not_controller", - ownerReferences: []meta_v1.OwnerReference{ - { - Name: "auth-service", - Kind: "Deployment", - UID: "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - Controller: &isController, + rules: ExtractionRules{ + DeploymentName: true, + DeploymentUID: true, + ReplicaSetID: true, }, - { - Name: "auth-service-not-controller", - Kind: "Deployment", - UID: "kkkk-gggg-hhhh-iiii-eeeeeeeeeeee", - Controller: &isNotController, + attributes: map[string]string{ + "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", + "k8s.deployment.name": "auth-service", + "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", }, - }, - rules: ExtractionRules{ - ReplicaSetID: true, - DeploymentName: true, - DeploymentUID: true, - }, - attributes: map[string]string{ - "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", - "k8s.deployment.name": "auth-service", - "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - }, - }, { - name: "one_deployment_is_not_controller", - ownerReferences: []meta_v1.OwnerReference{ - { - Name: "auth-service", - Kind: "Deployment", - UID: "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", - Controller: &isNotController, + }, { + name: "one_deployment_is_controller_another_deployment_is_not_controller", + ownerReferences: []meta_v1.OwnerReference{ + { + Name: "auth-service", + Kind: "Deployment", + UID: "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", + Controller: &isController, + }, + { + Name: "auth-service-not-controller", + Kind: "Deployment", + UID: "kkkk-gggg-hhhh-iiii-eeeeeeeeeeee", + Controller: &isNotController, + }, + }, + rules: ExtractionRules{ + ReplicaSetID: true, + DeploymentName: true, + DeploymentUID: true, + }, + attributes: map[string]string{ + "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", + "k8s.deployment.name": "auth-service", + "k8s.deployment.uid": "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", + }, + }, { + name: "one_deployment_is_not_controller", + ownerReferences: []meta_v1.OwnerReference{ + { + Name: "auth-service", + Kind: "Deployment", + UID: "ffff-gggg-hhhh-iiii-eeeeeeeeeeee", + Controller: &isNotController, + }, + }, + rules: ExtractionRules{ + ReplicaSetID: true, + DeploymentName: true, + DeploymentUID: true, + }, + attributes: map[string]string{ + "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", + }, + }, { + name: "no_deployment", + ownerReferences: []meta_v1.OwnerReference{}, + rules: ExtractionRules{ + ReplicaSetID: true, + DeploymentName: true, + DeploymentUID: true, + }, + attributes: map[string]string{ + "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", }, }, - rules: ExtractionRules{ - ReplicaSetID: true, - DeploymentName: true, - DeploymentUID: true, - }, - attributes: map[string]string{ - "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", - }, - }, { - name: "no_deployment", - ownerReferences: []meta_v1.OwnerReference{}, - rules: ExtractionRules{ - ReplicaSetID: true, - DeploymentName: true, - DeploymentUID: true, - }, - attributes: map[string]string{ - "k8s.replicaset.uid": "207ea729-c779-401d-8347-008ecbc137e3", - }, - }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { @@ -1150,38 +1174,43 @@ func TestNamespaceExtractionRules(t *testing.T) { name string rules ExtractionRules attributes map[string]string - }{{ - name: "no-rules", - rules: ExtractionRules{}, - attributes: nil, - }, { - name: "labels", - rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "a1", - Key: "annotation1", - From: MetadataFromNamespace, - }, - }, - Labels: []FieldExtractionRule{{ - Name: "l1", - Key: "label1", - From: MetadataFromNamespace, + }{ + { + name: "no-rules", + rules: ExtractionRules{}, + attributes: nil, + }, + { + name: "labels", + rules: ExtractionRules{ + Annotations: []FieldExtractionRule{ + { + Name: "a1", + Key: "annotation1", + From: MetadataFromNamespace, + }, + }, + Labels: []FieldExtractionRule{ + { + Name: "l1", + Key: "label1", + From: MetadataFromNamespace, + }, + }, }, + attributes: map[string]string{ + "l1": "lv1", + "a1": "av1", }, }, - attributes: map[string]string{ - "l1": "lv1", - "a1": "av1", - }, - }, { name: "all-labels", rules: ExtractionRules{ - Labels: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:la.*)$"), - From: MetadataFromNamespace, - }, + Labels: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:la.*)$"), + From: MetadataFromNamespace, + }, }, }, attributes: map[string]string{ @@ -1191,10 +1220,11 @@ func TestNamespaceExtractionRules(t *testing.T) { { name: "all-annotations", rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:an.*)$"), - From: MetadataFromNamespace, - }, + Annotations: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:an.*)$"), + From: MetadataFromNamespace, + }, }, }, attributes: map[string]string{ @@ -1240,38 +1270,43 @@ func TestNodeExtractionRules(t *testing.T) { name string rules ExtractionRules attributes map[string]string - }{{ - name: "no-rules", - rules: ExtractionRules{}, - attributes: nil, - }, { - name: "labels", - rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "a1", - Key: "annotation1", - From: MetadataFromNode, - }, - }, - Labels: []FieldExtractionRule{{ - Name: "l1", - Key: "label1", - From: MetadataFromNode, + }{ + { + name: "no-rules", + rules: ExtractionRules{}, + attributes: nil, + }, + { + name: "labels", + rules: ExtractionRules{ + Annotations: []FieldExtractionRule{ + { + Name: "a1", + Key: "annotation1", + From: MetadataFromNode, + }, + }, + Labels: []FieldExtractionRule{ + { + Name: "l1", + Key: "label1", + From: MetadataFromNode, + }, + }, }, + attributes: map[string]string{ + "l1": "lv1", + "a1": "av1", }, }, - attributes: map[string]string{ - "l1": "lv1", - "a1": "av1", - }, - }, { name: "all-labels", rules: ExtractionRules{ - Labels: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:la.*)$"), - From: MetadataFromNode, - }, + Labels: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:la.*)$"), + From: MetadataFromNode, + }, }, }, attributes: map[string]string{ @@ -1281,10 +1316,11 @@ func TestNodeExtractionRules(t *testing.T) { { name: "all-annotations", rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - KeyRegex: regexp.MustCompile("^(?:an.*)$"), - From: MetadataFromNode, - }, + Annotations: []FieldExtractionRule{ + { + KeyRegex: regexp.MustCompile("^(?:an.*)$"), + From: MetadataFromNode, + }, }, }, attributes: map[string]string{ @@ -1315,51 +1351,52 @@ func TestFilters(t *testing.T) { filters Filters labels string fields string - }{{ - name: "no-filters", - filters: Filters{}, - }, { - name: "namespace", - filters: Filters{ - Namespace: "default", - }, - }, { - name: "node", - filters: Filters{ - Node: "ec2-test", - }, - fields: "spec.nodeName=ec2-test", - }, { - name: "labels-and-fields", - filters: Filters{ - Labels: []FieldFilter{ - { - Key: "k1", - Value: "v1", - Op: selection.Equals, - }, - { - Key: "k2", - Value: "v2", - Op: selection.NotEquals, - }, + }{ + { + name: "no-filters", + filters: Filters{}, + }, { + name: "namespace", + filters: Filters{ + Namespace: "default", }, - Fields: []FieldFilter{ - { - Key: "k1", - Value: "v1", - Op: selection.Equals, + }, { + name: "node", + filters: Filters{ + Node: "ec2-test", + }, + fields: "spec.nodeName=ec2-test", + }, { + name: "labels-and-fields", + filters: Filters{ + Labels: []FieldFilter{ + { + Key: "k1", + Value: "v1", + Op: selection.Equals, + }, + { + Key: "k2", + Value: "v2", + Op: selection.NotEquals, + }, }, - { - Key: "k2", - Value: "v2", - Op: selection.NotEquals, + Fields: []FieldFilter{ + { + Key: "k1", + Value: "v1", + Op: selection.Equals, + }, + { + Key: "k2", + Value: "v2", + Op: selection.NotEquals, + }, }, }, + labels: "k1=v1,k2!=v2", + fields: "k1=v1,k2!=v2", }, - labels: "k1=v1,k2!=v2", - fields: "k1=v1,k2!=v2", - }, } for _, tc := range testCases { @@ -1377,81 +1414,82 @@ func TestPodIgnorePatterns(t *testing.T) { testCases := []struct { ignore bool pod *api_v1.Pod - }{{ - ignore: false, - pod: &api_v1.Pod{}, - }, { - ignore: false, - pod: &api_v1.Pod{ - Spec: api_v1.PodSpec{ - HostNetwork: true, + }{ + { + ignore: false, + pod: &api_v1.Pod{}, + }, { + ignore: false, + pod: &api_v1.Pod{ + Spec: api_v1.PodSpec{ + HostNetwork: true, + }, }, - }, - }, { - ignore: true, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Annotations: map[string]string{ - "opentelemetry.io/k8s-processor/ignore": "True ", + }, { + ignore: true, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Annotations: map[string]string{ + "opentelemetry.io/k8s-processor/ignore": "True ", + }, }, }, - }, - }, { - ignore: true, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Annotations: map[string]string{ - "opentelemetry.io/k8s-processor/ignore": "true", + }, { + ignore: true, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Annotations: map[string]string{ + "opentelemetry.io/k8s-processor/ignore": "true", + }, }, }, - }, - }, { - ignore: false, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Annotations: map[string]string{ - "opentelemetry.io/k8s-processor/ignore": "false", + }, { + ignore: false, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Annotations: map[string]string{ + "opentelemetry.io/k8s-processor/ignore": "false", + }, }, }, - }, - }, { - ignore: false, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Annotations: map[string]string{ - "opentelemetry.io/k8s-processor/ignore": "", + }, { + ignore: false, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Annotations: map[string]string{ + "opentelemetry.io/k8s-processor/ignore": "", + }, }, }, - }, - }, { - ignore: true, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Name: "jaeger-agent", + }, { + ignore: true, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: "jaeger-agent", + }, }, - }, - }, { - ignore: true, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Name: "jaeger-collector", + }, { + ignore: true, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: "jaeger-collector", + }, }, - }, - }, { - ignore: true, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Name: "jaeger-agent-b2zdv", + }, { + ignore: true, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: "jaeger-agent-b2zdv", + }, }, - }, - }, { - ignore: false, - pod: &api_v1.Pod{ - ObjectMeta: meta_v1.ObjectMeta{ - Name: "test-pod-name", + }, { + ignore: false, + pod: &api_v1.Pod{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: "test-pod-name", + }, }, }, - }, } c, _ := newTestClient(t) @@ -1800,50 +1838,55 @@ func TestExtractNamespaceLabelsAnnotations(t *testing.T) { name string shouldExtractNamespace bool rules ExtractionRules - }{{ - name: "empty-rules", - shouldExtractNamespace: false, - rules: ExtractionRules{}, - }, { - name: "pod-rules", - shouldExtractNamespace: false, - rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "a1", - Key: "annotation1", - From: MetadataFromPod, - }, - }, - Labels: []FieldExtractionRule{{ - Name: "l1", - Key: "label1", - From: MetadataFromPod, - }, - }, - }, - }, { - name: "namespace-rules-only-annotations", - shouldExtractNamespace: true, - rules: ExtractionRules{ - Annotations: []FieldExtractionRule{{ - Name: "a1", - Key: "annotation1", - From: MetadataFromNamespace, - }, + }{ + { + name: "empty-rules", + shouldExtractNamespace: false, + rules: ExtractionRules{}, + }, { + name: "pod-rules", + shouldExtractNamespace: false, + rules: ExtractionRules{ + Annotations: []FieldExtractionRule{ + { + Name: "a1", + Key: "annotation1", + From: MetadataFromPod, + }, + }, + Labels: []FieldExtractionRule{ + { + Name: "l1", + Key: "label1", + From: MetadataFromPod, + }, + }, }, - }, - }, { - name: "namespace-rules-only-labels", - shouldExtractNamespace: true, - rules: ExtractionRules{ - Labels: []FieldExtractionRule{{ - Name: "l1", - Key: "label1", - From: MetadataFromNamespace, + }, { + name: "namespace-rules-only-annotations", + shouldExtractNamespace: true, + rules: ExtractionRules{ + Annotations: []FieldExtractionRule{ + { + Name: "a1", + Key: "annotation1", + From: MetadataFromNamespace, + }, + }, }, + }, { + name: "namespace-rules-only-labels", + shouldExtractNamespace: true, + rules: ExtractionRules{ + Labels: []FieldExtractionRule{ + { + Name: "l1", + Key: "label1", + From: MetadataFromNamespace, + }, + }, }, }, - }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/processor/k8sattributesprocessor/internal/kube/fake_informer.go b/processor/k8sattributesprocessor/internal/kube/fake_informer.go index 20a9ca879afc..fc5f479a6ddd 100644 --- a/processor/k8sattributesprocessor/internal/kube/fake_informer.go +++ b/processor/k8sattributesprocessor/internal/kube/fake_informer.go @@ -163,6 +163,7 @@ func NewNoOpInformer( func (f *NoOpInformer) AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) { return f.AddEventHandlerWithResyncPeriod(handler, time.Second) } + func (f *NoOpInformer) AddEventHandlerWithResyncPeriod(_ cache.ResourceEventHandler, _ time.Duration) (cache.ResourceEventHandlerRegistration, error) { return nil, nil } @@ -193,9 +194,11 @@ func (c *NoOpController) Run(stopCh <-chan struct{}) { c.hasStopped = true }() } + func (c *NoOpController) IsStopped() bool { return c.hasStopped } + func (c *NoOpController) HasSynced() bool { return true } diff --git a/processor/logdedupprocessor/factory_test.go b/processor/logdedupprocessor/factory_test.go index 1db3284ca45b..14f523c5895c 100644 --- a/processor/logdedupprocessor/factory_test.go +++ b/processor/logdedupprocessor/factory_test.go @@ -23,7 +23,7 @@ func TestNewProcessorFactory(t *testing.T) { } func TestCreateLogs(t *testing.T) { - var testCases = []struct { + testCases := []struct { name string cfg component.Config expectedErr string diff --git a/processor/logstransformprocessor/factory.go b/processor/logstransformprocessor/factory.go index afc165adb1d2..a297a70ed9c6 100644 --- a/processor/logstransformprocessor/factory.go +++ b/processor/logstransformprocessor/factory.go @@ -37,7 +37,8 @@ func createLogsProcessor( _ context.Context, set processor.Settings, cfg component.Config, - nextConsumer consumer.Logs) (processor.Logs, error) { + nextConsumer consumer.Logs, +) (processor.Logs, error) { pCfg, ok := cfg.(*Config) if !ok { return nil, errors.New("could not initialize logs transform processor") diff --git a/processor/logstransformprocessor/processor_test.go b/processor/logstransformprocessor/processor_test.go index fed71c028099..c51bb042911d 100644 --- a/processor/logstransformprocessor/processor_test.go +++ b/processor/logstransformprocessor/processor_test.go @@ -25,30 +25,28 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/regex" ) -var ( - cfg = &Config{ - BaseConfig: adapter.BaseConfig{ - Operators: []operator.Config{ - { - Builder: func() *regex.Config { - cfg := regex.NewConfig() - cfg.Regex = "^(?P