/
telemetry_log.go
82 lines (78 loc) · 1.95 KB
/
telemetry_log.go
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
package stdlib
import (
"time"
"errors"
"github.com/Jeffail/gabs/v2"
"github.com/gammazero/deque"
"github.com/vulogov/monitoringbund/internal/conf"
tc "github.com/vulogov/ThreadComputation"
)
func BUNDTelemetryLog(l *tc.TCExecListener, name string, q *deque.Deque) (interface{}, error) {
var err error
stamp := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)
out := gabs.New()
out.Set("log", "type")
out.Set(stamp, "log", "timestamp")
out.Set(*conf.ApplicationId, "attributes", "ApplicationId")
out.Set(*conf.Name, "attributes", "ApplicationName")
for q.Len() > 0 {
v := q.PopFront()
switch data := v.(type) {
case *tc.TCPair:
switch key := data.X.(type) {
case string:
switch key {
case "service", "host", "logtype":
out.Set(data.Y, "log", key)
default:
out.Set(data.Y, "attributes", key)
}
}
case string:
out.Set(data, "log", "msg")
default:
out.Set(data, "attributes", "value")
}
}
if out.Search("log", "service").Data() == nil {
svc := l.TC.GetContext("key")
if svc == nil {
svc = "genericservice"
}
switch svc.(type) {
case string:
out.Set(svc, "log", "service")
}
}
if out.Search("log", "logtype").Data() == nil {
lt := l.TC.GetContext("logtype")
if lt == nil {
lt = "log"
}
switch lt.(type) {
case string:
out.Set(lt, "log", "logtype")
}
}
if out.Search("log", "host").Data() == nil {
host := l.TC.GetContext("host")
if host == nil {
host, err = tc.GetVariable("system.Hostname")
if err != nil {
return nil, err
}
}
switch host.(type) {
case string:
out.Set(host, "log", "host")
default:
return nil, errors.New("host attribute for Event is not a string")
}
}
res := new(tc.TCJson)
res.J = out
return res, nil
}
func init() {
tc.SetFunction("Log", BUNDTelemetryLog)
}