@@ -305,7 +305,6 @@ def list(cls, cmd, resource_group_name, container_app_name):
305305
306306class ContainerAppFunctionsPreviewClient ():
307307 api_version = "2025-10-02-preview"
308- APP_INSIGHTS_API_VERSION = "2018-04-20"
309308
310309 @classmethod
311310 def list_functions_by_revision (cls , cmd , resource_group_name , container_app_name , revision_name ):
@@ -379,114 +378,9 @@ def get_function(cls, cmd, resource_group_name, container_app_name, function_nam
379378 raise CLIError (f"Error retrieving function '{ function_name } ' for container app '{ container_app_name } '." )
380379 return r .json ()
381380
382- @classmethod
383- def get_function_invocation_summary (cls , cmd , resource_group_name , container_app_name , revision_name , function_name , timespan = "30d" ):
384- # Fetch the app insights resource app id
385- app_id = cls ._get_app_insights_id (cmd , resource_group_name , container_app_name , revision_name )
386-
387- # Use application insights query to get function invocations summary
388- invocation_summary_query = (
389- f"requests | extend functionNameFromCustomDimension = tostring(customDimensions['faas.name']) "
390- f"| where timestamp >= ago({ timespan } ) "
391- f"| where cloud_RoleName =~ '{ container_app_name } ' "
392- f"| where cloud_RoleInstance contains '{ revision_name } ' "
393- f"| where operation_Name =~ '{ function_name } ' or functionNameFromCustomDimension =~ '{ function_name } ' "
394- f"| summarize SuccessCount = coalesce(countif(success == true), 0), ErrorCount = coalesce(countif(success == false), 0)"
395- )
396-
397- try :
398- result = cls ._execute_app_insights_query (cmd , app_id , invocation_summary_query , "getLast30DaySummary" )
399- return result
400- except Exception as ex :
401- raise CLIError (f"Error retrieving function invocation summary: { str (ex )} " )
402-
403- @classmethod
404- def get_function_invocation_traces (cls , cmd , resource_group_name , container_app_name , revision_name , function_name , timespan = "30d" , limit = 20 ):
405- # Fetch the app insights resource app id
406- app_id = cls ._get_app_insights_id (cmd , resource_group_name , container_app_name , revision_name )
407-
408- # Use application insights query to get function invocations traces
409- invocation_traces_query = (
410- f"requests | extend functionNameFromCustomDimension = tostring(customDimensions['faas.name']) "
411- f"| project timestamp, id, operation_Name, success, resultCode, duration, operation_Id, functionNameFromCustomDimension, "
412- f"cloud_RoleName, cloud_RoleInstance, invocationId=coalesce(tostring(customDimensions['InvocationId']), tostring(customDimensions['faas.invocation_id'])) "
413- f"| where timestamp > ago({ timespan } ) "
414- f"| where cloud_RoleName =~ '{ container_app_name } ' "
415- f"| where cloud_RoleInstance contains '{ revision_name } ' "
416- f"| where operation_Name =~ '{ function_name } ' or functionNameFromCustomDimension =~ '{ function_name } ' "
417- f"| order by timestamp desc | take { limit } "
418- f"| project timestamp, success, resultCode, durationInMilliSeconds=duration, invocationId, operationId=operation_Id, operationName=operation_Name, functionNameFromCustomDimension "
419- )
420-
421- try :
422- result = cls ._execute_app_insights_query (cmd , app_id , invocation_traces_query , "getInvocationTraces" )
423- return result
424- except Exception as ex :
425- raise CLIError (f"Error retrieving function invocation traces: { str (ex )} " )
426-
427- @classmethod
428- def _get_app_insights_id (cls , cmd , resource_group_name , container_app_name , revision_name ):
429- # Fetch the revision details using the container app client
430- revision = ContainerAppPreviewClient .show_revision (cmd , resource_group_name , container_app_name , revision_name )
431- # Extract the list of environment variables from the revision's properties
432- env_vars = []
433- if revision and "properties" in revision and "template" in revision ["properties" ]:
434- containers = revision ["properties" ]["template" ].get ("containers" , [])
435- for container in containers :
436- env_vars .extend (container .get ("env" , []))
437-
438- # Check for APPLICATIONINSIGHTS_CONNECTION_STRING
439- ai_conn_str = None
440- for env in env_vars :
441- if env .get ("name" ) == "APPLICATIONINSIGHTS_CONNECTION_STRING" :
442- ai_conn_str = env .get ("value" )
443- break
444-
445- if not ai_conn_str :
446- raise CLIError (f"Required application setting APPLICATIONINSIGHTS_CONNECTION_STRING not present in the containerapp '{ container_app_name } '." )
447-
448- # Extract ApplicationId from the connection string
449- app_id = None
450- parts = ai_conn_str .split (";" )
451- for part in parts :
452- if part .startswith ("ApplicationId=" ):
453- app_id = part .split ("=" , 1 )[1 ]
454- break
455-
456- if not app_id :
457- raise CLIError (f"ApplicationId not found in APPLICATIONINSIGHTS_CONNECTION_STRING for containerapp '{ container_app_name } '." )
458- return app_id
459-
460- @classmethod
461- def _execute_app_insights_query (cls , cmd , app_id , query , queryType , timespan = "30D" ):
462-
463- # Application Insights REST API endpoint
464- api_endpoint = "https://api.applicationinsights.io"
465- url = f"{ api_endpoint } /v1/apps/{ app_id } /query?api-version={ cls .APP_INSIGHTS_API_VERSION } &queryType={ queryType } "
466-
467- # Prepare the request body
468- body = {
469- "query" : query ,
470- "timespan" : f"P{ timespan } "
471- }
472-
473- # Execute the query using Azure CLI's send_raw_request
474- response = send_raw_request (
475- cmd .cli_ctx ,
476- "POST" ,
477- url ,
478- body = json .dumps (body ),
479- headers = ["Content-Type=application/json" ]
480- )
481-
482- result = response .json ()
483- if isinstance (result , dict ) and 'error' in result :
484- raise CLIError (f"Error retrieving invocations details: { result ['error' ]} " )
485- return result
486-
487381 @classmethod
488382 def show_function_keys (cls , cmd , resource_group_name , name , key_type , key_name , function_name = None , revision_name = None , replica_name = None , container_name = None ):
489- from .custom import containerapp_debug
383+ from ._utils import execute_function_admin_command
490384
491385 command_fmt = ""
492386 if key_type != "functionKey" :
@@ -496,22 +390,22 @@ def show_function_keys(cls, cmd, resource_group_name, name, key_type, key_name,
496390 command_fmt = "/bin/azure-functions-admin keys show --key-type {} --key-name {} --function-name {}"
497391 command = command_fmt .format (key_type , key_name , function_name )
498392
499- r = containerapp_debug (
393+ r = execute_function_admin_command (
500394 cmd = cmd ,
501395 resource_group_name = resource_group_name ,
502396 name = name ,
503- container = container_name ,
504- revision = revision_name ,
505- replica = replica_name ,
506- debug_command = command
397+ command = command ,
398+ revision_name = revision_name ,
399+ replica_name = replica_name ,
400+ container_name = container_name
507401 )
508402 if not r :
509403 raise CLIError (f"Error retrieving function key '{ key_name } ' of type '{ key_type } '." )
510404 return r
511405
512406 @classmethod
513407 def list_function_keys (cls , cmd , resource_group_name , name , key_type , function_name = None , revision_name = None , replica_name = None , container_name = None ):
514- from .custom import containerapp_debug
408+ from ._utils import execute_function_admin_command
515409
516410 command_fmt = ""
517411 if key_type != "functionKey" :
@@ -521,14 +415,14 @@ def list_function_keys(cls, cmd, resource_group_name, name, key_type, function_n
521415 command_fmt = "/bin/azure-functions-admin keys list --key-type {} --function-name {}"
522416 command = command_fmt .format (key_type , function_name )
523417
524- r = containerapp_debug (
418+ r = execute_function_admin_command (
525419 cmd = cmd ,
526420 resource_group_name = resource_group_name ,
527421 name = name ,
528- container = container_name ,
529- revision = revision_name ,
530- replica = replica_name ,
531- debug_command = command
422+ command = command ,
423+ revision_name = revision_name ,
424+ replica_name = replica_name ,
425+ container_name = container_name
532426 )
533427 if not r :
534428 raise CLIError (f"Error retrieving function keys of type '{ key_type } '." )
@@ -537,7 +431,7 @@ def list_function_keys(cls, cmd, resource_group_name, name, key_type, function_n
537431 @classmethod
538432 def set_function_keys (cls , cmd , resource_group_name , name , key_type , key_name , key_value , function_name = None , revision_name = None , replica_name = None , container_name = None ):
539433 """Set/Update function keys based on key type"""
540- from .custom import containerapp_debug
434+ from ._utils import execute_function_admin_command
541435
542436 command_fmt = ""
543437 if key_type != "functionKey" :
@@ -550,14 +444,14 @@ def set_function_keys(cls, cmd, resource_group_name, name, key_type, key_name, k
550444 if key_value is not None :
551445 command += " --key-value {}" .format (key_value )
552446
553- r = containerapp_debug (
447+ r = execute_function_admin_command (
554448 cmd = cmd ,
555449 resource_group_name = resource_group_name ,
556450 name = name ,
557- container = container_name ,
558- revision = revision_name ,
559- replica = replica_name ,
560- debug_command = command
451+ command = command ,
452+ revision_name = revision_name ,
453+ replica_name = replica_name ,
454+ container_name = container_name
561455 )
562456 if not r :
563457 raise CLIError (f"Error setting function key '{ key_name } ' of type '{ key_type } '." )
0 commit comments