Skip to content

Commit 7837f60

Browse files
authored
Merge pull request #340 from sriemer/otel-traceparent
feature: Add W3C trace context support via adding the `traceparent` header from `$http_traceparent` (for OpenTracing + OpenTelemetry)
2 parents b92d324 + 81d90b7 commit 7837f60

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

lib/resty/http.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,10 @@ function _M.send_request(self, params)
738738
if params.version == 1.0 and not headers["Connection"] then
739739
headers["Connection"] = "Keep-Alive"
740740
end
741+
-- W3C trace context support with NGINX tracer
742+
if not headers["traceparent"] and ngx.var.http_traceparent then
743+
headers["traceparent"] = ngx.var.http_traceparent
744+
end
741745

742746
params.headers = headers
743747

t/21-traceparent-header.t

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use Test::Nginx::Socket 'no_plan';
2+
use Cwd qw(cwd);
3+
4+
my $pwd = cwd();
5+
6+
$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
7+
$ENV{TEST_NGINX_PWD} ||= $pwd;
8+
$ENV{TEST_COVERAGE} ||= 0;
9+
10+
our $HttpConfig = qq{
11+
lua_package_path "$pwd/lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
12+
error_log logs/error.log debug;
13+
resolver 8.8.8.8 ipv6=off;
14+
15+
init_by_lua_block {
16+
if $ENV{TEST_COVERAGE} == 1 then
17+
jit.off()
18+
require("luacov.runner").init()
19+
end
20+
21+
require("resty.http").debug(true)
22+
}
23+
};
24+
25+
no_long_string();
26+
#no_diff();
27+
28+
run_tests();
29+
30+
__DATA__
31+
=== TEST 1: No traceparent header is set
32+
--- http_config eval: $::HttpConfig
33+
--- config
34+
location /lua {
35+
content_by_lua '
36+
require("resty.http").debug(true)
37+
local http = require "resty.http"
38+
local httpc = http.new()
39+
40+
local res, err = httpc:request_uri("http://www.google.com")
41+
';
42+
}
43+
--- request
44+
GET /lua
45+
--- no_error_log
46+
[error]
47+
traceparent:
48+
49+
50+
=== TEST 2: The traceparent header is correctly added when ngx.var.http_traceparent is used
51+
--- http_config eval: $::HttpConfig
52+
--- config
53+
location /lua {
54+
# emulate nginx-otel behavior here
55+
set $http_traceparent '00-000000000000000019f4e02c82857913-11488c6e00d1d248-01';
56+
content_by_lua '
57+
require("resty.http").debug(true)
58+
local http = require "resty.http"
59+
local httpc = http.new()
60+
local res, err = httpc:request_uri("http://www.google.com")
61+
';
62+
}
63+
--- request
64+
GET /lua
65+
--- no_error_log
66+
[error]
67+
--- error_log
68+
traceparent: 00-000000000000000019f4e02c82857913-11488c6e00d1d248-01
69+
70+
71+
=== TEST 3: The traceparent header is not modified from ngx.var.http_traceparent if it is already set
72+
--- http_config eval: $::HttpConfig
73+
--- config
74+
location /lua {
75+
# emulate nginx-otel behavior here
76+
set $http_traceparent '00-000000000000000019f4e02c82857913-11488c6e00d1d248-01';
77+
content_by_lua '
78+
require("resty.http").debug(true)
79+
local http = require "resty.http"
80+
local httpc = http.new()
81+
local req_headers = {}
82+
req_headers["traceparent"] = "00-00000000000000006633c2d00527dd33-1af98f7e6ecd16ff-01"
83+
local res, err = httpc:request_uri("http://www.google.com", {method = GET, headers = req_headers})
84+
';
85+
}
86+
--- request
87+
GET /lua
88+
--- no_error_log
89+
[error]
90+
traceparent: 00-000000000000000019f4e02c82857913-11488c6e00d1d248-01
91+
--- error_log
92+
traceparent: 00-00000000000000006633c2d00527dd33-1af98f7e6ecd16ff-01

0 commit comments

Comments
 (0)