def sync_execute(query, args=None, settings=None, with_column_types=False): with ch_pool.get_client() as client: start_time = time() tags = {} if app_settings.SHELL_PLUS_PRINT_SQL: print() print(format_sql(query, args)) try: sql, tags = _annotate_tagged_query(query, args) result = client.execute(sql, args, settings=settings, with_column_types=with_column_types) except Exception as err: tags["failed"] = True tags["reason"] = type(err).__name__ raise _wrap_api_error(err) finally: execution_time = time() - start_time timing("clickhouse_sync_execution_time", execution_time * 1000.0, tags=tags) if app_settings.SHELL_PLUS_PRINT_SQL: print("Execution time: %.6fs" % (execution_time, )) if _request_information is not None and _request_information.get( "save", False): save_query(query, args, execution_time) return result
def test_methods_capture_disabled(db, mock_capture_internal, mocker: MockerFixture): mocker.patch.object(settings, "CAPTURE_INTERNAL_METRICS", False) timing("foo_metric", 100, tags={"team_id": 15}) gauge("bar_metric", 20, tags={"team_id": 15}) incr("zeta_metric") mock_capture_internal.assert_not_called()
def test_methods_capture_enabled(db, mock_capture_internal): timing("foo_metric", 100, tags={"team_id": 15}) gauge("bar_metric", 20, tags={"team_id": 15}) incr("zeta_metric") mock_capture_internal.assert_any_call( { "event": "$$foo_metric", "properties": { "value": 100, "team_id": 15 } }, "machine_id", None, None, mock.ANY, mock.ANY, get_internal_metrics_team_id(), ) mock_capture_internal.assert_any_call( { "event": "$$bar_metric", "properties": { "value": 20, "team_id": 15 } }, "machine_id", None, None, mock.ANY, mock.ANY, get_internal_metrics_team_id(), ) mock_capture_internal.assert_any_call( { "event": "$$zeta_metric", "properties": { "value": 1 } }, "machine_id", None, None, mock.ANY, mock.ANY, get_internal_metrics_team_id(), )
def sync_execute(query, args=None, settings=None, with_column_types=False): with ch_pool.get_client() as client: start_time = perf_counter() tags = {} if app_settings.SHELL_PLUS_PRINT_SQL: print() print(format_sql(query, args)) sql, tags = _annotate_tagged_query(query, args) timeout_task = QUERY_TIMEOUT_THREAD.schedule( _notify_of_slow_query_failure, tags) try: result = client.execute(sql, args, settings=settings, with_column_types=with_column_types) except Exception as err: tags["failed"] = True tags["reason"] = type(err).__name__ incr("clickhouse_sync_execution_failure", tags=tags) raise _wrap_api_error(err) finally: execution_time = perf_counter() - start_time if not timeout_task.done: QUERY_TIMEOUT_THREAD.cancel(timeout_task) timing("clickhouse_sync_execution_time", execution_time * 1000.0, tags=tags) if app_settings.SHELL_PLUS_PRINT_SQL: print("Execution time: %.6fs" % (execution_time, )) if _request_information is not None and _request_information.get( "save", False): save_query(query, args, execution_time) return result