Skip to content

Commit

Permalink
Add tests for env variables introduced in logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
pridhi-arora committed Dec 6, 2022
1 parent 48c9fa9 commit fc6b263
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

import time
from concurrent.futures import ThreadPoolExecutor
from os.path import dirname
from unittest import TestCase
from unittest.mock import patch

from google.protobuf.duration_pb2 import Duration
from google.rpc.error_details_pb2 import RetryInfo
from grpc import StatusCode, server
from grpc import ChannelCredentials, Compression, StatusCode, server

from opentelemetry._logs import SeverityNumber
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
Expand All @@ -44,12 +45,23 @@
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as OTLPResource,
)
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_COMPRESSION,
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE,
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
OTEL_EXPORTER_OTLP_LOGS_INSECURE,
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
)
from opentelemetry.sdk._logs import LogData, LogRecord
from opentelemetry.sdk._logs.export import LogExportResult
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.trace import TraceFlags

THIS_DIR = dirname(__file__)


class LogsServiceServicerUNAVAILABLEDelay(LogsServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
Expand Down Expand Up @@ -163,6 +175,33 @@ def test_exporting(self):
# pylint: disable=protected-access
self.assertEqual(self.exporter._exporting, "logs")

@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "logs:4317",
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE: THIS_DIR
+ "/fixtures/test.cert",
OTEL_EXPORTER_OTLP_LOGS_HEADERS: " key1=value1,KEY2 = value=2",
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: "10",
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: "gzip",
},
)
@patch(
"opentelemetry.exporter.otlp.proto.grpc.exporter.OTLPExporterMixin.__init__"
)
def test_env_variables(self, mock_exporter_mixin):
OTLPLogExporter()

self.assertTrue(len(mock_exporter_mixin.call_args_list) == 1)
_, kwargs = mock_exporter_mixin.call_args_list[0]

self.assertEqual(kwargs["endpoint"], "log:4317")
self.assertEqual(kwargs["headers"], " key1=value1,KEY2 = value=2")
self.assertEqual(kwargs["timeout"], 10)
self.assertEqual(kwargs["compression"], Compression.Gzip)
self.assertIsNotNone(kwargs["credentials"])
self.assertIsInstance(kwargs["credentials"], ChannelCredentials)

@patch(
"opentelemetry.exporter.otlp.proto.grpc.exporter.ssl_channel_credentials"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT,
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE,
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
)
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
Expand Down Expand Up @@ -91,6 +96,38 @@ def test_constructor_default(self):
"application/x-protobuf",
)

@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_CERTIFICATE: ENV_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
OTEL_EXPORTER_OTLP_ENDPOINT: ENV_ENDPOINT,
OTEL_EXPORTER_OTLP_HEADERS: ENV_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT: ENV_TIMEOUT,
OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE: "logs/certificate.env",
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: Compression.Deflate.value,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "https://logs.endpoint.env",
OTEL_EXPORTER_OTLP_LOGS_HEADERS: "metricsEnv1=val1,metricsEnv2=val2,metricEnv3===val3==",
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: "40",
},
)
def test_exporter_metrics_env_take_priority(self):
exporter = OTLPLogExporter()

self.assertEqual(exporter._endpoint, "https://metrics.endpoint.env")
self.assertEqual(exporter._certificate_file, "metrics/certificate.env")
self.assertEqual(exporter._timeout, 40)
self.assertIs(exporter._compression, Compression.Deflate)
self.assertEqual(
exporter._headers,
{
"metricsenv1": "val1",
"metricsenv2": "val2",
"metricenv3": "==val3==",
},
)
self.assertIsInstance(exporter._session, requests.Session)

@patch.dict(
"os.environ",
{
Expand Down

0 comments on commit fc6b263

Please sign in to comment.