This repository has been archived by the owner on Jun 24, 2019. It is now read-only.
forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.go
130 lines (103 loc) · 2.55 KB
/
pub.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cassandra
import (
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/packetbeat/protos"
)
// Transaction Publisher.
type transPub struct {
sendRequest bool
sendResponse bool
sendRequestHeader bool
sendResponseHeader bool
ignoredOps string
results protos.Reporter
}
func (pub *transPub) onTransaction(requ, resp *message) error {
if pub.results == nil {
return nil
}
event := pub.createEvent(requ, resp)
if event.Fields != nil {
pub.results(event)
}
return nil
}
func (pub *transPub) createEvent(requ, resp *message) beat.Event {
status := common.OK_STATUS
if resp.failed {
status = common.ERROR_STATUS
}
//ignore
if (resp != nil && resp.ignored) || (requ != nil && requ.ignored) {
return beat.Event{}
}
var timestamp time.Time
fields := common.MapStr{
"type": "cassandra",
"status": status,
"cassandra": common.MapStr{},
}
//requ can be null, if the message is a PUSHed message
if requ != nil {
// resp_time in milliseconds
responseTime := int32(resp.Ts.Sub(requ.Ts).Nanoseconds() / 1e6)
src := &common.Endpoint{
IP: requ.Tuple.SrcIP.String(),
Port: requ.Tuple.SrcPort,
Proc: string(requ.CmdlineTuple.Src),
}
timestamp = requ.Ts
fields["responsetime"] = responseTime
fields["bytes_in"] = requ.Size
fields["src"] = src
// add processing notes/errors to fields
if len(requ.Notes)+len(resp.Notes) > 0 {
fields["notes"] = append(requ.Notes, resp.Notes...)
}
if pub.sendRequest {
if pub.sendRequestHeader {
if requ.data == nil {
requ.data = map[string]interface{}{}
}
requ.data["headers"] = requ.header
}
if len(requ.data) > 0 {
fields["cassandra"].(common.MapStr)["request"] = requ.data
}
}
dst := &common.Endpoint{
IP: requ.Tuple.DstIP.String(),
Port: requ.Tuple.DstPort,
Proc: string(requ.CmdlineTuple.Dst),
}
fields["dst"] = dst
} else {
//dealing with PUSH message
fields["no_request"] = true
timestamp = resp.Ts
dst := &common.Endpoint{
IP: resp.Tuple.DstIP.String(),
Port: resp.Tuple.DstPort,
Proc: string(resp.CmdlineTuple.Dst),
}
fields["dst"] = dst
}
fields["bytes_out"] = resp.Size
if pub.sendResponse {
if pub.sendResponseHeader {
if resp.data == nil {
resp.data = map[string]interface{}{}
}
resp.data["headers"] = resp.header
}
if len(resp.data) > 0 {
fields["cassandra"].(common.MapStr)["response"] = resp.data
}
}
return beat.Event{
Timestamp: timestamp,
Fields: fields,
}
}