以 table 格式将 Azure 自动化输出发送到 application insights

Sending Azure automation output to application insights in a table format

我的主要问题是:我想将 Azure SQL 中最慢的 运行 查询发送到集中式日志系统以进行负载测试调试。

如何将我的结果集发送到 Application insights?我想将最慢的 运行 查询从 Azure 自动化发送到 Application Insights ?

当它是 table 格式时,我已经试过了,但运气不好

workflow Use-SqlCommandSample
{
param(
    [parameter(Mandatory=$True)]
    [string] $SqlServer,

    [parameter(Mandatory=$False)]
    [int] $SqlServerPort = 1433,

    [parameter(Mandatory=$True)]
    [string] $Database,

    [parameter(Mandatory=$True)]
    [string] $Table,

    [parameter(Mandatory=$True)]
    [PSCredential] $SqlCredential
)

# Get the username and password from the SQL Credential
$SqlUsername = $SqlCredential.UserName
$SqlPass = $SqlCredential.GetNetworkCredential().Password

inlinescript {
    # Define the connection to the SQL Database
    $Conn = New-Object System.Data.SqlClient.SqlConnection("xxxx")

    # Open the SQL connection
    $Conn.Open()
    $Cmd=new-object system.Data.SqlClient.SqlCommand("SELECT  top 10 creation_time"+ 
       ",last_execution_time"+
        ",total_physical_reads"+
        ",total_logical_reads "+
       ",total_logical_writes"+
        ", execution_count"+
        ", total_worker_time"+
       " , total_elapsed_time"+
        ", total_elapsed_time / execution_count avg_elapsed_time"+
        ",SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,"+
        " ((CASE statement_end_offset"+
        "  WHEN -1 THEN DATALENGTH(st.text)"+
        "  ELSE qs.statement_end_offset END"+
        "    - qs.statement_start_offset)/2) + 1) AS statement_text"+
        "  FROM sys.dm_exec_query_stats AS qs"+
        "  CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st"+
        " ORDER BY total_elapsed_time / execution_count DESC;", $Conn)
    $Cmd.CommandTimeout=120

    # Execute the SQL command
    $Ds=New-Object system.Data.DataSet
    $Da=New-Object system.Data.SqlClient.SqlDataAdapter($Cmd)
    [void]$Da.fill($Ds)



      $assemblyPath = 
     "C:\Modules\Global\Azure\Compute\Microsoft.ApplicationInsights.dll"
     [System.Reflection.Assembly]::LoadFrom($assemblyPath)
     $TelClient = New-Object "Microsoft.ApplicationInsights.TelemetryClient"
     $TelClient.InstrumentationKey = "1234"

     # Output the result


     $TelClient.TrackEvent($Ds.Tables)
     $TelClient.Flush

    # Close the SQL connection
    $Conn.Close()
}

}

您确定您已成功登录 SQL 服务器。我检查了你脚本中的行,

$Conn = New-Object System.Data.SqlClient.SqlConnection("xxxx")

我觉得有一点你需要注意,根据这个official document

By default, the variables that are defined in a workflow are not visible to the commands in the InlineScript script block. To make workflow variables visible to the InlineScript, use the $Using scope modifier. The $Using scope modifier is required only once for each variable in the InlineScript.

如果您想在内联脚本中使用 $SqlUsername$SqlPass,您应该使用以下行。

inlinescript {

# Get the username and password from workflow
$ServerName = $Using:SqlUsername
$Password = $Using:SqlPass
     ......
}

更多信息请参考link:Azure Automation: Your SQL Agent in the Cloud

在 Application Insights 事件中可以使用名称、值 属性 包。因此,您最好将 table 序列化为 json 以将其提交给 AppInsights