Ejemplo n.º 1
0
def test_no_environ_tags():
    """
        Test tags work even if no global tags present as env variable
    """

    timestamp = int(time.time())
    test_val = math.pi
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token"})
    mock_environ.start()
    client = Apptuit()
    dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1},
                    timestamp=timestamp, value=test_val)
    dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val)
    payload = client._create_payload_from_datapoints([dp1, dp2])
    assert_equals(len(payload), 2)
    assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1})
    assert_equals(payload[1]["tags"], {"test": 2})

    registry = MetricsRegistry()
    reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"})
    counter = registry.counter("counter")
    counter.inc(1)
    payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry))
    assert_equals(len(payload), 1)
    assert_equals(payload[0]["tags"], {'host': 'reporter', 'ip': '2.2.2.2'})
    mock_environ.stop()
Ejemplo n.º 2
0
def test_empty_results(mock_get):
    """
    Test that when results array is empty in the response and we try to access the
    outputs in the results object we get a KeyError
    """
    mock_get.return_value.content = '{"outputs":[{"id":"nyc:taxi:rides", \
                                    "result":[]}],"hints":[],"query": \
                                    {"querytext":"fetch(\'nyc.taxi.rides\')", \
                                    "startTime":1406831400, \
                                    "startTimeHumanReadableSYS":"July 31, 2014 6:30:00 PM UTC", \
                                    "startTimeHumanReadableIST":"August 1, 2014 12:00:00 AM IST", \
                                    "endTime":1407609000, \
                                    "endTimeHumanReadableSYS":"August 9, 2014 6:30:00 PM UTC", \
                                    "endTimeHumanReadableIST":"August 10, 2014 12:00:00 AM IST", \
                                    "digest":"Mdt8e+HDjnGByMMJdEnTnNdUxKo=:60845", \
                                    "optionsdigest":"", "options":"{}"}, \
                                    "query_stats":{"compactedRows":217, "processedRows":217, \
                                    "dataPointsProcessed":219, "numSeries":1, "queryTimeMillis":152, \
                                    "hbaseTimeMillis":21}, \
                                    "timing_diagnostics":[{"tag":"QUERY_EXECUTION_TOTAL_TIME", \
                                    "instanceCount":1, "totalElapsedTimeMillis":152}, \
                                    {"tag":"AST_BUILD_TOTAL_TIME", "instanceCount":1, \
                                    "totalElapsedTimeMillis":29},{"tag":"AST_JYTHON_TOTAL_TIME", \
                                    "instanceCount":1, "totalElapsedTimeMillis":29}, \
                                    {"tag":"STATEMENT_VALIDATION_TOTAL_TIME", \
                                    "instanceCount":1, "totalElapsedTimeMillis":0}, \
                                    {"tag":"PLAN_BUILDING_TOTAL_TIME", "instanceCount":1, \
                                    "totalElapsedTimeMillis":0},{"tag":"QUERY_OPTIMIZATION_TIME", \
                                    "instanceCount":1, "totalElapsedTimeMillis":0}, \
                                    {"tag":"PLAN_EXECUTION_TOTAL_TIME", "instanceCount":1, \
                                    "totalElapsedTimeMillis":106},{"tag":"SCHEMA_SERVICE_FETCH_TOTAL_TIME", \
                                    "instanceCount":1, "totalElapsedTimeMillis":93}, \
                                    {"tag":"DATASOURCE_FETCH_RUN_TIME", "instanceCount":2, \
                                    "totalElapsedTimeMillis":32},{"tag":"TSD_HBASE_TIME", \
                                    "instanceCount":2, "totalElapsedTimeMillis":21}, \
                                    {"tag":"DATASOURCE_FETCH_DP_DECODE_TIME", "instanceCount":2, \
                                    "totalElapsedTimeMillis":52}, \
                                    {"tag":"DATASOURCE_FETCH_DP_DECODE_GET_TAGS_TIME", \
                                    "instanceCount":2, "totalElapsedTimeMillis":51}, \
                                    {"tag":"DATASOURCE_FETCH_DP_DECODE_GET_DPS_TIME", \
                                    "instanceCount":2, "totalElapsedTimeMillis":0}, \
                                    {"tag":"DATASOURCE_FETCH_DP_DECODE_CORE_PROCESSING_TIME", \
                                    "instanceCount":4, "totalElapsedTimeMillis":0}, \
                                    {"tag":"DATASOURCE_FETCH_DP_DECODE_DS_WAIT_TIME", \
                                    "instanceCount":4, "totalElapsedTimeMillis":0}, \
                                    {"tag":"DATASOURCE_FETCH_TOTAL_TIME", "instanceCount":1, \
                                    "totalElapsedTimeMillis":12}, \
                                    {"tag":"PLAN_EXECUTION_JPY_REMOVE_DF_TOTAL_TIME", \
                                    "instanceCount":1, "totalElapsedTimeMillis":17}, \
                                    {"tag":"RESULT_DATA_MARSHALLING_TIME", "instanceCount":1, \
                                    "totalElapsedTimeMillis":0}]}'
    mock_get.return_value.status_code = 200
    token = 'sdksdk203afdsfj_sadasd3939'
    client = Apptuit(sanitize_mode=None, token=token)
    query = "fetch('nyc.taxi.rides')"
    start = 1406831400
    end = 1407609000
    resp = client.query(query, start, end)
    with assert_raises(KeyError):
        _ = resp[0]
Ejemplo n.º 3
0
def test_no_token():
    """
            Test that no token raises error
    """
    with assert_raises(ValueError) as ex:
        client = Apptuit(sanitize_mode=None, token="")
    with assert_raises(ValueError) as ex:
        client = Apptuit(sanitize_mode=None, token=None)
Ejemplo n.º 4
0
def test_api_endpoint_param():
    """
        Test the api_endpoint param of apptuit client
    """
    _ = Apptuit(sanitize_mode=None, token="test_token", api_endpoint="https://api.apptuit.ai/")
    with assert_raises(ValueError):
        _ = Apptuit(sanitize_mode=None, token="test_token", api_endpoint=None)
    with assert_raises(ValueError):
        _ = Apptuit(sanitize_mode=None, token="test_token", api_endpoint="")
Ejemplo n.º 5
0
def do_query(mock_get):
    """
    Execute the query API and return the mock response
    """
    mock_get.return_value.content = get_mock_response()
    mock_get.return_value.status_code = 200
    token = 'sdksdk203afdsfj_sadasd3939'
    client = Apptuit(sanitize_mode=None, token=token)
    query = "fetch('nyc.taxi.rides')"
    start = 1406831400
    end = 1407609000
    return client.query(query, start, end)
Ejemplo n.º 6
0
def test_token_negative():
    """
        Test that if no token parameter is passed and no env variable is set, we get an error
    """
    mock_environ = patch.dict(os.environ, {})
    mock_environ.start()
    with assert_raises(ValueError):
        Apptuit()
    with assert_raises(ValueError):
        Apptuit(token="")
    with assert_raises(ValueError):
        Apptuit(token=None)
    mock_environ.stop()
Ejemplo n.º 7
0
def test_tags_limit_indirect(mock_post):
    """
    Test for failure when too many tags are used indirectly (when combined with global tags)
    """
    gtags_list = [
        "gtk-%d:gtv-%d" % (i, i)
        for i in range(apptuit_client.MAX_TAGS_LIMIT // 2 + 1)
    ]
    global_tags = ",".join(gtags_list)
    tags = {
        'tagk-%d' % i: 'tagv-%d' % i
        for i in range(apptuit_client.MAX_TAGS_LIMIT // 2 + 1)
    }
    timestamp = int(time.time())
    with patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}):
        client = Apptuit(sanitize_mode=None, token="test_token")
        point1 = DataPoint("metric1", {"tagk1": "tagv1"}, timestamp, 3.14)
        point2 = DataPoint("metric1", tags, timestamp, 3.14)
        with assert_raises(ValueError):
            client.send([point1, point2])

    with patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}):
        client = Apptuit(sanitize_mode=None, token="test_token")
        series1 = TimeSeries('metric1', {"tagk1": "tagv1"})
        series1.add_point(timestamp, 3.14)
        series2 = TimeSeries('metric1', tags)
        series2.add_point(timestamp, 3.14)
        with assert_raises(ValueError):
            client.send_timeseries([series1, series2])
Ejemplo n.º 8
0
def test_multiple_retries_request_err(mock_get):
    """
    Test that the query API attempts retries when an error is returned from
    the backend API. Since we patch the status code as 504 and create an HTTPError
    as a side effect of the get call, we cannot verify that the retries succeed.
    """
    mock_get.side_effect = requests.exceptions.RequestException
    token = 'sdksdk203afdsfj_sadasd3939'
    client = Apptuit(sanitize_mode=None, token=token)
    query = "fetch('nyc.taxi.rides')"
    start = 1406831400
    end = 1407609000
    with assert_raises(requests.exceptions.RequestException):
        client.query(query, start, end, retry_count=3)
    assert_equals(mock_get.call_count, 4)
Ejemplo n.º 9
0
def test_env_global_tags_negative():
    """
        Negative test cases for global tags env variable
    """
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token",
                                           APPTUIT_PY_TAGS: "{InvalidTags"})
    mock_environ.start()
    with assert_raises(ValueError):
        Apptuit()
    mock_environ.stop()
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token",
                                           APPTUIT_PY_TAGS: '"tagk1":"tagv1"'})
    mock_environ.start()
    with assert_raises(ValueError):
        Apptuit()
    mock_environ.stop()
Ejemplo n.º 10
0
def test_env_global_tags_positive():
    """
        Test that the client works with global tags env variable and without them
    """
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token",
                                           APPTUIT_PY_TAGS: 'tagk1: 22, tagk2: tagv2'})
    mock_environ.start()
    client = Apptuit()
    assert_equals(client._global_tags, {"tagk1": "22", "tagk2": "tagv2"})
    mock_environ.stop()

    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token"})
    mock_environ.start()
    client1 = Apptuit()
    assert_equals({}, client1._global_tags)
    mock_environ.stop()
Ejemplo n.º 11
0
def test_tags_not_dict():
    """
    Test to validate that only dict type values are expected for tags
    """
    metric_name = "node.load_avg.1m"
    tags = [
        "host", "localhost", "region", "us-east-1", "service", "web-server"
    ]
    ts = int(time.time())
    client = Apptuit(sanitize_mode=None, token="test")
    with assert_raises(AttributeError) as ex:
        dp = DataPoint(metric=metric_name,
                       tags=tags,
                       timestamp=ts,
                       value=random.random())
        client.send([dp])
Ejemplo n.º 12
0
def test_get_error_retry_with_SSLError(mock_get):
    """
    Test that when the retry_count is 0 for the query API we get an exception
    """
    mock_get.return_value.content = get_mock_response()
    mock_get.return_value.status_code = 404
    err_response = Response()
    err_response.status_code = 505
    mock_get.return_value.raise_for_status.side_effect = requests.exceptions.SSLError(response=err_response)
    token = 'sdksdk203afdsfj_sadasd3939'
    client = Apptuit(sanitize_mode=None, token=token)
    query = "fetch('nyc.taxi.rides')"
    start = 1406831400
    end = 1407609000
    with assert_raises(ApptuitException):
        client.query(query, start, end, retry_count=2)
    assert_equals(1, mock_get.call_count)
Ejemplo n.º 13
0
def test_invalid_metric_name():
    """
    Test for invalid character in metric name
    """
    metric_name = "node.load+avg.1m"
    tags = {
        "host": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    ts = int(time.time())
    client = Apptuit(sanitize_mode=None, token="test")
    with assert_raises(ValueError) as ex:
        dp = DataPoint(metric=metric_name,
                       tags=tags,
                       timestamp=ts,
                       value=random.random())
        client.send([dp])
Ejemplo n.º 14
0
def test_invalid_chars_in_tag_keys():
    """
    Test for invalid character in tag keys
    """
    metric_name = "node.load_avg.1m"
    tags = {
        "ho\\st": "localhost",
        "region": "us-east-1",
        "service": "web-server"
    }
    ts = int(time.time())
    client = Apptuit(sanitize_mode=None, token="test")
    with assert_raises(ValueError) as ex:
        dp = DataPoint(metric=metric_name,
                       tags=tags,
                       timestamp=ts,
                       value=random.random())
        client.send([dp])
    with assert_raises(AttributeError) as ex:
        dp = DataPoint(metric=metric_name,
                       tags="error",
                       timestamp=ts,
                       value=random.random())
        client.send([dp])
    dp = DataPoint(metric=metric_name,
                   tags=None,
                   timestamp=ts,
                   value=random.random())
    assert_equals(dp.tags, None)
Ejemplo n.º 15
0
def test_datapoint_tags_and_envtags():
    """
        Test that datapoint tags take priority when global tags env variable is present
    """
    mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token",
                                           APPTUIT_PY_TAGS: 'host: host1, ip: 1.1.1.1'})
    mock_environ.start()
    client = Apptuit(None)
    timestamp = int(time.time())
    test_val = math.pi
    dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1},
                    timestamp=timestamp, value=test_val)
    dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val)
    dp3 = DataPoint(metric="test_metric", tags={}, timestamp=timestamp, value=test_val)
    dp4 = DataPoint(metric="test_metric", tags=None, timestamp=timestamp, value=test_val)
    payload = client._create_payload_from_datapoints([dp1, dp2, dp3, dp4])
    assert_equals(len(payload), 4)
    assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1})
    assert_equals(payload[1]["tags"], {"host": "host1", "ip": "1.1.1.1", "test": 2})
    assert_equals(payload[2]["tags"], {"host": "host1", "ip": "1.1.1.1"})
    assert_equals(payload[3]["tags"], {"host": "host1", "ip": "1.1.1.1"})
    assert_equals(client._global_tags, {"host": "host1", "ip": "1.1.1.1"})
    mock_environ.stop()
Ejemplo n.º 16
0
def test_timeseries_payload_with_globaltags():
    """
    Test payload creation from timeseries list with global tags
    """
    token = "asdashdsauh_8aeraerf"
    global_tags = {"gtagk1": "gtagv1"}
    client = Apptuit(sanitize_mode=None, token=token, global_tags=global_tags)
    series_list = []
    metric1_name = 'metric1'
    metric2_name = "metric2"
    tags2 = {"tagk3": "tagv3"}
    series1 = TimeSeries(metric1_name, tags=None)
    series2 = TimeSeries(metric2_name, tags2)
    timestamp = int(time.time())
    val1 = 3.14
    val2 = 42.0
    series1.add_point(timestamp, val1)
    series2.add_point(timestamp, val2)
    series_list.append(series1)
    series_list.append(series2)
    payload, points_count = client._create_payload_from_timeseries(series_list)
    assert_equals(points_count, 2)
    expected_payload = [{
        "metric": metric1_name,
        "tags": global_tags,
        "timestamp": timestamp,
        "value": val1
    }, {
        "metric": metric2_name,
        "tags": {
            "tagk3": "tagv3",
            "gtagk1": "gtagv1"
        },
        "timestamp": timestamp,
        "value": val2
    }]
    assert_equals(expected_payload, payload)
Ejemplo n.º 17
0
def test_client_global_tags():
    """
    Test that client object is working as expected with _global_tags
    """
    mock_environ = patch.dict(
        os.environ, {
            APPTUIT_PY_TOKEN: "environ_token",
            APPTUIT_PY_TAGS: 'tagk1: 22, tagk2: tagv2'
        })
    mock_environ.start()
    client = Apptuit(sanitize_mode=None, )
    assert_equals(client._global_tags, {"tagk1": "22", "tagk2": "tagv2"})
    mock_environ.stop()

    mock_environ = patch.dict(
        os.environ, {
            APPTUIT_PY_TOKEN: "environ_token",
            APPTUIT_PY_TAGS: 'tagk1: 22, tagk2: tagv2'
        })
    mock_environ.start()
    client = Apptuit(sanitize_mode=None, ignore_environ_tags=True)
    assert_equals(client._global_tags, None)
    mock_environ.stop()

    mock_environ = patch.dict(os.environ, {
        APPTUIT_PY_TOKEN: "environ_token",
        APPTUIT_PY_TAGS: 'tk1: tv1, tk2: tv2'
    })
    mock_environ.start()
    client = Apptuit(sanitize_mode=None,
                     global_tags={
                         "tagk1": "22",
                         "tagk2": "tagv2"
                     })
    assert_equals(client._global_tags, {"tagk1": "22", "tagk2": "tagv2"})
    mock_environ.stop()
Ejemplo n.º 18
0
def test_token_positive():
    """
        Test various combinations of token argument to apptuit, as parameter and as env variable
    """
    test_cases = [
        ("environ_token", "", "environ_token"),
        ("environ_token", None, "environ_token"),
        ("environ_token", "argument_token", "argument_token")
    ]
    for env_token, arg_token, expected in test_cases:
        mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: env_token})
        mock_environ.start()
        client = Apptuit(token=arg_token)
        assert_equals(client.token, expected)
        mock_environ.stop()
Ejemplo n.º 19
0
def test_tags_limit_direct(mock_post):
    """
    Test for failure when too many tags are used for datapoints/series
    """
    tags = {
        'tagk-%d' % i: 'tagv-%d' % i
        for i in range(apptuit_client.MAX_TAGS_LIMIT + 1)
    }
    timestamp = int(time.time())
    client = Apptuit(sanitize_mode=None, token="test_token")
    point1 = DataPoint("metric1", {"tagk1": "tagv1"}, timestamp, 3.14)
    point2 = DataPoint("metric1", tags, timestamp, 3.14)
    with assert_raises(ValueError):
        client.send([point1, point2])
    series1 = TimeSeries('metric1', {"tagk1": "tagv1"})
    series1.add_point(timestamp, 3.14)
    series2 = TimeSeries('metric1', tags)
    series2.add_point(timestamp, 3.14)
    with assert_raises(ValueError):
        client.send_timeseries([series1, series2])
Ejemplo n.º 20
0
def __get_apptuit_client():
    token = "asdashdsauh_8aeraerf"
    return Apptuit(token, api_endpoint="http://localhost")