Repository: open-telemetry/opentelemetry-python

Issue:
Update environment variable names
[This](https://github.com/open-telemetry/opentelemetry-specification/blob/88f974d3b14b664610129dde40d8bd40ddec2ef0/specification/sdk-environment-variables.md) now specifies some names for environment variables. Update existing environment variable names to match them.


Discussion:

Resolving patch:
diff --git a/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py b/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py
--- a/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py
+++ b/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py
@@ -40,7 +40,7 @@
 ---
 
 Elasticsearch instrumentation prefixes operation names with the string "Elasticsearch". This
-can be changed to a different string by either setting the `OPENTELEMETRY_PYTHON_ELASTICSEARCH_NAME_PREFIX`
+can be changed to a different string by either setting the `OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX`
 environment variable or by passing the prefix as an argument to the instrumentor. For example,
 
 
@@ -88,8 +88,7 @@ class ElasticsearchInstrumentor(BaseInstrumentor):
     def __init__(self, span_name_prefix=None):
         if not span_name_prefix:
             span_name_prefix = environ.get(
-                "OPENTELEMETRY_PYTHON_ELASTICSEARCH_NAME_PREFIX",
-                "Elasticsearch",
+                "OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX", "Elasticsearch",
             )
         self._span_name_prefix = span_name_prefix.strip()
         super().__init__()
diff --git a/opentelemetry-api/src/opentelemetry/configuration/__init__.py b/opentelemetry-api/src/opentelemetry/configuration/__init__.py
--- a/opentelemetry-api/src/opentelemetry/configuration/__init__.py
+++ b/opentelemetry-api/src/opentelemetry/configuration/__init__.py
@@ -16,30 +16,33 @@
 Simple configuration manager
 
 This is a configuration manager for OpenTelemetry. It reads configuration
-values from environment variables prefixed with ``OPENTELEMETRY_PYTHON_`` whose
-characters are only alphanumeric characters and unserscores, except for the
-first character after ``OPENTELEMETRY_PYTHON_`` which must not be a number.
+values from environment variables prefixed with ``OTEL_`` (for environment
+variables that apply to any OpenTelemetry implementation) or with
+``OTEL_PYTHON_`` (for environment variables that are specific to the Python
+implementation of OpenTelemetry) whose characters are only alphanumeric
+characters and unserscores, except for the first character after ``OTEL_`` or
+``OTEL_PYTHON_`` which must not be a number.
 
 For example, these environment variables will be read:
 
-1. ``OPENTELEMETRY_PYTHON_SOMETHING``
-2. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_``
-3. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_AND__ELSE``
-4. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_AND_else``
-5. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_AND_else2``
+1. ``OTEL_SOMETHING``
+2. ``OTEL_SOMETHING_ELSE_``
+3. ``OTEL_SOMETHING_ELSE_AND__ELSE``
+4. ``OTEL_SOMETHING_ELSE_AND_else``
+5. ``OTEL_SOMETHING_ELSE_AND_else2``
 
 These won't:
 
 1. ``OPENTELEMETRY_PYTH_SOMETHING``
-2. ``OPENTELEMETRY_PYTHON_2_SOMETHING_AND__ELSE``
-3. ``OPENTELEMETRY_PYTHON_SOMETHING_%_ELSE``
+2. ``OTEL_2_SOMETHING_AND__ELSE``
+3. ``OTEL_SOMETHING_%_ELSE``
 
 The values stored in the environment variables can be found in an instance of
 ``opentelemetry.configuration.Configuration``. This class can be instantiated
 freely because instantiating it returns always the same object.
 
 For example, if the environment variable
-``OPENTELEMETRY_PYTHON_METER_PROVIDER`` value is ``my_meter_provider``, then
+``OTEL_PYTHON_METER_PROVIDER`` value is ``my_meter_provider``, then
 ``Configuration().meter_provider == "my_meter_provider"`` would be ``True``.
 
 Non defined attributes will always return ``None``. This is intended to make it
@@ -49,8 +52,8 @@
 Environment variables used by OpenTelemetry
 -------------------------------------------
 
-1. OPENTELEMETRY_PYTHON_METER_PROVIDER
-2. OPENTELEMETRY_PYTHON_TRACER_PROVIDER
+1. OTEL_PYTHON_METER_PROVIDER
+2. OTEL_PYTHON_TRACER_PROVIDER
 
 The value of these environment variables should be the name of the entry point
 that points to the class that implements either provider. This OpenTelemetry
@@ -70,7 +73,7 @@
     }
 
 To use the meter provider above, then the
-``OPENTELEMETRY_PYTHON_METER_PROVIDER`` should be set to
+``OTEL_PYTHON_METER_PROVIDER`` should be set to
 ``"default_meter_provider"`` (this is not actually necessary since the
 OpenTelemetry API provided providers are the default ones used if no
 configuration is found in the environment variables).
@@ -110,13 +113,11 @@ def __new__(cls) -> "Configuration":
             instance = super().__new__(cls)
             for key, value_str in environ.items():
 
-                match = fullmatch(
-                    r"OPENTELEMETRY_PYTHON_([A-Za-z_][\w_]*)", key
-                )
+                match = fullmatch(r"OTEL_(PYTHON_)?([A-Za-z_][\w_]*)", key)
 
                 if match is not None:
 
-                    key = match.group(1)
+                    key = match.group(2)
                     value = value_str  # type: ConfigValue
 
                     if value_str == "True":
diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py
--- a/opentelemetry-api/src/opentelemetry/context/__init__.py
+++ b/opentelemetry-api/src/opentelemetry/context/__init__.py
@@ -55,7 +55,7 @@ def wrapper(
                     default_context = "contextvars_context"
 
                 configured_context = environ.get(
-                    "OPENTELEMETRY_CONTEXT", default_context
+                    "OTEL_CONTEXT", default_context
                 )  # type: str
                 try:
                     _RUNTIME_CONTEXT = next(


Test patch:
diff --git a/ext/opentelemetry-ext-django/tests/conftest.py b/ext/opentelemetry-ext-django/tests/conftest.py
--- a/ext/opentelemetry-ext-django/tests/conftest.py
+++ b/ext/opentelemetry-ext-django/tests/conftest.py
@@ -16,4 +16,4 @@
 
 
 def pytest_sessionstart(session):  # pylint: disable=unused-argument
-    environ.setdefault("OPENTELEMETRY_PYTHON_DJANGO_INSTRUMENT", "True")
+    environ.setdefault("OTEL_PYTHON_DJANGO_INSTRUMENT", "True")
diff --git a/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py b/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py
--- a/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py
+++ b/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py
@@ -95,7 +95,7 @@ def test_prefix_arg(self, request_mock):
 
     def test_prefix_env(self, request_mock):
         prefix = "prefix-from-args"
-        env_var = "OPENTELEMETRY_PYTHON_ELASTICSEARCH_NAME_PREFIX"
+        env_var = "OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX"
         os.environ[env_var] = prefix
         ElasticsearchInstrumentor().uninstrument()
         ElasticsearchInstrumentor().instrument()
diff --git a/opentelemetry-api/tests/configuration/test_configuration.py b/opentelemetry-api/tests/configuration/test_configuration.py
--- a/opentelemetry-api/tests/configuration/test_configuration.py
+++ b/opentelemetry-api/tests/configuration/test_configuration.py
@@ -32,10 +32,10 @@ def test_singleton(self) -> None:
     @patch.dict(
         "os.environ",  # type: ignore
         {
-            "OPENTELEMETRY_PYTHON_METER_PROVIDER": "meter_provider",
-            "OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider",
-            "OPENTELEMETRY_PYTHON_OThER": "other",
-            "OPENTELEMETRY_PYTHON_OTHER_7": "other_7",
+            "OTEL_PYTHON_METER_PROVIDER": "meter_provider",
+            "OTEL_PYTHON_TRACER_PROVIDER": "tracer_provider",
+            "OTEL_OThER": "other",
+            "OTEL_OTHER_7": "other_7",
             "OPENTELEMETRY_PTHON_TRACEX_PROVIDER": "tracex_provider",
         },
     )
@@ -56,7 +56,7 @@ def test_environment_variables(self) -> None:
 
     @patch.dict(
         "os.environ",  # type: ignore
-        {"OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider"},
+        {"OTEL_PYTHON_TRACER_PROVIDER": "tracer_provider"},
     )
     def test_property(self) -> None:
         with self.assertRaises(AttributeError):
@@ -79,8 +79,7 @@ def test_getattr(self) -> None:
 
     def test_reset(self) -> None:
         environ_patcher = patch.dict(
-            "os.environ",
-            {"OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider"},
+            "os.environ", {"OTEL_PYTHON_TRACER_PROVIDER": "tracer_provider"},
         )
 
         environ_patcher.start()
@@ -99,10 +98,7 @@ def test_reset(self) -> None:
 
     @patch.dict(
         "os.environ",  # type: ignore
-        {
-            "OPENTELEMETRY_PYTHON_TRUE": "True",
-            "OPENTELEMETRY_PYTHON_FALSE": "False",
-        },
+        {"OTEL_TRUE": "True", "OTEL_FALSE": "False"},
     )
     def test_boolean(self) -> None:
         self.assertIsInstance(
@@ -117,9 +113,9 @@ def test_boolean(self) -> None:
     @patch.dict(
         "os.environ",  # type: ignore
         {
-            "OPENTELEMETRY_PYTHON_POSITIVE_INTEGER": "123",
-            "OPENTELEMETRY_PYTHON_NEGATIVE_INTEGER": "-123",
-            "OPENTELEMETRY_PYTHON_NON_INTEGER": "-12z3",
+            "OTEL_POSITIVE_INTEGER": "123",
+            "OTEL_NEGATIVE_INTEGER": "-123",
+            "OTEL_NON_INTEGER": "-12z3",
         },
     )
     def test_integer(self) -> None:
@@ -136,9 +132,9 @@ def test_integer(self) -> None:
     @patch.dict(
         "os.environ",  # type: ignore
         {
-            "OPENTELEMETRY_PYTHON_POSITIVE_FLOAT": "123.123",
-            "OPENTELEMETRY_PYTHON_NEGATIVE_FLOAT": "-123.123",
-            "OPENTELEMETRY_PYTHON_NON_FLOAT": "-12z3.123",
+            "OTEL_POSITIVE_FLOAT": "123.123",
+            "OTEL_NEGATIVE_FLOAT": "-123.123",
+            "OTEL_NON_FLOAT": "-12z3.123",
         },
     )
     def test_float(self) -> None:
diff --git a/opentelemetry-sdk/tests/conftest.py b/opentelemetry-sdk/tests/conftest.py
--- a/opentelemetry-sdk/tests/conftest.py
+++ b/opentelemetry-sdk/tests/conftest.py
@@ -20,11 +20,11 @@ def pytest_sessionstart(session):
     # pylint: disable=unused-argument
     if version_info < (3, 5):
         # contextvars are not supported in 3.4, use thread-local storage
-        environ["OPENTELEMETRY_CONTEXT"] = "threadlocal_context"
+        environ["OTEL_CONTEXT"] = "threadlocal_context"
     else:
-        environ["OPENTELEMETRY_CONTEXT"] = "contextvars_context"
+        environ["OTEL_CONTEXT"] = "contextvars_context"
 
 
 def pytest_sessionfinish(session):
     # pylint: disable=unused-argument
-    environ.pop("OPENTELEMETRY_CONTEXT")
+    environ.pop("OTEL_CONTEXT")