View a markdown version of this page

SDK for C++를 사용한 CloudWatch 예제 - AWS SDK for C++

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

SDK for C++를 사용한 CloudWatch 예제

다음 코드 예제에서는 CloudWatch와 AWS SDK for C++ 함께를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.

기본 사항

다음 코드 예제에서는 다음과 같은 작업을 수행하는 방법을 보여줍니다.

  • CloudWatch 네임스페이스 및 지표를 나열합니다.

  • CloudWatch가 수신 OTLP 지표를 생성한 리소스와 상호 연관시키도록 OpenTelemetry 보강을 시작합니다.

  • OTLP 지표가 CloudWatch 지표 엔드포인트에 도달하는 방법을 알아봅니다. OTLP를 통한 지표 수집은 AWS SDK 작업이 아닙니다.

  • PromQL 쿼리를 평가하는 경보를 생성합니다.

  • 쿼리가 일치하는 개별 시리즈인 경보의 기여자를 검사합니다.

  • 지표에 대한 통계를 가져와 대시보드에서 차트로 작성합니다.

  • 유지 관리 기간에 대한 경보를 음소거한 다음 정리합니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 확인하고 설정 및 실행하는 방법을 알아보세요.

명령 프롬프트에서 대화형 시나리오를 실행합니다.

#include <aws/core/Aws.h> #include <aws/core/utils/json/JsonSerializer.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmPromQLCriteria.h> #include <aws/monitoring/model/DeleteAlarmMuteRuleRequest.h> #include <aws/monitoring/model/DeleteAlarmsRequest.h> #include <aws/monitoring/model/DeleteDashboardsRequest.h> #include <aws/monitoring/model/DescribeAlarmContributorsRequest.h> #include <aws/monitoring/model/EvaluationCriteria.h> #include <aws/monitoring/model/GetAlarmMuteRuleRequest.h> #include <aws/monitoring/model/GetDashboardRequest.h> #include <aws/monitoring/model/GetMetricStatisticsRequest.h> #include <aws/monitoring/model/GetOTelEnrichmentRequest.h> #include <aws/monitoring/model/ListAlarmMuteRulesRequest.h> #include <aws/monitoring/model/ListMetricsRequest.h> #include <aws/monitoring/model/MuteTargets.h> #include <aws/monitoring/model/PutAlarmMuteRuleRequest.h> #include <aws/monitoring/model/PutDashboardRequest.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <aws/monitoring/model/Rule.h> #include <aws/monitoring/model/Schedule.h> #include <aws/monitoring/model/StartOTelEnrichmentRequest.h> #include <aws/monitoring/model/StopOTelEnrichmentRequest.h> #include <chrono> #include <iostream> #include <map> #include <random> namespace { const Aws::String DASHES(80, '-'); const char DEFAULT_QUERY[] = "avg by (host) (system_cpu_utilization) > 80"; // Valid evaluation intervals are 10, 20, 30, or any multiple of 60 up to 3600 // seconds. const int EVALUATION_INTERVAL = 60; const int PENDING_PERIOD = 300; const int RECOVERY_PERIOD = 120; //! Wait for the reader before moving to the next step. void pressEnter() { std::cout << "Press Enter to continue..." << std::endl; std::cin.get(); } //! List the account's metrics and report how they are spread across namespaces. /*! \param client: A CloudWatch client. \param metric: Receives the first metric found, for later steps to chart. \return bool: Function succeeded. */ bool listMetricsAndNamespaces(const Aws::CloudWatch::CloudWatchClient &client, Aws::CloudWatch::Model::Metric &metric) { std::cout << "1. List metrics and namespaces" << std::endl << std::endl; std::cout << "Before configuring anything, let's see what CloudWatch is already" << std::endl << "collecting in this account." << std::endl << std::endl; std::map<Aws::String, int> counts; int metricCount = 0; bool haveMetric = false; Aws::CloudWatch::Model::ListMetricsRequest request; bool done = false; while (!done) { auto outcome = client.ListMetrics(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to list metrics: " << outcome.GetError().GetMessage() << std::endl; return false; } for (const auto &found : outcome.GetResult().GetMetrics()) { ++counts[found.GetNamespace()]; ++metricCount; if (!haveMetric) { metric = found; haveMetric = true; } } const auto &nextToken = outcome.GetResult().GetNextToken(); request.SetNextToken(nextToken); // This account may have a very large number of metrics, so stop once there // are enough to give the reader a sense of what is there. done = nextToken.empty() || metricCount >= 500; } std::cout << "Found " << metricCount << " metrics across " << counts.size() << " namespaces:" << std::endl; for (const auto &entry : counts) { std::cout << " " << entry.first << " (" << entry.second << " metrics)" << std::endl; } if (!haveMetric) { std::cout << "No metrics found in this account. The statistics and dashboard" << std::endl << "steps later on need an existing metric, so they will be " "skipped." << std::endl; } return true; } //! Start OTel enrichment, but only if it is not already running. /*! \param client: A CloudWatch client. \param startedEnrichment: Set to true if this run started enrichment, so that cleanup only stops what this run turned on. \return bool: Function succeeded. */ bool startOTelEnrichment(const Aws::CloudWatch::CloudWatchClient &client, bool &startedEnrichment) { std::cout << "2. Start OpenTelemetry enrichment" << std::endl << std::endl; std::cout << "Enrichment is what lets CloudWatch attach AWS resource context to" << std::endl << "the OTLP metrics you send it. Without it, your metrics arrive as" << std::endl << "opaque series with no connection to the resources that emitted " "them." << std::endl << std::endl; Aws::CloudWatch::Model::GetOTelEnrichmentRequest getRequest; auto getOutcome = client.GetOTelEnrichment(getRequest); if (!getOutcome.IsSuccess()) { std::cerr << "Failed to get OTel enrichment status: " << getOutcome.GetError().GetMessage() << std::endl; return false; } auto status = getOutcome.GetResult().GetStatus(); std::cout << "Enrichment status: " << Aws::CloudWatch::Model::OTelEnrichmentStatusMapper:: GetNameForOTelEnrichmentStatus(status) << std::endl; if (status == Aws::CloudWatch::Model::OTelEnrichmentStatus::Running) { std::cout << std::endl << "Enrichment was already running, so it will be left alone. The" << std::endl << "cleanup step will not stop it, because other workloads in this" << std::endl << "account may depend on it." << std::endl; return true; } Aws::CloudWatch::Model::StartOTelEnrichmentRequest startRequest; auto startOutcome = client.StartOTelEnrichment(startRequest); if (!startOutcome.IsSuccess()) { std::cerr << "Failed to start OTel enrichment: " << startOutcome.GetError().GetMessage() << std::endl; return false; } startedEnrichment = true; auto afterOutcome = client.GetOTelEnrichment(getRequest); if (afterOutcome.IsSuccess()) { std::cout << "Enrichment status: " << Aws::CloudWatch::Model::OTelEnrichmentStatusMapper:: GetNameForOTelEnrichmentStatus( afterOutcome.GetResult().GetStatus()) << std::endl; } std::cout << std::endl << "Note: this run started enrichment, so the cleanup step will stop it" << std::endl << "again." << std::endl; return true; } //! Explain how OTLP metrics reach CloudWatch. There is no SDK call for this step. void explainOtlpIngestion() { std::cout << "3. Send OTLP metrics to CloudWatch" << std::endl << std::endl; std::cout << "This step is not an AWS SDK operation, and that is worth being" << std::endl << "explicit about. Metrics reach CloudWatch over the OTLP protocol," << std::endl << "through the CloudWatch agent, an OpenTelemetry Collector, or an " "ADOT" << std::endl << "SDK. There is no PutOTelMetrics API to call." << std::endl << std::endl; std::cout << "Point your collector at the CloudWatch metrics endpoint, which" << std::endl << "follows the pattern" << std::endl << " https://monitoring.<region>.amazonaws.com/v1/metrics" << std::endl << std::endl; std::cout << "The endpoint is HTTP/1.1 only and does not support gRPC, so use an" << std::endl << "otlphttp exporter rather than otlp. The metrics endpoint signs as" << std::endl << "\"monitoring\"." << std::endl; } //! Create an alarm that evaluates a PromQL query. /*! \param client: A CloudWatch client. \param alarmName: The name of the alarm to create. \param query: The PromQL query to evaluate. \return bool: Function succeeded. */ bool createPromQLAlarm(const Aws::CloudWatch::CloudWatchClient &client, const Aws::String &alarmName, const Aws::String &query) { std::cout << "4. Create a PromQL alarm" << std::endl << std::endl; std::cout << "The comparison goes inside the query itself: a PromQL alarm has no" << std::endl << "separate threshold, comparison operator, statistic, or period." << std::endl << std::endl; Aws::CloudWatch::Model::AlarmPromQLCriteria promQLCriteria; promQLCriteria.SetQuery(query); promQLCriteria.SetPendingPeriod(PENDING_PERIOD); promQLCriteria.SetRecoveryPeriod(RECOVERY_PERIOD); Aws::CloudWatch::Model::EvaluationCriteria evaluationCriteria; evaluationCriteria.SetPromQLCriteria(promQLCriteria); // EvaluationCriteria is mutually exclusive with the classic MetricName and // Metrics fields. When you use it you must also set EvaluationInterval, and you // must not set Period, Statistic, Threshold, ComparisonOperator, // EvaluationPeriods, DatapointsToAlarm, or TreatMissingData. Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarmName); request.SetAlarmDescription( "A PromQL alarm created by the AWS SDK for C++ Basics scenario."); request.SetEvaluationCriteria(evaluationCriteria); request.SetEvaluationInterval(EVALUATION_INTERVAL); request.SetActionsEnabled(false); auto outcome = client.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to create PromQL alarm: " << outcome.GetError().GetMessage() << std::endl; return false; } std::cout << "Created alarm " << alarmName << ":" << std::endl; std::cout << " query: " << query << std::endl; std::cout << " evaluationInterval: " << EVALUATION_INTERVAL << " seconds" << std::endl; std::cout << " pendingPeriod: " << PENDING_PERIOD << " seconds" << std::endl; std::cout << " recoveryPeriod: " << RECOVERY_PERIOD << " seconds" << std::endl << std::endl; std::cout << "A PromQL alarm starts in the OK state rather than " "INSUFFICIENT_DATA," << std::endl << "which is another way it differs from a classic alarm." << std::endl; return true; } //! Report the alarm's contributors, one per series the query matched. /*! \param client: A CloudWatch client. \param alarmName: The name of the alarm. \return bool: Function succeeded. */ bool inspectAlarmContributors(const Aws::CloudWatch::CloudWatchClient &client, const Aws::String &alarmName) { std::cout << "5. Inspect the alarm's contributors" << std::endl << std::endl; std::cout << "Each contributor is one series the query matched, identified by its" << std::endl << "label set. This is how you find out which host is unhealthy rather" << std::endl << "than only that something is. Classic alarms have no equivalent." << std::endl << std::endl; Aws::Vector<Aws::CloudWatch::Model::AlarmContributor> contributors; Aws::CloudWatch::Model::DescribeAlarmContributorsRequest request; request.SetAlarmName(alarmName); bool done = false; while (!done) { auto outcome = client.DescribeAlarmContributors(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to describe alarm contributors: " << outcome.GetError().GetMessage() << std::endl; return false; } const auto &page = outcome.GetResult().GetAlarmContributors(); contributors.insert(contributors.end(), page.begin(), page.end()); const auto &nextToken = outcome.GetResult().GetNextToken(); request.SetNextToken(nextToken); // A page can come back empty while still carrying a token, so keep going // until the token itself is gone rather than stopping at the first empty // page. done = nextToken.empty(); } if (contributors.empty()) { std::cout << "No contributors yet. The query matched no series, which usually" << std::endl << "means no OTel metrics with these labels have arrived. Once your" << std::endl << "collector is sending data, each matching series appears here" << std::endl << "with its labels and the reason it breached." << std::endl; return true; } std::cout << "Found " << contributors.size() << " contributors:" << std::endl; for (const auto &contributor : contributors) { std::cout << " " << contributor.GetContributorId() << ": "; bool first = true; for (const auto &label : contributor.GetContributorAttributes()) { if (!first) { std::cout << ", "; } std::cout << label.first << "=" << label.second; first = false; } std::cout << std::endl; std::cout << " reason: " << contributor.GetStateReason() << std::endl; } return true; } //! Build a single-widget dashboard body that charts the given metric. /*! \param metric: The metric to chart. \param region: The region the metric is in. A metric widget must name its region, because a dashboard can chart metrics from several. \return Aws::String: The dashboard body, as JSON. */ Aws::String buildDashboardBody(const Aws::CloudWatch::Model::Metric &metric, const Aws::String &region) { const auto &dimensions = metric.GetDimensions(); // A metric is specified in a widget as a flat array, // [namespace, metricName, dimensionName, dimensionValue, ...]. Aws::Utils::Array<Aws::Utils::Json::JsonValue> metricSpec( 2 + 2 * dimensions.size()); metricSpec[0].AsString(metric.GetNamespace()); metricSpec[1].AsString(metric.GetMetricName()); size_t index = 2; for (const auto &dimension : dimensions) { metricSpec[index++].AsString(dimension.GetName()); metricSpec[index++].AsString(dimension.GetValue()); } Aws::Utils::Array<Aws::Utils::Json::JsonValue> metricsArray(1); metricsArray[0].AsArray(metricSpec); Aws::Utils::Json::JsonValue textProperties; textProperties.WithString( "markdown", "This dashboard was created programmatically by an AWS SDK code example."); Aws::Utils::Json::JsonValue textWidget; textWidget.WithString("type", "text") .WithInteger("x", 0) .WithInteger("y", 0) .WithInteger("width", 24) .WithInteger("height", 2) .WithObject("properties", textProperties); Aws::Utils::Json::JsonValue metricProperties; metricProperties.WithArray("metrics", metricsArray) .WithString("view", "timeSeries") .WithString("stat", "Average") .WithInteger("period", 300) .WithString("region", region) .WithString("title", metric.GetMetricName()); Aws::Utils::Json::JsonValue metricWidget; metricWidget.WithString("type", "metric") .WithInteger("x", 0) .WithInteger("y", 2) .WithInteger("width", 12) .WithInteger("height", 6) .WithObject("properties", metricProperties); Aws::Utils::Array<Aws::Utils::Json::JsonValue> widgets(2); widgets[0] = textWidget; widgets[1] = metricWidget; Aws::Utils::Json::JsonValue body; body.WithArray("widgets", widgets); return body.View().WriteCompact(); } //! Get statistics for a metric and chart it on a dashboard. /*! \param client: A CloudWatch client. \param metric: The metric to chart. If its name is empty, this step is skipped. \param dashboardName: The name of the dashboard to create. \param dashboardCreated: Set to true if a dashboard was created, so that cleanup only deletes a dashboard that exists. \return bool: Function succeeded. */ bool getStatisticsAndChartMetric(const Aws::CloudWatch::CloudWatchClient &client, const Aws::CloudWatch::Model::Metric &metric, const Aws::String &dashboardName, const Aws::String &region, bool &dashboardCreated) { std::cout << "6. Get statistics and chart the metric on a dashboard" << std::endl << std::endl; std::cout << "Statistics and dashboards are how you see what the alarm is" << std::endl << "evaluating." << std::endl << std::endl; if (metric.GetMetricName().empty()) { std::cout << "Skipping statistics and dashboard because no metrics exist yet." << std::endl; return true; } const auto now = std::chrono::system_clock::now(); Aws::CloudWatch::Model::GetMetricStatisticsRequest statsRequest; statsRequest.SetNamespace(metric.GetNamespace()); statsRequest.SetMetricName(metric.GetMetricName()); statsRequest.SetDimensions(metric.GetDimensions()); statsRequest.SetStartTime(Aws::Utils::DateTime(now - std::chrono::hours(24))); statsRequest.SetEndTime(Aws::Utils::DateTime(now)); statsRequest.SetPeriod(3600); statsRequest.AddStatistics(Aws::CloudWatch::Model::Statistic::Average); statsRequest.AddStatistics(Aws::CloudWatch::Model::Statistic::Maximum); auto statsOutcome = client.GetMetricStatistics(statsRequest); if (!statsOutcome.IsSuccess()) { std::cerr << "Failed to get metric statistics: " << statsOutcome.GetError().GetMessage() << std::endl; return false; } const auto &datapoints = statsOutcome.GetResult().GetDatapoints(); std::cout << "Statistics for " << metric.GetNamespace() << " " << metric.GetMetricName() << " over the last day:" << std::endl; std::cout << " Datapoints: " << datapoints.size() << std::endl; size_t shown = 0; for (const auto &datapoint : datapoints) { if (shown++ >= 3) { break; } std::cout << " " << datapoint.GetTimestamp().ToGmtString( Aws::Utils::DateFormat::ISO_8601) << " average " << datapoint.GetAverage() << ", maximum " << datapoint.GetMaximum() << std::endl; } Aws::CloudWatch::Model::PutDashboardRequest putRequest; putRequest.SetDashboardName(dashboardName); putRequest.SetDashboardBody(buildDashboardBody(metric, region)); auto putOutcome = client.PutDashboard(putRequest); if (!putOutcome.IsSuccess()) { std::cerr << "Failed to put dashboard: " << putOutcome.GetError().GetMessage() << std::endl; return false; } dashboardCreated = true; for (const auto &message : putOutcome.GetResult().GetDashboardValidationMessages()) { std::cout << "Dashboard validation message: " << message.GetMessage() << std::endl; } std::cout << "Created dashboard " << dashboardName << "." << std::endl; Aws::CloudWatch::Model::GetDashboardRequest getRequest; getRequest.SetDashboardName(dashboardName); auto getOutcome = client.GetDashboard(getRequest); if (getOutcome.IsSuccess()) { std::cout << "Read the dashboard back, " << getOutcome.GetResult().GetDashboardBody().size() << " characters of widget JSON." << std::endl; } return true; } //! Mute the alarm for a recurring maintenance window. /*! \param client: A CloudWatch client. \param muteRuleName: The name of the mute rule to create. \param alarmName: The alarm to target. \return bool: Function succeeded. */ bool muteAlarmForMaintenance(const Aws::CloudWatch::CloudWatchClient &client, const Aws::String &muteRuleName, const Aws::String &alarmName) { std::cout << "7. Mute the alarm for a maintenance window" << std::endl << std::endl; std::cout << "While a mute rule is active the targeted alarms keep evaluating and" << std::endl << "keep changing state, but their actions do not fire. This is the" << std::endl << "supported way to suppress notifications during planned maintenance," << std::endl << "instead of disabling alarm actions and hoping someone remembers to" << std::endl << "turn them back on." << std::endl << std::endl; // The expression is a five-field cron expression, // cron(Minutes Hours Day-of-month Month Day-of-week). Note that this is five // fields, not the six that Amazon EventBridge uses. For a one-time window, use // at(yyyy-MM-ddThh:mm), with no seconds. The duration is an ISO 8601 duration // from PT1M to P15D, so PT2H rather than 2h. const Aws::String expression("cron(0 2 * * SUN)"); const Aws::String duration("PT2H"); const Aws::String timezone("America/Los_Angeles"); Aws::CloudWatch::Model::Schedule schedule; schedule.SetExpression(expression); schedule.SetDuration(duration); schedule.SetTimezone(timezone); Aws::CloudWatch::Model::Rule rule; rule.SetSchedule(schedule); // Target up to 100 alarms. If MuteTargets is not set, the rule applies to every // alarm in the account. Aws::CloudWatch::Model::MuteTargets muteTargets; muteTargets.AddAlarmNames(alarmName); Aws::CloudWatch::Model::PutAlarmMuteRuleRequest putRequest; putRequest.SetName(muteRuleName); putRequest.SetDescription( "A mute rule created by the AWS SDK for C++ Basics scenario."); putRequest.SetRule(rule); putRequest.SetMuteTargets(muteTargets); auto putOutcome = client.PutAlarmMuteRule(putRequest); if (!putOutcome.IsSuccess()) { std::cerr << "Failed to put alarm mute rule: " << putOutcome.GetError().GetMessage() << std::endl; return false; } std::cout << "Created mute rule " << muteRuleName << ":" << std::endl; std::cout << " schedule: " << expression << " for " << duration << std::endl; std::cout << " timezone: " << timezone << std::endl; std::cout << " targets: " << alarmName << std::endl << std::endl; std::cout << "Note the two formats here. The expression is a five-field cron" << std::endl << "expression, five rather than the six Amazon EventBridge uses. The" << std::endl << "duration is an ISO 8601 duration, so \"PT2H\" and not \"2h\"." << std::endl << std::endl; std::cout << "Also note that MuteTargets is set explicitly. If you leave it out," << std::endl << "the rule applies to every alarm in the account." << std::endl; Aws::CloudWatch::Model::GetAlarmMuteRuleRequest getRequest; getRequest.SetAlarmMuteRuleName(muteRuleName); auto getOutcome = client.GetAlarmMuteRule(getRequest); if (getOutcome.IsSuccess()) { const auto &result = getOutcome.GetResult(); std::cout << "Read the rule back: status " << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(result.GetStatus()) << ", mute type " << result.GetMuteType() << "." << std::endl; } Aws::Vector<Aws::CloudWatch::Model::AlarmMuteRuleSummary> summaries; Aws::CloudWatch::Model::ListAlarmMuteRulesRequest listRequest; listRequest.SetAlarmName(alarmName); bool done = false; while (!done) { auto listOutcome = client.ListAlarmMuteRules(listRequest); if (!listOutcome.IsSuccess()) { std::cerr << "Failed to list alarm mute rules: " << listOutcome.GetError().GetMessage() << std::endl; return false; } const auto &page = listOutcome.GetResult().GetAlarmMuteRuleSummaries(); summaries.insert(summaries.end(), page.begin(), page.end()); const auto &nextToken = listOutcome.GetResult().GetNextToken(); listRequest.SetNextToken(nextToken); done = nextToken.empty(); } std::cout << "Found " << summaries.size() << " mute rules targeting this alarm." << std::endl; // Mute rule summaries carry no name field, only an ARN, so match on the ARN // suffix. for (const auto &summary : summaries) { const auto &arn = summary.GetAlarmMuteRuleArn(); const Aws::String slashSuffix = "/" + muteRuleName; const Aws::String colonSuffix = ":" + muteRuleName; if ((arn.size() >= slashSuffix.size() && arn.compare(arn.size() - slashSuffix.size(), slashSuffix.size(), slashSuffix) == 0) || (arn.size() >= colonSuffix.size() && arn.compare(arn.size() - colonSuffix.size(), colonSuffix.size(), colonSuffix) == 0)) { std::cout << " matched by ARN: " << arn << " (" << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(summary.GetStatus()) << ")" << std::endl; } } return true; } //! Delete everything the scenario created. /*! \param client: A CloudWatch client. \param alarmName: The alarm to delete. \param dashboardName: The dashboard to delete. \param muteRuleName: The mute rule to delete. \param dashboardCreated: Whether a dashboard was created. \param startedEnrichment: Whether this run started OTel enrichment. \return bool: Every deletion succeeded. */ bool cleanUp(const Aws::CloudWatch::CloudWatchClient &client, const Aws::String &alarmName, const Aws::String &dashboardName, const Aws::String &muteRuleName, bool dashboardCreated, bool startedEnrichment) { std::cout << "8. Clean up" << std::endl << std::endl; // Each deletion is attempted independently so that one failure does not leave // the remaining resources behind. bool result = true; Aws::CloudWatch::Model::DeleteAlarmMuteRuleRequest muteRuleRequest; muteRuleRequest.SetAlarmMuteRuleName(muteRuleName); auto muteRuleOutcome = client.DeleteAlarmMuteRule(muteRuleRequest); if (muteRuleOutcome.IsSuccess()) { std::cout << "Deleted mute rule " << muteRuleName << "." << std::endl; } else { std::cerr << "Could not delete the mute rule: " << muteRuleOutcome.GetError().GetMessage() << std::endl; result = false; } Aws::CloudWatch::Model::DeleteAlarmsRequest alarmRequest; alarmRequest.AddAlarmNames(alarmName); auto alarmOutcome = client.DeleteAlarms(alarmRequest); if (alarmOutcome.IsSuccess()) { std::cout << "Deleted alarm " << alarmName << "." << std::endl; } else { std::cerr << "Could not delete the alarm: " << alarmOutcome.GetError().GetMessage() << std::endl; result = false; } if (dashboardCreated) { Aws::CloudWatch::Model::DeleteDashboardsRequest dashboardRequest; dashboardRequest.AddDashboardNames(dashboardName); auto dashboardOutcome = client.DeleteDashboards(dashboardRequest); if (dashboardOutcome.IsSuccess()) { std::cout << "Deleted dashboard " << dashboardName << "." << std::endl; } else { std::cerr << "Could not delete the dashboard: " << dashboardOutcome.GetError().GetMessage() << std::endl; result = false; } } if (!startedEnrichment) { std::cout << "Left OTel enrichment running, because it was already on before" << std::endl << "this run." << std::endl; return result; } Aws::CloudWatch::Model::StopOTelEnrichmentRequest stopRequest; auto stopOutcome = client.StopOTelEnrichment(stopRequest); if (stopOutcome.IsSuccess()) { std::cout << "Stopped OTel enrichment, because this run started it." << std::endl; } else { std::cerr << "Could not stop OTel enrichment: " << stopOutcome.GetError().GetMessage() << std::endl; result = false; } return result; } } // namespace //! Run the Amazon CloudWatch Basics scenario. /*! \param query: The PromQL query to alarm on. \param clientConfig: AWS client configuration. \return bool: Function succeeded. */ bool runCloudWatchScenario(const Aws::String &query, const Aws::Client::ClientConfiguration &clientConfig) { Aws::CloudWatch::CloudWatchClient client(clientConfig); // Suffix the resource names so repeated runs do not collide. std::mt19937 generator(std::random_device{}()); const int suffix = std::uniform_int_distribution<int>(1000, 9999)(generator); const Aws::String alarmName = "doc-example-promql-alarm-" + std::to_string(suffix); const Aws::String dashboardName = "doc-example-dashboard-" + std::to_string(suffix); const Aws::String muteRuleName = "doc-example-mute-rule-" + std::to_string(suffix); Aws::CloudWatch::Model::Metric metric; bool startedEnrichment = false; bool dashboardCreated = false; bool result = true; std::cout << DASHES << std::endl; std::cout << "Welcome to the Amazon CloudWatch Basics scenario." << std::endl << std::endl; std::cout << "CloudWatch now ingests OpenTelemetry metrics natively. This scenario" << std::endl << "walks through that experience: it turns on OTel enrichment so" << std::endl << "CloudWatch can correlate incoming OTLP metrics with the resources that" << std::endl << "produced them, alarms on those metrics with a PromQL query, and shows" << std::endl << "you which individual series drove the alarm." << std::endl << std::endl; std::cout << "A PromQL alarm works differently from a classic metric alarm. Rather" << std::endl << "than watching one metric and counting breaching periods, it evaluates" << std::endl << "a query that can match many series at once, and tracks each one" << std::endl << "separately as a contributor." << std::endl; std::cout << DASHES << std::endl; pressEnter(); // Every step prompts for Enter before the next one, but only while the scenario is // still healthy. Once a step fails the remaining steps are skipped, so there is // nothing left to wait for and the run goes straight to cleanup. if (!listMetricsAndNamespaces(client, metric)) { result = false; } std::cout << DASHES << std::endl; if (result) { pressEnter(); } if (result && !startOTelEnrichment(client, startedEnrichment)) { result = false; } std::cout << DASHES << std::endl; if (result) { pressEnter(); } if (result) { explainOtlpIngestion(); std::cout << DASHES << std::endl; pressEnter(); } if (result && !createPromQLAlarm(client, alarmName, query)) { result = false; } std::cout << DASHES << std::endl; if (result) { pressEnter(); } if (result && !inspectAlarmContributors(client, alarmName)) { result = false; } std::cout << DASHES << std::endl; if (result) { pressEnter(); } if (result && !getStatisticsAndChartMetric(client, metric, dashboardName, clientConfig.region, dashboardCreated)) { result = false; } std::cout << DASHES << std::endl; if (result) { pressEnter(); } if (result && !muteAlarmForMaintenance(client, muteRuleName, alarmName)) { result = false; } std::cout << DASHES << std::endl; if (result) { pressEnter(); } // Clean up regardless of whether an earlier step failed, so a partial run does not // leave resources behind. if (!cleanUp(client, alarmName, dashboardName, muteRuleName, dashboardCreated, startedEnrichment)) { result = false; } std::cout << DASHES << std::endl; std::cout << "This concludes the Amazon CloudWatch Basics scenario." << std::endl; std::cout << DASHES << std::endl; return result; }

작업

다음 코드 예시는 DeleteAlarmMuteRule의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/DeleteAlarmMuteRuleRequest.h> #include <iostream>

경보 음소거 규칙을 삭제합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::DeleteAlarmMuteRuleRequest request; request.SetAlarmMuteRuleName(mute_rule_name); auto outcome = cw.DeleteAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted alarm mute rule " << mute_rule_name << std::endl; }

다음 코드 예시는 DeleteAlarms의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/DeleteAlarmsRequest.h> #include <iostream>

경보를 삭제합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DeleteAlarmsRequest request; request.AddAlarmNames(alarm_name); auto outcome = cw.DeleteAlarms(request); if (!outcome.IsSuccess()) { std::cout << "Failed to delete CloudWatch alarm:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted CloudWatch alarm " << alarm_name << std::endl; }
  • API 세부 정보는 AWS SDK for C++ API 참조의 DeleteAlarms를 참조하세요.

다음 코드 예시는 DescribeAlarmContributors의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmContributor.h> #include <aws/monitoring/model/DescribeAlarmContributorsRequest.h> #include <iostream>

PromQL 경보의 기여자를 설명합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::DescribeAlarmContributorsRequest request; request.SetAlarmName(alarm_name); // Collect every page before reporting. A page can come back empty while still // carrying a next token, so the loop must keep going until the token is empty // rather than stopping at the first empty page. Aws::Vector<Aws::CloudWatch::Model::AlarmContributor> contributors; bool failed = false; bool done = false; while (!done) { auto outcome = cw.DescribeAlarmContributors(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to describe alarm contributors: " << outcome.GetError().GetMessage() << std::endl; failed = true; break; } const auto &page = outcome.GetResult().GetAlarmContributors(); contributors.insert(contributors.end(), page.begin(), page.end()); const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); } if (!failed) { if (contributors.empty()) { std::cout << "No contributors yet. The query matched no series, " "which usually means no OTel metrics with these labels " "have arrived." << std::endl; } else { std::cout << "Contributors for alarm " << alarm_name << ":" << std::endl; for (const auto &contributor : contributors) { std::cout << " " << contributor.GetContributorId() << ": "; bool first = true; for (const auto &label : contributor.GetContributorAttributes()) { if (!first) { std::cout << ", "; } std::cout << label.first << "=" << label.second; first = false; } std::cout << std::endl; std::cout << " reason: " << contributor.GetStateReason() << std::endl; } } }

다음 코드 예시는 DescribeAlarmsForMetric의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/DescribeAlarmsRequest.h> #include <aws/monitoring/model/DescribeAlarmsResult.h> #include <iomanip> #include <iostream>

경보를 설명합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DescribeAlarmsRequest request; request.SetMaxRecords(1); bool done = false; bool header = false; while (!done) { auto outcome = cw.DescribeAlarms(request); if (!outcome.IsSuccess()) { std::cout << "Failed to describe CloudWatch alarms:" << outcome.GetError().GetMessage() << std::endl; break; } if (!header) { std::cout << std::left << std::setw(32) << "Name" << std::setw(64) << "Arn" << std::setw(64) << "Description" << std::setw(20) << "LastUpdated" << std::endl; header = true; } const auto &alarms = outcome.GetResult().GetMetricAlarms(); for (const auto &alarm : alarms) { std::cout << std::left << std::setw(32) << alarm.GetAlarmName() << std::setw(64) << alarm.GetAlarmArn() << std::setw(64) << alarm.GetAlarmDescription() << std::setw(20) << alarm.GetAlarmConfigurationUpdatedTimestamp().ToGmtString( SIMPLE_DATE_FORMAT_STR) << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }

다음 코드 예시는 DisableAlarmActions의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/DisableAlarmActionsRequest.h> #include <iostream>

경보 작업 사용을 중지합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DisableAlarmActionsRequest disableAlarmActionsRequest; disableAlarmActionsRequest.AddAlarmNames(alarm_name); auto disableAlarmActionsOutcome = cw.DisableAlarmActions(disableAlarmActionsRequest); if (!disableAlarmActionsOutcome.IsSuccess()) { std::cout << "Failed to disable actions for alarm " << alarm_name << ": " << disableAlarmActionsOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully disabled actions for alarm " << alarm_name << std::endl; }
  • API 세부 정보에 대한 내용은 AWS SDK for C++ API 참조의 DisableAlarmActions를 참조하세요.

다음 코드 예시는 EnableAlarmActions의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/EnableAlarmActionsRequest.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <iostream>

경보 작업을 사용합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarm_name); request.SetComparisonOperator( Aws::CloudWatch::Model::ComparisonOperator::GreaterThanThreshold); request.SetEvaluationPeriods(1); request.SetMetricName("CPUUtilization"); request.SetNamespace("AWS/EC2"); request.SetPeriod(60); request.SetStatistic(Aws::CloudWatch::Model::Statistic::Average); request.SetThreshold(70.0); request.SetActionsEnabled(false); request.SetAlarmDescription("Alarm when server CPU exceeds 70%"); request.SetUnit(Aws::CloudWatch::Model::StandardUnit::Seconds); request.AddAlarmActions(actionArn); Aws::CloudWatch::Model::Dimension dimension; dimension.SetName("InstanceId"); dimension.SetValue(instanceId); request.AddDimensions(dimension); auto outcome = cw.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create CloudWatch alarm:" << outcome.GetError().GetMessage() << std::endl; return; } Aws::CloudWatch::Model::EnableAlarmActionsRequest enable_request; enable_request.AddAlarmNames(alarm_name); auto enable_outcome = cw.EnableAlarmActions(enable_request); if (!enable_outcome.IsSuccess()) { std::cout << "Failed to enable alarm actions:" << enable_outcome.GetError().GetMessage() << std::endl; return; } std::cout << "Successfully created alarm " << alarm_name << " and enabled actions on it." << std::endl;
  • API 세부 정보는 AWS SDK for C++ API 참조의 EnableAlarmActions를 참조하세요.

다음 코드 예시는 GetAlarmMuteRule의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmMuteRuleStatus.h> #include <aws/monitoring/model/GetAlarmMuteRuleRequest.h> #include <iostream>

경보 음소거 규칙을 가져옵니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::GetAlarmMuteRuleRequest request; request.SetAlarmMuteRuleName(mute_rule_name); auto outcome = cw.GetAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to get alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { const auto &result = outcome.GetResult(); std::cout << "Mute rule " << result.GetName() << " is " << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(result.GetStatus()) << "." << std::endl; std::cout << " ARN: " << result.GetAlarmMuteRuleArn() << std::endl; std::cout << " schedule: " << result.GetRule().GetSchedule().GetExpression() << " for " << result.GetRule().GetSchedule().GetDuration() << std::endl; const auto &alarm_names = result.GetMuteTargets().GetAlarmNames(); if (!alarm_names.empty()) { std::cout << " muted alarms:"; for (const auto &alarm_name : alarm_names) { std::cout << " " << alarm_name; } std::cout << std::endl; } }
  • API 세부 정보는 API 참조의 GetAlarmMuteRuleAWS SDK for C++ 을 참조하세요.

다음 코드 예시는 GetOTelEnrichment의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/GetOTelEnrichmentRequest.h> #include <aws/monitoring/model/OTelEnrichmentStatus.h> #include <iostream>

OpenTelemetry 보강 상태를 가져옵니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::GetOTelEnrichmentRequest request; auto outcome = cw.GetOTelEnrichment(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to get OTel enrichment status: " << outcome.GetError().GetMessage() << std::endl; } else { auto status = outcome.GetResult().GetStatus(); std::cout << "OTel enrichment status is " << Aws::CloudWatch::Model::OTelEnrichmentStatusMapper:: GetNameForOTelEnrichmentStatus(status) << "." << std::endl; if (status == Aws::CloudWatch::Model::OTelEnrichmentStatus::Running) { std::cout << "Vended metrics are queryable with PromQL." << std::endl; } else { std::cout << "Start enrichment to enrich vended metrics with resource " "ARN and tag labels." << std::endl; } }
  • API 세부 정보는 API 참조의 GetOTelEnrichmentAWS SDK for C++ 를 참조하세요.

다음 코드 예시는 ListAlarmMuteRules의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmMuteRuleStatus.h> #include <aws/monitoring/model/ListAlarmMuteRulesRequest.h> #include <iostream>

경보 음소거 규칙을 나열합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::ListAlarmMuteRulesRequest request; if (argc == 2) { request.SetAlarmName(argv[1]); } bool done = false; while (!done) { auto outcome = cw.ListAlarmMuteRules(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to list alarm mute rules: " << outcome.GetError().GetMessage() << std::endl; break; } for (const auto &summary : outcome.GetResult().GetAlarmMuteRuleSummaries()) { std::cout << summary.GetAlarmMuteRuleArn() << " (" << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(summary.GetStatus()) << ")" << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }
  • API 세부 정보는 API 참조의 ListAlarmMuteRulesAWS SDK for C++ 를 참조하세요.

다음 코드 예시는 ListMetrics의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/ListMetricsRequest.h> #include <aws/monitoring/model/ListMetricsResult.h> #include <iomanip> #include <iostream>

지표를 나열합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::ListMetricsRequest request; if (argc > 1) { request.SetMetricName(argv[1]); } if (argc > 2) { request.SetNamespace(argv[2]); } bool done = false; bool header = false; while (!done) { auto outcome = cw.ListMetrics(request); if (!outcome.IsSuccess()) { std::cout << "Failed to list CloudWatch metrics:" << outcome.GetError().GetMessage() << std::endl; break; } if (!header) { std::cout << std::left << std::setw(48) << "MetricName" << std::setw(32) << "Namespace" << "DimensionNameValuePairs" << std::endl; header = true; } const auto &metrics = outcome.GetResult().GetMetrics(); for (const auto &metric : metrics) { std::cout << std::left << std::setw(48) << metric.GetMetricName() << std::setw(32) << metric.GetNamespace(); const auto &dimensions = metric.GetDimensions(); for (auto iter = dimensions.cbegin(); iter != dimensions.cend(); ++iter) { const auto &dimkv = *iter; std::cout << dimkv.GetName() << " = " << dimkv.GetValue(); if (iter + 1 != dimensions.cend()) { std::cout << ", "; } } std::cout << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }
  • API 세부 정보는 AWS SDK for C++ API 참조의 ListMetrics를 참조하세요.

다음 코드 예시는 PutAlarmMuteRule의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/MuteTargets.h> #include <aws/monitoring/model/PutAlarmMuteRuleRequest.h> #include <aws/monitoring/model/Rule.h> #include <aws/monitoring/model/Schedule.h> #include <iostream>

경보 음소거 규칙을 생성합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); // For a recurring window, use a five-field cron expression, // cron(Minutes Hours Day-of-month Month Day-of-week). Note that this is five // fields, not the six that Amazon EventBridge uses. For a one-time window, use // an at expression such as at(2026-09-05T02:00). Aws::CloudWatch::Model::Schedule schedule; schedule.SetExpression("cron(0 2 * * SUN)"); // The duration is in ISO 8601 duration format, from PT1M (one minute) to // P15D (15 days). schedule.SetDuration("PT2H"); schedule.SetTimezone("America/Los_Angeles"); Aws::CloudWatch::Model::Rule rule; rule.SetSchedule(schedule); // Target up to 100 alarms. If MuteTargets is not set, the rule applies to every // alarm in the account. Aws::CloudWatch::Model::MuteTargets muteTargets; muteTargets.AddAlarmNames(alarm_name); Aws::CloudWatch::Model::PutAlarmMuteRuleRequest request; request.SetName(mute_rule_name); request.SetDescription("A mute rule created by the AWS SDK for C++."); request.SetRule(rule); request.SetMuteTargets(muteTargets); auto outcome = cw.PutAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to put alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully put alarm mute rule " << mute_rule_name << std::endl; }
  • API 세부 정보는 API 참조의 PutAlarmMuteRuleAWS SDK for C++ 을 참조하세요.

다음 코드 예시는 PutMetricAlarm의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

PromQL 경보에 필요한 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmPromQLCriteria.h> #include <aws/monitoring/model/EvaluationCriteria.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <iostream>

OpenTelemetry 지표에 대해 PromQL 쿼리를 평가하는 경보를 생성합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::AlarmPromQLCriteria promQLCriteria; promQLCriteria.SetQuery(query); // A contributor moves to ALARM after breaching continuously for 300 seconds, // and back to OK after 120 seconds without breaching. promQLCriteria.SetPendingPeriod(300); promQLCriteria.SetRecoveryPeriod(120); Aws::CloudWatch::Model::EvaluationCriteria evaluationCriteria; evaluationCriteria.SetPromQLCriteria(promQLCriteria); Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarm_name); request.SetAlarmDescription("A PromQL alarm created by the AWS SDK for C++."); request.SetEvaluationCriteria(evaluationCriteria); // Valid values are 10, 20, 30, and any multiple of 60, up to 3600. request.SetEvaluationInterval(30); auto outcome = cw.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to create PromQL alarm: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created PromQL alarm " << alarm_name << " for query " << query << std::endl; }

지표 경보에 필요한 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <iostream>

지표를 감시할 경보를 생성합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarm_name); request.SetComparisonOperator( Aws::CloudWatch::Model::ComparisonOperator::GreaterThanThreshold); request.SetEvaluationPeriods(1); request.SetMetricName("CPUUtilization"); request.SetNamespace("AWS/EC2"); request.SetPeriod(60); request.SetStatistic(Aws::CloudWatch::Model::Statistic::Average); request.SetThreshold(70.0); request.SetActionsEnabled(false); request.SetAlarmDescription("Alarm when server CPU exceeds 70%"); request.SetUnit(Aws::CloudWatch::Model::StandardUnit::Seconds); Aws::CloudWatch::Model::Dimension dimension; dimension.SetName("InstanceId"); dimension.SetValue(instanceId); request.AddDimensions(dimension); auto outcome = cw.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create CloudWatch alarm:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created CloudWatch alarm " << alarm_name << std::endl; }
  • API 세부 정보는 AWS SDK for C++ API 참조의 PutMetricAlarm을 참조하세요.

다음 코드 예시는 PutMetricData의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/PutMetricDataRequest.h> #include <iostream>

지표에 데이터를 입력합니다.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::Dimension dimension; dimension.SetName("UNIQUE_PAGES"); dimension.SetValue("URLS"); Aws::CloudWatch::Model::MetricDatum datum; datum.SetMetricName("PAGES_VISITED"); datum.SetUnit(Aws::CloudWatch::Model::StandardUnit::None); datum.SetValue(data_point); datum.AddDimensions(dimension); Aws::CloudWatch::Model::PutMetricDataRequest request; request.SetNamespace("SITE/TRAFFIC"); request.AddMetricData(datum); auto outcome = cw.PutMetricData(request); if (!outcome.IsSuccess()) { std::cout << "Failed to put sample metric data:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully put sample metric data" << std::endl; }
  • API 세부 정보는 AWS SDK for C++ API 참조의 PutMetricData를 참조하세요.

다음 코드 예시는 StartOTelEnrichment의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/StartOTelEnrichmentRequest.h> #include <iostream>

OpenTelemetry 보강을 시작합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::StartOTelEnrichmentRequest request; auto outcome = cw.StartOTelEnrichment(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to start OTel enrichment: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully started OTel enrichment for this account." << std::endl; }

다음 코드 예시는 StopOTelEnrichment의 사용 방법을 보여줍니다.

SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

필수 파일을 포함합니다.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/StopOTelEnrichmentRequest.h> #include <iostream>

OpenTelemetry 보강을 중지합니다.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::StopOTelEnrichmentRequest request; auto outcome = cw.StopOTelEnrichment(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to stop OTel enrichment: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully stopped OTel enrichment for this account." << std::endl; }
  • API 세부 정보는 API 참조의 StopOTelEnrichmentAWS SDK for C++ 를 참조하세요.