github.com/m3db/m3@v1.5.0/scripts/docker-integration-tests/prometheus/test-correctness.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -ex
     4  source "$M3_PATH"/scripts/docker-integration-tests/common.sh
     5  t=$(date +%s)
     6  
     7  function write_metrics {
     8    NUM=$1
     9    EXTRA=${2:-default}
    10    echo "Writing $NUM metrics to [0.0.0.0:9003]"
    11    set +x
    12    for (( i=0; i<$NUM; i++ ))
    13    do
    14      curl -X POST 0.0.0.0:9003/writetagged -d '{
    15        "namespace": "unagg",
    16        "id": "{__name__=\"'$METRIC_NAME'\",'$EXTRA'=\"extra\",val=\"'$i'\"}",
    17        "tags": [
    18          {
    19            "name": "__name__",
    20            "value": "'$METRIC_NAME'"
    21          },
    22          { 
    23            "name": "'$EXTRA'",
    24            "value": "extra"
    25          },
    26          { 
    27            "name": "val",
    28            "value": "'$i'"
    29          }
    30        ],
    31        "datapoint": {
    32          "timestamp":'"$t"',
    33          "value": 1
    34        }
    35      }'
    36    done
    37    set -x
    38  }
    39   
    40  function test_instantaneous {
    41    QUERY=$1
    42    EXPECTED_COUNT=$2
    43    EXPECTED=$3
    44    RESPONSE=$(curl -sSL "http://localhost:7201/api/v1/query?query=$QUERY")
    45    ACTUAL_COUNT=$(echo $RESPONSE | jq '.data.result | length')
    46    ACTUAL=$(echo $RESPONSE | jq .data.result[].metric.foo | sort | tr -d "\n")
    47    CONCAT=$(echo $EXPECTED | tr -d " ")
    48    test $ACTUAL_COUNT = $EXPECTED_COUNT && test $ACTUAL = $CONCAT
    49  }
    50  
    51  function test_replace { 
    52    METRIC_NAME="quail_$t"
    53    write_metrics 5
    54    sleep 1
    55    query='label_replace('$METRIC_NAME',"foo","bar_$1","val","(.*)")'
    56    test_instantaneous $query 5 "\"bar_0\" \"bar_1\" \"bar_2\" \"bar_3\" \"bar_4\""
    57    query='label_replace('$METRIC_NAME',"foo","bar_$1","val","(.*)")-0'
    58    test_instantaneous $query 5 "\"bar_0\" \"bar_1\" \"bar_2\" \"bar_3\" \"bar_4\""
    59  }
    60  
    61  function test_exists {
    62    QUERY=$1
    63    EXPECTED_EXISTS=$2
    64    EXPECTED_NOT_EXISTS=$3
    65    EXPECTED_COUNT=$4
    66    RESPONSE=$(curl -sSL "http://localhost:7201/api/v1/query?query=$METRIC_NAME\{$QUERY\}")
    67    ACTUAL_COUNT_EXISTS=$(echo $RESPONSE | jq .data.result[].metric.$EXPECTED_EXISTS | grep extra | wc -l)
    68    ACTUAL_COUNT_NOT_EXISTS=$(echo $RESPONSE | jq .data.result[].metric.$EXPECTED_NOT_EXISTS | grep extra | wc -l)
    69    test $ACTUAL_COUNT_EXISTS = $EXPECTED_COUNT && test $ACTUAL_COUNT_NOT_EXISTS = 0
    70  }
    71  
    72  function test_empty_matcher {
    73    export METRIC_NAME="foo_$t"
    74    write_metrics 5 exists
    75    write_metrics 5 not_exists
    76   
    77    retry_with_backoff ATTEMPTS=3 TIMEOUT=1 test_exists 'not_exists=\"\"' exists not_exists 5
    78    retry_with_backoff ATTEMPTS=3 TIMEOUT=1 test_exists 'not_exists!=\"\"' not_exists exists 5
    79  
    80    retry_with_backoff ATTEMPTS=3 TIMEOUT=1 test_exists 'exists=\"\"' not_exists exists 5
    81    retry_with_backoff ATTEMPTS=3 TIMEOUT=1 test_exists 'exists!=\"\"' exists not_exists 5
    82  }
    83  
    84  function test_parse_threshold {
    85    test $(curl 'http://localhost:7201/api/v1/parse?query=up' | jq .name) = '"fetch"'
    86    THRESHOLD=$(curl 'http://localhost:7201/api/v1/threshold?query=up>1')
    87    test $(echo $THRESHOLD | jq .threshold.comparator) = '">"'
    88    test $(echo $THRESHOLD | jq .threshold.value) = 1
    89    test $(echo $THRESHOLD | jq .query.name) = '"fetch"'
    90  
    91    THRESHOLD=$(curl 'http://localhost:7201/api/v1/threshold?query=1>up')
    92    test $(echo $THRESHOLD | jq .threshold.comparator) = '"<"'
    93    test $(echo $THRESHOLD | jq .threshold.value) = 1
    94    test $(echo $THRESHOLD | jq .query.name) = '"fetch"' 
    95  }
    96  
    97  function test_duplicates {
    98    now=$(date +"%s")
    99    start=$(( $now - 100 ))
   100    end=$(( $now + 100 ))
   101    QUERY="query=$METRIC_NAME&start=$start&end=$end&format=json"
   102    ACTUAL=$(curl "localhost:7201/api/v1/prom/remote/read?$QUERY" | jq .[][].series[].tags[])
   103    EXPECTED=$(echo '[ "__name__", "'${METRIC_NAME}'" ] [ "val", "extra" ] [ "val", "0" ]' | jq)
   104    test "$ACTUAL"="$EXPECTED"
   105  }
   106  
   107  function test_debug_prom_returns_duplicates {
   108    export METRIC_NAME="duplicate_$t"
   109    # NB: this writes metrics of the form `duplicate_t{val="extra", val="1"}`
   110    #     with a duplicated `val` tag.
   111    write_metrics 1 val
   112    retry_with_backoff ATTEMPTS=3 TIMEOUT=1 test_duplicates
   113  }
   114  
   115  function test_correctness {
   116    test_parse_threshold
   117    test_replace 
   118    test_empty_matcher
   119  
   120    test_debug_prom_returns_duplicates
   121  }