Skip to content

Commit 84e7cf9

Browse files
authored
Merge pull request #306 from kw7oe/add-gzip-compression-option-for-exporter
Add gzip compression option for exporter
2 parents 0bc0f4d + 94ddb7f commit 84e7cf9

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

apps/opentelemetry_exporter/src/opentelemetry_exporter.erl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
ssl_options => []}.
9191

9292
-type protocol() :: grpc | http_protobuf | http_json.
93+
-type compression() :: gzip.
9394

9495
-type opts() :: #{endpoints => [endpoint()],
9596
headers => headers(),
@@ -103,6 +104,7 @@
103104
-record(state, {protocol :: protocol(),
104105
channel_pid :: pid() | undefined,
105106
headers :: headers(),
107+
compression :: compression() | undefined,
106108
grpc_metadata :: map() | undefined,
107109
endpoints :: [endpoint_map()]}).
108110

@@ -113,14 +115,22 @@ init(Opts) ->
113115
SSLOptions = maps:get(ssl_options, Opts1, undefined),
114116
Endpoints = endpoints(maps:get(endpoints, Opts1, ?DEFAULT_ENDPOINTS), SSLOptions),
115117
Headers = headers(maps:get(headers, Opts1, [])),
118+
Compression = maps:get(compression, Opts1, undefined),
116119
case maps:get(protocol, Opts1, http_protobuf) of
117120
grpc ->
118121
ChannelOpts = maps:get(channel_opts, Opts1, #{}),
119-
case grpcbox_channel:start_link(?MODULE, grpcbox_endpoints(Endpoints), ChannelOpts) of
122+
UpdatedChannelOpts = case Compression of
123+
undefined -> ChannelOpts;
124+
Encoding -> maps:put(encoding, Encoding, ChannelOpts)
125+
end,
126+
case grpcbox_channel:start_link(?MODULE,
127+
grpcbox_endpoints(Endpoints),
128+
UpdatedChannelOpts) of
120129
{ok, ChannelPid} ->
121130
{ok, #state{channel_pid=ChannelPid,
122131
endpoints=Endpoints,
123132
headers=Headers,
133+
compression=Compression,
124134
grpc_metadata=headers_to_grpc_metadata(Headers),
125135
protocol=grpc}};
126136
ErrorOrIgnore ->
@@ -130,15 +140,18 @@ init(Opts) ->
130140
"to http_protobuf protocol. reason=~p", [ErrorOrIgnore]),
131141
{ok, #state{endpoints=Endpoints,
132142
headers=Headers,
143+
compression=Compression,
133144
protocol=http_protobuf}}
134145
end;
135146
http_protobuf ->
136147
{ok, #state{endpoints=Endpoints,
137148
headers=Headers,
149+
compression=Compression,
138150
protocol=http_protobuf}};
139151
http_json ->
140152
{ok, #state{endpoints=Endpoints,
141153
headers=Headers,
154+
compression=Compression,
142155
protocol=http_json}}
143156
end.
144157

@@ -147,18 +160,24 @@ export(_Tab, _Resource, #state{protocol=http_json}) ->
147160
{error, unimplemented};
148161
export(Tab, Resource, #state{protocol=http_protobuf,
149162
headers=Headers,
163+
compression=Compression,
150164
endpoints=[#{scheme := Scheme,
151165
host := Host,
152166
path := Path,
153167
port := Port,
154168
ssl_options := SSLOptions} | _]}) ->
155169
Proto = opentelemetry_exporter_trace_service_pb:encode_msg(tab_to_proto(Tab, Resource),
156170
export_trace_service_request),
171+
{NewHeaders, NewProto} = case Compression of
172+
gzip -> {[{"content-encoding", "gzip"} | Headers], zlib:gzip(Proto)};
173+
_ -> {Headers, Proto}
174+
end,
157175
Address = uri_string:normalize(#{scheme => Scheme,
158176
host => Host,
159177
port => Port,
160178
path => Path}),
161-
case httpc:request(post, {Address, Headers, "application/x-protobuf", Proto},
179+
180+
case httpc:request(post, {Address, NewHeaders, "application/x-protobuf", NewProto},
162181
[{ssl, SSLOptions}], []) of
163182
{ok, {{_, Code, _}, _, _}} when Code >= 200 andalso Code =< 202 ->
164183
ok;
@@ -199,7 +218,7 @@ shutdown(#state{channel_pid=Pid}) ->
199218
%%
200219

201220
grpcbox_endpoints(Endpoints) ->
202-
[{scheme(Scheme), Host, Port, maps:get(ssl_options, Endpoint, [])} ||
221+
[{scheme(Scheme), Host, Port, maps:get(ssl_options, Endpoint, [])} ||
203222
#{scheme := Scheme, host := Host, port := Port} = Endpoint <- Endpoints].
204223

205224
headers_to_grpc_metadata(Headers) ->

apps/opentelemetry_exporter/test/opentelemetry_exporter_SUITE.erl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
-include_lib("opentelemetry/include/otel_span.hrl").
1010

1111
all() ->
12-
[{group, functional}, {group, http_protobuf}, {group, grpc}].
12+
[{group, functional}, {group, http_protobuf}, {group, http_protobuf_gzip},
13+
{group, grpc}, {group, grpc_gzip}].
1314

1415
groups() ->
1516
[{functional, [], [configuration, span_round_trip, ets_instrumentation_info]},
1617
{grpc, [], [verify_export]},
17-
{http_protobuf, [], [verify_export]}].
18+
{grpc_gzip, [], [verify_export]},
19+
{http_protobuf, [], [verify_export]},
20+
{http_protobuf_gzip, [], [verify_export]}].
1821

1922
init_per_suite(Config) ->
2023
Config.
@@ -26,6 +29,12 @@ init_per_group(Group, Config) when Group =:= grpc ;
2629
Group =:= http_protobuf ->
2730
application:ensure_all_started(opentelemetry_exporter),
2831
[{protocol, Group}| Config];
32+
init_per_group(http_protobuf_gzip, Config) ->
33+
application:ensure_all_started(opentelemetry_exporter),
34+
[{protocol, http_protobuf}, {compression, gzip} | Config];
35+
init_per_group(grpc_gzip, Config) ->
36+
application:ensure_all_started(opentelemetry_exporter),
37+
[{protocol, grpc}, {compression, gzip} | Config];
2938
init_per_group(_, _) ->
3039
application:load(opentelemetry_exporter),
3140
ok.
@@ -218,16 +227,22 @@ span_round_trip(_Config) ->
218227
verify_export(Config) ->
219228
os:putenv("OTEL_RESOURCE_ATTRIBUTES", "service.name=my-test-service,service.version=98da75ea6d38724743bf42b45565049238d86b3f"),
220229
Protocol = ?config(protocol, Config),
230+
Compression = ?config(compression, Config),
221231
Port = case Protocol of
222232
grpc ->
223233
4317;
224234
http_protobuf ->
225235
55681
226236
end,
227237
{ok, State} = opentelemetry_exporter:init(#{protocol => Protocol,
238+
compression => Compression,
228239
endpoints => [{http, "localhost", Port, []}]}),
229240
Tid = ets:new(span_tab, [duplicate_bag, {keypos, #span.instrumentation_library}]),
230241

242+
%% Tempoararily adding this because without this, we would face
243+
%% {error, no_endpoints} when attempt to export when we have more
244+
%% than 1 gprc test case.
245+
timer:sleep(500),
231246
?assertMatch(ok, opentelemetry_exporter:export(Tid, otel_resource:create([]), State)),
232247

233248
TraceId = otel_id_generator:generate_trace_id(),

0 commit comments

Comments
 (0)