Skip to content

upstream connects counter inaccurate #13

@jesson1

Description

@jesson1

hi
"nginx_sts_upstream_connects_total", I rely on this indicator to count the cps of nginx. After testing, it is found that this counter does not increase by 1 when the tcp handshake succeeds, but only after the tcp wave ends. Can you change it? I don't think it makes sense to increase this counter after the connection is disconnected, because the TCP connection usually lasts for a long time.

test code:
tcp_server.py:

import socket
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 4444))

s.listen(5)
print('Waiting for connection...')

def tcplink(sock, addr):
    print('Accept new connection from %s:%s...' % addr)
    while True:
        data = sock.recv(1024)
        if not data or data.decode('utf-8') == 'exit':
            break
        sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
    sock.close()
    print('Connection from %s:%s closed.' % addr)

while True:
    sock, addr = s.accept()
    t = threading.Thread(target=tcplink, args=(sock, addr))
    t.start()

nginx conf:

daemon off;
worker_processes 2;
error_log /opt/openresty/logs/error.log error;
pid /opt/openresty/logs/nginx.pid;

events {
	worker_connections 50000;
}

http {
	server_tokens off;
	stream_server_traffic_status_zone;
	vhost_traffic_status_zone shared:vhost_traffic_status:32m;

	server {
		listen [::]:9103;
		listen 9103;

		location /metrics/4 {
			allow all;
			stream_server_traffic_status_display;
			stream_server_traffic_status_display_format prometheus;
		}

		location /metrics/7 {
			allow all;
			vhost_traffic_status_display;
			vhost_traffic_status_display_format prometheus;
		}
	}

}
stream {
	server_traffic_status_zone;
	upstream vs-qb4x1fv1vvxrsw {
		server 10.0.1.2:4444	weight=1 max_fails=1 fail_timeout=10s;
	}

	server {
		listen 5001 so_keepalive=60s::;
		proxy_connect_timeout 60s;	# dail timeout
		proxy_timeout 60s;
		proxy_pass vs-qb4x1fv1vvxrsw;	# backend : vsID
	}


}

tcp_client.py:

import socket
import sys
import time

ip = sys.argv[1]
port = int(sys.argv[2])

socket_l = []

for i in range(1000):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))
    socket_l.append(s)
    s.send(b"data")
    print(i, s.recv(1024).decode('utf-8'))
    time.sleep(1)

for s in socket_l:
    s.close()

the number of connections cannot be counted in the beginning of a period of time:
image

connections can be seen in 60 seconds
image
This is because I set proxy_ timeout 60s;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions