Skip to content

Commit

Permalink
Added telemetries (#7474) (#7516)
Browse files Browse the repository at this point in the history
* Added telemetries

* Updating reference values

* Updated code as per review comments

* Adding a text message before force removing application package

* Modified code as per review comments

* re-factor telemetry to common code and re-use across all SF tasks

* make.json changes

* PR comments

# Conflicts:
#	Tasks/ServiceFabricDeployV1/task.json
#	Tasks/ServiceFabricDeployV1/task.loc.json
  • Loading branch information
bishal-pdMSFT authored Jun 20, 2018
1 parent 3a2159d commit 471aeee
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 79 deletions.
93 changes: 67 additions & 26 deletions Tasks/Common/Deployment/TelemetryHelper/TelemetryHelper.psm1
Original file line number Diff line number Diff line change
@@ -1,38 +1,79 @@
# Telemetry Codes
$telemetryCodes =
$telemetryCodes =
@{
"Input_Validation" = "Input_Validation_Error";
"Task_InternalError" = "Task_Internal_Error";
"DTLSDK_Error" = "Dtl_Sdk_Error";
}
"Input_Validation" = "Input_Validation_Error";
"Task_InternalError" = "Task_Internal_Error";
"DTLSDK_Error" = "Dtl_Sdk_Error";
}

# Telemetry Write Method
# Telemetry Write Method
function Write-Telemetry
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$codeKey,
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, Position = 1)]
[string]$codeKey,

[Parameter(Position = 2)]
[string]$errorMsg
)

$erroCodeMsg = $telemetryCodes[$codeKey]

## If no error is passed mark it as not available
if ([string]::IsNullOrEmpty($errorMsg))
{
$errorMsg = "No error details available"
}

$errorCode = @{
$erroCodeMsg = $errorMsg
}

[Parameter(Position=2)]
[string]$errorMsg
## Form errorcode as json string
$erroCode = ConvertTo-Json -InputObject $errorCode -Compress
$telemetryString = "##vso[task.logissue type=error;code=" + $erroCode + ";]"
Write-Host $telemetryString
}

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

$erroCodeMsg = $telemetryCodes[$codeKey]

## If no error is passed mark it as not available
if([string]::IsNullOrEmpty($errorMsg))
{
$errorMsg = "No error details available"
}
$erroCode = ('"{0}":{1}' -f $erroCodeMsg, $errorMsg)
## Form errorcode as json string
$erroCode = '{' + $erroCode + '}'

$telemetryString = "##vso[task.logissue type=error;code=" + $erroCode + ";]"
Write-Host $telemetryString

$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
20 changes: 11 additions & 9 deletions Tasks/Common/PowershellHelpers/Helpers.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Invoke-ActionWithRetries {
function Invoke-ActionWithRetries
{
[CmdletBinding()]
param(
[scriptblock]
Expand All @@ -14,7 +15,7 @@ function Invoke-ActionWithRetries {
$RetryIntervalInSeconds = 1,

[string[]]
[ValidateScript({[System.Exception].IsAssignableFrom([type]$_)})]
[ValidateScript( {[System.Exception].IsAssignableFrom([type]$_)})]
$RetryableExceptions,

[string]
Expand All @@ -23,7 +24,7 @@ function Invoke-ActionWithRetries {

Trace-VstsEnteringInvocation $MyInvocation

if(!$RetryMessage)
if (!$RetryMessage)
{
$RetryMessage = Get-VstsLocString -Key RetryAfterMessage $RetryIntervalInSeconds
}
Expand All @@ -40,7 +41,7 @@ function Invoke-ActionWithRetries {
}
catch
{
if(($null -eq $RetryableExceptions) -or (Test-RetryableException -Exception $_.Exception -AllowedExceptions $RetryableExceptions))
if (($null -eq $RetryableExceptions) -or (Test-RetryableException -Exception $_.Exception -AllowedExceptions $RetryableExceptions))
{
$exception = $_.Exception
}
Expand All @@ -50,14 +51,14 @@ function Invoke-ActionWithRetries {
}
}

if(!$exception -and (!$result -or $ActionSuccessValidator.Invoke($result)))
if (!$exception -and (!$result -or $ActionSuccessValidator.Invoke($result)))
{
return $result
}

if($retryIteration -eq $MaxTries)
if ($retryIteration -eq $MaxTries)
{
if($exception)
if ($exception)
{
throw $exception
}
Expand Down Expand Up @@ -94,7 +95,8 @@ function Get-TempDirectoryPath
return $envTemp
}

function Test-RetryableException {
function Test-RetryableException
{
[CmdletBinding()]
param(
[System.Object]
Expand All @@ -105,7 +107,7 @@ function Test-RetryableException {
)

$AllowedExceptions | ForEach-Object {
if($_ -and ($Exception -is ([type]$_)))
if ($_ -and ($Exception -is ([type]$_)))
{
return $true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Get-AadSecurityToken
$connectionParametersWithGetMetadata.Add("GetMetadata", $true)

# Query cluster metadata
$global:operationId = $SF_Operations.ConnectClusterMetadata
$connectResult = Connect-ServiceFabricCluster @connectionParametersWithGetMetadata
$authority = $connectResult.AzureActiveDirectoryMetadata.Authority
Write-Host (Get-VstsLocString -Key AadAuthority -ArgumentList $authority)
Expand Down Expand Up @@ -109,7 +110,6 @@ function Add-Certificate

return $certificate
}

function Connect-ServiceFabricClusterFromServiceEndpoint
{
[CmdletBinding()]
Expand Down Expand Up @@ -180,6 +180,7 @@ function Connect-ServiceFabricClusterFromServiceEndpoint
}

# Connect to cluster
$global:operationId = $SF_Operations.ConnectCluster
try
{
[void](Connect-ServiceFabricCluster @clusterConnectionParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Get-ServiceFabricEncryptedText

if ($serverCertThumbprints -is [array])
{
foreach($serverCertThumbprint in $serverCertThumbprints)
foreach ($serverCertThumbprint in $serverCertThumbprints)
{
$cert = Get-Item "Cert:\$defaultCertStoreLocation\$defaultCertStoreName\$serverCertThumbprint" -ErrorAction SilentlyContinue
if ($cert)
Expand All @@ -37,5 +37,6 @@ function Get-ServiceFabricEncryptedText
}

# Encrypt the text using the cluster connection's certificate.
$global:operationId = $SF_Operations.EncryptServiceFabricText
return Invoke-ServiceFabricEncryptText -Text $Text -CertStore -CertThumbprint $cert.Thumbprint -StoreName $defaultCertStoreName -StoreLocation $defaultCertStoreLocation
}
25 changes: 25 additions & 0 deletions Tasks/Common/ServiceFabricHelpers/SFOperations.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
$SF_Operations = @{
Undefined = 'Undefined';
ConnectCluster = 'ConnectCluster';
ConnectClusterMetadata = 'ConnectClusterMetadata';
TestClusterConnection = 'TestApplicationPackage';
TestApplicationPackage = 'TestApplicationPackage';
GetApplication = 'GetApplication';
GetApplicationType = 'GetApplicationType';
GetClusterManifest = 'GetClusterManifest';
RemoveApplication = 'RemoveApplication';
UnregisterApplicationType = 'UnregisterApplicationType';
CopyApplicationPackage = 'CopyApplicationPackage';
RegisterApplicationType = 'RegisterApplicationType';
RemoveApplicationPackage = 'RemoveApplicationPackage';
CreateNewApplication = 'CreateNewApplication'
StartApplicationUpgrade = 'StartApplicationUpgrade';
GetApplicationUpgradeStatus = 'GetApplicationUpgradeStatus';
EncryptServiceFabricText = 'EncryptServiceFabricText';
CreateDiffPackage = 'CreateDiffPackage';
GetComposeDeploymentStatus = 'GetComposeDeploymentStatus';
RemoveComposeDeployment = 'RemoveComposeDeployment';
GetComposeDeploymentUpgradeStatus = 'GetComposeDeploymentUpgradeStatus';
StartComposeDeploymentUpgrade = 'StartComposeDeploymentUpgrade';
CreateNewComposeDeployment = 'CreateNewComposeDeployment'
}
3 changes: 2 additions & 1 deletion Tasks/Common/ServiceFabricHelpers/ServiceFabricHelpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Import-VstsLocStrings "$PSScriptRoot\module.json"
Find-VstsFiles -LiteralDirectory $PSScriptRoot -LegacyPattern "*.ps1" | ForEach { . $_ }

Export-ModuleMember -Function Connect-ServiceFabricClusterFromServiceEndpoint
Export-ModuleMember -Function Get-ServiceFabricEncryptedText
Export-ModuleMember -Function Get-ServiceFabricEncryptedText
Export-ModuleMember -Variable SF_Operations
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ try
. "$PSScriptRoot\utilities.ps1"
Import-Module $PSScriptRoot\ps_modules\ServiceFabricHelpers

$global:operationId = $SF_Operations.Undefined

# Get inputs.
$serviceConnectionName = Get-VstsInput -Name serviceConnectionName -Require
$connectedServiceEndpoint = Get-VstsEndpoint -Name $serviceConnectionName -Require
Expand Down Expand Up @@ -98,6 +100,7 @@ try

# Test the compose file
Write-Host (Get-VstsLocString -Key CheckingComposeFile)
$global:operationId = $SF_Operations.TestApplicationPackage
$valid = Test-ServiceFabricApplicationPackage -ComposeFilePath $composeFilePath -ErrorAction Stop

# Connect to the cluster
Expand Down Expand Up @@ -247,8 +250,8 @@ try
Write-Host (Get-VstsLocString -Key WaitingForDeploy)
$newApplication = Get-ServiceFabricComposeApplicationStatusHelper -ApiVersion $apiVersion -GetStatusParameters $getStatusParameters
while (($newApplication -eq $null) -or `
($newApplication.Status -eq 'Provisioning') -or `
($newApplication.Status -eq 'Creating'))
($newApplication.Status -eq 'Provisioning') -or `
($newApplication.Status -eq 'Creating'))
{
if ($newApplication -eq $null)
{
Expand Down
6 changes: 5 additions & 1 deletion Tasks/ServiceFabricComposeDeployV0/make.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
{
"module": "../Common/TlsHelper_",
"type": "ps"
},
{
"module": "../Common/Deployment/TelemetryHelper",
"type": "ps"
}
],
"externals": {
Expand All @@ -32,4 +36,4 @@
}
]
}
}
}
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": 0
"Patch": 1
},
"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": 0
"Patch": 1
},
"demands": [
"Cmd"
Expand Down
9 changes: 8 additions & 1 deletion Tasks/ServiceFabricComposeDeployV0/utilities.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function Get-ServiceFabricComposeApplicationStatusHelper
$GetStatusParameters
)

$global:operationId = $SF_Operations.GetComposeDeploymentStatus
switch ($ApiVersion)
{
"255.255"
Expand Down Expand Up @@ -127,6 +128,8 @@ function Remove-ServiceFabricComposeApplicationHelper
$RemoveParameters
)

$global:operationId = $SF_Operations.RemoveComposeDeployment

switch ($ApiVersion)
{
"255.255"
Expand Down Expand Up @@ -156,6 +159,8 @@ function New-ServiceFabricComposeApplicationHelper
$DeployParameters
)

$global:operationId = $SF_Operations.CreateNewComposeDeployment

switch ($ApiVersion)
{
"255.255"
Expand Down Expand Up @@ -185,6 +190,7 @@ function Start-ServiceFabricComposeDeploymentUpgradeHelper
$UpgradeParameters
)

$global:operationId = $SF_Operations.StartComposeDeploymentUpgrade
Start-ServiceFabricComposeDeploymentUpgrade @UpgradeParameters
}

Expand All @@ -200,6 +206,7 @@ function Get-ServiceFabricComposeDeploymentUpgradeHelper
$GetUpgradeParameters
)

$global:operationId = $SF_Operations.GetComposeDeploymentUpgradeStatus
$composeDeploymentUpgrade = Get-ServiceFabricComposeDeploymentUpgrade @GetUpgradeParameters

if ($composeDeploymentUpgrade -eq $null)
Expand Down Expand Up @@ -265,7 +272,7 @@ function Test-ApplicationName
return
}
Default
{
{
if ($ApplicationName.StartsWith("fabric:/", [StringComparison]::OrdinalIgnoreCase))
{
Write-Warning (Get-VstsLocString -Key InvalidApplicationNameWarning -ArgumentList $ApplicationName)
Expand Down
Loading

0 comments on commit 471aeee

Please sign in to comment.