From 5f7bc6b8da9e0d258e4a9c6def58441bf0324115 Mon Sep 17 00:00:00 2001 From: Benjamin Raskin Date: Mon, 2 Dec 2019 15:58:03 -0500 Subject: [PATCH] Tests --- .../docker-compose.yml | 10 +++ .../m3coordinator.yml | 34 ++++---- .../coordinator_config_rules/prometheus.yml | 45 +++++++++++ .../coordinator_config_rules/test.sh | 78 ++++++++++++++----- src/metrics/rules/store/kv/store.go | 3 + 5 files changed, 136 insertions(+), 34 deletions(-) create mode 100644 scripts/docker-integration-tests/coordinator_config_rules/prometheus.yml diff --git a/scripts/docker-integration-tests/coordinator_config_rules/docker-compose.yml b/scripts/docker-integration-tests/coordinator_config_rules/docker-compose.yml index 53a28f0b88..30ee92aa16 100644 --- a/scripts/docker-integration-tests/coordinator_config_rules/docker-compose.yml +++ b/scripts/docker-integration-tests/coordinator_config_rules/docker-compose.yml @@ -24,5 +24,15 @@ services: image: "m3coordinator_integration:${REVISION}" volumes: - "./:/etc/m3coordinator/" + prometheus01: + expose: + - "9090" + ports: + - "0.0.0.0:9090:9090" + networks: + - backend + image: prom/prometheus:latest + volumes: + - "./prometheus.yml:/etc/prometheus/prometheus.yml" networks: backend: diff --git a/scripts/docker-integration-tests/coordinator_config_rules/m3coordinator.yml b/scripts/docker-integration-tests/coordinator_config_rules/m3coordinator.yml index 992fa08a5a..4fb3d4dc9b 100644 --- a/scripts/docker-integration-tests/coordinator_config_rules/m3coordinator.yml +++ b/scripts/docker-integration-tests/coordinator_config_rules/m3coordinator.yml @@ -39,25 +39,29 @@ clusters: downsample: rules: - # mappingRules: - # - filter: "foo:bar" - # aggregation: - # - 2 - # storagePolicies: - # - retention: 12h - # resolution: 10s - # drop: false - rollupRules: + mappingRules: - filter: "foo:bar" - transforms: - - rollup: - metricName: "new_metric" - aggregate: - type: Sum + aggregations: + - Sum storagePolicies: - retention: 10h resolution: 5s - name: "testRollup" + drop: false + # rollupRules: + # - filter: "foo:bar" + # transforms: + # - rollup: + # metricName: "new_metric" + # aggregations: + # - Sum + # groupBy: + # - foo + # # aggregate: + # # type: Sum + # storagePolicies: + # - retention: 10h + # resolution: 5s + # name: "testRollup" tagOptions: diff --git a/scripts/docker-integration-tests/coordinator_config_rules/prometheus.yml b/scripts/docker-integration-tests/coordinator_config_rules/prometheus.yml new file mode 100644 index 0000000000..ce96174fe2 --- /dev/null +++ b/scripts/docker-integration-tests/coordinator_config_rules/prometheus.yml @@ -0,0 +1,45 @@ +# my global config +global: + external_labels: + role: "remote" + scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. + evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. + # scrape_timeout is set to the global default (10s). + +# Alertmanager configuration +alerting: + alertmanagers: + - static_configs: + - targets: + # - alertmanager:9093 + +# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. +rule_files: + # - "first_rules.yml" + # - "second_rules.yml" + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: 'prometheus' + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ['localhost:9090'] + + - job_name: 'coordinator' + static_configs: + - targets: ['coordinator01:7203'] + + - job_name: 'dbnode' + static_configs: + - targets: ['dbnode01:9004'] + +remote_read: + - url: http://coordinator01:7201/api/v1/prom/remote/read + +remote_write: + - url: http://coordinator01:7201/api/v1/prom/remote/write diff --git a/scripts/docker-integration-tests/coordinator_config_rules/test.sh b/scripts/docker-integration-tests/coordinator_config_rules/test.sh index 96d3cec9de..05ca0c8785 100755 --- a/scripts/docker-integration-tests/coordinator_config_rules/test.sh +++ b/scripts/docker-integration-tests/coordinator_config_rules/test.sh @@ -3,18 +3,25 @@ set -xe source $GOPATH/src/github.com/m3db/m3/scripts/docker-integration-tests/common.sh +source $GOPATH/src/github.com/m3db/m3/scripts/docker-integration-tests/prometheus/test-correctness.sh REVISION=$(git rev-parse HEAD) -SCRIPT_PATH=$GOPATH/src/github.com/m3db/m3/scripts/docker-integration-tests/coordinator_config_rules -COMPOSE_FILE=$SCRIPT_PATH/docker-compose.yml -EXPECTED_PATH=$SCRIPT_PATH/expected -METRIC_NAME_TEST_OLD=foo +COMPOSE_FILE=$GOPATH/src/github.com/m3db/m3/scripts/docker-integration-tests/coordinator_config_rules/docker-compose.yml +# quay.io/m3db/prometheus_remote_client_golang @ v0.4.3 +PROMREMOTECLI_IMAGE=quay.io/m3db/prometheus_remote_client_golang@sha256:fc56df819bff9a5a087484804acf3a584dd4a78c68900c31a28896ed66ca7e7b +JQ_IMAGE=realguess/jq:1.4@sha256:300c5d9fb1d74154248d155ce182e207cf6630acccbaadd0168e18b15bfaa786 +METRIC_NAME_TEST_TOO_OLD=foo +METRIC_NAME_OLD=old_metric +METRIC_NAME_NEW=new_metric export REVISION +echo "Pull containers required for test" +docker pull $PROMREMOTECLI_IMAGE +docker pull $JQ_IMAGE + echo "Run m3dbnode and m3coordinator containers" docker-compose -f ${COMPOSE_FILE} up -d dbnode01 docker-compose -f ${COMPOSE_FILE} up -d coordinator01 -# Think of this as a defer func() in golang function defer { docker-compose -f ${COMPOSE_FILE} down || echo "unable to shutdown containers" # CI fails to stop all containers sometimes } @@ -22,6 +29,9 @@ trap defer EXIT setup_single_m3db_node +echo "Start Prometheus containers" +docker-compose -f ${COMPOSE_FILE} up -d prometheus01 + function prometheus_remote_write { local metric_name=$1 local datapoint_timestamp=$2 @@ -34,11 +44,11 @@ function prometheus_remote_write { local metrics_storage_policy=$9 network=$(docker network ls --format '{{.ID}}' | tail -n 1) - out=$((docker run -it --rm --network $network \ + out=$((docker run -it --rm --network coordinator_config_rules_backend \ $PROMREMOTECLI_IMAGE \ -u http://coordinator01:7201/api/v1/prom/remote/write \ -t __name__:${metric_name} \ - -t foo:bar + -t foo:bar \ -h "M3-Metrics-Type: ${metrics_type}" \ -h "M3-Storage-Policy: ${metrics_storage_policy}" \ -d ${datapoint_timestamp},${datapoint_value} | grep -v promremotecli_log) || true) @@ -46,25 +56,37 @@ function prometheus_remote_write { status=$(echo $out | grep -v promremotecli_log | docker run --rm -i $JQ_IMAGE jq .statusCode) if [[ "$success" != "$expect_success" ]]; then echo $expect_success_err + sleep 10000000 return 1 fi if [[ "$status" != "$expect_status" ]]; then echo "${expect_status_err}: actual=${status}" + sleep 10000000 return 1 fi echo "Returned success=${success}, status=${status} as expected" return 0 } -function prometheus_write_metric { - echo "Test write with aggregated metrics type works as expected" + +function test_prometheus_remote_write_restrict_metrics_type { + # Test we can specify metrics type + echo "Test write with unaggregated metrics type works as expected" prometheus_remote_write \ - old_metric now 84.84 \ + $METRIC_NAME_NEW now 42.42 \ true "Expected request to succeed" \ 200 "Expected request to return status code 200" \ - aggregated 15s:10h + unaggregated + +# echo "Test write with aggregated metrics type works as expected" +# prometheus_remote_write \ +# $METRIC_NAME_OLD now 84.84 \ +# true "Expected request to succeed" \ +# 200 "Expected request to return status code 200" \ +# aggregated 5s:10h } + function prometheus_query_native { local endpoint=${endpoint:-} local query=${query:-} @@ -87,22 +109,40 @@ function prometheus_query_native { return $? } - -function test_query_rollup_rule { +function test_query_new_name { now=$(date +"%s") hour_ago=$(expr $now - 3600) step="30s" params_instant="" params_range="start=${hour_ago}"'&'"end=${now}"'&'"step=30s" jq_path_instant=".data.result[0].value[1]" - jq_path_range=".data.result[0].metric[\"__name__\"]" + jq_path_range=".data.result[0].values[][1]" + + # # Test metric name is changed + # echo "Test query restrict to unaggregated metrics type (instant)" + # ATTEMPTS=50 TIMEOUT=2 MAX_TIMEOUT=4 \ + # endpoint=query query="$METRIC_NAME_NEW" params="$params_instant" \ + # metrics_type="unaggregated" jq_path="$jq_path_instant" expected_value="42.42" \ + # retry_with_backoff prometheus_query_native + # echo "Test query restrict to unaggregated metrics type (range)" + # ATTEMPTS=50 TIMEOUT=2 MAX_TIMEOUT=4 \ + # endpoint=query_range query="$METRIC_NAME_NEW" params="$params_range" \ + # metrics_type="unaggregated" jq_path="$jq_path_range" expected_value="42.42" \ + # retry_with_backoff prometheus_query_native - echo "Test query rollup rule" + # Test restricting to aggregated metrics + # echo "Test query restrict to aggregated metrics type (instant)" + # ATTEMPTS=50 TIMEOUT=2 MAX_TIMEOUT=4 \ + # endpoint=query query="$METRIC_NAME_NEW" params="$params_instant" \ + # metrics_type="aggregated" metrics_storage_policy="5s:10h" jq_path="$jq_path_instant" expected_value="84.84" \ + # retry_with_backoff prometheus_query_native + echo "Test query restrict to aggregated metrics type (range)" ATTEMPTS=50 TIMEOUT=2 MAX_TIMEOUT=4 \ - endpoint=query_range query=new_metric params="$params_range" \ - metrics_type="aggregated" metrics_storage_policy="15s:10h" jq_path="$jq_path_range" expected_value="new_metric" \ + endpoint=query_range query="$METRIC_NAME_NEW" params="$params_range" \ + metrics_type="aggregated" metrics_storage_policy="5s:10h" jq_path="$jq_path_range" expected_value="84.84" \ retry_with_backoff prometheus_query_native } -echo "Running prometehus tests" -test_query_rollup_rule +echo "Running rule config tests..." +test_prometheus_remote_write_restrict_metrics_type +test_query_new_name diff --git a/src/metrics/rules/store/kv/store.go b/src/metrics/rules/store/kv/store.go index 7381150d09..b11485be66 100644 --- a/src/metrics/rules/store/kv/store.go +++ b/src/metrics/rules/store/kv/store.go @@ -114,6 +114,9 @@ func (s *store) WriteAll(nss *rules.Namespaces, rs rules.MutableRuleSet) error { if err != nil { return err } + + r, _ := rs.RollupRules() + fmt.Println("ruleset rollup rules: ", r) conditions = append(conditions, ruleSetCond) ops = append(ops, ruleSetOp)