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,29 @@ 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
+
365
426
@patch .dict (
366
427
"os.environ" ,
367
428
{OTEL_EXPORTER_OTLP_METRICS_INSECURE : "True" },
@@ -449,6 +510,43 @@ def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
449
510
)
450
511
mock_method .reset_mock ()
451
512
513
+ # pylint: disable=no-self-use
514
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter._expo" )
515
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel" )
516
+ @patch .dict ("os.environ" , {OTEL_EXPORTER_OTLP_COMPRESSION : "gzip" })
517
+ def test_otlp_exporter_otlp_compression_envvar (
518
+ self , mock_insecure_channel , mock_expo
519
+ ):
520
+ """Just OTEL_EXPORTER_OTLP_COMPRESSION should work"""
521
+ OTLPMetricExporter (insecure = True )
522
+ mock_insecure_channel .assert_called_once_with (
523
+ "localhost:4317" , compression = Compression .Gzip
524
+ )
525
+
526
+ # pylint: disable=no-self-use
527
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel" )
528
+ @patch .dict ("os.environ" , {OTEL_EXPORTER_OTLP_COMPRESSION : "gzip" })
529
+ def test_otlp_exporter_otlp_compression_kwarg (self , mock_insecure_channel ):
530
+ """Specifying kwarg should take precedence over env"""
531
+ OTLPMetricExporter (
532
+ insecure = True , compression = Compression .NoCompression
533
+ )
534
+ mock_insecure_channel .assert_called_once_with (
535
+ "localhost:4317" , compression = Compression .NoCompression
536
+ )
537
+
538
+ # pylint: disable=no-self-use
539
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel" )
540
+ @patch .dict ("os.environ" , {})
541
+ def test_otlp_exporter_otlp_compression_unspecified (
542
+ self , mock_insecure_channel
543
+ ):
544
+ """No env or kwarg should be NoCompression"""
545
+ OTLPMetricExporter (insecure = True )
546
+ mock_insecure_channel .assert_called_once_with (
547
+ "localhost:4317" , compression = Compression .NoCompression
548
+ )
549
+
452
550
@patch ("opentelemetry.exporter.otlp.proto.grpc.exporter._expo" )
453
551
@patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep" )
454
552
def test_unavailable (self , mock_sleep , mock_expo ):
@@ -1243,6 +1341,11 @@ def test_split_metrics_data_many_resources_scopes_metrics(self):
1243
1341
split_metrics_data ,
1244
1342
)
1245
1343
1344
+ @patch ("opentelemetry.exporter.otlp.proto.grpc.exporter.secure_channel" )
1345
+ def test_insecure_https_endpoint (self , mock_secure_channel ):
1346
+ OTLPMetricExporter (endpoint = "https://ab.c:123" , insecure = True )
1347
+ mock_secure_channel .assert_called ()
1348
+
1246
1349
1247
1350
def _resource_metrics (
1248
1351
index : int , scope_metrics : List [ScopeMetrics ]
0 commit comments