Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the field PID to the TCP connect metrics #235

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions collector/analyzer/tcpconnectanalyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (a *TcpConnectAnalyzer) generateLabels(connectStats *internal.ConnectionSta
labels := model.NewAttributeMap()
// The connect events always come from the client-side
labels.AddBoolValue(constlabels.IsServer, false)
labels.AddIntValue(constlabels.Pid, int64(connectStats.Pid))
labels.AddStringValue(constlabels.ContainerId, connectStats.ContainerId)
labels.AddIntValue(constlabels.Errno, int64(connectStats.Code))
if connectStats.StateMachine.GetCurrentState() == internal.Success {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *ConnectMonitor) ReadInConnectExitSyscall(event *model.KindlingEvent) (*
}
// "connect_exit" comes to analyzer after "tcp_connect"
connStats.EndTimestamp = event.Timestamp
connStats.pid = event.GetPid()
connStats.Pid = event.GetPid()
connStats.ContainerId = event.GetContainerId()
var eventType EventType
if retValueInt == 0 {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *ConnectMonitor) ReadSendRequestSyscall(event *model.KindlingEvent) (*Co
if !ok {
return nil, nil
}
connStats.pid = event.GetPid()
connStats.Pid = event.GetPid()
connStats.ContainerId = event.GetContainerId()
return connStats.StateMachine.ReceiveEvent(sendRequestSyscall, c.connMap)
}
Expand Down Expand Up @@ -212,24 +212,24 @@ func (c *ConnectMonitor) readInTcpSetStateFromEstablished(connKey ConnKey, event

func (c *ConnectMonitor) TrimConnectionsWithTcpStat(waitForEventSecond int) []*ConnectionStats {
ret := make([]*ConnectionStats, 0, len(c.connMap))
// Only scan once for each pid
// Only scan once for each Pid
pidTcpStateMap := make(map[uint32]NetSocketStateMap)
waitForEventNano := int64(waitForEventSecond) * 1000000000
timeNow := time.Now().UnixNano()
for key, connStat := range c.connMap {
if connStat.pid == 0 {
if connStat.Pid == 0 {
continue
}
if timeNow-int64(connStat.InitialTimestamp) < waitForEventNano {
// Still waiting for other events
continue
}
tcpStateMap, ok := pidTcpStateMap[connStat.pid]
tcpStateMap, ok := pidTcpStateMap[connStat.Pid]
if !ok {
tcpState, err := NewPidTcpStat(c.hostProcPath, int(connStat.pid))
tcpState, err := NewPidTcpStat(c.hostProcPath, int(connStat.Pid))
if err != nil {
c.logger.Debug("error happened when scanning net/tcp",
zap.Uint32("pid", connStat.pid), zap.Error(err))
zap.Uint32("Pid", connStat.Pid), zap.Error(err))
// No such file or directory, which means the process has been purged.
// We consider the connection failed to be established.
stats, err := connStat.StateMachine.ReceiveEvent(expiredEvent, c.connMap)
Expand All @@ -241,7 +241,7 @@ func (c *ConnectMonitor) TrimConnectionsWithTcpStat(waitForEventSecond int) []*C
}
continue
}
pidTcpStateMap[connStat.pid] = tcpState
pidTcpStateMap[connStat.Pid] = tcpState
tcpStateMap = tcpState
}
state, ok := tcpStateMap[key.toSocketKey()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
)

type ConnectionStats struct {
pid uint32
Pid uint32
ContainerId string
ConnKey ConnKey
StateMachine *StateMachine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestCallback(t *testing.T) {
}
statesResource := createStatesResource()
connStats := &ConnectionStats{
pid: 0,
Pid: 0,
ConnKey: connKey,
InitialTimestamp: 0,
EndTimestamp: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func newTcpLabelSelectors() *aggregator.LabelSelectors {

func newTcpConnectLabelSelectors() *aggregator.LabelSelectors {
return aggregator.NewLabelSelectors(
aggregator.LabelSelector{Name: constlabels.Pid, VType: aggregator.IntType},
aggregator.LabelSelector{Name: constlabels.SrcNode, VType: aggregator.StringType},
aggregator.LabelSelector{Name: constlabels.SrcNodeIp, VType: aggregator.StringType},
aggregator.LabelSelector{Name: constlabels.SrcNamespace, VType: aggregator.StringType},
Expand Down
1 change: 1 addition & 0 deletions docs/prometheus_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ We made some rules for considering whether a request is abnormal. For the abnorm
### Labels List
| **Label Name** | **Example** | **Notes** |
| --- | --- | --- |
| `pid` | 1024 | The client's process ID |
| `src_node` | slave-node1 | Which node the source pod is on |
| `src_namespace` | default | Namespace of the source pod |
| `src_workload_kind` | deployment | Workload kind of the source pod |
Expand Down