diff --git a/examples/openresty/nginx.conf b/examples/openresty/nginx.conf index 79fa449..699fe14 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), 10000, 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..bf6e82c 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 = { } @@ -47,8 +51,17 @@ function _M.do_request(self, body, encode_gzip) -- 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_in, bytes_out = deflate_stream(body, "finish") + + otel_global.metrics_reporter:record_value( + exporter_request_uncompressed_payload_size, bytes_in) + 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..11dca76 100644 --- a/lib/opentelemetry/trace/exporter/otlp.lua +++ b/lib/opentelemetry/trace/exporter/otlp.lua @@ -14,14 +14,16 @@ local mt = { __index = _M } -function _M.new(http_client, timeout_ms, circuit_reset_timeout_ms, circuit_open_threshold) +function _M.new(http_client, timeout_ms, use_gzip, circuit_reset_timeout_ms, circuit_open_threshold) + use_gzip = use_gzip or false local self = { client = http_client, timeout_ms = timeout_ms or DEFAULT_TIMEOUT_MS, + use_gzip = use_gzip, circuit = circuit.new({ reset_timeout_ms = circuit_reset_timeout_ms, failure_threshold = circuit_open_threshold - }) + }), } return setmetatable(self, mt) end @@ -55,7 +57,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, exporter.use_gzip) 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/otlp_spec.lua b/spec/trace/exporter/otlp_spec.lua index dfece3d..10906a5 100644 --- a/spec/trace/exporter/otlp_spec.lua +++ b/spec/trace/exporter/otlp_spec.lua @@ -17,7 +17,22 @@ 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(), match.is_equal(false)) + end) + + it("invokes do_request with gzip compression when configured to do so", function() + local span + local ctx = context.new() + ctx, span = tracer:start(ctx, "test span") + span:finish() + local c = client.new("http://localhost:8080", 10) + spy.on(c, "do_request") + local cb = exporter.new(c, 10000, true) -- use_gzip = true + -- Supress log message, since we expect it + stub(ngx, "log") + cb:export_spans({ span }) + ngx.log:revert() + assert.spy(c.do_request).was_called_with(c, match.is_string(), match.is_equal(true)) end) it("doesn't invoke protected_call when failures is equal to retry limit", function()