diff --git a/examples/openresty/nginx.conf b/examples/openresty/nginx.conf index 79fa449..c5e9840 100644 --- a/examples/openresty/nginx.conf +++ b/examples/openresty/nginx.conf @@ -7,7 +7,8 @@ init_worker_by_lua_block { local attr = require("opentelemetry.attribute") -- create exporter - local exporter = otlp_exporter_new(exporter_client_new("otel-collector:4317", 3)) + local use_gzip = true + local exporter = otlp_exporter_new(exporter_client_new("otel-collector:4317", 3, {}, nil, use_gzip)) -- create span processor local batch_span_processor = batch_span_processor_new(exporter) -- create tracer provider diff --git a/lib/opentelemetry/trace/exporter/http_client.lua b/lib/opentelemetry/trace/exporter/http_client.lua index 7d291ea..c28c78f 100644 --- a/lib/opentelemetry/trace/exporter/http_client.lua +++ b/lib/opentelemetry/trace/exporter/http_client.lua @@ -1,5 +1,9 @@ local http = require("resty.http") local zlib = require("zlib") +local otel_global = require("opentelemetry.global") +local exporter_request_compressed_payload_size = "otel.otlp_exporter.request_compressed_payload_size" +local exporter_request_uncompressed_payload_size = "otel.otlp_exporter.request_uncompressed_payload_size" + local _M = { } @@ -14,11 +18,16 @@ local mt = { -- @timeout export request timeout second -- @headers export request headers -- @httpc openresty http client instance +-- @use_gzip flag to enable gzip compression on request body -- @return http client ------------------------------------------------------------------ -function _M.new(address, timeout, headers, httpc) +function _M.new(address, timeout, headers, httpc, use_gzip) headers = headers or {} headers["Content-Type"] = "application/x-protobuf" + use_gzip = use_gzip or false + if use_gzip then + headers["Content-Encoding"] = "gzip" + end local uri = address .. "/v1/traces" if address:find("http", 1, true) ~= 1 then @@ -30,25 +39,33 @@ function _M.new(address, timeout, headers, httpc) timeout = timeout, headers = headers, httpc = httpc, + use_gzip = use_gzip, } + return setmetatable(self, mt) end -function _M.do_request(self, body, encode_gzip) +function _M.do_request(self, body) self.httpc = self.httpc or http.new() - - encode_gzip = encode_gzip or false self.httpc:set_timeout(self.timeout * 1000) - if encode_gzip then + if self.use_gzip then -- Compress (deflate) request body -- the compression should be set to Best Compression and window size -- should be set to 15+16, see reference below: -- https://github.com/brimworks/lua-zlib/issues/4#issuecomment-26383801 - self.headers["Content-Encoding"] = "gzip" local deflate_stream = zlib.deflate(zlib.BEST_COMPRESSION, 15+16) - local compressed_body = deflate_stream(body, "finish") + local compressed_body, _, _, bytes_out = deflate_stream(body, "finish") + + otel_global.metrics_reporter:record_value( + exporter_request_uncompressed_payload_size, string.len(body)) + otel_global.metrics_reporter:record_value( + exporter_request_compressed_payload_size, bytes_out) + body = compressed_body + else + otel_global.metrics_reporter:record_value( + exporter_request_uncompressed_payload_size, string.len(body)) end local res, err = self.httpc:request_uri(self.uri, { diff --git a/lib/opentelemetry/trace/exporter/otlp.lua b/lib/opentelemetry/trace/exporter/otlp.lua index d35909c..68c09d6 100644 --- a/lib/opentelemetry/trace/exporter/otlp.lua +++ b/lib/opentelemetry/trace/exporter/otlp.lua @@ -21,7 +21,7 @@ function _M.new(http_client, timeout_ms, circuit_reset_timeout_ms, circuit_open_ circuit = circuit.new({ reset_timeout_ms = circuit_reset_timeout_ms, failure_threshold = circuit_open_threshold - }) + }), } return setmetatable(self, mt) end @@ -55,7 +55,7 @@ local function call_collector(exporter, pb_encoded_body) end -- Make request - res, res_error = exporter.client:do_request(pb_encoded_body, true) + res, res_error = exporter.client:do_request(pb_encoded_body) local after_time = util.gettimeofday_ms() otel_global.metrics_reporter:record_value( exporter_request_duration_metric, after_time - current_time) diff --git a/spec/trace/exporter/http_client_spec.lua b/spec/trace/exporter/http_client_spec.lua index f5dd641..b50fa35 100644 --- a/spec/trace/exporter/http_client_spec.lua +++ b/spec/trace/exporter/http_client_spec.lua @@ -1,6 +1,5 @@ local http = require("resty.http") local client = require "opentelemetry.trace.exporter.http_client" -local zlib = require("zlib") describe("export_spans", function() it("invokes an HTTP request", function () @@ -28,13 +27,13 @@ describe("export_spans", function() it("compresses the body when asked to", function () local httpc = http.new() spy.on(httpc, "request_uri") - local c = client.new("http://localhost:8080", 10, nil, httpc) + local c = client.new("http://localhost:8080", 10, nil, httpc, true) local expected_headers = {} expected_headers["Content-Type"] = "application/x-protobuf" expected_headers["Content-Encoding"] = "gzip" local expected_body = string.fromhex("1F8B0800000000000203F348CDC9C9D75108CF2FCA49510400D0C34AEC0D000000") - c:do_request("Hello, World!", true) + c:do_request("Hello, World!") assert.spy(httpc.request_uri).was_called_with( match.is_truthy(), -- TODO(wperron) this *should* be the same ref, why is it not? match.is_equal("http://localhost:8080/v1/traces"), diff --git a/spec/trace/exporter/otlp_spec.lua b/spec/trace/exporter/otlp_spec.lua index dfece3d..7b2f500 100644 --- a/spec/trace/exporter/otlp_spec.lua +++ b/spec/trace/exporter/otlp_spec.lua @@ -17,7 +17,7 @@ describe("export_spans", function() stub(ngx, "log") cb:export_spans({ span }) ngx.log:revert() - assert.spy(c.do_request).was_called_with(c, match.is_string(), match.is_all_of(match.is_boolean(), match.is_equal(true))) + assert.spy(c.do_request).was_called_with(c, match.is_string()) end) it("doesn't invoke protected_call when failures is equal to retry limit", function()