Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Aktivieren Sie Anwendungssignale auf Amazon ECS mit AWS CDK
Gehen Sie wie folgt vor AWS CDK, um Application Signals auf Amazon ECS zu aktivieren.
Aktivieren Sie Application Signals für Ihre Anwendungen — Wenn Sie Application Signals in diesem Konto noch nicht aktiviert haben, müssen Sie Application Signals die Berechtigungen gewähren, die es benötigt, um Ihre Dienste zu erkennen.
import { aws_applicationsignals as applicationsignals } from 'aws-cdk-lib'; const cfnDiscovery = new applicationsignals.CfnDiscovery(this, 'ApplicationSignalsServiceRole', { } );
Die CloudFormation Discovery-Ressource gewährt Application Signals die folgenden Berechtigungen:
-
xray:GetServiceGraph
-
logs:StartQuery
-
logs:GetQueryResults
-
cloudwatch:GetMetricData
-
cloudwatch:ListMetrics
-
tag:GetResources
Weitere Informationen über diese Rolle finden Sie unter Dienstbezogene Rollenberechtigungen für CloudWatch Application Signals.
-
Instrumentieren Sie Ihre Anwendung mit der AWS::ApplicationSignals Construct Library
im AWS CDK. Die Codefragmente in diesem Dokument finden Sie unter. TypeScript Weitere sprachspezifische Alternativen finden Sie unter Unterstützte Programmiersprachen für das CDK. AWS Anwendungssignale auf Amazon ECS im Sidecar-Modus aktivieren
Konfigurieren Sie
instrumentation
die Anwendung so, dass sie mit dem AWS Distro for OpenTelemetry (ADOT) SDK-Agenten instrumentiert wird. Im Folgenden finden Sie ein Beispiel für die Instrumentierung einer Java-Anwendung. Alle unterstützten Sprachversionen finden InstrumentationVersionSie unter.Geben Sie
cloudWatchAgentSidecar
an, dass der CloudWatch Agent als Sidecar-Container konfiguriert werden soll.import { Construct } from 'constructs'; import * as appsignals from '@aws-cdk/aws-applicationsignals-alpha'; import * as cdk from 'aws-cdk-lib'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; import * as ecs from 'aws-cdk-lib/aws-ecs'; class MyStack extends cdk.Stack { public constructor(scope?: Construct, id?: string, props: cdk.StackProps = {}) { super(); const vpc = new ec2.Vpc(this, 'TestVpc', {}); const cluster = new ecs.Cluster(this, 'TestCluster', { vpc }); const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'SampleAppTaskDefinition', { cpu: 2048, memoryLimitMiB: 4096, }); fargateTaskDefinition.addContainer('app', { image: ecs.ContainerImage.fromRegistry('test/sample-app'), }); new appsignals.ApplicationSignalsIntegration(this, 'ApplicationSignalsIntegration', { taskDefinition: fargateTaskDefinition, instrumentation: { sdkVersion: appsignals.JavaInstrumentationVersion.V2_10_0, }, serviceName: 'sample-app', cloudWatchAgentSidecar: { containerName: 'ecs-cwagent', enableLogging: true, cpu: 256, memoryLimitMiB: 512, } }); new ecs.FargateService(this, 'MySampleApp', { cluster: cluster, taskDefinition: fargateTaskDefinition, desiredCount: 1, }); } }
Anwendungssignale auf Amazon ECS im Daemon-Modus aktivieren
Anmerkung
Die Daemon-Bereitstellungsstrategie wird auf Amazon ECS Fargate nicht unterstützt und wird nur auf Amazon ECS auf Amazon unterstützt. EC2
Führen Sie den CloudWatch Agenten als Daemon-Service im Netzwerkmodus aus.
HOST
Konfigurieren Sie
instrumentation
die Anwendung so, dass sie mit dem ADOT Python Agent instrumentiert wird.import { Construct } from 'constructs'; import * as appsignals from '@aws-cdk/aws-applicationsignals-alpha'; import * as cdk from 'aws-cdk-lib'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; import * as ecs from 'aws-cdk-lib/aws-ecs'; class MyStack extends cdk.Stack { public constructor(scope?: Construct, id?: string, props: cdk.StackProps = {}) { super(scope, id, props); const vpc = new ec2.Vpc(this, 'TestVpc', {}); const cluster = new ecs.Cluster(this, 'TestCluster', { vpc }); // Define Task Definition for CloudWatch agent (Daemon) const cwAgentTaskDefinition = new ecs.Ec2TaskDefinition(this, 'CloudWatchAgentTaskDefinition', { networkMode: ecs.NetworkMode.HOST, }); new appsignals.CloudWatchAgentIntegration(this, 'CloudWatchAgentIntegration', { taskDefinition: cwAgentTaskDefinition, containerName: 'ecs-cwagent', enableLogging: false, cpu: 128, memoryLimitMiB: 64, portMappings: [ { containerPort: 4316, hostPort: 4316, }, { containerPort: 2000, hostPort: 2000, }, ], }); // Create the CloudWatch Agent daemon service new ecs.Ec2Service(this, 'CloudWatchAgentDaemon', { cluster, taskDefinition: cwAgentTaskDefinition, daemon: true, // Runs one container per EC2 instance }); // Define Task Definition for user application const sampleAppTaskDefinition = new ecs.Ec2TaskDefinition(this, 'SampleAppTaskDefinition', { networkMode: ecs.NetworkMode.HOST, }); sampleAppTaskDefinition.addContainer('app', { image: ecs.ContainerImage.fromRegistry('test/sample-app'), cpu: 0, memoryLimitMiB: 512, }); // No CloudWatch Agent sidecar is needed as application container communicates to CloudWatch Agent daemon through host network new appsignals.ApplicationSignalsIntegration(this, 'ApplicationSignalsIntegration', { taskDefinition: sampleAppTaskDefinition, instrumentation: { sdkVersion: appsignals.PythonInstrumentationVersion.V0_8_0 }, serviceName: 'sample-app' }); new ecs.Ec2Service(this, 'MySampleApp', { cluster, taskDefinition: sampleAppTaskDefinition, desiredCount: 1, }); } }
Aktivieren Sie Application Signals auf Amazon ECS im Replikatmodus
Anmerkung
Für die Ausführung CloudWatch des Agent-Dienstes im Replikatmodus sind spezielle Sicherheitsgruppenkonfigurationen erforderlich, um die Kommunikation mit anderen Diensten zu ermöglichen. Für die Funktionalität von Application Signals konfigurieren Sie die Sicherheitsgruppe mit den Mindestregeln für eingehende Nachrichten: Port 2000 (HTTP) und Port 4316 (HTTP). Diese Konfiguration gewährleistet eine ordnungsgemäße Konnektivität zwischen dem CloudWatch Agenten und den abhängigen Diensten.
Führen Sie den CloudWatch Agenten als Replikatdienst mit Service Connect aus.
Konfigurieren Sie
instrumentation
die Anwendung so, dass sie mit dem ADOT Python Agent instrumentiert wird.Überschreiben Sie Umgebungsvariablen, indem Sie so konfigurieren
overrideEnvironments
, dass Service Connect-Endpunkte für die Kommunikation mit dem CloudWatch Agent-Server verwendet werden.import { Construct } from 'constructs'; import * as appsignals from '@aws-cdk/aws-applicationsignals-alpha'; import * as cdk from 'aws-cdk-lib'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; import * as ecs from 'aws-cdk-lib/aws-ecs'; import { PrivateDnsNamespace } from 'aws-cdk-lib/aws-servicediscovery'; class MyStack extends cdk.Stack { public constructor(scope?: Construct, id?: string, props: cdk.StackProps = {}) { super(scope, id, props); const vpc = new ec2.Vpc(this, 'TestVpc', {}); const cluster = new ecs.Cluster(this, 'TestCluster', { vpc }); const dnsNamespace = new PrivateDnsNamespace(this, 'Namespace', { vpc, name: 'local', }); const securityGroup = new ec2.SecurityGroup(this, 'ECSSG', { vpc }); securityGroup.addIngressRule(securityGroup, ec2.Port.tcpRange(0, 65535)); // Define Task Definition for CloudWatch agent (Replica) const cwAgentTaskDefinition = new ecs.FargateTaskDefinition(this, 'CloudWatchAgentTaskDefinition', {}); new appsignals.CloudWatchAgentIntegration(this, 'CloudWatchAgentIntegration', { taskDefinition: cwAgentTaskDefinition, containerName: 'ecs-cwagent', enableLogging: false, cpu: 128, memoryLimitMiB: 64, portMappings: [ { name: 'cwagent-4316', containerPort: 4316, hostPort: 4316, }, { name: 'cwagent-2000', containerPort: 2000, hostPort: 2000, }, ], }); // Create the CloudWatch Agent replica service with service connect new ecs.FargateService(this, 'CloudWatchAgentService', { cluster: cluster, taskDefinition: cwAgentTaskDefinition, securityGroups: [securityGroup], serviceConnectConfiguration: { namespace: dnsNamespace.namespaceArn, services: [ { portMappingName: 'cwagent-4316', dnsName: 'cwagent-4316-http', port: 4316, }, { portMappingName: 'cwagent-2000', dnsName: 'cwagent-2000-http', port: 2000, }, ], }, desiredCount: 1, }); // Define Task Definition for user application const sampleAppTaskDefinition = new ecs.FargateTaskDefinition(this, 'SampleAppTaskDefinition', {}); sampleAppTaskDefinition.addContainer('app', { image: ecs.ContainerImage.fromRegistry('test/sample-app'), cpu: 0, memoryLimitMiB: 512, }); // Overwrite environment variables to connect to the CloudWatch Agent service just created new appsignals.ApplicationSignalsIntegration(this, 'ApplicationSignalsIntegration', { taskDefinition: sampleAppTaskDefinition, instrumentation: { sdkVersion: appsignals.PythonInstrumentationVersion.V0_8_0, }, serviceName: 'sample-app', overrideEnvironments: [ { name: appsignals.CommonExporting.OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT, value: 'http://cwagent-4316-http:4316/v1/metrics', }, { name: appsignals.TraceExporting.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, value: 'http://cwagent-4316-http:4316/v1/traces', }, { name: appsignals.TraceExporting.OTEL_TRACES_SAMPLER_ARG, value: 'endpoint=http://cwagent-2000-http:2000', }, ], }); // Create ECS Service with service connect configuration new ecs.FargateService(this, 'MySampleApp', { cluster: cluster, taskDefinition: sampleAppTaskDefinition, serviceConnectConfiguration: { namespace: dnsNamespace.namespaceArn, }, desiredCount: 1, }); } }
Einrichtung einer Anwendung Node.js mit dem ESM-Modulformat. Die Unterstützung für Node.js -Anwendungen im ESM-Modulformat ist begrenzt. Weitere Informationen finden Sie unter Bekannte Einschränkungen für Node.js mit ESM.
Für das ESM-Modulformat gilt die Aktivierung von Application Signals mithilfe des
init
Containers zum Einfügen des Instrumentierungs-SDK Node.js nicht. Überspringen Sie Schritt 2 in diesem Verfahren und gehen Sie stattdessen wie folgt vor.Installieren Sie die entsprechenden Abhängigkeiten in Ihrer Anwendung Node.js für die automatische Instrumentierung.
npm install @aws/aws-distro-opentelemetry-node-autoinstrumentation npm install @opentelemetry/instrumentation@0.54.
Aktualisieren TaskDefinition.
Fügen Sie Ihrem Anwendungscontainer zusätzliche Konfigurationen hinzu.
Konfiguration
NODE_OPTIONS
.(Optional) Fügen Sie CloudWatch Agent hinzu, wenn Sie den Sidecar-Modus wählen.
import { Construct } from 'constructs'; import * as appsignals from '@aws-cdk/aws-applicationsignals-alpha'; import * as ecs from 'aws-cdk-lib/aws-ecs'; class MyStack extends cdk.Stack { public constructor(scope?: Construct, id?: string, props: cdk.StackProps = {}) { super(scope, id, props); const fargateTaskDefinition = new ecs.FargateTaskDefinition(stack, 'TestTaskDefinition', { cpu: 256, memoryLimitMiB: 512, }); const appContainer = fargateTaskDefinition.addContainer('app', { image: ecs.ContainerImage.fromRegistry('docker/cdk-test'), }); const volumeName = 'opentelemetry-auto-instrumentation' fargateTaskDefinition.addVolume({name: volumeName}); // Inject additional configurations const injector = new appsignals.NodeInjector(volumeName, appsignals.NodeInstrumentationVersion.V0_5_0); injector.renderDefaultContainer(fargateTaskDefinition); // Configure NODE_OPTIONS appContainer.addEnvironment('NODE_OPTIONS', '--import @aws/aws-distro-opentelemetry-node-autoinstrumentation/register --experimental-loader=@opentelemetry/instrumentation/hook.mjs') // Optional: add CloudWatch agent const cwAgent = new appsignals.CloudWatchAgentIntegration(stack, 'AddCloudWatchAgent', { containerName: 'ecs-cwagent', taskDefinition: fargateTaskDefinition, memoryReservationMiB: 50, }); appContainer.addContainerDependencies({ container: cwAgent.agentContainer, condition: ecs.ContainerDependencyCondition.START, }); }
Den aktualisierten Stack bereitstellen — Führen Sie den
cdk synth
Befehl im Hauptverzeichnis Ihrer Anwendung aus. Um den Dienst in Ihrem AWS Konto bereitzustellen, führen Sie dencdk deploy
Befehl im Hauptverzeichnis Ihrer Anwendung aus.Wenn Sie die Sidecar-Strategie verwendet haben, wird ein Dienst erstellt:
APPLICATION_SERVICE
ist der Dienst Ihrer Anwendung. Er umfasst die drei folgenden Container:init
— Ein erforderlicher Container für die Initialisierung von Anwendungssignalen.ecs-cwagent
— Ein Container, in dem der CloudWatch Agent ausgeführt wird
— Dies ist der Beispielanwendungscontainer in unserer Dokumentation. In Ihren tatsächlichen Workloads ist dieser spezielle Container möglicherweise nicht vorhanden oder wurde möglicherweise durch Ihre eigenen Servicecontainer ersetzt.my-app
Wenn Sie die Daemon-Strategie verwendet haben, werden zwei Dienste erstellt:
CloudWatchAgentDaemon
ist der CloudWatch Agent-Daemon-Dienst.APPLICATION_SERVICE
ist der Dienst Ihrer Anwendung. Er umfasst die beiden folgenden Container:init
— Ein erforderlicher Container für die Initialisierung von Anwendungssignalen.
— Dies ist der Beispiel-Anwendungscontainer in unserer Dokumentation. In Ihren tatsächlichen Workloads ist dieser spezielle Container möglicherweise nicht vorhanden oder wurde möglicherweise durch Ihre eigenen Servicecontainer ersetzt.my-app
Wenn Sie die Replikatstrategie verwendet haben, werden zwei Dienste erstellt:
CloudWatchAgentService
ist der CloudWatch Agent-Replikationsdienst.APPLICATION_SERVICE
ist der Dienst Ihrer Anwendung. Er umfasst die beiden folgenden Container:init
— Ein erforderlicher Container für die Initialisierung von Anwendungssignalen.
— Dies ist der Beispiel-Anwendungscontainer in unserer Dokumentation. In Ihren tatsächlichen Workloads ist dieser spezielle Container möglicherweise nicht vorhanden oder wurde möglicherweise durch Ihre eigenen Servicecontainer ersetzt.my-app