Skip to content

Add context propagation checks to component lifecycle tests #13182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

samuelarogbonlo
Copy link

@samuelarogbonlo samuelarogbonlo commented Jun 9, 2025

Description

This change introduces validation logic for context propagation during component lifecycle execution in the OpenTelemetry Collector.

A new helper (context_check_consumer.go) is added to assert context values during Start, Consume, and Shutdown.
The code generation template was updated to inject context.WithValue(...) across all components.
The metadata files were also fixed to pass schema validation for mdatagen.

Link to tracking issue

Fixes #13142

Testing

  • Ran full test suite with go test ./... – all tests pass
  • Verified injected context values are correctly propagated and asserted during lifecycle phases

Documentation

  • Inline comments added to context_check_consumer.go
  • Template updated with context injection logic for lifecycle tests
  • Regenerated test files reflect the changes across receivers, processors, scrapers, and connectors

- Introduced context_check_consumer.go to validate context handling
- Updated component_test.go.tmpl to inject context in tests
- Regenerated component test files across sample components
- Cleaned up metadata.yaml files to fix mdatagen errors
@samuelarogbonlo samuelarogbonlo requested review from dmitryax and a team as code owners June 9, 2025 22:43
@samuelarogbonlo
Copy link
Author

@dmathieu @dmitryax Just a follow-up to review. Thanks

@@ -6,7 +6,6 @@ github_project: open-telemetry/opentelemetry-collector
sem_conv_version: 1.9.0

status:
disable_codecov_badge: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep only changes relevant to the PR.

Copy link
Author

@samuelarogbonlo samuelarogbonlo Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to metadata.yaml files — including the removal of disable_codecov_badge and events keys were necessary to make mdatagen run successfully.

Without cleaning these fields, mdatagen throws the following error:
Error: failed loading ...metadata.yaml: decoding failed due to the following error(s): 'status' has invalid keys: disable_codecov_badge '' has invalid keys: events
These fields look to be legacy or invalid keys no longer supported by the current mdatagen schema. Cleaning them was essential to regenerate component test files and validate the context propagation logic as part of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clean them in another PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this makes sense. I’ll separate the cleanup of the metadata.yaml files into another PR and keep this one focused on the context propagation validation.

T *testing.T
}

func (c *ContextCheckLogsConsumer) ConsumeLogs(ctx context.Context, logs plog.Logs) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way of writing tests seems weird, and not really a Go pattern. Why can't we run the assertion inline?

Copy link
Author

@samuelarogbonlo samuelarogbonlo Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. You're right that the current structure isn’t a typical Go pattern.
I introduced ContextCheck*Consumer as a lightweight reusable helper to avoid duplicating the same context assertion logic in each test case. The idea was to keep individual test methods focused on wiring rather than repeating assertions.
That said, I can definitely refactor it to assert inline if preferred. Let me know if you’d like to see it changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mdatagen] Validate context propagation on processors and connectors
2 participants