@@ -8,8 +8,11 @@ use crate::exporter::grpcio::GrpcioExporterBuilder;
8
8
use crate :: exporter:: http:: HttpExporterBuilder ;
9
9
#[ cfg( feature = "grpc-tonic" ) ]
10
10
use crate :: exporter:: tonic:: TonicExporterBuilder ;
11
- use crate :: Protocol ;
11
+ use crate :: { Error , Protocol } ;
12
+ #[ cfg( feature = "serialize" ) ]
13
+ use serde:: { Deserialize , Serialize } ;
12
14
use std:: collections:: HashMap ;
15
+ use std:: fmt:: { Display , Formatter } ;
13
16
use std:: str:: FromStr ;
14
17
use std:: time:: Duration ;
15
18
@@ -21,6 +24,8 @@ pub const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
21
24
pub const OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT : & str = OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ;
22
25
/// Protocol the exporter will use. Either `http/protobuf` or `grpc`.
23
26
pub const OTEL_EXPORTER_OTLP_PROTOCOL : & str = "OTEL_EXPORTER_OTLP_PROTOCOL" ;
27
+ /// Compression algorithm to use, defaults to none.
28
+ pub const OTEL_EXPORTER_OTLP_COMPRESSION : & str = "OTEL_EXPORTER_OTLP_COMPRESSION" ;
24
29
25
30
#[ cfg( feature = "http-proto" ) ]
26
31
/// Default protocol, using http-proto.
@@ -79,6 +84,33 @@ impl Default for ExportConfig {
79
84
}
80
85
}
81
86
87
+ /// The compression algorithm to use when sending data.
88
+ #[ cfg_attr( feature = "serialize" , derive( Deserialize , Serialize ) ) ]
89
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
90
+ pub enum Compression {
91
+ /// Compresses data using gzip.
92
+ Gzip ,
93
+ }
94
+
95
+ impl Display for Compression {
96
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
97
+ match self {
98
+ Compression :: Gzip => write ! ( f, "gzip" ) ,
99
+ }
100
+ }
101
+ }
102
+
103
+ impl FromStr for Compression {
104
+ type Err = Error ;
105
+
106
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
107
+ match s {
108
+ "gzip" => Ok ( Compression :: Gzip ) ,
109
+ _ => Err ( Error :: UnsupportedCompressionAlgorithm ( s. to_string ( ) ) ) ,
110
+ }
111
+ }
112
+ }
113
+
82
114
/// default protocol based on enabled features
83
115
fn default_protocol ( ) -> Protocol {
84
116
match OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT {
@@ -217,13 +249,15 @@ impl<B: HasExportConfig> WithExportConfig for B {
217
249
mod tests {
218
250
// If an env test fails then the mutex will be poisoned and the following error will be displayed.
219
251
const LOCK_POISONED_MESSAGE : & str = "one of the other pipeline builder from env tests failed" ;
252
+
220
253
use crate :: exporter:: {
221
254
default_endpoint, default_protocol, WithExportConfig , OTEL_EXPORTER_OTLP_ENDPOINT ,
222
255
OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT , OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
223
256
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC , OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF ,
224
257
OTEL_EXPORTER_OTLP_TIMEOUT , OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ,
225
258
} ;
226
- use crate :: { new_exporter, Protocol , OTEL_EXPORTER_OTLP_PROTOCOL } ;
259
+ use crate :: { new_exporter, Compression , Protocol , OTEL_EXPORTER_OTLP_PROTOCOL } ;
260
+ use std:: str:: FromStr ;
227
261
use std:: sync:: Mutex ;
228
262
229
263
// Make sure env tests are not running concurrently
@@ -345,4 +379,15 @@ mod tests {
345
379
std:: env:: remove_var ( OTEL_EXPORTER_OTLP_TIMEOUT ) ;
346
380
assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_TIMEOUT ) . is_err( ) ) ;
347
381
}
382
+
383
+ #[ test]
384
+ fn test_compression_parse ( ) {
385
+ assert_eq ! ( Compression :: from_str( "gzip" ) . unwrap( ) , Compression :: Gzip ) ;
386
+ Compression :: from_str ( "bad_compression" ) . expect_err ( "bad compression" ) ;
387
+ }
388
+
389
+ #[ test]
390
+ fn test_compression_to_str ( ) {
391
+ assert_eq ! ( Compression :: Gzip . to_string( ) , "gzip" ) ;
392
+ }
348
393
}
0 commit comments