Skip to content

Commit 5d72fa4

Browse files
authored
Refactor the socket detail message in the Access log module (#154)
1 parent 5c6fe8b commit 5d72fa4

File tree

12 files changed

+85
-32
lines changed

12 files changed

+85
-32
lines changed

pkg/accesslog/collector/protocols/http1.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (p *HTTP1Protocol) handleResponse(metrics ProtocolMetrics, b *buffer.Buffer
143143
}
144144

145145
func (p *HTTP1Protocol) handleHTTPData(metrics *HTTP1Metrics, request *reader.Request, response *reader.Response) {
146-
detailEvents := make([]*events.SocketDetailEvent, 0)
146+
detailEvents := make([]events.SocketDetail, 0)
147147
detailEvents = appendSocketDetailsFromBuffer(detailEvents, request.HeaderBuffer())
148148
detailEvents = appendSocketDetailsFromBuffer(detailEvents, request.BodyBuffer())
149149
detailEvents = appendSocketDetailsFromBuffer(detailEvents, response.HeaderBuffer())
@@ -166,8 +166,8 @@ func (p *HTTP1Protocol) handleHTTPData(metrics *HTTP1Metrics, request *reader.Re
166166
forwarder.SendTransferProtocolEvent(p.ctx, detailEvents, &v3.AccessLogProtocolLogs{
167167
Protocol: &v3.AccessLogProtocolLogs_Http{
168168
Http: &v3.AccessLogHTTPProtocol{
169-
StartTime: forwarder.BuildOffsetTimestamp(detailEvents[0].StartTime),
170-
EndTime: forwarder.BuildOffsetTimestamp(detailEvents[len(detailEvents)-1].EndTime),
169+
StartTime: forwarder.BuildOffsetTimestamp(detailEvents[0].GetStartTime()),
170+
EndTime: forwarder.BuildOffsetTimestamp(detailEvents[len(detailEvents)-1].GetEndTime()),
171171
Version: v3.AccessLogHTTPProtocolVersion_HTTP1,
172172
Request: &v3.AccessLogHTTPProtocolRequest{
173173
Method: transformHTTPMethod(originalRequest.Method),

pkg/accesslog/collector/protocols/http2.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (r *HTTP2Protocol) validateIsStreamOpenTooLong(metrics *HTTP2Metrics, id ui
224224
}
225225

226226
func (r *HTTP2Protocol) handleWholeStream(stream *HTTP2Streaming) {
227-
detailEvents := make([]*events.SocketDetailEvent, 0)
227+
detailEvents := make([]events.SocketDetail, 0)
228228
detailEvents = appendSocketDetailsFromBuffer(detailEvents, stream.reqHeaderBuffer)
229229
detailEvents = appendSocketDetailsFromBuffer(detailEvents, stream.reqBodyBuffer)
230230
detailEvents = appendSocketDetailsFromBuffer(detailEvents, stream.respHeaderBuffer)
@@ -239,8 +239,8 @@ func (r *HTTP2Protocol) handleWholeStream(stream *HTTP2Streaming) {
239239
forwarder.SendTransferProtocolEvent(r.ctx, detailEvents, &v3.AccessLogProtocolLogs{
240240
Protocol: &v3.AccessLogProtocolLogs_Http{
241241
Http: &v3.AccessLogHTTPProtocol{
242-
StartTime: forwarder.BuildOffsetTimestamp(r.firstDetail(stream.reqBodyBuffer, detailEvents[0]).StartTime),
243-
EndTime: forwarder.BuildOffsetTimestamp(detailEvents[len(detailEvents)-1].EndTime),
242+
StartTime: forwarder.BuildOffsetTimestamp(r.firstDetail(stream.reqBodyBuffer, detailEvents[0]).GetStartTime()),
243+
EndTime: forwarder.BuildOffsetTimestamp(detailEvents[len(detailEvents)-1].GetEndTime()),
244244
Version: v3.AccessLogHTTPProtocolVersion_HTTP2,
245245
Request: &v3.AccessLogHTTPProtocolRequest{
246246
Method: r.parseHTTPMethod(stream),
@@ -271,11 +271,11 @@ func (r *HTTP2Protocol) parseHTTPMethod(streaming *HTTP2Streaming) v3.AccessLogH
271271
return transformHTTPMethod(strings.ToUpper(method))
272272
}
273273

274-
func (r *HTTP2Protocol) firstDetail(buf *buffer.Buffer, def *events.SocketDetailEvent) *events.SocketDetailEvent {
274+
func (r *HTTP2Protocol) firstDetail(buf *buffer.Buffer, def events.SocketDetail) events.SocketDetail {
275275
if buf == nil || buf.Details() == nil || buf.Details().Len() == 0 {
276276
return def
277277
}
278-
return buf.Details().Front().Value.(*events.SocketDetailEvent)
278+
return buf.Details().Front().Value.(events.SocketDetail)
279279
}
280280

281281
func (r *HTTP2Protocol) bufferSizeOfZero(buf *buffer.Buffer) uint64 {

pkg/accesslog/collector/protocols/protocol.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ type Protocol interface {
5858
Analyze(metrics ProtocolMetrics, buffer *buffer.Buffer, helper *AnalyzeHelper) error
5959
}
6060

61-
func appendSocketDetailsFromBuffer(result []*events.SocketDetailEvent, buf *buffer.Buffer) []*events.SocketDetailEvent {
61+
func appendSocketDetailsFromBuffer(result []events.SocketDetail, buf *buffer.Buffer) []events.SocketDetail {
6262
if buf == nil || buf.DetailLength() == 0 {
6363
return result
6464
}
6565
for e := buf.Details().Front(); e != nil; e = e.Next() {
6666
if len(result) > 0 && result[len(result)-1] == e.Value {
6767
continue
6868
}
69-
result = append(result, e.Value.(*events.SocketDetailEvent))
69+
result = append(result, e.Value.(events.SocketDetail))
7070
}
7171
return result
7272
}

pkg/accesslog/collector/protocols/queue.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type AnalyzeQueue struct {
4848
context *common.AccessLogContext
4949
eventQueue *btf.EventQueue
5050
perCPUBuffer int64
51+
52+
detailSupplier func() events.SocketDetail
5153
}
5254

5355
func NewAnalyzeQueue(ctx *common.AccessLogContext) (*AnalyzeQueue, error) {
@@ -71,14 +73,17 @@ func NewAnalyzeQueue(ctx *common.AccessLogContext) (*AnalyzeQueue, error) {
7173
eventQueue: btf.NewEventQueue(ctx.Config.ProtocolAnalyze.Parallels, ctx.Config.ProtocolAnalyze.QueueSize, func() btf.PartitionContext {
7274
return NewPartitionContext(ctx)
7375
}),
76+
detailSupplier: func() events.SocketDetail {
77+
return &events.SocketDetailEvent{}
78+
},
7479
}, nil
7580
}
7681

7782
func (q *AnalyzeQueue) Start(ctx context.Context) {
7883
q.eventQueue.RegisterReceiver(q.context.BPF.SocketDetailDataQueue, int(q.perCPUBuffer), func() interface{} {
79-
return &events.SocketDetailEvent{}
84+
return q.detailSupplier()
8085
}, func(data interface{}) string {
81-
return fmt.Sprintf("%d", data.(*events.SocketDetailEvent).GetConnectionID())
86+
return fmt.Sprintf("%d", data.(events.SocketDetail).GetConnectionID())
8287
})
8388
q.eventQueue.RegisterReceiver(q.context.BPF.SocketDataUploadEventQueue, int(q.perCPUBuffer), func() interface{} {
8489
return &events.SocketDataUploadEvent{}
@@ -89,6 +94,10 @@ func (q *AnalyzeQueue) Start(ctx context.Context) {
8994
q.eventQueue.Start(ctx, q.context.BPF.Linker)
9095
}
9196

97+
func (q *AnalyzeQueue) ChangeDetailSupplier(supplier func() events.SocketDetail) {
98+
q.detailSupplier = supplier
99+
}
100+
92101
type PartitionContext struct {
93102
context *common.AccessLogContext
94103
protocolMgr *ProtocolManager
@@ -164,18 +173,18 @@ func (p *PartitionContext) Start(ctx context.Context) {
164173

165174
func (p *PartitionContext) Consume(data interface{}) {
166175
switch event := data.(type) {
167-
case *events.SocketDetailEvent:
168-
pid, _ := events.ParseConnectionID(event.ConnectionID)
176+
case events.SocketDetail:
177+
pid, _ := events.ParseConnectionID(event.GetConnectionID())
169178
log.Debugf("receive the socket detail event, connection ID: %d, random ID: %d, pid: %d, data id: %d, "+
170-
"function name: %s, package count: %d, package size: %d, l4 duration: %d, ssl: %d",
171-
event.ConnectionID, event.RandomID, pid, event.DataID0, event.FunctionName,
172-
event.L4PackageCount, event.L4TotalPackageSize, event.L4Duration, event.SSL)
173-
if event.Protocol == enums.ConnectionProtocolUnknown {
179+
"function name: %s, package count: %d, package size: %d, ssl: %d",
180+
event.GetConnectionID(), event.GetRandomID(), pid, event.DataID(), event.GetFunctionName(),
181+
event.GetL4PackageCount(), event.GetL4TotalPackageSize(), event.GetSSL())
182+
if event.GetProtocol() == enums.ConnectionProtocolUnknown {
174183
// if the connection protocol is unknown, we just needs to add this into the kernel log
175184
forwarder.SendTransferNoProtocolEvent(p.context, event)
176185
return
177186
}
178-
connection := p.getConnectionContext(event.GetConnectionID(), event.GetRandomID(), event.Protocol)
187+
connection := p.getConnectionContext(event.GetConnectionID(), event.GetRandomID(), event.GetProtocol())
179188
connection.appendDetail(p.context, event)
180189
case *events.SocketDataUploadEvent:
181190
pid, _ := events.ParseConnectionID(event.ConnectionID)
@@ -303,7 +312,7 @@ type PartitionConnection struct {
303312
lastCheckCloseTime time.Time
304313
}
305314

306-
func (p *PartitionConnection) appendDetail(ctx *common.AccessLogContext, detail *events.SocketDetailEvent) {
315+
func (p *PartitionConnection) appendDetail(ctx *common.AccessLogContext, detail events.SocketDetail) {
307316
if p.skipAllDataAnalyze {
308317
// if the connection is already skip all data analyze, then just send the detail event
309318
forwarder.SendTransferNoProtocolEvent(ctx, detail)

pkg/accesslog/common/connection.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ func (c *ConnectionManager) connectionPostHandle(connection *ConnectionInfo, eve
313313
// if not all processor finished, then add into the map
314314
c.allUnfinishedConnections[fmt.Sprintf("%d_%d", event.GetConnectionID(), event.GetRandomID())] = &e.allProcessorFinished
315315
}
316-
case *events.SocketDetailEvent:
317-
if e.SSL == 1 && connection.RPCConnection.TlsMode == v3.AccessLogConnectionTLSMode_Plain {
316+
case events.SocketDetail:
317+
if e.GetSSL() == 1 && connection.RPCConnection.TlsMode == v3.AccessLogConnectionTLSMode_Plain {
318318
connection.RPCConnection.TlsMode = v3.AccessLogConnectionTLSMode_TLS
319319
}
320-
if e.Protocol != enums.ConnectionProtocolUnknown && connection.RPCConnection.Protocol == v3.AccessLogProtocolType_TCP {
321-
switch e.Protocol {
320+
if e.GetProtocol() != enums.ConnectionProtocolUnknown && connection.RPCConnection.Protocol == v3.AccessLogProtocolType_TCP {
321+
switch e.GetProtocol() {
322322
case enums.ConnectionProtocolHTTP:
323323
connection.RPCConnection.Protocol = v3.AccessLogProtocolType_HTTP_1
324324
case enums.ConnectionProtocolHTTP2:

pkg/accesslog/common/queue.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type KernelLog struct {
3939
}
4040

4141
type ProtocolLog struct {
42-
KernelLogs []*events.SocketDetailEvent
42+
KernelLogs []events.SocketDetail
4343
Protocol *v3.AccessLogProtocolLogs
4444
}
4545

@@ -84,7 +84,7 @@ func (q *Queue) AppendKernelLog(tp LogType, event events.Event) {
8484
q.consumeIfNeed()
8585
}
8686

87-
func (q *Queue) AppendProtocolLog(kernelLogs []*events.SocketDetailEvent, protocol *v3.AccessLogProtocolLogs) {
87+
func (q *Queue) AppendProtocolLog(kernelLogs []events.SocketDetail, protocol *v3.AccessLogProtocolLogs) {
8888
select {
8989
case q.protocolLogs <- &ProtocolLog{
9090
KernelLogs: kernelLogs,

pkg/accesslog/events/detail.go

+44
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,26 @@ package events
2020
import (
2121
"time"
2222

23+
"github.com/apache/skywalking-rover/pkg/tools/buffer"
2324
"github.com/apache/skywalking-rover/pkg/tools/enums"
2425
"github.com/apache/skywalking-rover/pkg/tools/host"
2526
)
2627

28+
type SocketDetail interface {
29+
Event
30+
buffer.SocketDataDetail
31+
32+
GetStartTime() uint64
33+
GetEndTime() uint64
34+
35+
GetL4PackageCount() uint8
36+
GetL4TotalPackageSize() uint64
37+
38+
GetFunctionName() enums.SocketFunctionName
39+
GetProtocol() enums.ConnectionProtocol
40+
GetSSL() uint8
41+
}
42+
2743
type SocketDetailEvent struct {
2844
ConnectionID uint64
2945
RandomID uint64
@@ -71,3 +87,31 @@ func (d *SocketDetailEvent) Timestamp() time.Time {
7187
func (d *SocketDetailEvent) DataID() uint64 {
7288
return d.DataID0
7389
}
90+
91+
func (d *SocketDetailEvent) GetStartTime() uint64 {
92+
return d.StartTime
93+
}
94+
95+
func (d *SocketDetailEvent) GetEndTime() uint64 {
96+
return d.EndTime
97+
}
98+
99+
func (d *SocketDetailEvent) GetL4PackageCount() uint8 {
100+
return d.L4PackageCount
101+
}
102+
103+
func (d *SocketDetailEvent) GetL4TotalPackageSize() uint64 {
104+
return d.L4TotalPackageSize
105+
}
106+
107+
func (d *SocketDetailEvent) GetFunctionName() enums.SocketFunctionName {
108+
return d.FunctionName
109+
}
110+
111+
func (d *SocketDetailEvent) GetProtocol() enums.ConnectionProtocol {
112+
return d.Protocol
113+
}
114+
115+
func (d *SocketDetailEvent) GetSSL() uint8 {
116+
return d.SSL
117+
}

pkg/accesslog/forwarder/close.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func init() {
28-
registerKernelLogBuilder(common.LogTypeClose, closeLogBuilder)
28+
RegisterKernelLogBuilder(common.LogTypeClose, closeLogBuilder)
2929
}
3030

3131
func SendCloseEvent(context *common.AccessLogContext, event *common.CloseEventWithNotify) {

pkg/accesslog/forwarder/connect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
func init() {
30-
registerKernelLogBuilder(common.LogTypeConnect, connectLogBuilder)
30+
RegisterKernelLogBuilder(common.LogTypeConnect, connectLogBuilder)
3131
}
3232

3333
func SendConnectEvent(context *common.AccessLogContext, event *events.SocketConnectEvent, socketPair *ip.SocketPair) {

pkg/accesslog/forwarder/forwarder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type KernelLogBuilder func(data events.Event) *v3.AccessLogKernelLog
2828

2929
var kernelLogBuilders = make([]KernelLogBuilder, 10)
3030

31-
func registerKernelLogBuilder(tp common.LogType, builder KernelLogBuilder) {
31+
func RegisterKernelLogBuilder(tp common.LogType, builder KernelLogBuilder) {
3232
kernelLogBuilders[tp] = builder
3333
}
3434

pkg/accesslog/forwarder/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ import (
2424
v3 "skywalking.apache.org/repo/goapi/collect/ebpf/accesslog/v3"
2525
)
2626

27-
func SendTransferProtocolEvent(context *common.AccessLogContext, kernelLogs []*events.SocketDetailEvent, protocolData *v3.AccessLogProtocolLogs) {
27+
func SendTransferProtocolEvent(context *common.AccessLogContext, kernelLogs []events.SocketDetail, protocolData *v3.AccessLogProtocolLogs) {
2828
context.Queue.AppendProtocolLog(kernelLogs, protocolData)
2929
}

pkg/accesslog/forwarder/transfer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
)
2727

2828
func init() {
29-
registerKernelLogBuilder(common.LogTypeKernelTransfer, kernelTransferLogBuilder)
29+
RegisterKernelLogBuilder(common.LogTypeKernelTransfer, kernelTransferLogBuilder)
3030
}
3131

32-
func SendTransferNoProtocolEvent(context *common.AccessLogContext, event *events.SocketDetailEvent) {
32+
func SendTransferNoProtocolEvent(context *common.AccessLogContext, event events.SocketDetail) {
3333
context.Queue.AppendKernelLog(common.LogTypeKernelTransfer, event)
3434
}
3535

0 commit comments

Comments
 (0)