Skip to content

Commit

Permalink
feat(server): add bandwidth stats to the server metrics (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens authored Feb 12, 2025
1 parent dbca08f commit 76a23c6
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 62 deletions.
172 changes: 134 additions & 38 deletions src/shadowbox/server/manager_metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ describe('PrometheusManagerMetrics', () => {
const managerMetrics = new PrometheusManagerMetrics(
new QueryMapPrometheusClient(
{
'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[300s]))': {
resultType: 'vector',
result: [
{
metric: {
location: 'US',
asn: '49490',
asorg: 'Test AS Org',
},
value: [1739284734, '1234'],
},
],
},
'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[0s])) by (location, asn, asorg)':
{
resultType: 'vector',
Expand Down Expand Up @@ -93,6 +106,22 @@ describe('PrometheusManagerMetrics', () => {
},
},
{
'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[300s]))': {
resultType: 'matrix',
result: [
{
metric: {
location: 'US',
asn: '49490',
asorg: 'Test AS Org',
},
values: [
[1738959398, '5678'],
[1739284734, '1234'],
],
},
],
},
'sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[300s])) by (access_key)': {
resultType: 'matrix',
result: [
Expand All @@ -102,7 +131,7 @@ describe('PrometheusManagerMetrics', () => {
},
values: [
[1738959398, '1000'],
[1738959398, '2000'],
[1739284734, '2000'],
],
},
],
Expand All @@ -116,7 +145,7 @@ describe('PrometheusManagerMetrics', () => {
},
values: [
[1738959398, '1000'],
[1738959398, '0'],
[1739284734, '0'],
],
},
],
Expand All @@ -128,19 +157,38 @@ describe('PrometheusManagerMetrics', () => {
const serverMetrics = await managerMetrics.getServerMetrics({seconds: 0});

expect(JSON.stringify(serverMetrics, null, 2)).toEqual(`{
"server": [
{
"location": "US",
"asn": 49490,
"asOrg": "Test AS Org",
"dataTransferred": {
"server": {
"tunnelTime": {
"seconds": 1000
},
"dataTransferred": {
"total": {
"bytes": 1000
},
"tunnelTime": {
"seconds": 1000
"current": {
"bytes": 1234
},
"peak": {
"data": {
"bytes": 5678
},
"timestamp": 1738959398
}
}
],
},
"locations": [
{
"location": "US",
"asn": 49490,
"asOrg": "Test AS Org",
"dataTransferred": {
"bytes": 1000
},
"tunnelTime": {
"seconds": 1000
}
}
]
},
"accessKeys": [
{
"accessKeyId": 0,
Expand All @@ -152,9 +200,9 @@ describe('PrometheusManagerMetrics', () => {
},
"connection": {
"lastConnected": 1738959398,
"lastTrafficSeen": 1738959398,
"peakDevices": {
"count": 4,
"lastTrafficSeen": 1739284734,
"peakDeviceCount": {
"data": 4,
"timestamp": 1738959398
}
}
Expand All @@ -168,6 +216,19 @@ describe('PrometheusManagerMetrics', () => {
const managerMetrics = new PrometheusManagerMetrics(
new QueryMapPrometheusClient(
{
'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[300s]))': {
resultType: 'vector',
result: [
{
metric: {
location: 'US',
asn: '49490',
asorg: 'Test AS Org',
},
value: [1739284734, '1234'],
},
],
},
'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[0s])) by (location, asn, asorg)':
{
resultType: 'vector',
Expand Down Expand Up @@ -218,6 +279,22 @@ describe('PrometheusManagerMetrics', () => {
},
},
{
'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[300s]))': {
resultType: 'matrix',
result: [
{
metric: {
location: 'US',
asn: '49490',
asorg: 'Test AS Org',
},
values: [
[1738959398, '5678'],
[1739284734, '1234'],
],
},
],
},
'sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[300s])) by (access_key)': {
resultType: 'matrix',
result: [
Expand Down Expand Up @@ -253,30 +330,49 @@ describe('PrometheusManagerMetrics', () => {
const serverMetrics = await managerMetrics.getServerMetrics({seconds: 0});

expect(JSON.stringify(serverMetrics, null, 2)).toEqual(`{
"server": [
{
"location": "CA",
"asn": null,
"asOrg": null,
"dataTransferred": {
"bytes": 0
"server": {
"tunnelTime": {
"seconds": 1000
},
"dataTransferred": {
"total": {
"bytes": 1000
},
"tunnelTime": {
"seconds": 1000
"current": {
"bytes": 1234
},
"peak": {
"data": {
"bytes": 5678
},
"timestamp": 1738959398
}
},
{
"location": "US",
"asn": 49490,
"asOrg": "Test AS Org",
"dataTransferred": {
"bytes": 1000
"locations": [
{
"location": "CA",
"asn": null,
"asOrg": null,
"dataTransferred": {
"bytes": 0
},
"tunnelTime": {
"seconds": 1000
}
},
"tunnelTime": {
"seconds": 0
{
"location": "US",
"asn": 49490,
"asOrg": "Test AS Org",
"dataTransferred": {
"bytes": 1000
},
"tunnelTime": {
"seconds": 0
}
}
}
],
]
},
"accessKeys": [
{
"accessKeyId": 1,
Expand All @@ -289,8 +385,8 @@ describe('PrometheusManagerMetrics', () => {
"connection": {
"lastConnected": null,
"lastTrafficSeen": null,
"peakDevices": {
"count": 0,
"peakDeviceCount": {
"data": 0,
"timestamp": null
}
}
Expand All @@ -306,8 +402,8 @@ describe('PrometheusManagerMetrics', () => {
"connection": {
"lastConnected": 1738959398,
"lastTrafficSeen": 1738959398,
"peakDevices": {
"count": 4,
"peakDeviceCount": {
"data": 4,
"timestamp": 1738959398
}
}
Expand Down
Loading

0 comments on commit 76a23c6

Please sign in to comment.