diff --git a/changelogs/fragments/3498-callback_opentelemetry-only_in_ci.yml b/changelogs/fragments/3498-callback_opentelemetry-only_in_ci.yml new file mode 100644 index 0000000000..7187ba3770 --- /dev/null +++ b/changelogs/fragments/3498-callback_opentelemetry-only_in_ci.yml @@ -0,0 +1,2 @@ +minor_changes: + - opentelemetry callback plugin - added option ``enable_from_environment`` to support enabling the plugin only if the given environment variable exists and it is set to true (https://github.com/ansible-collections/community.general/pull/3498). diff --git a/plugins/callback/opentelemetry.py b/plugins/callback/opentelemetry.py index b48d22b049..7604730461 100644 --- a/plugins/callback/opentelemetry.py +++ b/plugins/callback/opentelemetry.py @@ -23,6 +23,17 @@ DOCUMENTATION = ''' - Hide the arguments for a task. env: - name: ANSIBLE_OPENTELEMETRY_HIDE_TASK_ARGUMENTS + enable_from_environment: + type: str + description: + - Whether to enable this callback only if the given environment variable exists and it is set to C(true). + - This is handy when you use Configuration as Code and want to send distributed traces + if running in the CI rather when running Ansible locally. + - For such, it evaluates the given I(enable_from_environment) value as environment variable + and if set to true this plugin will be enabled. + env: + - name: ANSIBLE_OPENTELEMETRY_ENABLE_FROM_ENVIRONMENT + version_added: 3.8.0 otel_service_name: default: ansible type: str @@ -57,6 +68,7 @@ examples: | ''' import getpass +import os import socket import sys import time @@ -325,6 +337,12 @@ class CallbackModule(CallbackBase): var_options=var_options, direct=direct) + environment_variable = self.get_option('enable_from_environment') + if environment_variable is not None and os.environ.get(environment_variable, 'false').lower() != 'true': + self.disabled = True + self._display.warning("The `enable_from_environment` option has been set and {0} is not enabled. " + "Disabling the `opentelemetry` callback plugin.".format(environment_variable)) + self.hide_task_arguments = self.get_option('hide_task_arguments') self.otel_service_name = self.get_option('otel_service_name')