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

feat: Make net_(tcp|udp) read limit configurable #622

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions fs_statfs_notype.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package procfs

// isRealProc returns true on architectures that don't have a Type argument
// in their Statfs_t struct
func isRealProc(mountPoint string) (bool, error) {
// in their Statfs_t struct.
func isRealProc(_ string) (bool, error) {
return true, nil
}
19 changes: 19 additions & 0 deletions internal/util/ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

// PtrTo returns a pointer to the given value.
func PtrTo[T any](v T) *T {
return &v
}
21 changes: 14 additions & 7 deletions net_ip_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ import (
"bufio"
"encoding/hex"
"fmt"
"github.com/prometheus/procfs/internal/util"
"io"
"net"
"os"
"strconv"
"strings"
)

const (
// readLimit is used by io.LimitReader while reading the content of the
var (
// defaultReadLimitPtr is used by io.LimitReader while reading the content of the
// /proc/net/udp{,6} files. The number of lines inside such a file is dynamic
// as each line represents a single used socket.
// In theory, the number of available sockets is 65535 (2^16 - 1) per IP.
// With e.g. 150 Byte per line and the maximum number of 65535,
// the reader needs to handle 150 Byte * 65535 =~ 10 MB for a single IP.
readLimit = 4294967296 // Byte -> 4 GiB
defaultReadLimit = util.PtrTo(4294967296) // Byte -> 4 GiB
)

// This contains generic data structures for both udp and tcp sockets.
Expand Down Expand Up @@ -73,7 +74,7 @@ type (
}
)

func newNetIPSocket(file string) (NetIPSocket, error) {
func newNetIPSocket(file string, readLimit *int) (NetIPSocket, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
Expand All @@ -83,7 +84,10 @@ func newNetIPSocket(file string) (NetIPSocket, error) {
var netIPSocket NetIPSocket
isUDP := strings.Contains(file, "udp")

lr := io.LimitReader(f, readLimit)
if readLimit == nil {
readLimit = defaultReadLimit
}
lr := io.LimitReader(f, int64(*readLimit))
s := bufio.NewScanner(lr)
s.Scan() // skip first line with headers
for s.Scan() {
Expand All @@ -101,7 +105,7 @@ func newNetIPSocket(file string) (NetIPSocket, error) {
}

// newNetIPSocketSummary creates a new NetIPSocket{,6} from the contents of the given file.
func newNetIPSocketSummary(file string) (*NetIPSocketSummary, error) {
func newNetIPSocketSummary(file string, readLimit *int) (*NetIPSocketSummary, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
Expand All @@ -112,7 +116,10 @@ func newNetIPSocketSummary(file string) (*NetIPSocketSummary, error) {
var udpPacketDrops uint64
isUDP := strings.Contains(file, "udp")

lr := io.LimitReader(f, readLimit)
if readLimit == nil {
readLimit = defaultReadLimit
}
lr := io.LimitReader(f, int64(*readLimit))
s := bufio.NewScanner(lr)
s.Scan() // skip first line with headers
for s.Scan() {
Expand Down
24 changes: 12 additions & 12 deletions net_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ type (

// NetTCP returns the IPv4 kernel/networking statistics for TCP datagrams
// read from /proc/net/tcp.
func (fs FS) NetTCP() (NetTCP, error) {
return newNetTCP(fs.proc.Path("net/tcp"))
func (fs FS) NetTCP(readLimit *int) (NetTCP, error) {
return newNetTCP(fs.proc.Path("net/tcp"), readLimit)
}

// NetTCP6 returns the IPv6 kernel/networking statistics for TCP datagrams
// read from /proc/net/tcp6.
func (fs FS) NetTCP6() (NetTCP, error) {
return newNetTCP(fs.proc.Path("net/tcp6"))
func (fs FS) NetTCP6(readLimit *int) (NetTCP, error) {
return newNetTCP(fs.proc.Path("net/tcp6"), readLimit)
}

// NetTCPSummary returns already computed statistics like the total queue lengths
// for TCP datagrams read from /proc/net/tcp.
func (fs FS) NetTCPSummary() (*NetTCPSummary, error) {
return newNetTCPSummary(fs.proc.Path("net/tcp"))
func (fs FS) NetTCPSummary(readLimit *int) (*NetTCPSummary, error) {
return newNetTCPSummary(fs.proc.Path("net/tcp"), readLimit)
}

// NetTCP6Summary returns already computed statistics like the total queue lengths
// for TCP datagrams read from /proc/net/tcp6.
func (fs FS) NetTCP6Summary() (*NetTCPSummary, error) {
return newNetTCPSummary(fs.proc.Path("net/tcp6"))
func (fs FS) NetTCP6Summary(readLimit *int) (*NetTCPSummary, error) {
return newNetTCPSummary(fs.proc.Path("net/tcp6"), readLimit)
}

// newNetTCP creates a new NetTCP{,6} from the contents of the given file.
func newNetTCP(file string) (NetTCP, error) {
n, err := newNetIPSocket(file)
func newNetTCP(file string, readLimit *int) (NetTCP, error) {
n, err := newNetIPSocket(file, readLimit)
n1 := NetTCP(n)
return n1, err
}

func newNetTCPSummary(file string) (*NetTCPSummary, error) {
n, err := newNetIPSocketSummary(file)
func newNetTCPSummary(file string, readLimit *int) (*NetTCPSummary, error) {
n, err := newNetIPSocketSummary(file, readLimit)
if n == nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions net_tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_newNetTCP(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newNetTCP(tt.file)
got, err := newNetTCP(tt.file, nil)
if (err != nil) != tt.wantErr {
t.Errorf("newNetTCP() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -161,7 +161,7 @@ func Test_newNetTCPSummary(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newNetTCPSummary(tt.file)
got, err := newNetTCPSummary(tt.file, nil)
if (err != nil) != tt.wantErr {
t.Errorf("newNetTCPSummary() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
4 changes: 2 additions & 2 deletions net_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (fs FS) NetUDP6Summary() (*NetUDPSummary, error) {

// newNetUDP creates a new NetUDP{,6} from the contents of the given file.
func newNetUDP(file string) (NetUDP, error) {
n, err := newNetIPSocket(file)
n, err := newNetIPSocket(file, nil)
n1 := NetUDP(n)
return n1, err
}

func newNetUDPSummary(file string) (*NetUDPSummary, error) {
n, err := newNetIPSocketSummary(file)
n, err := newNetIPSocketSummary(file, nil)
if n == nil {
return nil, err
}
Expand Down
Loading