local
worker_pid
=
ngx
.
worker
.
pid
()
local
fmt
=
string.format
[
"
uuid
"
]
=
function
()
[
"
uuid#counter
"
]
=
function
()
worker_counter
=
worker_counter
+
1
return
worker_uuid
..
"
#
"
..
worker_counter
[
"
tracker
"
]
=
function
()
return
fmt
(
"
%s-%s-%d-%s-%s-%0.3f
"
,
var
.
connection
,
--
connection serial number
var
.
connection_requests
,
--
current number of requests made through a connection
now
()
--
the current time stamp from the nginx cached time.
local
CorrelationIdHandler
=
{}
CorrelationIdHandler
.
PRIORITY
=
1
CorrelationIdHandler
.
VERSION
=
kong_meta
.
version
function
CorrelationIdHandler
:
init_worker
()
function
CorrelationIdHandler
:
access
(
conf
)
--
Set header for upstream
local
correlation_id
=
kong
.
request
.
get_header
(
conf
.
header_name
)
if
not
correlation_id
or
correlation_id
==
"
"
then
--
Generate the header value
correlation_id
=
generators
[
conf
.
generator
]()
kong
.
service
.
request
.
set_header
(
conf
.
header_name
,
correlation_id
)
kong
.
log
.
set_serialize_value
(
"
correlation_id
"
,
correlation_id
)
if
conf
.
echo_downstream
then
--
For later use, to echo it back downstream
kong
.
ctx
.
plugin
.
correlation_id
=
correlation_id
function
CorrelationIdHandler
:
header_filter
(
conf
)
if
not
conf
.
echo_downstream
then
local
correlation_id
=
kong
.
ctx
.
plugin
.
correlation_id
or
kong
.
request
.
get_header
(
conf
.
header_name
)
if
not
correlation_id
or
correlation_id
==
"
"
then
correlation_id
=
generators
[
conf
.
generator
]()
kong
.
response
.
set_header
(
conf
.
header_name
,
correlation_id
)
return
CorrelationIdHandler