Skip to content

Instantly share code, notes, and snippets.

@jlattimer
Created January 5, 2019 23:21
Show Gist options
  • Save jlattimer/2cc1f31c5995cfbd35ba98a7ddfd38fc to your computer and use it in GitHub Desktop.
Save jlattimer/2cc1f31c5995cfbd35ba98a7ddfd38fc to your computer and use it in GitHub Desktop.
Send Azure DevOps build time to Application Insights as a custom metric #blog
$startBuild = Get-Date -Date "$Env:System_PipelineStartTime"
Write-Host "Start time: $startBuild"
$endBuild = Get-Date
Write-Host "End time: $endBuild"
$ts = New-TimeSpan -Start $startBuild -End $endBuild
$tm = [math]::Round($ts.TotalMinutes, 2)
Write-Host "Build time (min): $tm"
# $(AppInsightsKey) should be your Application Insights Instrumentation Key
if (-Not ('$(AppInsightsKey)' -match("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")))
{
Write-Host "Invalid Application Insights Instrumentation Key"
exit 1
}
else
{
$iKey = '$(AppInsightsKey)'
$date = $endBuild.ToUniversalTime()
$json =
'{
"data": {
"baseData": {
"metrics": [
{
"name": "Build Time: $(Build.DefinitionName)",
"kind": 0,
"value": ' + $tm + '
}
],
"properties": {
"source": "Azure DevOps",
"buildNumber": $(Build.BuildNumber),
"jobStatus": "$(Agent.JobStatus)"
}
},
"baseType": "MetricData"
},
"iKey": "' + $iKey + '",
"name": "Microsoft.ApplicationInsights.' + $iKey + '.Metric",
"time": "' + $date + '"
}'
$response = Invoke-RestMethod 'https://dc.services.visualstudio.com/v2/track' -Method POST -Body $json -ContentType 'application/json'
if ($response.itemsAccepted -eq 1)
{
Write-Host 'Logged build time to Application Insights'
}
else
{
Write-Host 'Failed logging build time to Application Insights'
exit 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment