Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a7e3fc

Browse files
authored
Support insecure configuration (open-telemetry#2350)
1 parent 13fa220 commit 2a7e3fc

File tree

9 files changed

+165
-12
lines changed

9 files changed

+165
-12
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# Otherwise, set variable to the commit of your branch on
1111
# opentelemetry-python-contrib which is compatible with these Core repo
1212
# changes.
13-
CONTRIB_REPO_SHA: 23394ccd80878a91534f8421b82a7410eb775e65
13+
CONTRIB_REPO_SHA: 741321434f0803dd3b304f3a93022d8d451a4c90
1414
# This is needed because we do not clone the core repo in contrib builds anymore.
1515
# When running contrib builds as part of core builds, we use actions/checkout@v2 which
1616
# does not set an environment variable (simply just runs tox), which is different when

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
([#2334](https://github.com/open-telemetry/opentelemetry-python/pull/2334))
2828
- Add otlp entrypoint for log exporter
2929
([#2322](https://github.com/open-telemetry/opentelemetry-python/pull/2322))
30+
- Support insecure configuration for OTLP gRPC exporter
31+
([#2350](https://github.com/open-telemetry/opentelemetry-python/pull/2350))
3032

3133
## [1.7.1-0.26b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.7.0-0.26b0) - 2021-11-11
3234

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14+
from os import environ
1415
from typing import Optional, Sequence
1516
from grpc import ChannelCredentials, Compression
1617
from opentelemetry.exporter.otlp.proto.grpc.exporter import (
@@ -29,6 +30,9 @@
2930
ResourceMetrics,
3031
)
3132
from opentelemetry.proto.metrics.v1.metrics_pb2 import Metric as PB2Metric
33+
from opentelemetry.sdk.environment_variables import (
34+
OTEL_EXPORTER_OTLP_METRICS_INSECURE,
35+
)
3236
from opentelemetry.sdk._metrics.data import (
3337
MetricData,
3438
)
@@ -57,6 +61,12 @@ def __init__(
5761
timeout: Optional[int] = None,
5862
compression: Optional[Compression] = None,
5963
):
64+
65+
if insecure is None:
66+
insecure = environ.get(OTEL_EXPORTER_OTLP_METRICS_INSECURE)
67+
if insecure is not None:
68+
insecure = insecure.lower() == "true"
69+
6070
super().__init__(
6171
**{
6272
"endpoint": endpoint,

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
OTEL_EXPORTER_OTLP_COMPRESSION,
4848
OTEL_EXPORTER_OTLP_ENDPOINT,
4949
OTEL_EXPORTER_OTLP_HEADERS,
50+
OTEL_EXPORTER_OTLP_INSECURE,
5051
OTEL_EXPORTER_OTLP_TIMEOUT,
5152
)
5253
from opentelemetry.sdk.resources import Resource as SDKResource
@@ -218,11 +219,17 @@ def __init__(
218219

219220
parsed_url = urlparse(endpoint)
220221

222+
if parsed_url.scheme == "https":
223+
insecure = False
221224
if insecure is None:
222-
if parsed_url.scheme == "https":
223-
insecure = False
225+
insecure = environ.get(OTEL_EXPORTER_OTLP_INSECURE)
226+
if insecure is not None:
227+
insecure = insecure.lower() == "true"
224228
else:
225-
insecure = True
229+
if parsed_url.scheme == "http":
230+
insecure = True
231+
else:
232+
insecure = False
226233

227234
if parsed_url.netloc:
228235
endpoint = parsed_url.netloc

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
4545
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
4646
OTEL_EXPORTER_OTLP_TRACES_HEADERS,
47+
OTEL_EXPORTER_OTLP_TRACES_INSECURE,
4748
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
4849
)
4950
from opentelemetry.sdk.trace import ReadableSpan
@@ -84,6 +85,12 @@ def __init__(
8485
timeout: Optional[int] = None,
8586
compression: Optional[Compression] = None,
8687
):
88+
89+
if insecure is None:
90+
insecure = environ.get(OTEL_EXPORTER_OTLP_TRACES_INSECURE)
91+
if insecure is not None:
92+
insecure = insecure.lower() == "true"
93+
8794
if (
8895
not insecure
8996
and environ.get(OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE) is not None

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/logs/test_otlp_logs_exporter.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,33 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
193193
(
194194
"localhost:4317",
195195
None,
196+
mock_secure,
197+
),
198+
(
199+
"http://localhost:4317",
200+
True,
196201
mock_insecure,
197202
),
203+
(
204+
"localhost:4317",
205+
True,
206+
mock_insecure,
207+
),
208+
(
209+
"http://localhost:4317",
210+
False,
211+
mock_secure,
212+
),
198213
(
199214
"localhost:4317",
200215
False,
201216
mock_secure,
202217
),
218+
(
219+
"https://localhost:4317",
220+
False,
221+
mock_secure,
222+
),
203223
(
204224
"https://localhost:4317",
205225
None,
@@ -208,9 +228,10 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
208228
(
209229
"https://localhost:4317",
210230
True,
211-
mock_insecure,
231+
mock_secure,
212232
),
213233
]
234+
214235
# pylint: disable=C0209
215236
for endpoint, insecure, mock_method in endpoints:
216237
OTLPLogExporter(endpoint=endpoint, insecure=insecure)

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
)
3333
from opentelemetry.sdk._metrics.data import Metric, MetricData
3434
from opentelemetry.sdk._metrics.export import MetricExportResult
35+
from opentelemetry.sdk.environment_variables import (
36+
OTEL_EXPORTER_OTLP_METRICS_INSECURE,
37+
)
3538
from opentelemetry.sdk.resources import Resource as SDKResource
3639
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
3740

@@ -119,6 +122,22 @@ def test_no_credentials_error(
119122
OTLPMetricExporter(insecure=False)
120123
self.assertTrue(mock_ssl_channel.called)
121124

125+
@patch.dict(
126+
"os.environ",
127+
{OTEL_EXPORTER_OTLP_METRICS_INSECURE: "True"},
128+
)
129+
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
130+
# pylint: disable=unused-argument
131+
def test_otlp_insecure_from_env(self, mock_insecure):
132+
OTLPMetricExporter()
133+
# pylint: disable=protected-access
134+
self.assertTrue(mock_insecure.called)
135+
self.assertEqual(
136+
1,
137+
mock_insecure.call_count,
138+
f"expected {mock_insecure} to be called",
139+
)
140+
122141
# pylint: disable=no-self-use
123142
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
124143
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.secure_channel")
@@ -133,13 +152,33 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
133152
(
134153
"localhost:4317",
135154
None,
155+
mock_secure,
156+
),
157+
(
158+
"http://localhost:4317",
159+
True,
136160
mock_insecure,
137161
),
162+
(
163+
"localhost:4317",
164+
True,
165+
mock_insecure,
166+
),
167+
(
168+
"http://localhost:4317",
169+
False,
170+
mock_secure,
171+
),
138172
(
139173
"localhost:4317",
140174
False,
141175
mock_secure,
142176
),
177+
(
178+
"https://localhost:4317",
179+
False,
180+
mock_secure,
181+
),
143182
(
144183
"https://localhost:4317",
145184
None,
@@ -148,7 +187,7 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
148187
(
149188
"https://localhost:4317",
150189
True,
151-
mock_insecure,
190+
mock_secure,
152191
),
153192
]
154193
# pylint: disable=C0209

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
5959
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
6060
OTEL_EXPORTER_OTLP_TRACES_HEADERS,
61+
OTEL_EXPORTER_OTLP_TRACES_INSECURE,
6162
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
6263
)
6364
from opentelemetry.sdk.resources import Resource as SDKResource
@@ -123,6 +124,8 @@ def Export(self, request, context):
123124

124125

125126
class TestOTLPSpanExporter(TestCase):
127+
# pylint: disable=too-many-public-methods
128+
126129
def setUp(self):
127130
tracer_provider = TracerProvider()
128131
self.exporter = OTLPSpanExporter(insecure=True)
@@ -289,6 +292,22 @@ def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure):
289292
exporter._headers, (("key5", "value5"), ("key6", "value6"))
290293
)
291294

295+
@patch.dict(
296+
"os.environ",
297+
{OTEL_EXPORTER_OTLP_TRACES_INSECURE: "True"},
298+
)
299+
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
300+
# pylint: disable=unused-argument
301+
def test_otlp_insecure_from_env(self, mock_insecure):
302+
OTLPSpanExporter()
303+
# pylint: disable=protected-access
304+
self.assertTrue(mock_insecure.called)
305+
self.assertEqual(
306+
1,
307+
mock_insecure.call_count,
308+
f"expected {mock_insecure} to be called",
309+
)
310+
292311
# pylint: disable=no-self-use
293312
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
294313
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.secure_channel")
@@ -304,10 +323,30 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
304323
(
305324
"localhost:4317",
306325
None,
326+
mock_secure,
327+
),
328+
(
329+
"http://localhost:4317",
330+
True,
307331
mock_insecure,
308332
),
309333
(
310334
"localhost:4317",
335+
True,
336+
mock_insecure,
337+
),
338+
(
339+
"http://localhost:4317",
340+
False,
341+
mock_secure,
342+
),
343+
(
344+
"localhost:4317",
345+
False,
346+
mock_secure,
347+
),
348+
(
349+
"https://localhost:4317",
311350
False,
312351
mock_secure,
313352
),
@@ -319,7 +358,7 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
319358
(
320359
"https://localhost:4317",
321360
True,
322-
mock_insecure,
361+
mock_secure,
323362
),
324363
]
325364
for endpoint, insecure, mock_method in endpoints:

opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,37 @@
281281
.. envvar:: OTEL_EXPORTER_OTLP_ENDPOINT
282282
283283
The :envvar:`OTEL_EXPORTER_OTLP_ENDPOINT` target to which the exporter is going to send spans or metrics.
284-
The endpoint MUST be a valid URL with scheme (http or https) and host, and MAY contain a port and path.
285-
A scheme of https indicates a secure connection.
286-
Default: "https://localhost:4317"
284+
The endpoint MUST be a valid URL host, and MAY contain a scheme (http or https), port and path.
285+
A scheme of https indicates a secure connection and takes precedence over the insecure configuration setting.
286+
Default: "http://localhost:4317"
287287
"""
288288

289+
OTEL_EXPORTER_OTLP_INSECURE = "OTEL_EXPORTER_OTLP_INSECURE"
290+
"""
291+
.. envvar:: OTEL_EXPORTER_OTLP_INSECURE
292+
293+
The :envvar:`OTEL_EXPORTER_OTLP_INSECURE` represents whether to enable client transport security for gRPC requests.
294+
A scheme of https takes precedence over this configuration setting.
295+
Default: False
296+
"""
297+
298+
OTEL_EXPORTER_OTLP_TRACES_INSECURE = "OTEL_EXPORTER_OTLP_TRACES_INSECURE"
299+
"""
300+
.. envvar:: OTEL_EXPORTER_OTLP_TRACES_INSECURE
301+
302+
The :envvar:`OTEL_EXPORTER_OTLP_TRACES_INSECURE` represents whether to enable client transport security
303+
for gRPC requests for spans. A scheme of https takes precedence over the this configuration setting.
304+
Default: False
305+
"""
306+
307+
289308
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
290309
"""
291310
.. envvar:: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
292311
293312
The :envvar:`OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` target to which the span exporter is going to send spans.
294-
The endpoint MUST be a valid URL with scheme (http or https) and host, and MAY contain a port and path.
295-
A scheme of https indicates a secure connection.
313+
The endpoint MUST be a valid URL host, and MAY contain a scheme (http or https), port and path.
314+
A scheme of https indicates a secure connection and takes precedence over this configuration setting.
296315
"""
297316

298317
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
@@ -335,6 +354,15 @@
335354
wait for each batch export for spans.
336355
"""
337356

357+
OTEL_EXPORTER_OTLP_METRICS_INSECURE = "OTEL_EXPORTER_OTLP_METRICS_INSECURE"
358+
"""
359+
.. envvar:: OTEL_EXPORTER_OTLP_METRICS_INSECURE
360+
361+
The :envvar:`OTEL_EXPORTER_OTLP_METRICS_INSECURE` represents whether to enable client transport security
362+
for gRPC requests for metrics. A scheme of https takes precedence over the this configuration setting.
363+
Default: False
364+
"""
365+
338366
OTEL_EXPORTER_JAEGER_CERTIFICATE = "OTEL_EXPORTER_JAEGER_CERTIFICATE"
339367
"""
340368
.. envvar:: OTEL_EXPORTER_JAEGER_CERTIFICATE

0 commit comments

Comments
 (0)