-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetry.lua
More file actions
59 lines (43 loc) · 1.31 KB
/
Copy pathTelemetry.lua
File metadata and controls
59 lines (43 loc) · 1.31 KB
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
local Telemetry = {}
Telemetry.__index = Telemetry
local LOG_LEVEL_INFO = 3
local LOG_LEVEL_DEBUG = 4
local NoOpTelemetry = {}
NoOpTelemetry.__index = NoOpTelemetry
function NoOpTelemetry.new()
return setmetatable({}, NoOpTelemetry)
end
function NoOpTelemetry:span(operationName, fn)
return fn()
end
function NoOpTelemetry:setLogLevel(logLevel)
end
function Telemetry.new(componentName, logLevel)
local self = setmetatable({}, Telemetry)
self._componentName = componentName
self._logger = hs and hs.logger and hs.logger.new(componentName, logLevel or 'debug')
return self
end
function Telemetry:setLogLevel(logLevel)
self._logger = hs and hs.logger and hs.logger.new(self._componentName, logLevel)
end
function Telemetry:span(operationName, fn)
if not self._logger then
return fn()
end
local shouldLogOperation = self._logger.level >= LOG_LEVEL_INFO
local shouldLogPerformance = self._logger.level >= LOG_LEVEL_DEBUG
if shouldLogOperation then
self._logger.i(operationName)
end
if not shouldLogPerformance then
return fn()
end
local start = hs.timer.secondsSinceEpoch()
local results = {fn()}
local duration = (hs.timer.secondsSinceEpoch() - start) * 1000
self._logger.df("%s took %.2fms", operationName, duration)
return table.unpack(results)
end
Telemetry.NoOp = NoOpTelemetry
return Telemetry