Skip to content

Commit f050819

Browse files
wilguoaabmasslzchen
authored
[Issue open-telemetry#1107] Add gzip compression support for OTLP exporter (open-telemetry#1141)
Co-authored-by: Aaron Abbott <[email protected]> Co-authored-by: Leighton Chen <[email protected]>
1 parent f815a72 commit f050819

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

exporter/opentelemetry-exporter-otlp/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add Gzip compression for exporter
6+
([#1141](https://github.com/open-telemetry/opentelemetry-python/pull/1141))
57
## Version 0.15b0
68

79
Released 2020-11-02

exporter/opentelemetry-exporter-otlp/src/opentelemetry/exporter/otlp/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
.. _OTLP: https://github.com/open-telemetry/opentelemetry-collector/
2727
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
2828
29+
.. envvar:: OTEL_EXPORTER_OTLP_COMPRESSION
30+
31+
The :envvar:`OTEL_EXPORTER_OTLP_COMPRESSION` environment variable allows a
32+
compression algorithm to be passed to the OTLP exporter. The compression
33+
algorithms that are supported include gzip and no compression. The value should
34+
be in the format of a string "gzip" for gzip compression, and no value specified
35+
if no compression is the desired choice.
36+
Additional details are available `in the specification
37+
<https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/protocol/exporter.md#opentelemetry-protocol-exporter>`_.
38+
2939
.. code:: python
3040
3141
from opentelemetry import trace

exporter/opentelemetry-exporter-otlp/src/opentelemetry/exporter/otlp/exporter.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414

1515
"""OTLP Exporter"""
1616

17+
import enum
1718
import logging
18-
import os
1919
from abc import ABC, abstractmethod
2020
from collections.abc import Mapping, Sequence
2121
from time import sleep
2222
from typing import Any, Callable, Dict, Generic, List, Optional
2323
from typing import Sequence as TypingSequence
24-
from typing import Text, Tuple, TypeVar
24+
from typing import Text, TypeVar
2525

2626
from backoff import expo
2727
from google.rpc.error_details_pb2 import RetryInfo
2828
from grpc import (
2929
ChannelCredentials,
30+
Compression,
3031
RpcError,
3132
StatusCode,
3233
insecure_channel,
@@ -47,6 +48,10 @@
4748
ExportResultT = TypeVar("ExportResultT")
4849

4950

51+
class OTLPCompression(enum.Enum):
52+
gzip = "gzip"
53+
54+
5055
def _translate_key_values(key: Text, value: Any) -> KeyValue:
5156

5257
if isinstance(value, bool):
@@ -137,6 +142,7 @@ class OTLPExporterMixin(
137142
insecure: Connection type
138143
credentials: ChannelCredentials object for server authentication
139144
metadata: Metadata to send when exporting
145+
compression: Compression algorithm to be used in channel
140146
timeout: Backend request timeout in seconds
141147
"""
142148

@@ -147,6 +153,7 @@ def __init__(
147153
credentials: Optional[ChannelCredentials] = None,
148154
headers: Optional[str] = None,
149155
timeout: Optional[int] = None,
156+
compression: str = None,
150157
):
151158
super().__init__()
152159

@@ -169,13 +176,40 @@ def __init__(
169176
)
170177
self._collector_span_kwargs = None
171178

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+
172200
if insecure:
173-
self._client = self._stub(insecure_channel(endpoint))
201+
self._client = self._stub(
202+
insecure_channel(endpoint, compression=compression_algorithm)
203+
)
174204
else:
175205
credentials = credentials or _load_credential_from_file(
176206
Configuration().EXPORTER_OTLP_CERTIFICATE
177207
)
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+
)
179213

180214
@abstractmethod
181215
def _translate_data(

0 commit comments

Comments
 (0)