Skip to content

Commit

Permalink
Merge pull request #26 from unfor19/feature/improve-tests
Browse files Browse the repository at this point in the history
Feature/improve tests
  • Loading branch information
unfor19 committed Feb 12, 2021
2 parents d53c8b4 + ac40a3c commit 9662256
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Commands:
prometheus-apply Alias: pa Applies .metrics.json for a given...
prometheus-get Alias: pg Get total number of data series
prometheus-reload Alias: pr Reload Prometheus
```
```

<!-- available_commands_end -->

Expand Down Expand Up @@ -119,7 +119,7 @@ Commands:
Metrics json path [./.metrics.json]: /home/willywonka/.metrics.json
>> [LOG] Reading documents from docker-compose/prometheus.yml
...
>> [LOG] Done! Now reload docker-compose/prometheus.yml with 'docker exec $PROM_CONTAINER_NAME kill -HUP 1'
>> [LOG] Done! Now reload docker-compose/prometheus.yml with 'frigga pr -u http://localhost:9090'
```

1. As mentioned in the previous step, reload the `prometheus.yml` to Prometheus, here are two ways of doing it
Expand All @@ -129,6 +129,10 @@ Commands:
```
- Send a POST request to `/-/reload` - this requires Prometheus to be loaded with `--web.enable-lifecycle`, for example, see [docker-compose.yml](docker-compose/docker-compose.yml)
```bash
$ frigga prometheus-reload --prom-url http://localhost:9090
```
Or with curl
```
$ curl -X POST http://localhost:9090/-/reload
```
1. Make sure the `prometheus.yml` was loaded successfully to Prometheus
Expand Down Expand Up @@ -163,7 +167,7 @@ Commands:
```bash
$ bash docker-compose/deploy_stack.sh

Creating network frigga_net1
Creating network "frigga_net1" with the default driver
...
>> Grafana - Generating API Key - for Viewer
eyJrIjoiT29hNGxGZjAwT2hZcU1BSmpPRXhndXVwUUE4ZVNFcGQiLCJuIjoibG9jYWwiLCJpZCI6MX0=
Expand All @@ -187,6 +191,14 @@ Commands:
>> [LOG] Found a total of 269 unique metrics to keep
# Generated .metrics.json in pwd
```
1. Check the number of data series **BEFORE** filtering with frigga
```bash
$ frigga pg -u http://localhost:9090

# prometheus-get

>> [LOG] Total number of data-series: 1863
```

1. Apply the rules to `prometheus.yml`, keep the defaults

Expand All @@ -204,14 +216,23 @@ Commands:
1. Reload `prometheus.yml` to Prometheus

```bash
$ bash docker-compose/reload_prom_config.sh show
$ frigga pr -u http://localhost:9090

>> Reloading prometheus.yml configuration file
...
level=info ts=2020-06-27T16:25:17.656Z caller=main.go:827 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml
# prometheus-reload

>> [LOG] Successfully reloaded Prometheus - http://localhost:9090/-/reload
```
1. Check the number of data series **AFTER** filtering with frigga
```bash
$ frigga pg -u http://localhost:9090

# prometheus-get

>> [LOG] Total number of data-series: 898
# Decreased from 1863 to 898, decreased 51% !
```

1. Go to [Jobs Usage](http://localhost:3000/d/U9Se3uZMz/jobs-usage?orgId=1), you'll see that Prometheus is processing only ~1000 DataSeries (previously ~2800)
1. Go to [Jobs Usage](http://localhost:3000/d/U9Se3uZMz/jobs-usage?orgId=1), you'll see that Prometheus is processing only ~898 DataSeries (previously ~1863)
- In case you don't see the change, don't forget to hit the refersh button
1. Cleanup
```bash
Expand Down
9 changes: 6 additions & 3 deletions docker-compose/apply_changes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
set -e
set -o pipefail

PROMETHEUS_HOST="http://localhost:9090"
GRAFANA_API_KEY="$(cat .apikey || true)"

error_msg(){
Expand All @@ -13,7 +14,7 @@ error_msg(){

[[ -z "${GRAFANA_API_KEY}" ]] && error_msg ".apikey file is empty"

num_series_before=$(frigga pg -u "http://localhost:9090" -r)
num_series_before=$(frigga pg -u "$PROMETHEUS_HOST" -r)

# Generate .metrics.json
frigga grafana-list \
Expand All @@ -26,13 +27,15 @@ frigga prometheus-apply \
--metrics-json-path .metrics.json

# Reload prometheus configuration
source docker-compose/reload_prom_config.sh show
frigga prometheus-reload \
--prom-url "$PROMETHEUS_HOST" \
--raw

echo ">> [LOG] Sleeping for 10 seconds ..."
sleep 10

# Comparing results
num_series_after=$(frigga pg -u "http://localhost:9090" -r)
num_series_after=$(frigga pg -u "$PROMETHEUS_HOST" -r)
echo ">> [LOG] Before: ${num_series_before}"
echo ">> [LOG] After: ${num_series_after}"
if [[ "$num_series_after" -lt "$num_series_before" ]]; then
Expand Down
8 changes: 4 additions & 4 deletions docker-compose/deploy_stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ generate_apikey(){
-H "Content-Type: application/json" \
--data '{"name":"local","role":"Viewer","secondsToLive":86400}' \
http://localhost:3000/api/auth/keys | jq -r .key)
echo $apikey
echo $apikey > .apikey && echo ">> API Key was saved in .apikey file"
echo "$apikey"
echo "$apikey" > .apikey && echo ">> API Key was saved in .apikey file"
echo ">> Export the key as environment variable for later use"
echo "export GRAFANA_API_KEY=${apikey}"
}
Expand All @@ -22,12 +22,12 @@ grafana_update_admin_password(){
"newPassword": "admin",
"confirmNew": "admin"
}' http://admin:admin@localhost:3000/api/user/password)
msg=$(echo ${response} | jq -r .message)
msg=$(echo "$response" | jq -r .message)
echo ">> Grafana - ${msg}"
}

network=$(docker network ls | grep frigga_net || true)
[[ ! -z $network ]] && echo "ERROR: wait for network to be deleted, docker network ls, or restart docker daemon" && exit
[[ -n "$network" ]] && echo "ERROR: wait for network to be deleted, docker network ls, or restart docker daemon" && exit
cp docker-compose/prometheus-original.yml docker-compose/prometheus.yml
docker-compose --project-name frigga \
--file docker-compose/docker-compose.yml \
Expand Down
26 changes: 0 additions & 26 deletions docker-compose/reload_prom_config.sh

This file was deleted.

17 changes: 7 additions & 10 deletions kubernetes/apply_changes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,39 @@ error_msg(){
exit 1
}

get_num_series(){
source scripts/get_total_dataseries_num.sh http://prometheus.default.svc.cluster.local:9090
}

PROMETHEUS_HOST="http://prometheus.default.svc.cluster.local:9090"

FRIGGA_FOLDER="/root/frigga/.frigga"
cd "$FRIGGA_FOLDER"

GRAFANA_HOST="http://grafana.default.svc.cluster.local:3000"
RANDOM_KEY_NAME=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10 ; echo '')
GRAFANA_API_KEY=$(curl -s -X POST -sL --user admin:admin -H "Content-Type: application/json" --data '{"name":"'${RANDOM_KEY_NAME}'","role":"Viewer","secondsToLive":86400}' ${GRAFANA_HOST}/api/auth/keys | jq -r .key)
GRAFANA_API_KEY=$(curl -s -X POST -sL --user admin:admin -H "Content-Type: application/json" --data '{"name":"'"${RANDOM_KEY_NAME}"'","role":"Viewer","secondsToLive":86400}' ${GRAFANA_HOST}/api/auth/keys | jq -r .key)

# Install frigga
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt
python3 -m pip install .

echo ">> [LOG] Check num of dataseries before"
num_series_before=$(get_num_series)
num_series_before=$(frigga pg -r -u "$PROMETHEUS_HOST")
echo ">> [LOG] Before: ${num_series_before}"

# Generate .metrics.json
frigga gl -gurl ${GRAFANA_HOST} -gkey ${GRAFANA_API_KEY}
frigga gl -gurl "$GRAFANA_HOST" -gkey "$GRAFANA_API_KEY"

# Add filters to prometheus.yml
frigga pa -ppath kubernetes/prometheus-original.yml -mjpath .metrics.json

# Reload prometheus
curl -s -X POST http://prometheus.default.svc.cluster.local:9090/-/reload
echo ">> [LOG] Prometheus was reloaded"
frigga pr -u "$PROMETHEUS_HOST"

echo ">> [LOG] Sleeping for 10 seconds ..."
sleep 10

# Comparing results
num_series_after=$(get_num_series)
num_series_after=$(frigga pg -r -u "$PROMETHEUS_HOST")
echo ">> [LOG] Before: ${num_series_before}"
echo ">> [LOG] After: ${num_series_after}"
if [[ "$num_series_after" -lt "$num_series_before" ]]; then
Expand All @@ -54,4 +51,4 @@ elif [[ "$num_series_after" -eq "$num_series_before" ]]; then
exit 0
else
error_msg "Failed testing! Before is smaller than after"
fi
fi
2 changes: 1 addition & 1 deletion src/frigga/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def apply_yaml(prom_yaml_path, metrics_json_path, create_backup_file=True, skip_
indent=2
)
print_msg(
msg_content=f"Done! Now reload {prom_yaml_path} with 'docker exec $PROM_CONTAINER_NAME kill -HUP 1'")
msg_content=f"Done! Now reload {prom_yaml_path} with 'frigga pr -u http://localhost:9090'")


def reload_prom(prom_url="http://localhost:9090", raw=False):
Expand Down

0 comments on commit 9662256

Please sign in to comment.