-
Notifications
You must be signed in to change notification settings - Fork 69
feat(tcp): add praxis_tcp_connections_total counter metric #1037
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
Merged
shaneutt
merged 2 commits into
praxis-proxy:main
from
tsisodia10:feat/tcp-connections-total-counter
Sep 1, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # TCP Connections Total | ||
| # | ||
| # Prometheus counter for total accepted TCP connections per listener. | ||
| # Incremented once per connection, regardless of duration. Scraped | ||
| # via the admin /metrics endpoint. | ||
| # | ||
| # Usage: | ||
| # cargo run -p praxis-proxy -- -c examples/configs/observability/tcp-connections-total.yaml | ||
| # | ||
| # Exercise: | ||
| # for i in 1 2 3; do echo "hi" | nc localhost 5432; done | ||
| # curl -s http://localhost:9901/metrics | grep tcp_connections_total | ||
|
|
||
| admin: | ||
| address: "127.0.0.1:9901" | ||
|
|
||
| insecure_options: | ||
| allow_private_upstreams: true | ||
|
|
||
| listeners: | ||
| - name: postgres | ||
| address: "127.0.0.1:5432" | ||
| protocol: tcp | ||
| upstream: "127.0.0.1:15432" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
tests/integration/tests/suite/examples/tcp_connections_total.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) 2026 Praxis Contributors | ||
|
|
||
| //! Functional integration tests for the tcp-connections-total example | ||
| //! configuration. | ||
|
|
||
| use std::{ | ||
| collections::HashMap, | ||
| io::{Read as _, Write as _}, | ||
| net::TcpStream, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use praxis_test_utils::{free_port, http_get, start_full_proxy, start_tcp_tagged_backend, wait_for_tcp}; | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Tests | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| #[test] | ||
| fn tcp_connections_total_example_emits_counter() { | ||
| let backend_port = start_tcp_tagged_backend("pg"); | ||
| let proxy_port = free_port(); | ||
| let admin_port = free_port(); | ||
| let config = super::load_example_config( | ||
| "observability/tcp-connections-total.yaml", | ||
| proxy_port, | ||
| HashMap::from([ | ||
| ("127.0.0.1:5432", proxy_port), | ||
| ("127.0.0.1:15432", backend_port), | ||
| ("127.0.0.1:9901", admin_port), | ||
| ]), | ||
| ); | ||
|
|
||
| let _proxy = start_full_proxy(&config); | ||
| wait_for_tcp(&format!("127.0.0.1:{proxy_port}")); | ||
| wait_for_tcp(&format!("127.0.0.1:{admin_port}")); | ||
|
|
||
| let mut stream = TcpStream::connect(format!("127.0.0.1:{proxy_port}")).expect("TCP connect"); | ||
| stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); | ||
| stream.set_write_timeout(Some(Duration::from_secs(2))).unwrap(); | ||
| stream.write_all(b"SELECT 1").expect("TCP write"); | ||
| stream.shutdown(std::net::Shutdown::Write).expect("shutdown write"); | ||
|
|
||
| let mut buf = Vec::new(); | ||
| stream.read_to_end(&mut buf).expect("TCP read"); | ||
| drop(stream); | ||
|
|
||
| std::thread::sleep(Duration::from_millis(100)); | ||
|
|
||
| let (status, body) = http_get(&format!("127.0.0.1:{admin_port}"), "/metrics", None); | ||
| assert_eq!(status, 200, "/metrics should return 200"); | ||
| assert!( | ||
| body.contains("praxis_tcp_connections_total"), | ||
| "metrics should contain praxis_tcp_connections_total counter: {body}" | ||
| ); | ||
| assert!( | ||
| body.contains("listener=\"postgres\""), | ||
| "metrics should contain listener=postgres label: {body}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tcp_connections_total_example_forwards_traffic() { | ||
| let backend_port = start_tcp_tagged_backend("dbdata"); | ||
| let proxy_port = free_port(); | ||
| let admin_port = free_port(); | ||
| let config = super::load_example_config( | ||
| "observability/tcp-connections-total.yaml", | ||
| proxy_port, | ||
| HashMap::from([ | ||
| ("127.0.0.1:5432", proxy_port), | ||
| ("127.0.0.1:15432", backend_port), | ||
| ("127.0.0.1:9901", admin_port), | ||
| ]), | ||
| ); | ||
|
|
||
| let _proxy = start_full_proxy(&config); | ||
| wait_for_tcp(&format!("127.0.0.1:{proxy_port}")); | ||
|
|
||
| let mut stream = TcpStream::connect(format!("127.0.0.1:{proxy_port}")).expect("TCP connect"); | ||
| stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); | ||
| stream.set_write_timeout(Some(Duration::from_secs(2))).unwrap(); | ||
| stream.write_all(b"hello").expect("TCP write"); | ||
| stream.shutdown(std::net::Shutdown::Write).expect("shutdown write"); | ||
|
|
||
| let mut buf = Vec::new(); | ||
| stream.read_to_end(&mut buf).expect("TCP read"); | ||
| let resp = String::from_utf8_lossy(&buf); | ||
| assert!( | ||
| resp.contains("dbdata"), | ||
| "tcp-connections-total example should forward to tagged backend, got: {resp}" | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) 2026 Praxis Contributors | ||
|
|
||
| //! Integration tests for TCP connections total counter. | ||
|
|
||
| use std::{ | ||
| io::{Read as _, Write as _}, | ||
| net::TcpStream, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use praxis_core::config::Config; | ||
| use praxis_test_utils::{free_port, http_get, start_full_proxy, start_tcp_tagged_backend, wait_for_tcp}; | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Tests | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| #[test] | ||
| fn tcp_connections_total_increments_on_each_connection() { | ||
| let backend_port = start_tcp_tagged_backend("counter-test"); | ||
| let proxy_port = free_port(); | ||
| let admin_port = free_port(); | ||
|
|
||
| let yaml = format!( | ||
| r#" | ||
| admin: | ||
| address: "127.0.0.1:{admin_port}" | ||
|
|
||
| insecure_options: | ||
| allow_private_upstreams: true | ||
|
|
||
| listeners: | ||
| - name: tcp-counter-test | ||
| address: "127.0.0.1:{proxy_port}" | ||
| protocol: tcp | ||
| upstream: "127.0.0.1:{backend_port}" | ||
| "# | ||
| ); | ||
|
|
||
| let config = Config::from_yaml(&yaml).unwrap(); | ||
| let _proxy = start_full_proxy(&config); | ||
| wait_for_tcp(&format!("127.0.0.1:{proxy_port}")); | ||
| wait_for_tcp(&format!("127.0.0.1:{admin_port}")); | ||
|
|
||
| for i in 0..3 { | ||
| let mut stream = | ||
| TcpStream::connect(format!("127.0.0.1:{proxy_port}")).unwrap_or_else(|e| panic!("connect {i}: {e}")); | ||
| stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); | ||
| stream.set_write_timeout(Some(Duration::from_secs(2))).unwrap(); | ||
| stream.write_all(b"ping").expect("write"); | ||
| stream.shutdown(std::net::Shutdown::Write).expect("shutdown"); | ||
| let mut buf = Vec::new(); | ||
| stream.read_to_end(&mut buf).expect("read"); | ||
| } | ||
|
|
||
| std::thread::sleep(Duration::from_millis(100)); | ||
|
|
||
| let (status, body) = http_get(&format!("127.0.0.1:{admin_port}"), "/metrics", None); | ||
| assert_eq!(status, 200, "/metrics should return 200"); | ||
| assert!( | ||
| body.contains("praxis_tcp_connections_total"), | ||
| "/metrics should contain praxis_tcp_connections_total: {body}" | ||
| ); | ||
| assert!( | ||
| body.contains("listener=\"tcp-counter-test\""), | ||
| "/metrics should contain listener=tcp-counter-test label: {body}" | ||
| ); | ||
|
|
||
| let count_line = body | ||
| .lines() | ||
| .find(|l| l.contains("praxis_tcp_connections_total") && l.contains("tcp-counter-test") && !l.starts_with('#')) | ||
| .expect("should find counter line"); | ||
| let count: f64 = count_line | ||
| .split_whitespace() | ||
| .last() | ||
| .expect("counter should have a value") | ||
| .parse() | ||
| .expect("counter value should parse as f64"); | ||
| assert!( | ||
| count >= 3.0, | ||
| "counter should be at least 3 after 3 connections, got: {count}" | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.