14
14
15
15
# pylint: disable=too-many-lines
16
16
from concurrent .futures import ThreadPoolExecutor
17
+ from os .path import dirname
17
18
from typing import List
18
19
from unittest import TestCase
19
20
from unittest .mock import patch
20
21
21
22
from google .protobuf .duration_pb2 import Duration
22
23
from google .rpc .error_details_pb2 import RetryInfo
23
- from grpc import StatusCode , server
24
+ from grpc import ChannelCredentials , Compression , StatusCode , server
24
25
25
26
from opentelemetry .exporter .otlp .proto .grpc .metric_exporter import (
26
27
OTLPMetricExporter ,
43
44
Resource as OTLPResource ,
44
45
)
45
46
from opentelemetry .sdk .environment_variables import (
47
+ OTEL_EXPORTER_OTLP_COMPRESSION ,
48
+ OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE ,
49
+ OTEL_EXPORTER_OTLP_METRICS_COMPRESSION ,
50
+ OTEL_EXPORTER_OTLP_METRICS_ENDPOINT ,
51
+ OTEL_EXPORTER_OTLP_METRICS_HEADERS ,
46
52
OTEL_EXPORTER_OTLP_METRICS_INSECURE ,
47
53
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE ,
54
+ OTEL_EXPORTER_OTLP_METRICS_TIMEOUT ,
48
55
)
49
56
from opentelemetry .sdk .metrics import (
50
57
Counter ,
71
78
)
72
79
from opentelemetry .test .metrictestutil import _generate_gauge , _generate_sum
73
80
81
+ THIS_DIR = dirname (__file__ )
82
+
74
83
75
84
class MetricsServiceServicerUNAVAILABLEDelay (MetricsServiceServicer ):
76
85
# pylint: disable=invalid-name,unused-argument,no-self-use
@@ -119,6 +128,8 @@ def Export(self, request, context):
119
128
120
129
121
130
class TestOTLPMetricExporter (TestCase ):
131
+ # pylint: disable=too-many-public-methods
132
+
122
133
def setUp (self ):
123
134
124
135
self .exporter = OTLPMetricExporter ()
@@ -348,6 +359,33 @@ def test_preferred_temporality(self):
348
359
AggregationTemporality .CUMULATIVE ,
349
360
)
350
361
362
+ @patch .dict (
363
+ "os.environ" ,
364
+ {
365
+ OTEL_EXPORTER_OTLP_METRICS_ENDPOINT : "collector:4317" ,
366
+ OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE : THIS_DIR
367
+ + "/fixtures/test.cert" ,
368
+ OTEL_EXPORTER_OTLP_METRICS_HEADERS : " key1=value1,KEY2 = value=2" ,
369
+ OTEL_EXPORTER_OTLP_METRICS_TIMEOUT : "10" ,
370
+ OTEL_EXPORTER_OTLP_METRICS_COMPRESSION : "gzip" ,
371
+ },
372
+ )
373
+ @patch (
374
+ "opentelemetry.exporter.otlp.proto.grpc.exporter.OTLPExporterMixin.__init__"
375
+ )
376
+ def test_env_variables (self , mock_exporter_mixin ):
377
+ OTLPMetricExporter ()
378
+
379
+ self .assertTrue (len (mock_exporter_mixin .call_args_list ) == 1 )
380
+ _ , kwargs = mock_exporter_mixin .call_args_list [0 ]
381
+
382
+ self .assertEqual (kwargs ["endpoint" ], "collector:4317" )
383
+ self .assertEqual (kwargs ["headers" ], " key1=value1,KEY2 = value=2" )
384
+ self .assertEqual (kwargs ["timeout" ], 10 )
385
+ self .assertEqual (kwargs ["compression" ], Compression .Gzip )
386
+ self .assertIsNotNone (kwargs ["credentials" ])
387
+ self .assertIsInstance (kwargs ["credentials" ], ChannelCredentials )
388
+
351
389
@patch (
352
390
"opentelemetry.exporter.otlp.proto.grpc.exporter.ssl_channel_credentials"
353
391
)
@@ -362,6 +400,36 @@ def test_no_credentials_error(
362
400
OTLPMetricExporter (insecure = False )
363
401
self .assertTrue (mock_ssl_channel .called )
364
402
403
+ @patch .dict (
404
+ "os.environ" ,
405
+ {OTEL_EXPORTER_OTLP_METRICS_HEADERS : " key1=value1,KEY2 = VALUE=2 " },
406
+ )
407
+ @patch (
408
+ "opentelemetry.exporter.otlp.proto.grpc.exporter.ssl_channel_credentials"
409
+ )
410
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.secure_channel" )
411
+ # pylint: disable=unused-argument
412
+ def test_otlp_headers_from_env (self , mock_ssl_channel , mock_secure ):
413
+ exporter = OTLPMetricExporter ()
414
+ # pylint: disable=protected-access
415
+ self .assertEqual (
416
+ exporter ._headers , (("key1" , "value1" ), ("key2" , "VALUE=2" ))
417
+ )
418
+ exporter = OTLPMetricExporter (
419
+ headers = (("key3" , "value3" ), ("key4" , "value4" ))
420
+ )
421
+ # pylint: disable=protected-access
422
+ self .assertEqual (
423
+ exporter ._headers , (("key3" , "value3" ), ("key4" , "value4" ))
424
+ )
425
+ exporter = OTLPMetricExporter (
426
+ headers = {"key5" : "value5" , "key6" : "value6" }
427
+ )
428
+ # pylint: disable=protected-access
429
+ self .assertEqual (
430
+ exporter ._headers , (("key5" , "value5" ), ("key6" , "value6" ))
431
+ )
432
+
365
433
@patch .dict (
366
434
"os.environ" ,
367
435
{OTEL_EXPORTER_OTLP_METRICS_INSECURE : "True" },
@@ -449,6 +517,42 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
449
517
)
450
518
mock_method .reset_mock ()
451
519
520
+ # pylint: disable=no-self-use
521
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel" )
522
+ @patch .dict ("os.environ" , {OTEL_EXPORTER_OTLP_COMPRESSION : "gzip" })
523
+ def test_otlp_exporter_otlp_compression_envvar (
524
+ self , mock_insecure_channel
525
+ ):
526
+ """Just OTEL_EXPORTER_OTLP_COMPRESSION should work"""
527
+ OTLPMetricExporter (insecure = True )
528
+ mock_insecure_channel .assert_called_once_with (
529
+ "localhost:4317" , compression = Compression .Gzip
530
+ )
531
+
532
+ # pylint: disable=no-self-use
533
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel" )
534
+ @patch .dict ("os.environ" , {OTEL_EXPORTER_OTLP_COMPRESSION : "gzip" })
535
+ def test_otlp_exporter_otlp_compression_kwarg (self , mock_insecure_channel ):
536
+ """Specifying kwarg should take precedence over env"""
537
+ OTLPMetricExporter (
538
+ insecure = True , compression = Compression .NoCompression
539
+ )
540
+ mock_insecure_channel .assert_called_once_with (
541
+ "localhost:4317" , compression = Compression .NoCompression
542
+ )
543
+
544
+ # pylint: disable=no-self-use
545
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel" )
546
+ @patch .dict ("os.environ" , {})
547
+ def test_otlp_exporter_otlp_compression_unspecified (
548
+ self , mock_insecure_channel
549
+ ):
550
+ """No env or kwarg should be NoCompression"""
551
+ OTLPMetricExporter (insecure = True )
552
+ mock_insecure_channel .assert_called_once_with (
553
+ "localhost:4317" , compression = Compression .NoCompression
554
+ )
555
+
452
556
@patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.expo" )
453
557
@patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep" )
454
558
def test_unavailable (self , mock_sleep , mock_expo ):
0 commit comments