14
14
15
15
"""OTLP Exporter"""
16
16
17
+ import enum
17
18
import logging
18
- import os
19
19
from abc import ABC , abstractmethod
20
20
from collections .abc import Mapping , Sequence
21
21
from time import sleep
22
22
from typing import Any , Callable , Dict , Generic , List , Optional
23
23
from typing import Sequence as TypingSequence
24
- from typing import Text , Tuple , TypeVar
24
+ from typing import Text , TypeVar
25
25
26
26
from backoff import expo
27
27
from google .rpc .error_details_pb2 import RetryInfo
28
28
from grpc import (
29
29
ChannelCredentials ,
30
+ Compression ,
30
31
RpcError ,
31
32
StatusCode ,
32
33
insecure_channel ,
47
48
ExportResultT = TypeVar ("ExportResultT" )
48
49
49
50
51
+ class OTLPCompression (enum .Enum ):
52
+ gzip = "gzip"
53
+
54
+
50
55
def _translate_key_values (key : Text , value : Any ) -> KeyValue :
51
56
52
57
if isinstance (value , bool ):
@@ -137,6 +142,7 @@ class OTLPExporterMixin(
137
142
insecure: Connection type
138
143
credentials: ChannelCredentials object for server authentication
139
144
metadata: Metadata to send when exporting
145
+ compression: Compression algorithm to be used in channel
140
146
timeout: Backend request timeout in seconds
141
147
"""
142
148
@@ -147,6 +153,7 @@ def __init__(
147
153
credentials : Optional [ChannelCredentials ] = None ,
148
154
headers : Optional [str ] = None ,
149
155
timeout : Optional [int ] = None ,
156
+ compression : str = None ,
150
157
):
151
158
super ().__init__ ()
152
159
@@ -169,13 +176,40 @@ def __init__(
169
176
)
170
177
self ._collector_span_kwargs = None
171
178
179
+ if compression is None :
180
+ compression_algorithm = Compression .NoCompression
181
+ elif (
182
+ compression in OTLPCompression ._value2member_map_
183
+ and OTLPCompression (compression ) is OTLPCompression .gzip
184
+ ):
185
+ compression_algorithm = Compression .Gzip
186
+ else :
187
+ compression_str = Configuration ().EXPORTER_OTLP_INSECURE or None
188
+ if compression_str is None :
189
+ compression_algorithm = Compression .NoCompression
190
+ elif (
191
+ compression_str in OTLPCompression ._value2member_map_
192
+ and OTLPCompression (compression_str ) is OTLPCompression .gzip
193
+ ):
194
+ compression_algorithm = Compression .Gzip
195
+ else :
196
+ raise ValueError (
197
+ "OTEL_EXPORTER_OTLP_COMPRESSION environment variable does not match gzip."
198
+ )
199
+
172
200
if insecure :
173
- self ._client = self ._stub (insecure_channel (endpoint ))
201
+ self ._client = self ._stub (
202
+ insecure_channel (endpoint , compression = compression_algorithm )
203
+ )
174
204
else :
175
205
credentials = credentials or _load_credential_from_file (
176
206
Configuration ().EXPORTER_OTLP_CERTIFICATE
177
207
)
178
- self ._client = self ._stub (secure_channel (endpoint , credentials ))
208
+ self ._client = self ._stub (
209
+ secure_channel (
210
+ endpoint , credentials , compression = compression_algorithm
211
+ )
212
+ )
179
213
180
214
@abstractmethod
181
215
def _translate_data (
0 commit comments