Skip to content

Commit

Permalink
Log status for publish errors with client id (Azure#2240)
Browse files Browse the repository at this point in the history
Log client context with errors and general log messages in opc ua client.
  • Loading branch information
marcschier authored Jun 10, 2024
1 parent 46ebdbf commit a49d38f
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 165 deletions.
13 changes: 7 additions & 6 deletions deploy/scripts/deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -841,16 +841,17 @@ Function New-Deployment() {
}
else {
if ($script:acrSubscriptionName -eq "IOT_GERMANY") {
if (($script:acrRegistryName -eq "industrialiot") -or `
($script:acrRegistryName -eq "industrialiotprod")) {
$namespace = "public"
}
elseif ($script:acrRegistryName -eq "industrialiotdev") {
if ($script:acrRegistryName -eq "industrialiotdev") {
$namespace = $script:branchName
if ($script:branchName.StartsWith("feature/")) {
$namespace = $namespace.Replace("feature/", "")
}
$namespace = $namespace.Replace("_", "/").Substring(0, [Math]::Min($namespace.Length, 24))
$namespace = $namespace.Replace("_", "/")
$namespace = $namespace.Substring(0, [Math]::Min($namespace.Length, 24))
}
elseif (($script:acrRegistryName -eq "industrialiot") -or `
($script:acrRegistryName -eq "industrialiotprod")) {
$namespace = "public"
}
}
}
Expand Down
33 changes: 23 additions & 10 deletions e2e-tests/OpcPublisher-E2E-Tests/Standalone/DynamicAciTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,30 @@ protected async Task PublishNodesAsync(string json, CancellationToken ct)

protected async Task UnpublishAllNodesAsync(CancellationToken ct = default)
{
var result = await CallMethodAsync(
new MethodParameterModel
{
Name = TestConstants.DirectMethodNames.UnpublishAllNodes,
MethodResultModel result = null;
for (var i = 0; i < 5; i++)
{
result = await CallMethodAsync(
new MethodParameterModel
{
Name = TestConstants.DirectMethodNames.UnpublishAllNodes,

// TODO: Remove this line to test fix for null request crash
JsonPayload = _serializer.SerializeToString(new PublishedNodesEntryModel())
},
ct
).ConfigureAwait(false);
Assert.Equal((int)HttpStatusCode.OK, result.Status);
// TODO: Remove this line to test fix for null request crash
JsonPayload = _serializer.SerializeToString(new PublishedNodesEntryModel())
},
ct
).ConfigureAwait(false);

if (result.Status == 405)
{
// Retry if method not yet mounted
_context.OutputHelper?.WriteLine(result.JsonPayload);
await Task.Delay(TestConstants.DefaultDelayMilliseconds, ct);
continue;
}
break;
}
Assert.Equal((int)HttpStatusCode.OK, result?.Status);

var result1 = await CallMethodAsync(
new MethodParameterModel
Expand Down
147 changes: 80 additions & 67 deletions src/Azure.IIoT.OpcUa.Publisher/src/Stack/Services/OpcUaClient.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected override void Dispose(bool disposing)
try
{
_cts.Cancel();
_logger.LogInformation("Session {Session} disposed.", this);
_logger.LogInformation("{Session}: Session disposed.", this);
}
finally
{
Expand All @@ -145,7 +145,8 @@ protected override void Dispose(bool disposing)
}

/// <inheritdoc/>
public override Session CloneSession(ITransportChannel channel, bool copyEventHandlers)
public override Session CloneSession(ITransportChannel channel,
bool copyEventHandlers)
{
return new OpcUaSession(this, channel, this, copyEventHandlers);
}
Expand Down Expand Up @@ -233,8 +234,8 @@ public async ValueTask<HistoryServerCapabilitiesModel> GetHistoryCapabilitiesAsy
catch (Exception ex)
{
_logger.LogError(ex,
"Attempt #{Attempt}. Failed to get complex type system for client {Client}.",
attempt, this);
"{Session}: Attempt #{Attempt}. Failed to get complex type system.",
this, attempt);

// Try again. TODO: Throttle using a timer or so...
_complexTypeSystem = LoadComplexTypeSystemAsync();
Expand Down Expand Up @@ -966,7 +967,7 @@ private Task<ComplexTypeSystem> LoadComplexTypeSystemAsync()
if (Connected)
{
_logger.LogInformation(
"Complex type system loaded into client {Client}.", this);
"{Session}: Complex type system loaded into client.", this);
// Clear cache to release memory.
// TODO: we should have a real node cache here
Expand Down
5 changes: 5 additions & 0 deletions tools/e2etesting/DeployTestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Write-Host "KeyVault: $($keyVault.VaultName)"
Write-Host "IoTHub: $($IoTHubName)"
Write-Host "=============================================="

Write-Host "##vso[build.addbuildtag]$($suffix)"
Write-Host "##vso[build.addbuildtag]$($context.Subscription.Id)"
Write-Host "##vso[build.addbuildtag]$($ResourceGroupName)"
Write-Host "##vso[build.addbuildtag]$($keyVault.VaultName)"

## Check existence of IoT Hub ##

if (!$IoTHubName) {
Expand Down
2 changes: 1 addition & 1 deletion tools/e2etesting/K8s-Standalone/steps/deployaks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ steps:
Write-Host "##vso[task.setvariable variable=ServicePrincipalKey]$($env:servicePrincipalKey)"
- task: AzurePowerShell@5
displayName: 'Auto-Set Variables'
displayName: 'Select Image to test'
inputs:
azureSubscription: '$(AzureSubscription)'
azurePowerShellVersion: 'latestVersion'
Expand Down
129 changes: 72 additions & 57 deletions tools/e2etesting/SetVariables.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,61 @@
$ErrorActionPreference = "Stop"
# Set-PSDebug -Trace 2

if (![string]::IsNullOrEmpty($script:BranchName))
$registry = $script:ContainerRegistryServer
if ([string]::IsNullOrWhiteSpace($registry))
{
if ($script:BranchName.StartsWith("refs/heads/"))
Write-Host "No container registry provided, using default."
$registry = "industrialiotdev"
}

function Get-ContainerRegistrySecret
{
param(
[Parameter(Mandatory=$true)]
[string] $keyVaultName,

[Parameter(Mandatory=$true)]
[string] $secret
)
$secretValueSec = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $secret
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secretValueSec.SecretValue)
try
{
$secretValueText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
}
finally
{
$script:BranchName = $script:BranchName.Replace("refs/heads/", "")
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}
Write-Host "##vso[task.setvariable variable=$($secret)]$($secretValueText)"
}

$KeyVaultName = $null
if ($registry -eq "industrialiot")
{
$KeyVaultName = "kv-release-pipeline"
}
if ($registry -eq "industrialiotdev")
{
$KeyVaultName = "kv-developer-pipeline"
}
if ($registry -eq "industrialiotprod")
{
# $KeyVaultName = "kv-release-pipeline" #todo
}
else
if ($KeyVaultName)
{
$script:BranchName = "main"
Write-Host "Looking up credentials for $($registry) registry in KV $KeyVaultName."
Get-ContainerRegistrySecret -keyVaultName $KeyVaultName -secret "ContainerRegistryPassword"
Get-ContainerRegistrySecret -keyVaultName $KeyVaultName -secret "ContainerRegistryServer"
Get-ContainerRegistrySecret -keyVaultName $KeyVaultName -secret "ContainerRegistryUsername"
}

if ([string]::IsNullOrEmpty($script:ImageTag))
if ([string]::IsNullOrWhiteSpace($script:ImageTag))
{
$script:ImageTag = "$($env:Version_Prefix)$($env:Version_Prerelease)"
}
if ([string]::IsNullOrEmpty($script:ImageTag))
if ([string]::IsNullOrWhiteSpace($script:ImageTag))
{
try
{
Expand All @@ -50,78 +88,55 @@ if ([string]::IsNullOrEmpty($script:ImageTag))
}
}


# Set namespace name based on branch name
$imageNamespace = $script:BranchName
if ($imageNamespace.StartsWith("feature/"))
if ($registry -eq "industrialiotdev")
{
# dev feature builds
$imageNamespace = $imageNamespace.Replace("feature/", "")
}
elseif ($imageNamespace.StartsWith("release/") -or ($imageNamespace -eq "releases"))
{
$imageNamespace = "public"
}
$imageNamespace = $imageNamespace.Replace("_", "/").Substring(0, [Math]::Min($imageNamespace.Length, 24))

function Get-ContainerRegistrySecret
{
param(
[Parameter(Mandatory=$true)]
[string] $keyVaultName,

[Parameter(Mandatory=$true)]
[string] $secret
)
$secretValueSec = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $secret
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secretValueSec.SecretValue)
try
if (![string]::IsNullOrWhiteSpace($script:BranchName))
{
$secretValueText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
if ($script:BranchName.StartsWith("refs/heads/"))
{
$script:BranchName = $script:BranchName.Replace("refs/heads/", "")
}
}
finally
else
{
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
$script:BranchName = "main"
}
Write-Host "##vso[task.setvariable variable=$($secret)]$($secretValueText)"
}

if ([string]::IsNullOrEmpty($script:ContainerRegistryServer))
{
if ($imageNamespace -eq "public")
$imageNamespace = $script:BranchName
if ($imageNamespace.StartsWith("feature/"))
{
# Release and Preview builds are in staging
$registry = "industrialiot"
$KeyVaultName = "kv-release-pipeline"
# dev feature builds
$imageNamespace = $imageNamespace.Replace("feature/", "")
}
else
{
# Feature builds by default into dev registry
$registry = "industrialiotdev"
$KeyVaultName = "kv-developer-pipeline"
}

Write-Host "Looking up credentials for $($registry) registry in KV $KeyVaultName."
Get-ContainerRegistrySecret -keyVaultName $KeyVaultName -secret "ContainerRegistryPassword"
Get-ContainerRegistrySecret -keyVaultName $KeyVaultName -secret "ContainerRegistryServer"
Get-ContainerRegistrySecret -keyVaultName $KeyVaultName -secret "ContainerRegistryUsername"
$imageNamespace = $imageNamespace.Replace("_", "/")
$imageNamespace = $imageNamespace.Substring(0, [Math]::Min($imageNamespace.Length, 24))
}
elseif ($registry -eq "mcr.microsoft.com")
{
$imageNamespace = ""
}
else
else
{
$registry = $script:ContainerRegistryServer
$imageNamespace = "public"
}

Write-Host "=============================================================================="
Write-Host "Use $($script:ImageTag) images in namespace $($imageNamespace) from $($registry)."
Write-Host "Selected $($script:ImageTag) images in namespace $($imageNamespace) from $($registry)."
Write-Host "=============================================================================="
Write-Host ""

Write-Host "##vso[task.setvariable variable=ImageTag]$($script:ImageTag)"
Write-Host "##vso[task.setvariable variable=ImageNamespace]$($imageNamespace)"

if ([string]::IsNullOrEmpty($script:Region))
{
$script:Region = "westus"
}
Write-Host "##vso[task.setvariable variable=Region]$($script:Region)"

Write-Host "##vso[build.addbuildtag]$($script:ImageTag)"
Write-Host "##vso[build.addbuildtag]$($registry)"
if (![string]::IsNullOrWhiteSpace($imageNamespace))
{
Write-Host "##vso[build.addbuildtag]$($imageNamespace)"
}
2 changes: 1 addition & 1 deletion tools/e2etesting/steps/deployplatform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ steps:
Write-Host "##vso[task.setvariable variable=ServicePrincipalTenantId]$($env:tenantId)"
- task: AzurePowerShell@5
displayName: 'Auto-Set Variables'
displayName: 'Select Image to test'
inputs:
azureSubscription: '$(AzureSubscription)'
azurePowerShellVersion: 'latestVersion'
Expand Down
2 changes: 1 addition & 1 deletion tools/e2etesting/steps/deploystandalone.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
steps:
- task: AzurePowerShell@5
displayName: 'Auto-Set Variables'
displayName: 'Select Image to test'
inputs:
azureSubscription: '$(AzureSubscription)'
azurePowerShellVersion: 'latestVersion'
Expand Down
2 changes: 1 addition & 1 deletion tools/e2etesting/steps/runtests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ steps:
SecretsFilter: 'PCS-SERVICE-URL,PCS-AUTH-TENANT,PCS-AUTH-CLIENT-APPID,PCS-AUTH-CLIENT-SECRET,PCS-AUTH-SERVICE-APPID'

- task: AzurePowerShell@5
displayName: 'Auto-Set Variables'
displayName: 'Select Image to test'
inputs:
azureSubscription: '$(AzureSubscription)'
azurePowerShellVersion: 'latestVersion'
Expand Down
35 changes: 19 additions & 16 deletions tools/scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
is compared to the default images when publishing, which
are debian.
.PARAMETER Registry
.PARAMETER ContainerRegistry
The name of the container registry to push to (optional)
.PARAMETER Os
Operating system to build for. Defaults to Linux
Expand Down Expand Up @@ -48,19 +48,22 @@ if ($script:Debug.IsPresent) {
$configuration = "Debug"
}

$repositoryName = $null
if ($script:BranchName) {
# Set namespace name based on branch name
$namespace = $script:BranchName
if ($namespace.StartsWith("feature/")) {
# dev feature builds
$namespace = $namespace.Replace("feature/", "")
}
elseif ($namespace.StartsWith("release/") -or ($namespace -eq "releases")) {
$namespace = "public"
}
$namespace = $namespace.Replace("_", "/")
$repositoryName = $namespace.Substring(0, [Math]::Min($namespace.Length, 24))
$imageNamespace = $null
if (![string]::IsNullOrWhiteSpace($script:ContainerRegistry)) {
$imageNamespace = "public"

if ($script:ContainerRegistry -eq "industrialiotdev") {
if (![string]::IsNullOrWhiteSpace($script:BranchName)) {
# Set namespace name based on branch name
$namespace = $script:BranchName
if ($namespace.StartsWith("feature/")) {
# dev feature builds
$namespace = $namespace.Replace("feature/", "")
}
$namespace = $namespace.Replace("_", "/")
$imageNamespace = $namespace.Substring(0, [Math]::Min($namespace.Length, 24))
}
}
}

$env:SDK_CONTAINER_REGISTRY_CHUNKED_UPLOAD = $true
Expand Down Expand Up @@ -99,8 +102,8 @@ Get-ChildItem $Path -Filter *.csproj -Recurse | ForEach-Object {
$fullName = ""
$extra = @()

if ($repositoryName) {
$fullName = "$($fullName)$($repositoryName)/"
if ($imageNamespace) {
$fullName = "$($fullName)$($imageNamespace)/"
}
$fullName = "$($fullName)$($properties.ContainerRepository)"

Expand Down

0 comments on commit a49d38f

Please sign in to comment.