/
namer.go
132 lines (122 loc) · 3.61 KB
/
namer.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
131
132
package trace
import (
"bytes"
"strings"
flowV1 "github.com/cilium/cilium/api/v1/flow"
hubbleObserver "github.com/cilium/cilium/api/v1/observer"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
hubblePrinter "github.com/cilium/hubble/pkg/printer"
)
func (c *FlowConverter) getSpanName(f *flowV1.Flow) string {
eventType := f.GetEventType()
namer, ok := spanNamers[eventType.GetType()]
if ok {
return namer(f, uint8(eventType.GetSubType()))
}
return "internal Cilium event: " + eventType.String()
}
var spanNamers = map[int32]func(*flowV1.Flow, uint8) string{
monitorAPI.MessageTypeTrace: func(f *flowV1.Flow, eventSubType uint8) string {
observationPoint := "[" + monitorAPI.TraceObservationPoint(eventSubType) + "]"
if l4 := f.GetL4(); l4 != nil {
switch l4.GetProtocol().(type) {
case *flowV1.Layer4_ICMPv4:
return "ICMPv4 " + observationPoint
case *flowV1.Layer4_ICMPv6:
return "ICMPv6 " + observationPoint
case *flowV1.Layer4_TCP:
flags := l4.GetTCP().Flags
fl := []string{}
if flags.ACK {
fl = append(fl, "ACK")
}
if flags.CWR {
fl = append(fl, "CWR")
}
if flags.ECE {
fl = append(fl, "ECE")
}
if flags.FIN {
fl = append(fl, "FIN")
}
if flags.NS {
fl = append(fl, "NS")
}
if flags.PSH {
fl = append(fl, "PSH")
}
if flags.RST {
fl = append(fl, "RST")
}
if flags.SYN {
fl = append(fl, "SYN")
}
if flags.URG {
fl = append(fl, "URG")
}
return "TCP (flags: " + strings.Join(fl, ", ") + ") " + observationPoint
case *flowV1.Layer4_UDP:
return "UDP " + observationPoint
default:
return "Cilium L4 event " + observationPoint
}
}
return "unknown Cilium trace event"
},
monitorAPI.MessageTypeAccessLog: func(f *flowV1.Flow, _ uint8) string {
if l7 := f.GetL7(); l7 != nil {
t := strings.ToLower(l7.Type.String())
switch l7.GetRecord().(type) {
case *flowV1.Layer7_Http:
return "HTTP " + l7.GetHttp().Method + " (" + t + ")"
case *flowV1.Layer7_Dns:
return "DNS " + t + " (query types: " + strings.Join(l7.GetDns().Qtypes, " ") + ")"
case *flowV1.Layer7_Kafka:
return "Kafka" + t
default:
return "Cilium L7 event (type: " + t + ")"
}
}
return "unknown Cilium access log event"
},
monitorAPI.MessageTypeDrop: func(_ *flowV1.Flow, eventSubType uint8) string {
return "Cilium drop event (reason: " + monitorAPI.DropReason(eventSubType) + ")"
},
monitorAPI.MessageTypePolicyVerdict: func(f *flowV1.Flow, _ uint8) string {
verdict := f.GetVerdict()
name := "Cilium policy verdict: "
switch verdict {
case flowV1.Verdict_VERDICT_UNKNOWN:
return name + "unknown"
case flowV1.Verdict_FORWARDED:
return name + "forwarded (match type: " + monitorAPI.PolicyMatchType(f.GetPolicyMatchType()).String() + ")"
case flowV1.Verdict_DROPPED:
return name + "dropped (reason: " + monitorAPI.DropReason(uint8(f.GetDropReason())) + ")"
case flowV1.Verdict_ERROR:
return name + "error"
case flowV1.Verdict_AUDIT:
return name + "audit"
case flowV1.Verdict_REDIRECTED:
return name + "redirected"
}
return "unknown Cilium policy verdict event"
},
}
func (c *FlowConverter) getSpanDesc(hubbleResp *hubbleObserver.GetFlowsResponse) (string, error) {
b := bytes.NewBuffer([]byte{})
p := hubblePrinter.New(
hubblePrinter.Writer(b),
hubblePrinter.Compact(),
hubblePrinter.WithTimeFormat(""),
hubblePrinter.WithColor("never"),
hubblePrinter.IgnoreStderr(),
)
if err := p.WriteProtoFlow(hubbleResp); err != nil {
return "", err
}
s := strings.TrimSuffix(
strings.TrimPrefix(b.String(), ": "),
"\n",
)
return s, nil
}