AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
TracingUtils.h
1
5#pragma once
6
7#include <aws/core/monitoring/HttpClientMetrics.h>
8#include <aws/core/utils/logging/LogMacros.h>
9#include <smithy/Smithy_EXPORTS.h>
10#include <smithy/tracing/Meter.h>
11#include <functional>
12#include <chrono>
13#include <utility>
14
15namespace smithy {
16 namespace components {
17 namespace tracing {
21 class SMITHY_API TracingUtils {
22 public:
23 TracingUtils() = default;
24
25 static const char COUNT_METRIC_TYPE[];
26 static const char MICROSECOND_METRIC_TYPE[];
27 static const char BYTES_PER_SECOND_METRIC_TYPE[];
28 static const char SMITHY_CLIENT_DURATION_METRIC[];
29 static const char SMITHY_CLIENT_ENDPOINT_RESOLUTION_METRIC[];
30 static const char SMITHY_CLIENT_DESERIALIZATION_METRIC[];
31 static const char SMITHY_CLIENT_SIGNING_METRIC[];
32 static const char SMITHY_CLIENT_SERIALIZATION_METRIC[];
33 static const char SMITHY_CLIENT_SERVICE_CALL_METRIC[];
34 static const char SMITHY_CLIENT_SERVICE_BACKOFF_DELAY_METRIC[];
35 static const char SMITHY_CLIENT_SERVICE_ATTEMPTS_METRIC[];
36 static const char SMITHY_METHOD_AWS_VALUE[];
37 static const char SMITHY_SERVICE_DIMENSION[];
38 static const char SMITHY_METHOD_DIMENSION[];
39 static const char SMITHY_SYSTEM_DIMENSION[];
40 static const char SMITHY_METRICS_DNS_DURATION[];
41 static const char SMITHY_METRICS_CONNECT_DURATION[];
42 static const char SMITHY_METRICS_SSL_DURATION[];
43 static const char SMITHY_METRICS_DOWNLOAD_SPEED_METRIC[];
44 static const char SMITHY_METRICS_UPLOAD_SPEED_METRIC[];
45 static const char SMITHY_METRICS_UNKNOWN_METRIC[];
46
58 template<typename T>
59 static T MakeCallWithTiming(std::function<T()> func,
60 const Aws::String &metricName,
61 const Meter &meter,
63 const Aws::String &description = "")
64 {
65 auto before = std::chrono::steady_clock::now();
66 auto returnValue = func();
67 auto after = std::chrono::steady_clock::now();
68 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(after - before).count();
69 auto histogram = meter.CreateHistogram(metricName, MICROSECOND_METRIC_TYPE, description);
70 if (!histogram) {
71 AWS_LOG_ERROR("TracingUtil", "Failed to create histogram");
72 return {};
73 }
74 histogram->record((double) duration, std::forward<Aws::Map<Aws::String, Aws::String>>(attributes));
75 return returnValue;
76 }
77
87 static void MakeCallWithTiming(std::function<void(void)> func,
88 Aws::String metricName,
89 const Meter &meter,
91 Aws::String description = "")
92 {
93 auto before = std::chrono::steady_clock::now();
94 func();
95 auto after = std::chrono::steady_clock::now();
96 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(after - before).count();
97 auto histogram = meter.CreateHistogram(std::move(metricName), MICROSECOND_METRIC_TYPE, std::move(description));
98 if (!histogram) {
99 AWS_LOG_ERROR("TracingUtil", "Failed to create histogram");
100 return;
101 }
102 histogram->record((double) duration, std::forward<Aws::Map<Aws::String, Aws::String>>(attributes));
103 }
104
113 const Meter &meter,
115 Aws::String description = "")
116 {
117 for (auto const &entry: metrics) {
118 auto smithyMetric = ConvertCoreMetricToSmithy(entry.first);
119 if (smithyMetric.first != SMITHY_METRICS_UNKNOWN_METRIC) {
120 auto histogram = meter.CreateHistogram(std::move(smithyMetric.first),
121 smithyMetric.second,
122 std::move(description));
123 if (!histogram) {
124 AWS_LOG_ERROR("TracingUtil", "Failed to create histogram");
125 }
126 histogram->record((double) entry.second, attributes);
127 }
128 }
129 }
130
136 static std::pair<Aws::String, Aws::String> ConvertCoreMetricToSmithy(const Aws::String &name) {
137 //TODO: Make static map, Aws::Map cannot be made static with a customer memory manager as of the moment.
139 {
140 std::pair<int, std::pair<Aws::String, Aws::String>>(
142 std::make_pair(SMITHY_METRICS_DNS_DURATION, MICROSECOND_METRIC_TYPE)),
143 std::pair<int, std::pair<Aws::String, Aws::String>>(
145 std::make_pair(SMITHY_METRICS_CONNECT_DURATION, MICROSECOND_METRIC_TYPE)),
146 std::pair<int, std::pair<Aws::String, Aws::String>>(
148 std::make_pair(SMITHY_METRICS_SSL_DURATION, MICROSECOND_METRIC_TYPE)),
149 std::pair<int, std::pair<Aws::String, Aws::String>>(
151 std::make_pair(SMITHY_METRICS_DOWNLOAD_SPEED_METRIC, BYTES_PER_SECOND_METRIC_TYPE)),
152 std::pair<int, std::pair<Aws::String, Aws::String>>(
154 std::make_pair(SMITHY_METRICS_UPLOAD_SPEED_METRIC, BYTES_PER_SECOND_METRIC_TYPE)),
155 };
156
158 auto it = metricsTypeToName.find(static_cast<int>(metricType));
159 if (it == metricsTypeToName.end()) {
160 return std::make_pair(SMITHY_METRICS_UNKNOWN_METRIC, "unknown");
161 }
162 return it->second;
163 }
164 };
165 }
166 }
167}
virtual Aws::UniquePtr< Histogram > CreateHistogram(Aws::String name, Aws::String units, Aws::String description) const =0
static void EmitCoreHttpMetrics(const Aws::Monitoring::HttpClientMetricsCollection &metrics, const Meter &meter, Aws::Map< Aws::String, Aws::String > &&attributes, Aws::String description="")
static T MakeCallWithTiming(std::function< T()> func, const Aws::String &metricName, const Meter &meter, Aws::Map< Aws::String, Aws::String > &&attributes, const Aws::String &description="")
static std::pair< Aws::String, Aws::String > ConvertCoreMetricToSmithy(const Aws::String &name)
static void MakeCallWithTiming(std::function< void(void)> func, Aws::String metricName, const Meter &meter, Aws::Map< Aws::String, Aws::String > &&attributes, Aws::String description="")
Aws::Map< Aws::String, int64_t > HttpClientMetricsCollection
AWS_CORE_API HttpClientMetricsType GetHttpClientMetricTypeByName(const Aws::String &name)
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition AWSMap.h:20
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition AWSString.h:97