-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathencoder.lua
85 lines (77 loc) · 2.95 KB
/
encoder.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-------------------------------------------------------------------------------
-- The encoder is responsible for taking spans and serializing them into
-- different export formats.
-------------------------------------------------------------------------------
local util = require("opentelemetry.util")
local _M = {
}
local mt = {
__index = _M
}
--------------------------------------------------------------------------------
-- hex2bytes converts a hex string into bytes (used for transit over OTLP).
--
-- @param str Hex string.
-- @return Table to be used as basis for more specific exporters.
--------------------------------------------------------------------------------
local function hex2bytes(str)
return (str:gsub('..', function(cc)
local n = tonumber(cc, 16)
if n then
return string.char(n)
end
end))
end
--------------------------------------------------------------------------------
-- for_export structures span data for export; used as basis for more specific
-- exporters.
--
-- @param span The span to export
-- @return Table to be used as basis for more specific exporters.
--------------------------------------------------------------------------------
function _M.for_export(span)
return {
trace_id = span.ctx.trace_id,
span_id = span.ctx.span_id,
trace_state = span.ctx.trace_state:as_string(),
parent_span_id = span.parent_ctx.span_id or "",
name = span.name,
kind = span.kind,
start_time_unix_nano = string.format("%d", span.start_time),
end_time_unix_nano = string.format("%d", span.end_time),
attributes = span.attributes,
dropped_attributes_count = 0,
events = span.events,
dropped_events_count = 0,
links = {},
dropped_links_count = 0,
status = span.status
}
end
--------------------------------------------------------------------------------
-- for_otlp returns a table that can be protobuf-encoded for transmission over
-- OTLP.
--
-- @param span The span to export
-- @return Table to be protobuf-encoded
--------------------------------------------------------------------------------
function _M.for_otlp(span)
local ret = _M.for_export(span)
ret.trace_id = hex2bytes(ret.trace_id)
ret.span_id = hex2bytes(ret.span_id)
ret.parent_span_id = hex2bytes(ret.parent_span_id)
return ret
end
--------------------------------------------------------------------------------
-- for_console renders a string representation of span for console output.
--
-- @param span The span to export
-- @return String representation of span.
--------------------------------------------------------------------------------
function _M.for_console(span)
local ret = "\n---------------------------------------------------------\n"
ret = ret .. util.table_as_string(_M.for_export(span), 2)
ret = ret .. "---------------------------------------------------------\n"
return ret
end
return _M