Skip to content

Commit 844aadd

Browse files
authored
Merge pull request #39 from reorx/metrics-only
Metrics only
2 parents dd88e9f + 0e3e3a3 commit 844aadd

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

README.md

+33-45
Original file line numberDiff line numberDiff line change
@@ -46,68 +46,56 @@ httpstat httpbin.org/post -X POST --data-urlencode "a=b" -v
4646
`httpstat` has a bunch of environment variables to control its behavior.
4747
Here are some usage demos, you can also run `httpstat --help` to see full explanation.
4848

49-
<details>
50-
<summary><strong><code>HTTPSTAT_SHOW_BODY</code></strong></summary>
49+
- <strong><code>HTTPSTAT_SHOW_BODY</code></strong>
5150

52-
Set to `true` to show response body in the output, note that body length
53-
is limited to 1023 bytes, will be truncated if exceeds. Default is `false`.
54-
</details>
51+
Set to `true` to show response body in the output, note that body length
52+
is limited to 1023 bytes, will be truncated if exceeds. Default is `false`.
5553

54+
- <strong><code>HTTPSTAT_SHOW_IP</code></strong>
5655

57-
<details>
58-
<summary><strong><code>HTTPSTAT_SHOW_IP</code></strong></summary>
56+
By default httpstat shows remote and local IP/port address.
57+
Set to `false` to disable this feature. Default is `true`.
5958

60-
By default httpstat shows remote and local IP/port address.
61-
Set to `false` to disable this feature. Default is `true`.
62-
</details>
59+
- <strong><code>HTTPSTAT_SHOW_SPEED</code></strong>
6360

61+
Set to `true` to show download and upload speed. Default is `false`.
6462

65-
<details>
66-
<summary><strong><code>HTTPSTAT_SHOW_SPEED</code></strong></summary>
63+
```bash
64+
HTTPSTAT_SHOW_SPEED=true httpstat http://cachefly.cachefly.net/10mb.test
65+
66+
...
67+
speed_download: 3193.3 KiB/s, speed_upload: 0.0 KiB/s
68+
```
6769

68-
Set to `true` to show download and upload speed. Default is `false`.
70+
- <strong><code>HTTPSTAT_SAVE_BODY</code></strong>
6971

70-
```bash
71-
HTTPSTAT_SHOW_SPEED=true httpstat http://cachefly.cachefly.net/10mb.test
72-
73-
...
74-
speed_download: 3193.3 KiB/s, speed_upload: 0.0 KiB/s
75-
```
76-
</details>
77-
78-
79-
<details>
80-
<summary><strong><code>HTTPSTAT_SAVE_BODY</code></strong></summary>
72+
By default httpstat stores body in a tmp file,
73+
set to `false` to disable this feature. Default is `true`
8174

82-
By default httpstat stores body in a tmp file,
83-
set to `false` to disable this feature. Default is `true`
84-
</details>
75+
- <strong><code>HTTPSTAT_CURL_BIN</code></strong>
8576

77+
Indicate the cURL bin path to use. Default is `curl` from current shell $PATH.
8678

87-
<details>
88-
<summary><strong><code>HTTPSTAT_CURL_BIN</code></strong></summary>
79+
This exampe uses brew installed cURL to make HTTP2 request:
8980

90-
Indicate the cURL bin path to use. Default is `curl` from current shell $PATH.
81+
```bash
82+
HTTPSTAT_CURL_BIN=/usr/local/Cellar/curl/7.50.3/bin/curl httpstat https://http2.akamai.com/ --http2
83+
84+
HTTP/2 200
85+
...
86+
```
9187

92-
This exampe uses brew installed cURL to make HTTP2 request:
93-
94-
```bash
95-
HTTPSTAT_CURL_BIN=/usr/local/Cellar/curl/7.50.3/bin/curl httpstat https://http2.akamai.com/ --http2
96-
97-
HTTP/2 200
98-
...
99-
```
88+
> cURL must be compiled with nghttp2 to enable http2 feature
89+
> ([#12](https://github.com/reorx/httpstat/issues/12)).
10090
101-
> cURL must be compiled with nghttp2 to enable http2 feature
102-
> ([#12](https://github.com/reorx/httpstat/issues/12)).
103-
</details>
91+
- <strong><code>HTTPSTAT_METRICS_ONLY</code></strong>
10492

93+
If set to `true`, httpstat will only output metrics in json format,
94+
this is useful if you want to parse the data instead of reading it.
10595

106-
<details>
107-
<summary><strong><code>HTTPSTAT_DEBUG</code></strong></summary>
96+
- <strong><code>HTTPSTAT_DEBUG</code></strong>
10897

109-
Set to `true` to see debugging logs. Default is `false`
110-
</details>
98+
Set to `true` to see debugging logs. Default is `false`
11199

112100

113101
For convenience, you can export these environments in your `.zshrc` or `.bashrc`,

httpstat.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import subprocess
1717

1818

19-
__version__ = '1.2.1'
19+
__version__ = '1.3.0'
2020

2121

2222
PY3 = sys.version_info >= (3,)
@@ -43,6 +43,7 @@ def get(self, default=None):
4343
ENV_SHOW_SPEED = Env('{prefix}_SHOW_SPEED')
4444
ENV_SAVE_BODY = Env('{prefix}_SAVE_BODY')
4545
ENV_CURL_BIN = Env('{prefix}_CURL_BIN')
46+
ENV_METRICS_ONLY = Env('{prefix}_METRICS_ONLY')
4647
ENV_DEBUG = Env('{prefix}_DEBUG')
4748

4849

@@ -160,6 +161,7 @@ def main():
160161
show_speed = 'true'in ENV_SHOW_SPEED.get('false').lower()
161162
save_body = 'true' in ENV_SAVE_BODY.get('true').lower()
162163
curl_bin = ENV_CURL_BIN.get('curl')
164+
metrics_only = 'true' in ENV_METRICS_ONLY.get('false').lower()
163165
is_debug = 'true' in ENV_DEBUG.get('false').lower()
164166

165167
# configure logging
@@ -243,6 +245,8 @@ def main():
243245
print(yellow('Could not decode json: {}'.format(e)))
244246
print('curl result:', p.returncode, grayscale[16](out), grayscale[16](err))
245247
quit(None, 1)
248+
249+
# convert time_ metrics from seconds to milliseconds
246250
for k in d:
247251
if k.startswith('time_'):
248252
d[k] = int(d[k] * 1000)
@@ -256,6 +260,11 @@ def main():
256260
range_transfer=d['time_total'] - d['time_starttransfer'],
257261
)
258262

263+
# print json if metrics_only is enabled
264+
if metrics_only:
265+
print(json.dumps(d, indent=2))
266+
quit(None, 0)
267+
259268
# ip
260269
if show_ip:
261270
s = 'Connected to {}:{} from {}:{}'.format(

0 commit comments

Comments
 (0)