Skip to content

Commit 1d78d88

Browse files
committed
Add tests for env variables introduced in logger.
1 parent a81fdf5 commit 1d78d88

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

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

+40-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
import time
1616
from concurrent.futures import ThreadPoolExecutor
17+
from os.path import dirname
1718
from unittest import TestCase
1819
from unittest.mock import patch
1920

2021
from google.protobuf.duration_pb2 import Duration
2122
from google.rpc.error_details_pb2 import RetryInfo
22-
from grpc import StatusCode, server
23+
from grpc import ChannelCredentials, Compression, StatusCode, server
2324

2425
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
2526
OTLPLogExporter,
@@ -43,6 +44,15 @@
4344
from opentelemetry.proto.resource.v1.resource_pb2 import (
4445
Resource as OTLPResource,
4546
)
47+
from opentelemetry.sdk.environment_variables import (
48+
OTEL_EXPORTER_OTLP_COMPRESSION,
49+
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE,
50+
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
51+
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
52+
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
53+
OTEL_EXPORTER_OTLP_LOGS_INSECURE,
54+
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
55+
)
4656
from opentelemetry.sdk._logs import LogData, LogRecord
4757
from opentelemetry.sdk._logs.export import LogExportResult
4858
from opentelemetry.sdk._logs.severity import (
@@ -52,6 +62,8 @@
5262
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
5363
from opentelemetry.trace import TraceFlags
5464

65+
THIS_DIR = dirname(__file__)
66+
5567

5668
class LogsServiceServicerUNAVAILABLEDelay(LogsServiceServicer):
5769
# pylint: disable=invalid-name,unused-argument,no-self-use
@@ -165,6 +177,33 @@ def test_exporting(self):
165177
# pylint: disable=protected-access
166178
self.assertEqual(self.exporter._exporting, "logs")
167179

180+
@patch.dict(
181+
"os.environ",
182+
{
183+
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "logs:4317",
184+
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE: THIS_DIR
185+
+ "/fixtures/test.cert",
186+
OTEL_EXPORTER_OTLP_LOGS_HEADERS: " key1=value1,KEY2 = value=2",
187+
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: "10",
188+
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: "gzip",
189+
},
190+
)
191+
@patch(
192+
"opentelemetry.exporter.otlp.proto.grpc.exporter.OTLPExporterMixin.__init__"
193+
)
194+
def test_env_variables(self, mock_exporter_mixin):
195+
OTLPLogExporter()
196+
197+
self.assertTrue(len(mock_exporter_mixin.call_args_list) == 1)
198+
_, kwargs = mock_exporter_mixin.call_args_list[0]
199+
200+
self.assertEqual(kwargs["endpoint"], "log:4317")
201+
self.assertEqual(kwargs["headers"], " key1=value1,KEY2 = value=2")
202+
self.assertEqual(kwargs["timeout"], 10)
203+
self.assertEqual(kwargs["compression"], Compression.Gzip)
204+
self.assertIsNotNone(kwargs["credentials"])
205+
self.assertIsInstance(kwargs["credentials"], ChannelCredentials)
206+
168207
@patch(
169208
"opentelemetry.exporter.otlp.proto.grpc.exporter.ssl_channel_credentials"
170209
)

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py

+37
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
OTEL_EXPORTER_OTLP_ENDPOINT,
6262
OTEL_EXPORTER_OTLP_HEADERS,
6363
OTEL_EXPORTER_OTLP_TIMEOUT,
64+
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE,
65+
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
66+
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
67+
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
68+
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
6469
)
6570
from opentelemetry.sdk.resources import Resource as SDKResource
6671
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
@@ -91,6 +96,38 @@ def test_constructor_default(self):
9196
"application/x-protobuf",
9297
)
9398

99+
@patch.dict(
100+
"os.environ",
101+
{
102+
OTEL_EXPORTER_OTLP_CERTIFICATE: ENV_CERTIFICATE,
103+
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
104+
OTEL_EXPORTER_OTLP_ENDPOINT: ENV_ENDPOINT,
105+
OTEL_EXPORTER_OTLP_HEADERS: ENV_HEADERS,
106+
OTEL_EXPORTER_OTLP_TIMEOUT: ENV_TIMEOUT,
107+
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE: "logs/certificate.env",
108+
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: Compression.Deflate.value,
109+
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "https://logs.endpoint.env",
110+
OTEL_EXPORTER_OTLP_LOGS_HEADERS: "metricsEnv1=val1,metricsEnv2=val2,metricEnv3===val3==",
111+
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: "40",
112+
},
113+
)
114+
def test_exporter_metrics_env_take_priority(self):
115+
exporter = OTLPLogExporter()
116+
117+
self.assertEqual(exporter._endpoint, "https://metrics.endpoint.env")
118+
self.assertEqual(exporter._certificate_file, "metrics/certificate.env")
119+
self.assertEqual(exporter._timeout, 40)
120+
self.assertIs(exporter._compression, Compression.Deflate)
121+
self.assertEqual(
122+
exporter._headers,
123+
{
124+
"metricsenv1": "val1",
125+
"metricsenv2": "val2",
126+
"metricenv3": "==val3==",
127+
},
128+
)
129+
self.assertIsInstance(exporter._session, requests.Session)
130+
94131
@patch.dict(
95132
"os.environ",
96133
{

0 commit comments

Comments
 (0)