20
20
21
21
from google .protobuf .duration_pb2 import Duration
22
22
from google .rpc .error_details_pb2 import RetryInfo
23
- from grpc import ChannelCredentials , StatusCode , server
23
+ from grpc import ChannelCredentials , Compression , StatusCode , server
24
24
25
25
from opentelemetry .exporter .otlp .trace_exporter import OTLPSpanExporter
26
26
from opentelemetry .proto .collector .trace .v1 .trace_service_pb2 import (
46
46
from opentelemetry .proto .trace .v1 .trace_pb2 import Span as OTLPSpan
47
47
from opentelemetry .proto .trace .v1 .trace_pb2 import Status
48
48
from opentelemetry .sdk .environment_variables import (
49
+ OTEL_EXPORTER_OTLP_COMPRESSION ,
49
50
OTEL_EXPORTER_OTLP_SPAN_CERTIFICATE ,
51
+ OTEL_EXPORTER_OTLP_SPAN_COMPRESSION ,
50
52
OTEL_EXPORTER_OTLP_SPAN_ENDPOINT ,
51
53
OTEL_EXPORTER_OTLP_SPAN_HEADERS ,
52
54
OTEL_EXPORTER_OTLP_SPAN_TIMEOUT ,
@@ -174,6 +176,7 @@ def tearDown(self):
174
176
+ "/fixtures/test.cert" ,
175
177
OTEL_EXPORTER_OTLP_SPAN_HEADERS : "key1=value1,key2=value2" ,
176
178
OTEL_EXPORTER_OTLP_SPAN_TIMEOUT : "10" ,
179
+ OTEL_EXPORTER_OTLP_SPAN_COMPRESSION : "gzip" ,
177
180
},
178
181
)
179
182
@patch ("opentelemetry.exporter.otlp.exporter.OTLPExporterMixin.__init__" )
@@ -186,6 +189,7 @@ def test_env_variables(self, mock_exporter_mixin):
186
189
self .assertEqual (kwargs ["endpoint" ], "collector:4317" )
187
190
self .assertEqual (kwargs ["headers" ], "key1=value1,key2=value2" )
188
191
self .assertEqual (kwargs ["timeout" ], 10 )
192
+ self .assertEqual (kwargs ["compression" ], Compression .Gzip )
189
193
self .assertIsNotNone (kwargs ["credentials" ])
190
194
self .assertIsInstance (kwargs ["credentials" ], ChannelCredentials )
191
195
@@ -220,6 +224,57 @@ def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure):
220
224
exporter ._headers , (("key3" , "value3" ), ("key4" , "value4" ))
221
225
)
222
226
227
+ # pylint: disable=no-self-use
228
+ def test_otlp_compression_from_env (self ):
229
+ # Specifying kwarg should take precedence over env
230
+ with patch (
231
+ "opentelemetry.exporter.otlp.exporter.insecure_channel"
232
+ ) as mock_insecure_channel , patch .dict (
233
+ "os.environ" , {OTEL_EXPORTER_OTLP_COMPRESSION : "gzip" }
234
+ ):
235
+ OTLPSpanExporter (
236
+ insecure = True , compression = Compression .NoCompression
237
+ )
238
+ mock_insecure_channel .assert_called_once_with (
239
+ "localhost:4317" , compression = Compression .NoCompression
240
+ )
241
+
242
+ # No env or kwarg should be NoCompression
243
+ with patch (
244
+ "opentelemetry.exporter.otlp.exporter.insecure_channel"
245
+ ) as mock_insecure_channel , patch .dict ("os.environ" , {}):
246
+ OTLPSpanExporter (insecure = True )
247
+ mock_insecure_channel .assert_called_once_with (
248
+ "localhost:4317" , compression = Compression .NoCompression
249
+ )
250
+
251
+ # Just OTEL_EXPORTER_OTLP_COMPRESSION should work
252
+ with patch (
253
+ "opentelemetry.exporter.otlp.exporter.insecure_channel"
254
+ ) as mock_insecure_channel , patch .dict (
255
+ "os.environ" , {OTEL_EXPORTER_OTLP_COMPRESSION : "deflate" }
256
+ ):
257
+ OTLPSpanExporter (insecure = True )
258
+ mock_insecure_channel .assert_called_once_with (
259
+ "localhost:4317" , compression = Compression .Deflate
260
+ )
261
+
262
+ # OTEL_EXPORTER_OTLP_SPAN_COMPRESSION as higher priority than
263
+ # OTEL_EXPORTER_OTLP_COMPRESSION
264
+ with patch (
265
+ "opentelemetry.exporter.otlp.exporter.insecure_channel"
266
+ ) as mock_insecure_channel , patch .dict (
267
+ "os.environ" ,
268
+ {
269
+ OTEL_EXPORTER_OTLP_COMPRESSION : "deflate" ,
270
+ OTEL_EXPORTER_OTLP_SPAN_COMPRESSION : "gzip" ,
271
+ },
272
+ ):
273
+ OTLPSpanExporter (insecure = True )
274
+ mock_insecure_channel .assert_called_once_with (
275
+ "localhost:4317" , compression = Compression .Gzip
276
+ )
277
+
223
278
@patch ("opentelemetry.exporter.otlp.exporter.ssl_channel_credentials" )
224
279
@patch ("opentelemetry.exporter.otlp.exporter.secure_channel" )
225
280
# pylint: disable=unused-argument
0 commit comments