Skip to content

Commit

Permalink
Refactor for log streaming validators. (#7467)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiec-msft committed Apr 8, 2024
1 parent 03a4317 commit c4a009f
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 77 deletions.
28 changes: 16 additions & 12 deletions src/spring/azext_spring/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from azure.cli.core.commands.parameters import get_enum_type, get_three_state_flag, tags_type
from azure.cli.core.commands.parameters import (name_type, get_location_type, resource_group_name_type)
from ._validators import (validate_env, validate_cosmos_type, validate_resource_id, validate_location,
validate_name, validate_app_name, validate_deployment_name, validate_log_lines,
validate_log_limit, validate_log_since, validate_sku, normalize_sku, validate_jvm_options,
validate_name, validate_app_name, validate_deployment_name, validate_sku,
normalize_sku, validate_jvm_options,
validate_vnet, validate_vnet_required_parameters, validate_node_resource_group,
validate_tracing_parameters_asc_create, validate_tracing_parameters_asc_update,
validate_app_insights_parameters, validate_instance_count, validate_java_agent_parameters,
Expand Down Expand Up @@ -39,6 +39,7 @@
ensure_not_active_deployment, validate_deloy_path, validate_deloyment_create_path,
validate_cpu, validate_build_cpu, validate_memory, validate_build_memory,
fulfill_deployment_param_or_warning, active_deployment_exist_or_warning)
from .log_stream.log_stream_validators import (validate_log_lines, validate_log_limit, validate_log_since)
from ._app_managed_identity_validator import (validate_create_app_with_user_identity_or_warning,
validate_create_app_with_system_identity_or_warning,
validate_app_force_set_system_identity_or_warning,
Expand Down Expand Up @@ -1115,30 +1116,33 @@ def prepare_logs_argument(c):
with self.argument_context(scope) as c:
c.argument('service', service_name_type)

with self.argument_context('spring component logs') as c:
c.argument('name', options_list=['--name', '-n'],
help="Name of the component. Find component names from command `az spring component list`")
c.argument('all_instances',
help='The flag to indicate get logs for all instances of the component.',
action='store_true')
c.argument('instance',
options_list=['--instance', '-i'],
help='Name of an existing instance of the component.')
def prepare_common_logs_argument(c):
c.argument('follow',
options_list=['--follow ', '-f'],
help='The flag to indicate logs should be streamed.',
action='store_true')
c.argument('lines',
type=int,
help='Number of lines to show. Maximum is 10000. Default is 50.')
help='Number of lines to show. Maximum is 10000.')
c.argument('since',
help='Only return logs newer than a relative duration like 5s, 2m, or 1h. Maximum is 1h')
c.argument('limit',
type=int,
help='Maximum kibibyte of logs to return. Ceiling number is 2048.')

with self.argument_context('spring component logs') as c:
c.argument('name', options_list=['--name', '-n'],
help="Name of the component. Find component names from command `az spring component list`")
c.argument('all_instances',
help='The flag to indicate get logs for all instances of the component.',
action='store_true')
c.argument('instance',
options_list=['--instance', '-i'],
help='Name of an existing instance of the component.')
c.argument('max_log_requests',
type=int,
help="Specify maximum number of concurrent logs to follow when get logs by all-instances.")
prepare_common_logs_argument(c)

with self.argument_context('spring component instance') as c:
c.argument('component', options_list=['--component', '-c'],
Expand Down
42 changes: 0 additions & 42 deletions src/spring/azext_spring/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,48 +178,6 @@ def validate_cosmos_type(namespace):
"Cosmosdb with type {} should specify collection name".format(type))


def validate_log_limit(namespace):
temp_limit = None
try:
temp_limit = namespace.limit
except:
raise InvalidArgumentValueError('--limit must contains only digit')
if temp_limit < 1:
raise InvalidArgumentValueError('--limit must be in the range [1,2048]')
if temp_limit > 2048:
temp_limit = 2048
logger.error("--limit can not be more than 2048, using 2048 instead")
namespace.limit = temp_limit * 1024


def validate_log_lines(namespace):
temp_lines = None
try:
temp_lines = namespace.lines
except:
raise InvalidArgumentValueError('--lines must contains only digit')
if temp_lines < 1:
raise InvalidArgumentValueError('--lines must be in the range [1,10000]')
if temp_lines > 10000:
temp_lines = 10000
logger.error("--lines can not be more than 10000, using 10000 instead")
namespace.lines = temp_lines


def validate_log_since(namespace):
if namespace.since:
last = namespace.since[-1:]
try:
namespace.since = int(
namespace.since[:-1]) if last in ("hms") else int(namespace.since)
except:
raise InvalidArgumentValueError("--since contains invalid characters")
namespace.since *= 60 if last == "m" else 1
namespace.since *= 3600 if last == "h" else 1
if namespace.since > 3600:
raise InvalidArgumentValueError("--since can not be more than 1h")


def validate_jvm_options(namespace):
if namespace.jvm_options is not None:
namespace.jvm_options = namespace.jvm_options.strip('\'')
Expand Down
68 changes: 68 additions & 0 deletions src/spring/azext_spring/log_stream/log_stream_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.azclierror import InvalidArgumentValueError
from knack.log import get_logger
from knack.util import CLIError

logger = get_logger(__name__)


def validate_all_instances_and_instance_are_mutually_exclusive(namespace):
if namespace.all_instances is True and namespace.instance is not None:
raise InvalidArgumentValueError("--all-instances cannot be set together with --instance/-i.")


def validate_log_limit(namespace):
temp_limit = None
try:
temp_limit = namespace.limit
except:
raise InvalidArgumentValueError('--limit must contains only digit')
if temp_limit < 1:
raise InvalidArgumentValueError('--limit must be in the range [1,2048]')
if temp_limit > 2048:
temp_limit = 2048
logger.error("--limit can not be more than 2048, using 2048 instead")
namespace.limit = temp_limit * 1024


def validate_log_lines(namespace):
temp_lines = None
try:
temp_lines = namespace.lines
except:
raise InvalidArgumentValueError('--lines must contains only digit')
if temp_lines < 1:
raise InvalidArgumentValueError('--lines must be in the range [1,10000]')
if temp_lines > 10000:
temp_lines = 10000
logger.error("--lines can not be more than 10000, using 10000 instead")
namespace.lines = temp_lines


def validate_log_since(namespace):
if namespace.since:
last = namespace.since[-1:]
try:
namespace.since = int(
namespace.since[:-1]) if last in ("hms") else int(namespace.since)
except:
raise InvalidArgumentValueError("--since contains invalid characters")
namespace.since *= 60 if last == "m" else 1
namespace.since *= 3600 if last == "h" else 1
if namespace.since > 3600:
raise InvalidArgumentValueError("--since can not be more than 1h")


def validate_max_log_requests(namespace):
if namespace.max_log_requests < 1:
raise InvalidArgumentValueError("--max-log-requests should be larger than 0.")


def validate_thread_number(follow, thread_number, max_log_requests):
if (follow is True and thread_number > max_log_requests):
raise CLIError("You are attempting to follow {} log streams, but maximum allowed concurrency is {}, "
"use --max-log-requests to increase the limit".format(thread_number, max_log_requests))
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from ..log_stream.writer import (DefaultWriter, PrefixWriter)
from ..log_stream.log_stream_operations import log_stream_from_url
from ..log_stream.log_stream_validators import validate_thread_number
from .._utils import (get_proxy_api_endpoint, BearerAuth)


Expand Down Expand Up @@ -51,9 +52,7 @@ def managed_component_logs(cmd, client, resource_group, service,
else:
url_dict = _get_log_stream_urls(cmd, client, resource_group, service, name, all_instances,
instance, queryOptions)
if (follow is True and len(url_dict) > max_log_requests):
raise CLIError("You are attempting to follow {} log streams, but maximum allowed concurrency is {}, "
"use --max-log-requests to increase the limit".format(len(url_dict), max_log_requests))
validate_thread_number(follow, len(url_dict), max_log_requests)
threads = _get_log_threads(all_instances, url_dict, auth, exceptions)

if follow and len(threads) > 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

from azure.cli.core.azclierror import InvalidArgumentValueError
from knack.log import get_logger
from ..managed_components.managed_component import supported_components
from .._validators import validate_log_lines as _validate_n_normalize_component_log_lines
from .._validators import validate_log_since as _validate_n_normalize_component_log_since
from .._validators import validate_log_limit as _validate_n_normalize_component_log_limit

from .._clierror import NotSupportedPricingTierError
from .._util_enterprise import is_enterprise_tier

from ..log_stream.log_stream_validators import (validate_log_limit, validate_log_lines, validate_log_since,
validate_max_log_requests,
validate_all_instances_and_instance_are_mutually_exclusive)
from ..managed_components.managed_component import supported_components

logger = get_logger(__name__)

Expand All @@ -21,10 +21,10 @@ def validate_component_logs(cmd, namespace):
_validate_component_log_mutual_exclusive_param(namespace)
_validate_component_log_required_param(namespace)
_validate_n_normalize_component_for_logs(namespace)
_validate_n_normalize_component_log_lines(namespace)
_validate_n_normalize_component_log_since(namespace)
_validate_n_normalize_component_log_limit(namespace)
_validate_max_log_requests(namespace)
validate_log_lines(namespace)
validate_log_since(namespace)
validate_log_limit(namespace)
validate_max_log_requests(namespace)
_validate_is_enterprise_tier(cmd, namespace)


Expand All @@ -37,11 +37,6 @@ def validate_instance_list(cmd, namespace):
_validate_is_enterprise_tier(cmd, namespace)


def _validate_max_log_requests(namespace):
if namespace.max_log_requests <= 1:
raise InvalidArgumentValueError("--max-log-requests should be larger than 0.")


def _validate_is_enterprise_tier(cmd, namespace):
if is_enterprise_tier(cmd, namespace.resource_group, namespace.service) is False:
raise NotSupportedPricingTierError("Only enterprise tier service instance is supported in this command.")
Expand Down Expand Up @@ -84,8 +79,7 @@ def _raise_invalid_component_error(user_input_component_name):


def _validate_component_log_mutual_exclusive_param(namespace):
if namespace.all_instances is True and namespace.instance is not None:
raise InvalidArgumentValueError("--all-instances cannot be set together with --instance/-i.")
validate_all_instances_and_instance_are_mutually_exclusive(namespace)


def _validate_component_log_required_param(namespace):
Expand Down
5 changes: 5 additions & 0 deletions src/spring/azext_spring/tests/latest/log_stream/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -----------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -----------------------------------------------------------------------------
Loading

0 comments on commit c4a009f

Please sign in to comment.