Skip to content

Commit bd40e7d

Browse files
committed
Merge branch 'main' into merge_main_0
2 parents ae757a9 + 18b5cb0 commit bd40e7d

File tree

14 files changed

+101
-50
lines changed

14 files changed

+101
-50
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
([#2153](https://github.com/open-telemetry/opentelemetry-python/pull/2153))
1111
- Add metrics API
1212
([#1887](https://github.com/open-telemetry/opentelemetry-python/pull/1887))
13+
- Automatically load OTEL environment variables as options for `opentelemetry-instrument`
14+
([#1969](https://github.com/open-telemetry/opentelemetry-python/pull/1969))
1315
- `opentelemetry-semantic-conventions` Update to semantic conventions v1.6.1
1416
([#2077](https://github.com/open-telemetry/opentelemetry-python/pull/2077))
1517
- Do not count invalid attributes for dropped

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ rather than conform to specific API names or argument patterns in the spec.
214214

215215
For a deeper discussion, see: https://github.com/open-telemetry/opentelemetry-specification/issues/165
216216

217+
### Environment Variables
218+
219+
If you are adding a component that introduces new OpenTelemetry environment variables, put them all in a module,
220+
as it is done in `opentelemetry.environment_variables` or in `opentelemetry.sdk.environment_variables`.
221+
222+
Keep in mind that any new environment variable must be declared in all caps and must start with `OTEL_PYTHON_`.
223+
224+
Register this module with the `opentelemetry_environment_variables` entry point to make your environment variables
225+
automatically load as options for the `opentelemetry-instrument` command.
226+
217227
## Style Guide
218228

219229
* docstrings should adhere to the [Google Python Style

opentelemetry-api/setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ opentelemetry_tracer_provider =
5656
opentelemetry_propagator =
5757
tracecontext = opentelemetry.trace.propagation.tracecontext:TraceContextTextMapPropagator
5858
baggage = opentelemetry.baggage.propagation:W3CBaggagePropagator
59+
opentelemetry_environment_variables =
60+
api = opentelemetry.environment_variables
5961

6062
[options.extras_require]
6163
test =

opentelemetry-api/src/opentelemetry/environment_variables/__init__.py renamed to opentelemetry-api/src/opentelemetry/environment_variables.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
.. envvar:: OTEL_PYTHON_CONTEXT
2323
"""
2424

25-
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"
26-
"""
27-
.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS
28-
"""
29-
3025
OTEL_PYTHON_ID_GENERATOR = "OTEL_PYTHON_ID_GENERATOR"
3126
"""
3227
.. envvar:: OTEL_PYTHON_ID_GENERATOR

opentelemetry-distro/src/opentelemetry/distro/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
#
14+
1515
import os
1616

1717
from opentelemetry.environment_variables import OTEL_TRACES_EXPORTER

opentelemetry-instrumentation/setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ where = src
5151
console_scripts =
5252
opentelemetry-instrument = opentelemetry.instrumentation.auto_instrumentation:run
5353
opentelemetry-bootstrap = opentelemetry.instrumentation.bootstrap:run
54+
opentelemetry_environment_variables =
55+
instrumentation = opentelemetry.instrumentation.environment_variables
5456

5557
[options.extras_require]
5658
test =

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,72 +14,74 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import argparse
17+
from argparse import REMAINDER, ArgumentParser
1818
from logging import getLogger
1919
from os import environ, execl, getcwd
2020
from os.path import abspath, dirname, pathsep
21+
from re import sub
2122
from shutil import which
2223

23-
from opentelemetry.environment_variables import (
24-
OTEL_PYTHON_ID_GENERATOR,
25-
OTEL_TRACES_EXPORTER,
26-
)
24+
from pkg_resources import iter_entry_points
2725

28-
logger = getLogger(__file__)
26+
_logger = getLogger(__file__)
2927

3028

31-
def parse_args():
32-
parser = argparse.ArgumentParser(
29+
def run() -> None:
30+
31+
parser = ArgumentParser(
3332
description="""
3433
opentelemetry-instrument automatically instruments a Python
3534
program and its dependencies and then runs the program.
36-
"""
35+
""",
36+
epilog="""
37+
Optional arguments (except for --help) for opentelemetry-instrument
38+
directly correspond with OpenTelemetry environment variables. The
39+
corresponding optional argument is formed by removing the OTEL_ or
40+
OTEL_PYTHON_ prefix from the environment variable and lower casing the
41+
rest. For example, the optional argument --attribute_value_length_limit
42+
corresponds with the environment variable
43+
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT.
44+
45+
These optional arguments will override the current value of the
46+
corresponding environment variable during the execution of the command.
47+
""",
3748
)
3849

39-
parser.add_argument(
40-
"--trace-exporter",
41-
required=False,
42-
help="""
43-
Uses the specified exporter to export spans.
44-
Accepts multiple exporters as comma separated values.
50+
argument_otel_environment_variable = {}
4551

46-
Examples:
52+
for entry_point in iter_entry_points(
53+
"opentelemetry_environment_variables"
54+
):
55+
environment_variable_module = entry_point.load()
4756

48-
--trace-exporter=jaeger
49-
""",
50-
)
57+
for attribute in dir(environment_variable_module):
5158

52-
parser.add_argument(
53-
"--id-generator",
54-
required=False,
55-
help="""
56-
The IDs Generator to be used with the Tracer Provider.
59+
if attribute.startswith("OTEL_"):
5760

58-
Examples:
61+
argument = sub(r"OTEL_(PYTHON_)?", "", attribute).lower()
5962

60-
--id-generator=random
61-
""",
62-
)
63+
parser.add_argument(
64+
f"--{argument}",
65+
required=False,
66+
)
67+
argument_otel_environment_variable[argument] = attribute
6368

6469
parser.add_argument("command", help="Your Python application.")
6570
parser.add_argument(
6671
"command_args",
6772
help="Arguments for your application.",
68-
nargs=argparse.REMAINDER,
73+
nargs=REMAINDER,
6974
)
70-
return parser.parse_args()
71-
7275

73-
def load_config_from_cli_args(args):
74-
if args.trace_exporter:
75-
environ[OTEL_TRACES_EXPORTER] = args.trace_exporter
76-
if args.id_generator:
77-
environ[OTEL_PYTHON_ID_GENERATOR] = args.id_generator
76+
args = parser.parse_args()
7877

78+
for argument, otel_environment_variable in (
79+
argument_otel_environment_variable
80+
).items():
81+
value = getattr(args, argument)
82+
if value is not None:
7983

80-
def run() -> None:
81-
args = parse_args()
82-
load_config_from_cli_args(args)
84+
environ[otel_environment_variable] = value
8385

8486
python_path = environ.get("PYTHONPATH")
8587

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
from pkg_resources import iter_entry_points
2222

23-
from opentelemetry.environment_variables import (
24-
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
25-
)
2623
from opentelemetry.instrumentation.dependencies import (
2724
get_dist_dependency_conflicts,
2825
)
2926
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
27+
from opentelemetry.instrumentation.environment_variables import (
28+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
29+
)
3030

3131
logger = getLogger(__file__)
3232

opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"instrumentation": "opentelemetry-instrumentation-elasticsearch==0.24b0",
5454
},
5555
"falcon": {
56-
"library": "falcon ~= 2.0",
56+
"library": "falcon >= 2.0.0, < 4.0.0",
5757
"instrumentation": "opentelemetry-instrumentation-falcon==0.24b0",
5858
},
5959
"fastapi": {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"
16+
"""
17+
.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS
18+
"""

opentelemetry-instrumentation/tests/test_run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def test_exporter(self, _): # pylint: disable=no-self-use
111111
self.assertIsNone(environ.get(OTEL_TRACES_EXPORTER))
112112

113113
with patch(
114-
"sys.argv", ["instrument", "--trace-exporter", "jaeger", "1", "2"]
114+
"sys.argv",
115+
["instrument", "--traces_exporter", "jaeger", "1", "2"],
115116
):
116117
auto_instrumentation.run()
117118
self.assertEqual(environ.get(OTEL_TRACES_EXPORTER), "jaeger")

opentelemetry-sdk/setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ opentelemetry_traces_exporter =
5656
console = opentelemetry.sdk.trace.export:ConsoleSpanExporter
5757
opentelemetry_id_generator =
5858
random = opentelemetry.sdk.trace.id_generator:RandomIdGenerator
59+
opentelemetry_environment_variables =
60+
sdk = opentelemetry.sdk.environment_variables
5961

6062
[options.extras_require]
6163
test =

tests/util/src/opentelemetry/test/wsgitestutil.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io
1616
import wsgiref.util as wsgiref_util
1717

18+
from opentelemetry import trace
1819
from opentelemetry.test.spantestutil import SpanTestBase
1920

2021

@@ -37,3 +38,19 @@ def start_response(self, status, response_headers, exc_info=None):
3738
self.response_headers = response_headers
3839
self.exc_info = exc_info
3940
return self.write
41+
42+
def assertTraceResponseHeaderMatchesSpan(
43+
self, headers, span
44+
): # pylint: disable=invalid-name
45+
self.assertIn("traceresponse", headers)
46+
self.assertEqual(
47+
headers["access-control-expose-headers"],
48+
"traceresponse",
49+
)
50+
51+
trace_id = trace.format_trace_id(span.get_span_context().trace_id)
52+
span_id = trace.format_span_id(span.get_span_context().span_id)
53+
self.assertEqual(
54+
f"00-{trace_id}-{span_id}-01",
55+
headers["traceresponse"],
56+
)

0 commit comments

Comments
 (0)