Skip to content

Commit 2a86257

Browse files
committed
t: Add 3 traceparent header tests
The new `traceparent` header auto-injection from `ngx.var.http_traceparent` has to be tested. So add a new file `t/21-traceparent-header.t` containing 3 tests for that: * TEST 1: No traceparent header is set * TEST 2: The traceparent header is correctly added when ngx.var.http_traceparent is used * TEST 3: The traceparent header is not modified from ngx.var.http_traceparent if it is already set Use an NGINX directive `set $http_traceparent '00-000...-01';` in front of the LUA block to emulate `nginx-otel` behavior. The case that there is a `traceparent` header added with `request_uri()` and `ngx.var.http_traceparent` is not set, is already covered by tests 1 and 3.
1 parent 69816ea commit 2a86257

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

‎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)