Skip to content

Commit

Permalink
[processor/k8sattributes] implement lifecycle tests and report errors…
Browse files Browse the repository at this point in the history
… through component status
  • Loading branch information
atoulme committed Jan 10, 2024
1 parent 7d03122 commit f7b2292
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 63 deletions.
27 changes: 27 additions & 0 deletions .chloggen/k8sattributes_lifecycle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: k8sattributesprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Apply lifecycle tests to k8sprocessor, change its behavior to report fatal error

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [30387]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
39 changes: 9 additions & 30 deletions processor/k8sattributesprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ func createTracesProcessorWithOptions(
next consumer.Traces,
options ...option,
) (processor.Traces, error) {
kp, err := createKubernetesProcessor(set, cfg, options...)
if err != nil {
return nil, err
}
kp := createKubernetesProcessor(set, cfg, options...)

return processorhelper.NewTracesProcessor(
ctx,
Expand All @@ -98,10 +95,7 @@ func createMetricsProcessorWithOptions(
nextMetricsConsumer consumer.Metrics,
options ...option,
) (processor.Metrics, error) {
kp, err := createKubernetesProcessor(set, cfg, options...)
if err != nil {
return nil, err
}
kp := createKubernetesProcessor(set, cfg, options...)

return processorhelper.NewMetricsProcessor(
ctx,
Expand All @@ -121,10 +115,7 @@ func createLogsProcessorWithOptions(
nextLogsConsumer consumer.Logs,
options ...option,
) (processor.Logs, error) {
kp, err := createKubernetesProcessor(set, cfg, options...)
if err != nil {
return nil, err
}
kp := createKubernetesProcessor(set, cfg, options...)

return processorhelper.NewLogsProcessor(
ctx,
Expand All @@ -141,26 +132,14 @@ func createKubernetesProcessor(
params processor.CreateSettings,
cfg component.Config,
options ...option,
) (*kubernetesprocessor, error) {
kp := &kubernetesprocessor{logger: params.Logger}

allOptions := append(createProcessorOpts(cfg), options...)

for _, opt := range allOptions {
if err := opt(kp); err != nil {
return nil, err
}
}

// This might have been set by an option already
if kp.kc == nil {
err := kp.initKubeClient(kp.logger, kubeClientProvider)
if err != nil {
return nil, err
}
) *kubernetesprocessor {
kp := &kubernetesprocessor{logger: params.Logger,
cfg: cfg,
options: options,
telemetrySettings: params.TelemetrySettings,
}

return kp, nil
return kp
}

func createProcessorOpts(cfg component.Config) []option {
Expand Down
8 changes: 7 additions & 1 deletion processor/k8sattributesprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
Expand Down Expand Up @@ -130,3 +130,9 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8ste
replace cloud.google.com/go v0.54.0 => cloud.google.com/go v0.110.10

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden
6 changes: 2 additions & 4 deletions processor/k8sattributesprocessor/go.sum

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

36 changes: 28 additions & 8 deletions processor/k8sattributesprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ const (
)

type kubernetesprocessor struct {
logger *zap.Logger
apiConfig k8sconfig.APIConfig
kc kube.Client
passthroughMode bool
rules kube.ExtractionRules
filters kube.Filters
podAssociations []kube.Association
podIgnore kube.Excludes
cfg component.Config
options []option
telemetrySettings component.TelemetrySettings
logger *zap.Logger
apiConfig k8sconfig.APIConfig
kc kube.Client
passthroughMode bool
rules kube.ExtractionRules
filters kube.Filters
podAssociations []kube.Association
podIgnore kube.Excludes
}

func (kp *kubernetesprocessor) initKubeClient(logger *zap.Logger, kubeClient kube.ClientProvider) error {
Expand All @@ -50,6 +53,23 @@ func (kp *kubernetesprocessor) initKubeClient(logger *zap.Logger, kubeClient kub
}

func (kp *kubernetesprocessor) Start(_ context.Context, _ component.Host) error {
allOptions := append(createProcessorOpts(kp.cfg), kp.options...)

for _, opt := range allOptions {
if err := opt(kp); err != nil {
kp.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
return nil
}
}

// This might have been set by an option already
if kp.kc == nil {
err := kp.initKubeClient(kp.logger, kubeClientProvider)
if err != nil {
kp.telemetrySettings.ReportStatus(component.NewFatalErrorEvent(err))
return nil
}
}
if !kp.passthroughMode {
go kp.kc.Start()
}
Expand Down
58 changes: 38 additions & 20 deletions processor/k8sattributesprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,48 @@ func newPodIdentifier(from string, name string, value string) kube.PodIdentifier
}
}

func newTracesProcessor(cfg component.Config, next consumer.Traces, options ...option) (processor.Traces, error) {
func newTracesProcessor(cfg component.Config, next consumer.Traces, errFunc func(error), options ...option) (processor.Traces, error) {
opts := options
opts = append(opts, withKubeClientProvider(newFakeClient))
set := processortest.NewNopCreateSettings()
set.ReportStatus = func(event *component.StatusEvent) {
errFunc(event.Err())
}
return createTracesProcessorWithOptions(
context.Background(),
processortest.NewNopCreateSettings(),
set,
cfg,
next,
opts...,
)
}

func newMetricsProcessor(cfg component.Config, nextMetricsConsumer consumer.Metrics, options ...option) (processor.Metrics, error) {
func newMetricsProcessor(cfg component.Config, nextMetricsConsumer consumer.Metrics, errFunc func(error), options ...option) (processor.Metrics, error) {
opts := options
opts = append(opts, withKubeClientProvider(newFakeClient))
set := processortest.NewNopCreateSettings()
set.ReportStatus = func(event *component.StatusEvent) {
errFunc(event.Err())
}
return createMetricsProcessorWithOptions(
context.Background(),
processortest.NewNopCreateSettings(),
set,
cfg,
nextMetricsConsumer,
opts...,
)
}

func newLogsProcessor(cfg component.Config, nextLogsConsumer consumer.Logs, options ...option) (processor.Logs, error) {
func newLogsProcessor(cfg component.Config, nextLogsConsumer consumer.Logs, errFunc func(error), options ...option) (processor.Logs, error) {
opts := options
opts = append(opts, withKubeClientProvider(newFakeClient))
set := processortest.NewNopCreateSettings()
set.ReportStatus = func(event *component.StatusEvent) {
errFunc(event.Err())
}
return createLogsProcessorWithOptions(
context.Background(),
processortest.NewNopCreateSettings(),
set,
cfg,
nextLogsConsumer,
opts...,
Expand Down Expand Up @@ -122,31 +134,28 @@ func newMultiTest(
nextLogs: new(consumertest.LogsSink),
}

tp, err := newTracesProcessor(cfg, m.nextTrace, append(options, withExtractKubernetesProcessorInto(&m.kpTrace))...)
tp, err := newTracesProcessor(cfg, m.nextTrace, errFunc, append(options, withExtractKubernetesProcessorInto(&m.kpTrace))...)
require.NoError(t, err)
err = tp.Start(context.Background(), componenttest.NewNopHost())
if errFunc == nil {
assert.NotNil(t, tp)
require.NoError(t, err)
} else {
assert.Nil(t, tp)
errFunc(err)
}

mp, err := newMetricsProcessor(cfg, m.nextMetrics, append(options, withExtractKubernetesProcessorInto(&m.kpMetrics))...)
mp, err := newMetricsProcessor(cfg, m.nextMetrics, errFunc, append(options, withExtractKubernetesProcessorInto(&m.kpMetrics))...)
require.NoError(t, err)
err = mp.Start(context.Background(), componenttest.NewNopHost())
if errFunc == nil {
assert.NotNil(t, mp)
require.NoError(t, err)
} else {
assert.Nil(t, mp)
errFunc(err)
}

lp, err := newLogsProcessor(cfg, m.nextLogs, append(options, withExtractKubernetesProcessorInto(&m.kpLogs))...)
lp, err := newLogsProcessor(cfg, m.nextLogs, errFunc, append(options, withExtractKubernetesProcessorInto(&m.kpLogs))...)
require.NoError(t, err)
err = lp.Start(context.Background(), componenttest.NewNopHost())
if errFunc == nil {
assert.NotNil(t, lp)
require.NoError(t, err)
} else {
assert.Nil(t, lp)
errFunc(err)
}

m.tp = tp
Expand Down Expand Up @@ -220,7 +229,7 @@ func TestProcessorBadClientProvider(t *testing.T) {
}

newMultiTest(t, NewFactory().CreateDefaultConfig(), func(err error) {
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, "bad client error", err.Error())
}, withKubeClientProvider(clientProvider))
}
Expand Down Expand Up @@ -1180,10 +1189,13 @@ func TestMetricsProcessorHostname(t *testing.T) {
p, err := newMetricsProcessor(
NewFactory().CreateDefaultConfig(),
next,
nil,
withExtractMetadata(conventions.AttributeK8SPodName),
withExtractKubernetesProcessorInto(&kp),
)
require.NoError(t, err)
err = p.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
kc := kp.kc.(*fakeClient)

// invalid ip should not be used to lookup k8s pod
Expand Down Expand Up @@ -1250,10 +1262,13 @@ func TestMetricsProcessorHostnameWithPodAssociation(t *testing.T) {
p, err := newMetricsProcessor(
NewFactory().CreateDefaultConfig(),
next,
nil,
withExtractMetadata(conventions.AttributeK8SPodName),
withExtractKubernetesProcessorInto(&kp),
)
require.NoError(t, err)
err = p.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
kc := kp.kc.(*fakeClient)
kp.podAssociations = []kube.Association{
{
Expand Down Expand Up @@ -1332,6 +1347,7 @@ func TestPassthroughStart(t *testing.T) {
p, err := newTracesProcessor(
NewFactory().CreateDefaultConfig(),
next,
nil,
opts...,
)
require.NoError(t, err)
Expand All @@ -1346,7 +1362,7 @@ func TestRealClient(t *testing.T) {
t,
NewFactory().CreateDefaultConfig(),
func(err error) {
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, "unable to load k8s config, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined", err.Error())
},
withKubeClientProvider(kubeClientProvider),
Expand All @@ -1358,6 +1374,7 @@ func TestCapabilities(t *testing.T) {
p, err := newTracesProcessor(
NewFactory().CreateDefaultConfig(),
consumertest.NewNop(),
nil,
)
assert.NoError(t, err)
caps := p.Capabilities()
Expand All @@ -1369,6 +1386,7 @@ func TestStartStop(t *testing.T) {
p, err := newTracesProcessor(
NewFactory().CreateDefaultConfig(),
consumertest.NewNop(),
nil,
withExtractKubernetesProcessorInto(&kp),
)
require.NoError(t, err)
Expand Down

0 comments on commit f7b2292

Please sign in to comment.