Skip to content

Commit

Permalink
Compress otlp payload with zlib
Browse files Browse the repository at this point in the history
Fixes #71

Also updates the otel-collector configuration file:

* The `logging` exporter was renamed to `debug`
* Jaeger now supports receiving OTLP traffic which makes things simpler
  • Loading branch information
wperron committed Feb 5, 2024
1 parent 6e44fe8 commit 915bc61
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
FROM openresty/openresty:1.21.4.1-0-centos

RUN yum install -y gcc
RUN yum install -y gcc git zlib-devel
RUN yum -y --enablerepo=powertools install libyaml-devel libffi-devel
RUN luarocks install lua-resty-http 0.16.1-0
RUN luarocks install lua-protobuf 0.3.3
RUN luarocks install lua-zlib 1.2
RUN luarocks install busted 2.0.0-1
RUN luarocks --server=http://rocks.moonscript.org install lyaml

Expand Down
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ services:
image: jaegertracing/all-in-one
ports:
- 26686:16686
- 4319-4320:4317-4318
networks:
- opentelemetry-lua
otel-collector:
image: otel/opentelemetry-collector-contrib-dev:latest
command: [ "--config=/etc/otel-collector-config.yaml" ]
ports:
- 4317:4317
volumes:
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml
depends_on:
- jaeger
networks:
- opentelemetry-lua
test-server:
build:
context: ./examples/server
ports:
- 10080:80
depends_on:
- otel-collector
networks:
Expand Down
31 changes: 23 additions & 8 deletions lib/opentelemetry/trace/exporter/http_client.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local http = require("resty.http")

local zlib = require("zlib")
local _M = {
}

Expand All @@ -13,9 +13,10 @@ local mt = {
-- @address opentelemetry collector: host:port
-- @timeout export request timeout second
-- @headers export request headers
-- @httpc openresty http client instance
-- @return http client
------------------------------------------------------------------
function _M.new(address, timeout, headers)
function _M.new(address, timeout, headers, httpc)
headers = headers or {}
headers["Content-Type"] = "application/x-protobuf"

Expand All @@ -28,29 +29,43 @@ function _M.new(address, timeout, headers)
uri = uri,
timeout = timeout,
headers = headers,
httpc = httpc,
}
return setmetatable(self, mt)
end

function _M.do_request(self, body)
local httpc = http.new()
httpc:set_timeout(self.timeout * 1000)
function _M.do_request(self, body, encode_gzip)
self.httpc = self.httpc or http.new()

encode_gzip = encode_gzip or false
self.httpc:set_timeout(self.timeout * 1000)

if encode_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")
body = compressed_body
end

local res, err = httpc:request_uri(self.uri, {
local res, err = self.httpc:request_uri(self.uri, {
method = "POST",
headers = self.headers,
body = body,
})

if not res then
ngx.log(ngx.ERR, "request failed: ", err)
httpc:close()
self.httpc:close()
return nil, err
end

if res.status ~= 200 then
ngx.log(ngx.ERR, "request failed: ", res.body)
httpc:close()
self.httpc:close()
return nil, "request failed: " .. res.status
end

Expand Down
2 changes: 1 addition & 1 deletion lib/opentelemetry/trace/exporter/otlp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
res, res_error = exporter.client:do_request(pb_encoded_body, true)
local after_time = util.gettimeofday_ms()
otel_global.metrics_reporter:record_value(
exporter_request_duration_metric, after_time - current_time)
Expand Down
17 changes: 6 additions & 11 deletions otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ exporters:
endpoint: "0.0.0.0:8889"
const_labels:
label1: value1
logging:
loglevel: debug

#zipkin:
# endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
# format: proto

jaeger:
endpoint: jaeger:14250
debug:
verbosity: detailed
otlp/jaeger:
endpoint: jaeger:4317
tls:
insecure: true

Expand All @@ -37,8 +32,8 @@ service:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging, jaeger]
exporters: [debug, otlp/jaeger]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [logging, prometheus]
exporters: [debug, prometheus]
1 change: 1 addition & 0 deletions rockspec/opentelemetry-lua-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ description = {
dependencies = {
"lua-protobuf = 0.3.3",
"lua-resty-http = 0.16.1-0",
"lua-zlib = 1.2",
}

build = {
Expand Down
57 changes: 57 additions & 0 deletions spec/trace/exporter/http_client_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 ()
local httpc = http.new()
spy.on(httpc, "request_uri")
local c = client.new("http://localhost:8080", 10, nil, httpc)
local expected_headers = {}
expected_headers["Content-Type"] = "application/x-protobuf"

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"),
match.is_all_of(
match.is_table(),
match.is_same({
method = "POST",
headers = expected_headers,
body = "Hello, World!"
})
)
)
end)

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 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)
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"),
match.is_all_of(
match.is_table(),
match.is_same({
method = "POST",
headers = expected_headers,
body = expected_body
})
)
)
end)
end)

function string.fromhex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
2 changes: 1 addition & 1 deletion spec/trace/exporter/otlp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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())
assert.spy(c.do_request).was_called_with(c, match.is_string(), match.is_all_of(match.is_boolean(), 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 915bc61

Please sign in to comment.