Skip to content

Commit

Permalink
Port from master: Fix service fabric tasks telemetry (#7807) (#7813)
Browse files Browse the repository at this point in the history
  • Loading branch information
bishal-pdMSFT committed Jul 24, 2018
1 parent bbc742b commit 5926505
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 74 deletions.
38 changes: 0 additions & 38 deletions Tasks/Common/Deployment/TelemetryHelper/TelemetryHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,6 @@ function Write-Telemetry
Write-Host $telemetryString
}

function Get-ExceptionData
{
param(
[System.Management.Automation.ErrorRecord]
$error
)

$exceptionData = ""
try
{
$src = $error.InvocationInfo.PSCommandPath + "|" + $error.InvocationInfo.ScriptLineNumber
$exceptionTypes = ""

$exception = $error.Exception
if ($exception.GetType().Name -eq 'AggregateException')
{
$flattenedException = ([System.AggregateException]$exception).Flatten()
$flattenedException.InnerExceptions | ForEach-Object {
$exceptionTypes += $_.GetType().FullName + ";"
}
}
else
{
do
{
$exceptionTypes += $exception.GetType().FullName + ";"
$exception = $exception.InnerException
} while ($exception -ne $null)
}
$exceptionData = "$exceptionTypes|$src"
}
catch
{}

return $exceptionData
}

# Export only the public function.
Export-ModuleMember -Function Write-Telemetry
Export-ModuleMember -Function Get-ExceptionData
Export-ModuleMember -Variable telemetryCodes
67 changes: 67 additions & 0 deletions Tasks/Common/ServiceFabricHelpers/CommonHelpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function Publish-Telemetry
{
Param (
[String]
$TaskName,

[String]
$OperationId,

[System.Management.Automation.ErrorRecord]
$ErrorData
)

try
{
$telemetryData = @{
'OperationId' = $OperationId
'ExceptionData' = (Get-ExceptionData $ErrorData)
'JobId' = (Get-VstsTaskVariable -Name 'System.JobId')
}

$telemetryJson = ConvertTo-Json $telemetryData -Compress
Write-Host "##vso[telemetry.publish area=TaskHub;feature=$TaskName]$telemetryJson"
}
catch
{
# suppress
Write-Warning (Get-VstsLocString -Key TelemetryWarning -ArgumentList $_)
}
}

function Get-ExceptionData
{
param(
[System.Management.Automation.ErrorRecord]
$error
)

$exceptionData = ""
try
{
$src = $error.InvocationInfo.PSCommandPath + "|" + $error.InvocationInfo.ScriptLineNumber
$exceptionTypes = ""

$exception = $error.Exception
if ($exception.GetType().Name -eq 'AggregateException')
{
$flattenedException = ([System.AggregateException]$exception).Flatten()
$flattenedException.InnerExceptions | ForEach-Object {
$exceptionTypes += $_.GetType().FullName + ";"
}
}
else
{
do
{
$exceptionTypes += $exception.GetType().FullName + ";"
$exception = $exception.InnerException
} while ($exception -ne $null)
}
$exceptionData = "$exceptionTypes|$src"
}
catch
{}

return $exceptionData
}
3 changes: 2 additions & 1 deletion Tasks/Common/ServiceFabricHelpers/ServiceFabricHelpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Find-VstsFiles -LiteralDirectory $PSScriptRoot -LegacyPattern "*.ps1" | ForEach
Export-ModuleMember -Function Connect-ServiceFabricClusterFromServiceEndpoint
Export-ModuleMember -Function Remove-ClientCertificate
Export-ModuleMember -Function Get-ServiceFabricEncryptedText
Export-ModuleMember -Variable SF_Operations
Export-ModuleMember -Variable SF_Operations
Export-ModuleMember -Function Publish-Telemetry
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"loc.messages.UseWindowsSecurity": "Windows security is specified. -WindowsCredential switch will be used while connecting.",
"loc.messages.ServerCertificateNotFoundForTextEncrypt": "The cluster's server certificate with thumbprint '{0}' is required in order to encrypt text but the certificate could not be found on the agent machine in the 'CurrentUser\\My' certificate store location.",
"loc.messages.RetryingClusterConnection": "Retrying to connect to cluster..",
"loc.messages.WarningOnRemoveCertificate": "An error occurred attempting to remove the client certificate. Error message: {0}"
"loc.messages.WarningOnRemoveCertificate": "An error occurred attempting to remove the client certificate. Error message: {0}",
"loc.messages.TelemetryWarning": "Unable to push telemetry data. Error: {0}."
}
3 changes: 2 additions & 1 deletion Tasks/Common/ServiceFabricHelpers/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"UseWindowsSecurity": "Windows security is specified. -WindowsCredential switch will be used while connecting.",
"ServerCertificateNotFoundForTextEncrypt": "The cluster's server certificate with thumbprint '{0}' is required in order to encrypt text but the certificate could not be found on the agent machine in the 'CurrentUser\\My' certificate store location.",
"RetryingClusterConnection": "Retrying to connect to cluster..",
"WarningOnRemoveCertificate": "An error occurred attempting to remove the client certificate. Error message: {0}"
"WarningOnRemoveCertificate": "An error occurred attempting to remove the client certificate. Error message: {0}",
"TelemetryWarning": "Unable to push telemetry data. Error: {0}."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
param()

Trace-VstsEnteringInvocation $MyInvocation
Import-Module $PSScriptRoot\ps_modules\TelemetryHelper

$certificate = $null
try
Expand Down Expand Up @@ -277,8 +276,7 @@ try
}
catch
{
$exceptionData = Get-ExceptionData $_
Write-Telemetry "Task_InternalError" "$global:operationId|$exceptionData"
Publish-Telemetry -TaskName 'ServiceFabricComposeDeploy' -OperationId $global:operationId -ErrorData $_
throw
}
finally
Expand Down
4 changes: 0 additions & 4 deletions Tasks/ServiceFabricComposeDeployV0/make.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
"module": "../Common/TlsHelper_",
"type": "ps"
},
{
"module": "../Common/Deployment/TelemetryHelper",
"type": "ps"
},
{
"module": "../Common/PowershellHelpers",
"type": "ps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricComposeDeployV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 0,
"Minor": 3,
"Patch": 5
"Patch": 7
},
"demands": [
"Cmd"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricComposeDeployV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 0,
"Minor": 3,
"Patch": 5
"Patch": 7
},
"demands": [
"Cmd"
Expand Down
4 changes: 1 addition & 3 deletions Tasks/ServiceFabricDeployV1/deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ try
. "$PSScriptRoot\utilities.ps1"
Import-Module $PSScriptRoot\ps_modules\ServiceFabricHelpers
Import-Module $PSScriptRoot\ps_modules\PowershellHelpers
Import-Module $PSScriptRoot\ps_modules\TelemetryHelper

$global:operationId = $SF_Operations.Undefined

Expand Down Expand Up @@ -196,8 +195,7 @@ try
}
catch
{
$exceptionData = Get-ExceptionData $_
Write-Telemetry "Task_InternalError" "$global:operationId|$exceptionData"
Publish-Telemetry -TaskName 'ServiceFabricDeploy' -OperationId $global:operationId -ErrorData $_
throw
}
finally
Expand Down
4 changes: 0 additions & 4 deletions Tasks/ServiceFabricDeployV1/make.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
{
"module": "../Common/TlsHelper_",
"type": "ps"
},
{
"module": "../Common/Deployment/TelemetryHelper",
"type": "ps"
}
],
"externals": {
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricDeployV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 7,
"Patch": 17
"Patch": 19
},
"demands": [
"Cmd"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricDeployV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 7,
"Patch": 17
"Patch": 19
},
"demands": [
"Cmd"
Expand Down
19 changes: 10 additions & 9 deletions Tasks/ServiceFabricPowerShellV1/ServiceFabricPowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ if ($scriptArguments -match '[\r\n]')
throw (Get-VstsLocString -Key InvalidScriptArguments0 -ArgumentList $scriptArguments)
}

Import-Module $PSScriptRoot\ps_modules\TelemetryHelper

$certificate = $null
try
{
Expand All @@ -36,7 +34,16 @@ try
$global:operationId = $SF_Operations.Undefined
$connectedServiceEndpoint = Get-VstsEndpoint -Name $serviceConnectionName -Require
$clusterConnectionParameters = @{}
$certificate = Connect-ServiceFabricClusterFromServiceEndpoint -ClusterConnectionParameters $clusterConnectionParameters -ConnectedServiceEndpoint $connectedServiceEndpoint

try
{
$certificate = Connect-ServiceFabricClusterFromServiceEndpoint -ClusterConnectionParameters $clusterConnectionParameters -ConnectedServiceEndpoint $connectedServiceEndpoint
}
catch
{
Publish-Telemetry -TaskName 'ServiceFabricPowerShell' -OperationId $global:operationId -ErrorData $_
throw
}

# Trace the expression as it will be invoked.
If ($scriptType -eq "InlineScript")
Expand Down Expand Up @@ -90,12 +97,6 @@ try
}
}
}
catch
{
$exceptionData = Get-ExceptionData $_
Write-Telemetry "Task_InternalError" "$global:operationId|$exceptionData"
throw
}
Finally
{
If ($scriptType -eq "InlineScript" -and (Test-Path -LiteralPath $scriptPath) -eq $true )
Expand Down
4 changes: 0 additions & 4 deletions Tasks/ServiceFabricPowerShellV1/make.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
"module": "../Common/TlsHelper_",
"type": "ps"
},
{
"module": "../Common/Deployment/TelemetryHelper",
"type": "ps"
},
{
"module": "../Common/PowershellHelpers",
"type": "ps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricPowerShellV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 14
"Patch": 16
},
"demands": [
"Cmd"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricPowerShellV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 14
"Patch": 16
},
"demands": [
"Cmd"
Expand Down

0 comments on commit 5926505

Please sign in to comment.