Search code examples
azurepowershellazure-powershellazure-databricks

Set-AzDiagnosticSetting - Skip 'metricsCategory' for Azure databricks


I have the below 'generic' code that sets the diagnostic setting for all my Azure resources. However, I am not able to use it for Azure Databricks, as the Azure databricks does not have 'MetricsCategory' but just the log category.

How can I bypass the 'MetricCategory' in the '$diagParams' parameter. I do not want to change the entire function just for Azure Databricks. It defeats the very purpose I created the Generic Function.

Code:

$diagParams = @{
        Name                = "coemonitoreu"
        ResourceId          = "/subscriptions/-f30a-4bfd-a6be-1c59594b8592/resourcegroups/manjug-dev/providers/Microsoft.Databricks/workspaces/manjug-adbv-dev"
        StorageAccountId    = "/subscriptions/-2ad9-4167-b9f2-45be0e48c465/resourcegroups/-monitor-dev/providers/microsoft.storage/storageaccounts/monitorblobdev"
        WorkspaceId         = "/subscriptions/-2ad9-4167-b9f2-45be0e48c465/resourcegroups/-monitor-dev/providers/microsoft.operationalinsights/workspaces/diag-dev"
        MetricCategory      = @("AllMetrics")
        Category            = @("dbfs", "clusters", "accounts", "jobs", "notebook", "ssh", "workspace", "secrets", "sqlPermissions", "instancePools")
        Enabled             = $true
        RetentionEnabled    = $true
        RetentionInDays     = 0
    }
$DebugPreference = 'continue'
Set-AzDiagnosticSetting @diagParams

I get the error, because I am mentioning 'metrics' in my parameter set. And this is not consumed by Azure databricks diagnostic setting.

Set-AzDiagnosticSetting : Exception type: ErrorResponseException, Message: Null/Empty, Code: BadRequest, Status code:BadRequest, Reason phrase: Bad Request
At line:14 char:1
+ Set-AzDiagnosticSetting @diagParams
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzDiagnosticSetting], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Diagnostics.SetAzureRmDiagnosticSettingCommand

DEBUG: AzureQoSEvent: CommandName - Set-AzDiagnosticSetting; IsSuccess - False; Duration - 00:00:01.8132017; Exception - System.Management.Automation.PSInvalidOperationEx
ception: Exception type: ErrorResponseException, Message: Null/Empty, Code: BadRequest, Status code:BadRequest, Reason phrase: Bad Request ---> Microsoft.Azure.Management
.Monitor.Models.ErrorResponseException: Operation returned an invalid status code 'BadRequest'
   at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperations.<CreateOrUpdateWithHttpMessagesAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperationsExtensions.<CreateOrUpdateAsync>d__3.MoveNext()
   --- End of inner exception stack trace ---
   at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord();
DEBUG: Finish sending metric.
DEBUG: 5:38:51 AM - SetAzureRmDiagnosticSettingCommand end processing.
DEBUG: 5:38:51 AM - SetAzureRmDiagnosticSettingCommand end processing.

Solution

  • Well, to create an exclusion you should use if statement:

    if ($diagParam.resourceId -match 'DataBricks') {
        $diagParams.Remove('MetricCategory')
    }