Skip to content

Commit 99a55d4

Browse files
committed
Move change to exporter
1 parent 0dc2861 commit 99a55d4

File tree

17 files changed

+207
-282
lines changed

17 files changed

+207
-282
lines changed

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- :envvar:`OTEL_EXPORTER_OTLP_TRACES_HEADERS`
3030
- :envvar:`OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`
3131
- :envvar:`OTEL_EXPORTER_OTLP_TRACES_COMPRESSION`
32+
- :envvar:`OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER`
3233
- :envvar:`OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE`
3334
- :envvar:`OTEL_EXPORTER_OTLP_TIMEOUT`
3435
- :envvar:`OTEL_EXPORTER_OTLP_PROTOCOL`

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
4141
OTEL_EXPORTER_OTLP_LOGS_INSECURE,
4242
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
43+
OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER,
4344
)
4445

4546

@@ -73,6 +74,7 @@ def __init__(
7374
):
7475
credentials = _get_credentials(
7576
credentials,
77+
OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER,
7678
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE,
7779
OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY,
7880
OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE,

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@
6868
OTEL_EXPORTER_OTLP_HEADERS,
6969
OTEL_EXPORTER_OTLP_INSECURE,
7070
OTEL_EXPORTER_OTLP_TIMEOUT,
71+
OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER,
7172
)
7273
from opentelemetry.sdk.metrics.export import MetricsData
7374
from opentelemetry.sdk.resources import Resource as SDKResource
7475
from opentelemetry.sdk.trace import ReadableSpan
76+
from opentelemetry.util._importlib_metadata import entry_points
7577
from opentelemetry.util.re import parse_env_headers
7678

7779
_RETRYABLE_ERROR_CODES = frozenset(
@@ -166,12 +168,31 @@ def _load_credentials(
166168

167169
def _get_credentials(
168170
creds: Optional[ChannelCredentials],
171+
credential_entry_point_env_key: str,
169172
certificate_file_env_key: str,
170173
client_key_file_env_key: str,
171174
client_certificate_file_env_key: str,
172175
) -> ChannelCredentials:
173176
if creds is not None:
174177
return creds
178+
credential_env = environ.get(credential_entry_point_env_key)
179+
if credential_env:
180+
try:
181+
maybe_channel_creds = next(
182+
iter(
183+
entry_points(
184+
group="opentelemetry_otlp_credential_provider",
185+
name=credential_env,
186+
)
187+
)
188+
).load()("GRPC")
189+
except StopIteration:
190+
raise RuntimeError(
191+
f"Requested component '{credential_env}' not found in "
192+
f"entry point 'opentelemetry_otlp_credential_provider'"
193+
)
194+
if isinstance(maybe_channel_creds, ChannelCredentials):
195+
return maybe_channel_creds
175196

176197
certificate_file = environ.get(certificate_file_env_key)
177198
if certificate_file:
@@ -275,15 +296,16 @@ def __init__(
275296
options=self._channel_options,
276297
)
277298
else:
278-
credentials = _get_credentials(
299+
self._credentials = _get_credentials(
279300
credentials,
301+
OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER,
280302
OTEL_EXPORTER_OTLP_CERTIFICATE,
281303
OTEL_EXPORTER_OTLP_CLIENT_KEY,
282304
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE,
283305
)
284306
self._channel = secure_channel(
285307
self._endpoint,
286-
credentials,
308+
self._credentials,
287309
compression=compression,
288310
options=self._channel_options,
289311
)

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
OTEL_EXPORTER_OTLP_METRICS_HEADERS,
5252
OTEL_EXPORTER_OTLP_METRICS_INSECURE,
5353
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
54+
OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER,
5455
)
5556
from opentelemetry.sdk.metrics._internal.aggregation import Aggregation
5657
from opentelemetry.sdk.metrics.export import ( # noqa: F401
@@ -118,6 +119,7 @@ def __init__(
118119
):
119120
credentials = _get_credentials(
120121
credentials,
122+
OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER,
121123
OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
122124
OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY,
123125
OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE,

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/trace_exporter/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
OTEL_EXPORTER_OTLP_TRACES_HEADERS,
5555
OTEL_EXPORTER_OTLP_TRACES_INSECURE,
5656
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
57+
OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER,
5758
)
5859
from opentelemetry.sdk.trace import ReadableSpan
5960
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
@@ -106,6 +107,7 @@ def __init__(
106107
):
107108
credentials = _get_credentials(
108109
credentials,
110+
OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER,
109111
OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE,
110112
OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY,
111113
OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE,

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from google.rpc.error_details_pb2 import ( # pylint: disable=no-name-in-module
2929
RetryInfo,
3030
)
31-
from grpc import Compression, StatusCode, server
31+
from grpc import ChannelCredentials, Compression, StatusCode, server
3232

3333
from opentelemetry.exporter.otlp.proto.common.trace_encoder import (
3434
encode_spans,
@@ -50,6 +50,7 @@
5050
)
5151
from opentelemetry.sdk.environment_variables import (
5252
OTEL_EXPORTER_OTLP_COMPRESSION,
53+
OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER,
5354
)
5455
from opentelemetry.sdk.trace import ReadableSpan, _Span
5556
from opentelemetry.sdk.trace.export import (
@@ -60,6 +61,15 @@
6061
logger = getLogger(__name__)
6162

6263

64+
class IterEntryPoint:
65+
def __init__(self, name, class_type):
66+
self.name = name
67+
self.class_type = class_type
68+
69+
def load(self):
70+
return self.class_type
71+
72+
6373
# The below tests use this test SpanExporter and Spans, but are testing the
6474
# underlying behavior in the mixin. A MetricExporter or LogExporter could
6575
# just as easily be used.
@@ -276,6 +286,23 @@ def test_otlp_exporter_otlp_compression_unspecified(
276286
),
277287
)
278288

289+
@patch.dict(
290+
"os.environ",
291+
{OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER: "credential_provider"},
292+
)
293+
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.entry_points")
294+
def test_that_credential_gets_passed_to_exporter(self, mock_entry_points):
295+
credential = ChannelCredentials(None)
296+
297+
def f(_):
298+
return credential
299+
300+
mock_entry_points.configure_mock(
301+
return_value=[IterEntryPoint("custom_credential", f)]
302+
)
303+
exporter = OTLPSpanExporterForTesting(insecure=False)
304+
assert exporter._credentials is credential
305+
279306
# pylint: disable=no-self-use, disable=unused-argument
280307
@patch(
281308
"opentelemetry.exporter.otlp.proto.grpc.exporter.ssl_channel_credentials"

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_common/__init__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,56 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from os import environ
16+
from typing import Literal, Optional
17+
1518
import requests
1619

20+
from opentelemetry.sdk.environment_variables import (
21+
OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER,
22+
OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER,
23+
OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER,
24+
OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER,
25+
)
26+
from opentelemetry.util._importlib_metadata import entry_points
27+
1728

1829
def _is_retryable(resp: requests.Response) -> bool:
1930
if resp.status_code == 408:
2031
return True
2132
if resp.status_code >= 500 and resp.status_code <= 599:
2233
return True
2334
return False
35+
36+
37+
def _load_session_from_envvar(
38+
exporter_type: Literal["logs", "traces", "metrics"],
39+
) -> Optional[requests.Session]:
40+
if exporter_type == "logs":
41+
env_var = OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER
42+
elif exporter_type == "traces":
43+
env_var = OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER
44+
else:
45+
env_var = OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER
46+
credential_env = environ.get(
47+
OTEL_PYTHON_EXPORTER_OTLP_CREDENTIAL_PROVIDER
48+
) or environ.get(env_var)
49+
if credential_env:
50+
try:
51+
maybe_session = next(
52+
iter(
53+
entry_points(
54+
group="opentelemetry_otlp_credential_provider",
55+
name=credential_env,
56+
)
57+
)
58+
).load()("HTTP")
59+
except StopIteration:
60+
raise RuntimeError(
61+
f"Requested component '{credential_env}' not found in "
62+
f"entry point 'opentelemetry_otlp_credential_provider'"
63+
)
64+
if isinstance(maybe_session, requests.Session):
65+
print("returning session !!")
66+
return maybe_session
67+
return None

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
from opentelemetry.exporter.otlp.proto.http._common import (
3434
_is_retryable,
35+
_load_session_from_envvar,
3536
)
3637
from opentelemetry.sdk._logs import LogData
3738
from opentelemetry.sdk._logs.export import (
@@ -117,7 +118,9 @@ def __init__(
117118
)
118119
)
119120
self._compression = compression or _compression_from_env()
120-
self._session = session or requests.Session()
121+
self._session = (
122+
session or _load_session_from_envvar("logs") or requests.Session()
123+
)
121124
self._session.headers.update(self._headers)
122125
self._session.headers.update(_OTLP_HTTP_HEADERS)
123126
if self._compression is not Compression.NoCompression:

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
)
5050
from opentelemetry.exporter.otlp.proto.http._common import (
5151
_is_retryable,
52+
_load_session_from_envvar,
5253
)
5354
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import ( # noqa: F401
5455
ExportMetricsServiceRequest,
@@ -159,7 +160,11 @@ def __init__(
159160
)
160161
)
161162
self._compression = compression or _compression_from_env()
162-
self._session = session or requests.Session()
163+
self._session = (
164+
session
165+
or _load_session_from_envvar("metrics")
166+
or requests.Session()
167+
)
163168
self._session.headers.update(self._headers)
164169
self._session.headers.update(_OTLP_HTTP_HEADERS)
165170
if self._compression is not Compression.NoCompression:

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
)
3535
from opentelemetry.exporter.otlp.proto.http._common import (
3636
_is_retryable,
37+
_load_session_from_envvar,
3738
)
3839
from opentelemetry.sdk.environment_variables import (
3940
OTEL_EXPORTER_OTLP_CERTIFICATE,
@@ -115,7 +116,11 @@ def __init__(
115116
)
116117
)
117118
self._compression = compression or _compression_from_env()
118-
self._session = session or requests.Session()
119+
self._session = (
120+
session
121+
or _load_session_from_envvar("traces")
122+
or requests.Session()
123+
)
119124
self._session.headers.update(self._headers)
120125
self._session.headers.update(_OTLP_HTTP_HEADERS)
121126
if self._compression is not Compression.NoCompression:

0 commit comments

Comments
 (0)