本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
可觀測性
這是預覽版本之服務的發行前版本文件。內容可能變動。 |
可觀測性是系統目前狀態可從其發出之資料推斷的程度。發出的資料通常稱為遙測。如需使用 AWS 服務時遙測的其他資訊,請參閱 AWS SDK for .NET 開發人員指南 中的可觀測性。
下列程式碼顯示如何在 中啟用可觀測性的範例 AWS Tools for PowerShell。
<# This is an example of generating telemetry for AWS Tools for PowerShell. Each cmdlet that interacts with an Amazon Web Service creates a trace containing spans for underlying processes and AWS SDK for .NET operations. This example is written using PowerShell 7 and .NET 8. It requires the installation of the .NET CLI tool. Note that implementation varies by the exporter/endpoint, which is not specified in this example. For more information, see https://opentelemetry.io/docs/languages/net/exporters/. #> # Set this value to a common folder path on your computer for local development of code repositories. $devProjectsPath = [System.IO.Path]::Join('C:', 'Dev', 'Repos') # If these values are changed, update the hardcoded method invocation toward the end of this script. # Values must follow constraints for namespaces and classes. $telemetryProjectName = 'ExampleAWSPowerShellTelemetryImplementation' $serviceName = 'ExamplePowerShellService' # This example supposes that the OTLP exporter requires these two properties, # but some exporters require different properties or no properties. $telemetryEndPoint = 'https://example-endpoint-provider.io' $telemetryHeaders = 'x-example-header=abc123' $dllsPath = [System.IO.Path]::Join($devProjectsPath, $telemetryProjectName, 'bin', 'Release', 'net8.0', 'publish') $telemetryProjectPath = [System.IO.Path]::Join($devProjectsPath, $telemetryProjectName) # This script is designed to recreate the example telemetry project each time it's executed. Remove-Item -Path $telemetryProjectPath -Recurse -Force -ErrorAction 'SilentlyContinue' $null = New-Item -Path $devProjectsPath -Name $telemetryProjectName -ItemType 'Directory' <# Create and build a C#-based .NET 8 project that implements OpenTelemetry Instrumentation for the AWS Tools for PowerShell. #> Set-Location -Path $telemetryProjectPath dotnet new classlib # Other exporters are available. # For more information, see https://opentelemetry.io/docs/languages/net/exporters/. dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol dotnet add package OpenTelemetry.Instrumentation.AWS --prerelease $classContent = @" using OpenTelemetry; using OpenTelemetry.Resources; using OpenTelemetry.Trace; namespace Example.Telemetry; public class AWSToolsForPowerShellTelemetry { public static void InitializeAWSInstrumentation() { Sdk.CreateTracerProviderBuilder() .ConfigureResource(e => e.AddService("$ServiceName")) .AddAWSInstrumentation() // Exporters vary so options might need to be changed or omitted. .AddOtlpExporter(options => { options.Endpoint = new Uri("$telemetryEndPoint"); options.Headers = "$telemetryHeaders"; }) .Build(); } } "@ $csFilePath = [System.IO.Path]::Join($telemetryProjectPath, ($serviceName + '.cs')) Set-Content -Path $csFilePath -Value $classContent dotnet build dotnet publish -c Release <# Add additional modules here for any other cmdlets that you require. Beyond this point, additional AWS Tools for PowerShell modules will fail to import due to conflicts with the AWS SDK for .NET assemblies that are added next. #> Import-Module -Name 'AWS.Tools.Common' Import-Module -Name 'AWS.Tools.DynamoDBv2' # Load assemblies for the telemetry project, excluding the AWS SDK for .NET assemblies # that were already loaded by importing AWS Tools for PowerShell modules. $dlls = (Get-ChildItem $dllsPath -Filter *.dll -Recurse ).FullName $AWSSDKAssembliesAlreadyLoaded = [Threading.Thread]::GetDomain().GetAssemblies().Location | Where-Object {$_ -like '*AWSSDK*' } | Split-Path -Leaf $dlls.Where{$AWSSDKAssembliesAlreadyLoaded -notcontains ($_ | Split-Path -Leaf)}.ForEach{Add-Type -Path $_} # Invoke the method defined earlier in this script. [Example.Telemetry.AWSToolsForPowerShellTelemetry]::InitializeAWSInstrumentation() <# Now telemetry will be exported for AWS Tools for PowerShell cmdlets that are invoked directly or indirectly. Execute this cmdlet or execute your own PowerShell script. #> Get-DDBTable -TableName 'DotNetTests-HashTable' -Region 'us-east-1'