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

upstream connects counter inaccurate #13

Open
jesson1 opened this issue Sep 26, 2022 · 0 comments
Open

upstream connects counter inaccurate #13

jesson1 opened this issue Sep 26, 2022 · 0 comments

Comments

@jesson1
Copy link

jesson1 commented Sep 26, 2022

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant