View a markdown version of this page

Use StartOTelEnrichment with an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use StartOTelEnrichment with an AWS SDK

The following code examples show how to use StartOTelEnrichment.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

.NET
SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/// <summary> /// Turn on OTel enrichment for the account. Once enrichment is running, CloudWatch /// vended metrics that carry a resource identifier dimension, such as the Amazon EC2 /// CPUUtilization metric with its InstanceId dimension, are decorated with resource /// ARN and resource tag labels and become queryable with PromQL. /// /// Resource tags on telemetry must already be enabled for the account before you /// call this operation. /// </summary> /// <returns>True if successful.</returns> public async Task<bool> StartOTelEnrichment() { var response = await _amazonCloudWatch.StartOTelEnrichmentAsync( new StartOTelEnrichmentRequest()); _logger.LogInformation("Started OTel enrichment for this account."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Include the required files.

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

Start OpenTelemetry enrichment.

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; }
Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/** * Turns on OTel enrichment for the account. Once enrichment is running, CloudWatch * vended metrics that carry a resource identifier dimension, such as the EC2 * CPUUtilization metric with its InstanceId dimension, are decorated with resource * ARN and resource tag labels and become queryable with PromQL. * * <p>Resource tags on telemetry must already be enabled for the account before you * call this operation. * * @param cw the CloudWatch client */ public static void startOTelEnrichment(CloudWatchClient cw) { try { cw.startOTelEnrichment(StartOTelEnrichmentRequest.builder().build()); System.out.println("Started OTel enrichment for this account."); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import { StartOTelEnrichmentCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Turn on OTel enrichment for the account. Once enrichment is running, CloudWatch // vended metrics that carry a resource identifier dimension - for example the EC2 // CPUUtilization metric with its InstanceId dimension - are decorated with resource // ARN and resource tag labels, and become queryable with PromQL. // // Resource tags on telemetry must already be enabled for the account before you call // this operation. const run = async () => { const command = new StartOTelEnrichmentCommand({}); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun startOTelEnrichment() { CloudWatchClient.fromEnvironment { region = "us-east-1" }.use { cwClient -> cwClient.startOTelEnrichment(StartOTelEnrichmentRequest {}) println("Successfully started OTel enrichment for this account") } }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

class CloudWatchOTelWrapper: """Encapsulates the OpenTelemetry-oriented Amazon CloudWatch operations.""" def __init__(self, cloudwatch_client): """ :param cloudwatch_client: A Boto3 CloudWatch client. The OpenTelemetry operations are only available on the client interface, not on the higher-level ``boto3.resource("cloudwatch")`` interface. """ self.cloudwatch_client = cloudwatch_client @classmethod def from_client(cls): """ Creates a wrapper backed by a default CloudWatch client. :return: A CloudWatchOTelWrapper. """ return cls(boto3.client("cloudwatch")) def start_otel_enrichment(self): """ Turns on OTel enrichment for the account. Once enrichment is running, CloudWatch vended metrics that carry a resource identifier dimension, such as the EC2 CPUUtilization metric with its InstanceId dimension, are decorated with resource ARN and resource tag labels and become queryable with PromQL. Resource tags on telemetry must already be enabled for the account before you call this operation. """ try: # Boto3 splits the OTel prefix when it converts the StartOTelEnrichment # operation name to snake case, so the method is start_o_tel_enrichment. self.cloudwatch_client.start_o_tel_enrichment() logger.info("Started OTel enrichment for this account.") except ClientError: logger.exception("Couldn't start OTel enrichment.") raise
Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

# Turns on OTel enrichment for the account. Once enrichment is running, CloudWatch vended # metrics that carry a resource identifier dimension, such as the Amazon EC2 # CPUUtilization metric with its InstanceId dimension, are decorated with resource ARN # and resource tag labels and become queryable with PromQL. # # Resource tags on telemetry must already be enabled for the account before you call # this operation. # # Note that the Ruby SDK renders the OTel prefix as +o_tel+, so the method is # +start_o_tel_enrichment+ rather than +start_otel_enrichment+. # # @param cloudwatch_client [Aws::CloudWatch::Client] An initialized CloudWatch client. # @return [Boolean] true if enrichment was started; otherwise, false. def otel_enrichment_started?(cloudwatch_client) cloudwatch_client.start_o_tel_enrichment true rescue StandardError => e puts "Error starting OTel enrichment: #{e.message}" false end