Skip to content

Commit

Permalink
Merge pull request #39 from reorx/metrics-only
Browse files Browse the repository at this point in the history
Metrics only
  • Loading branch information
reorx committed Oct 15, 2020
2 parents dd88e9f + 0e3e3a3 commit 844aadd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
78 changes: 33 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,68 +46,56 @@ httpstat httpbin.org/post -X POST --data-urlencode "a=b" -v
`httpstat` has a bunch of environment variables to control its behavior.
Here are some usage demos, you can also run `httpstat --help` to see full explanation.

<details>
<summary><strong><code>HTTPSTAT_SHOW_BODY</code></strong></summary>
- <strong><code>HTTPSTAT_SHOW_BODY</code></strong>

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

- <strong><code>HTTPSTAT_SHOW_IP</code></strong>

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

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

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

<details>
<summary><strong><code>HTTPSTAT_SHOW_SPEED</code></strong></summary>
```bash
HTTPSTAT_SHOW_SPEED=true httpstat http://cachefly.cachefly.net/10mb.test

...
speed_download: 3193.3 KiB/s, speed_upload: 0.0 KiB/s
```

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

```bash
HTTPSTAT_SHOW_SPEED=true httpstat http://cachefly.cachefly.net/10mb.test

...
speed_download: 3193.3 KiB/s, speed_upload: 0.0 KiB/s
```
</details>


<details>
<summary><strong><code>HTTPSTAT_SAVE_BODY</code></strong></summary>
By default httpstat stores body in a tmp file,
set to `false` to disable this feature. Default is `true`

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

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

<details>
<summary><strong><code>HTTPSTAT_CURL_BIN</code></strong></summary>
This exampe uses brew installed cURL to make HTTP2 request:

Indicate the cURL bin path to use. Default is `curl` from current shell $PATH.
```bash
HTTPSTAT_CURL_BIN=/usr/local/Cellar/curl/7.50.3/bin/curl httpstat https://http2.akamai.com/ --http2

HTTP/2 200
...
```

This exampe uses brew installed cURL to make HTTP2 request:

```bash
HTTPSTAT_CURL_BIN=/usr/local/Cellar/curl/7.50.3/bin/curl httpstat https://http2.akamai.com/ --http2

HTTP/2 200
...
```
> cURL must be compiled with nghttp2 to enable http2 feature
> ([#12](https://github.com/reorx/httpstat/issues/12)).
> cURL must be compiled with nghttp2 to enable http2 feature
> ([#12](https://github.com/reorx/httpstat/issues/12)).
</details>
- <strong><code>HTTPSTAT_METRICS_ONLY</code></strong>

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

<details>
<summary><strong><code>HTTPSTAT_DEBUG</code></strong></summary>
- <strong><code>HTTPSTAT_DEBUG</code></strong>

Set to `true` to see debugging logs. Default is `false`
</details>
Set to `true` to see debugging logs. Default is `false`


For convenience, you can export these environments in your `.zshrc` or `.bashrc`,
Expand Down
11 changes: 10 additions & 1 deletion httpstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import subprocess


__version__ = '1.2.1'
__version__ = '1.3.0'


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


Expand Down Expand Up @@ -160,6 +161,7 @@ def main():
show_speed = 'true'in ENV_SHOW_SPEED.get('false').lower()
save_body = 'true' in ENV_SAVE_BODY.get('true').lower()
curl_bin = ENV_CURL_BIN.get('curl')
metrics_only = 'true' in ENV_METRICS_ONLY.get('false').lower()
is_debug = 'true' in ENV_DEBUG.get('false').lower()

# configure logging
Expand Down Expand Up @@ -243,6 +245,8 @@ def main():
print(yellow('Could not decode json: {}'.format(e)))
print('curl result:', p.returncode, grayscale[16](out), grayscale[16](err))
quit(None, 1)

# convert time_ metrics from seconds to milliseconds
for k in d:
if k.startswith('time_'):
d[k] = int(d[k] * 1000)
Expand All @@ -256,6 +260,11 @@ def main():
range_transfer=d['time_total'] - d['time_starttransfer'],
)

# print json if metrics_only is enabled
if metrics_only:
print(json.dumps(d, indent=2))
quit(None, 0)

# ip
if show_ip:
s = 'Connected to {}:{} from {}:{}'.format(
Expand Down

0 comments on commit 844aadd

Please sign in to comment.