Skip to content

Commit

Permalink
add metrics for compressed/uncompressed payload size
Browse files Browse the repository at this point in the history
  • Loading branch information
wperron committed Feb 7, 2024
1 parent 915bc61 commit 01e73b6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion examples/openresty/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lib/opentelemetry/trace/exporter/http_client.lua
Original file line number Diff line number Diff line change
@@ -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 = {
}

Expand Down Expand Up @@ -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, {
Expand Down
8 changes: 5 additions & 3 deletions lib/opentelemetry/trace/exporter/otlp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion spec/trace/exporter/otlp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 01e73b6

Please sign in to comment.