Skip to content
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

[PR #3091/517570a6 backport][stable-3] Add opentelemetry callback plugin #3373

Conversation

patchback[bot]
Copy link

@patchback patchback bot commented Sep 14, 2021

This is a backport of PR #3091 as merged into main (517570a).

SUMMARY

Send distributed traces for the ansible runs with OpenTelemetry

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

plugins/callback/opentelemetry.py

ADDITIONAL INFORMATION

You can configure this plugin and then run your playbook with the below environment variables. Those environment variables are the ones defined by the opentelemetry project (see https://opentelemetry-python.readthedocs.io/en/latest/exporter/otlp/otlp.html)

In order to configure this plugins is requried to install the below python libs:

pip3 install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp

Then you can start sending traces to your OTEL endpoint:

$ OTEL_SERVICE_NAME='ansible-otel' \
   OTEL_EXPORTER_OTLP_ENDPOINT='<your_otlp_endpoint>' \
   OTEL_EXPORTER_OTLP_HEADERS='authorization=Bearer <your_token>' \
   ansible-playbook <your_playbook>

Or even you can see the distributed traces in the console:

$ OPENTELEMETRY_CONSOLE_OUTPUT=true \
   ansible-playbook <your_playbook>

This is vendor agnostic, as long as the OpenTelemetry is supported those traces are sent, for instance, when running the molecule test the converge stage produced the below traces for one of our roles.

image

Molecule was configured with something like:

provisioner:
  name: ansible
  env:
    JUNIT_OUTPUT_DIR: $MOLECULE_SCENARIO_DIRECTORY/test-results
  config_options:
    defaults:
      callback_plugins: $MOLECULE_SCENARIO_DIRECTORY/../../../../callback_plugins
      callback_whitelist: opentelemetry
Questions
  • Can I add some ITs to help?
Tasks
  • Add tests
  • Verify changes locally.
  • Verify changes in a staging environment.
Follow ups

The below follow ups will be tackle in future PRs after merging this

  1. Add context propagation
  2. Handle task failures as errors

* Add opentelemetry callback plugin

* Apply suggestions from code review

Co-authored-by: Felix Fontein <[email protected]>

* Formatting (text), booleans and renamed env variables

* This should be done in a future release

* Remove insecure in favour of the OTEL env variable. Add descriptions

* Use OpenTelemetrySource

* Move generate_distributed_traces

* Move update_span_data and set_span_attribute

* Move finish_task

* Move start_task

* Refactor to support UTs

* Add first UT

* Fix codestyle

* opentelemetry callback entry in the botmeta

* Fix linting

* Fix signature

* Mock methods

* Use MagicMock

* Mock the methods

* UT for transform_to_boolean_or_default

* Fix linting

* Set test data

* Mock _time_ns

* Exclude tests for python <= 3.6

* Remove obsoleted setup task type configuration

* Remove unused docs

* Apply suggestions from code review

Co-authored-by: Felix Fontein <[email protected]>

* Fix docs

* unrequired logic that was originally took from https:/ansible/ansible/blob/devel/lib/ansible/plugins/callback/junit.py\#L226

* Use raise_from for the required dependencies

* Fix linting

* Add requirements for the UTs

* add missing dependency for the opentelemetry plugin in the UTs

* Add ANSIBLE_ prefix for the ansible specific options

* Add more context in the docs and remove duplicated docs

* As suggested in the code review

* Verify if the OTEL env variables for the endpoint were set

* Fix docs typo

* Fix linting

* Revert "Fix linting"

This reverts commit 3a54c827c5472553a6baf5598bc76a0f63f020c1.

* Revert "Verify if the OTEL env variables for the endpoint were set"

This reverts commit cab9d8648899c28c0345745690c4ec7a41f7e680.

* Remove console_output as suggested

* Apply suggestions from code review

Co-authored-by: flowerysong <[email protected]>

* Delegate the definition of OTEL_EXPORTER_OTLP_INSECURE to the user

* Move definitions above, close to the class that uses them

Co-authored-by: Felix Fontein <[email protected]>
Co-authored-by: flowerysong <[email protected]>
(cherry picked from commit 517570a)
@patchback patchback bot mentioned this pull request Sep 14, 2021
3 tasks
@ansibullbot ansibullbot added backport callback callback plugin new_contributor Help guide this first time contributor new_plugin New plugin plugins plugin (any type) tests tests unit tests/unit labels Sep 14, 2021
@felixfontein felixfontein merged commit 839880d into stable-3 Sep 15, 2021
@felixfontein felixfontein deleted the patchback/backports/stable-3/517570a64fcbbfdad5704f88c63eabb4cf74a84d/pr-3091 branch September 15, 2021 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callback callback plugin new_contributor Help guide this first time contributor new_plugin New plugin plugins plugin (any type) tests tests unit tests/unit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants