View a markdown version of this page

Use GetOTelEnrichment with an AWS SDK - AWS SDK Code Examples

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

Use GetOTelEnrichment with an AWS SDK

The following code examples show how to use GetOTelEnrichment.

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> /// Get the current OTel enrichment status for the account. /// </summary> /// <returns>The status, either Running or Stopped.</returns> public async Task<OTelEnrichmentStatus> GetOTelEnrichmentStatus() { var response = await _amazonCloudWatch.GetOTelEnrichmentAsync( new GetOTelEnrichmentRequest()); _logger.LogInformation($"OTel enrichment status is {response.Status}."); return response.Status; }
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/GetOTelEnrichmentRequest.h> #include <aws/monitoring/model/OTelEnrichmentStatus.h> #include <iostream>

Get the OpenTelemetry enrichment status.

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; } }
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.

/** * Gets the current OTel enrichment status for the account. * * @param cw the CloudWatch client * @return the status, either {@code Running} or {@code Stopped} */ public static String getOTelEnrichmentStatus(CloudWatchClient cw) { try { String status = cw.getOTelEnrichment(GetOTelEnrichmentRequest.builder().build()) .statusAsString(); System.out.printf("OTel enrichment status is %s.%n", status); return status; } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); return null; } }
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 { GetOTelEnrichmentCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Get the current OTel enrichment status for the account. Status is either // "Running" or "Stopped". const run = async () => { const command = new GetOTelEnrichmentCommand({}); try { const response = await client.send(command); console.log(`OTel enrichment status is ${response.Status}.`); return response; } 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 getOTelEnrichmentStatus(): OTelEnrichmentStatus? { CloudWatchClient.fromEnvironment { region = "us-east-1" }.use { cwClient -> val response = cwClient.getOTelEnrichment(GetOTelEnrichmentRequest {}) val status = response.status when (status) { is OTelEnrichmentStatus.Running -> println("OTel enrichment is running. Vended metrics are queryable with PromQL") is OTelEnrichmentStatus.Stopped -> println("OTel enrichment is stopped. Start it to enrich vended metrics") else -> println("OTel enrichment status is ${status?.value}") } return status } }
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 get_otel_enrichment_status(self): """ Gets the current OTel enrichment status for the account. :return: The status, either 'Running' or 'Stopped'. """ try: response = self.cloudwatch_client.get_o_tel_enrichment() except ClientError: logger.exception("Couldn't get the OTel enrichment status.") raise else: status = response["Status"] logger.info("OTel enrichment status is %s.", status) return status
  • For API details, see GetOTelEnrichment in AWS SDK for Python (Boto3) API Reference.

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.

# Gets the current OTel enrichment status for the account. # # @param cloudwatch_client [Aws::CloudWatch::Client] An initialized CloudWatch client. # @return [String, nil] 'Running' or 'Stopped', or nil if the status could not be read. def otel_enrichment_status(cloudwatch_client) cloudwatch_client.get_o_tel_enrichment.status rescue StandardError => e puts "Error getting OTel enrichment status: #{e.message}" nil end