-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTracingUtils.h
More file actions
155 lines (147 loc) · 8.15 KB
/
TracingUtils.h
File metadata and controls
155 lines (147 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once
#include <aws/core/monitoring/HttpClientMetrics.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <smithy/Smithy_EXPORTS.h>
#include <smithy/tracing/Meter.h>
#include <functional>
#include <chrono>
#include <utility>
namespace smithy {
namespace components {
namespace tracing {
/**
* A utility class for common tracing activities.
*/
class SMITHY_API TracingUtils {
using SteadyTime = std::chrono::steady_clock::time_point;
static void RecordExecutionDuration(
SteadyTime before,
SteadyTime after,
Aws::String metricName,
const Meter &meter,
Aws::Map<Aws::String, Aws::String> attributes,
Aws::String description
);
public:
TracingUtils() = default;
static const char COUNT_METRIC_TYPE[];
static const char MICROSECOND_METRIC_TYPE[];
static const char BYTES_PER_SECOND_METRIC_TYPE[];
static const char SMITHY_CLIENT_DURATION_METRIC[];
static const char SMITHY_CLIENT_ENDPOINT_RESOLUTION_METRIC[];
static const char SMITHY_CLIENT_DESERIALIZATION_METRIC[];
static const char SMITHY_CLIENT_SIGNING_METRIC[];
static const char SMITHY_CLIENT_SERIALIZATION_METRIC[];
static const char SMITHY_CLIENT_SERVICE_CALL_METRIC[];
static const char SMITHY_CLIENT_SERVICE_BACKOFF_DELAY_METRIC[];
static const char SMITHY_CLIENT_SERVICE_ATTEMPTS_METRIC[];
static const char SMITHY_METHOD_AWS_VALUE[];
static const char SMITHY_SERVICE_DIMENSION[];
static const char SMITHY_METHOD_DIMENSION[];
static const char SMITHY_SYSTEM_DIMENSION[];
static const char SMITHY_METRICS_DNS_DURATION[];
static const char SMITHY_METRICS_CONNECT_DURATION[];
static const char SMITHY_METRICS_SSL_DURATION[];
static const char SMITHY_METRICS_DOWNLOAD_SPEED_METRIC[];
static const char SMITHY_METRICS_UPLOAD_SPEED_METRIC[];
static const char SMITHY_METRICS_UNKNOWN_METRIC[];
/**
* Will run a function and emit the duration of that function in millisecond timing to the
* meter provided as a Histogram metrics. Will return the result af the function.
* @tparam T The type that is being returned from the function.
* @param func A function that returns T.
* @param metricName The name of the metric that is being captured by the function.
* @param meter The meter making the measurement.
* @param attributes The attributes or dimensions associate with this measurement.
* @param description The description of the measurement.
* @return the result of func.
*/
template<typename T>
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 = "")
{
auto before = std::chrono::steady_clock::now();
auto returnValue = func();
auto after = std::chrono::steady_clock::now();
RecordExecutionDuration(before, after, std::move(metricName), meter, std::move(attributes), std::move(description));
return returnValue;
}
/**
* Will run a function and emit the duration of that function in millisecond timing to the
* meter provided as a Histogram metrics.
* @param func a function that does not return anything but will be measured.
* @param metricName The name of the metric that is being captured by the function.
* @param meter The meter making the measurement.
* @param attributes The attributes or dimensions associate with this measurement.
* @param description The description of the measurement.
*/
static void MakeCallWithTiming(std::function<void(void)> func,
Aws::String metricName,
const Meter &meter,
Aws::Map<Aws::String, Aws::String>&& attributes,
Aws::String description = "")
{
auto before = std::chrono::steady_clock::now();
func();
auto after = std::chrono::steady_clock::now();
RecordExecutionDuration(before, after, std::move(metricName), meter, std::move(attributes), std::move(description));
}
/**
* Emits http metrics to a specified meter.
* @param metrics A http metrics collection that we will emit.
* @param meter The meter used for metrics emissions.
* @param attributes The attributes or dimensions associate with this measurement.
* @param description The description of the measurement.
*/
static void EmitCoreHttpMetrics(const Aws::Monitoring::HttpClientMetricsCollection &metrics,
const Meter &meter,
Aws::Map<Aws::String, Aws::String>&& attributes,
Aws::String description = "")
{
for (auto const &entry: metrics) {
auto smithyMetric = ConvertCoreMetricToSmithy(entry.first);
if (smithyMetric.first != SMITHY_METRICS_UNKNOWN_METRIC) {
auto histogram = meter.CreateHistogram(std::move(smithyMetric.first),
smithyMetric.second,
std::move(description));
if (!histogram) {
AWS_LOG_ERROR("TracingUtil", "Failed to create histogram");
} else {
histogram->record((double) entry.second, attributes);
}
}
}
}
/**
* Converts the string Representation of a Core metric to a smithy metric.
* @param name the metric name.
* @return A tuple of metric name to measurement unit.
*/
static std::pair<Aws::String, Aws::String> ConvertCoreMetricToSmithy(const Aws::String &name) {
switch (Aws::Monitoring::GetHttpClientMetricTypeByName(name))
{
case Aws::Monitoring::HttpClientMetricsType::DnsLatency:
return std::make_pair(SMITHY_METRICS_DNS_DURATION, MICROSECOND_METRIC_TYPE);
case Aws::Monitoring::HttpClientMetricsType::ConnectLatency:
return std::make_pair(SMITHY_METRICS_CONNECT_DURATION, MICROSECOND_METRIC_TYPE);
case Aws::Monitoring::HttpClientMetricsType::SslLatency:
return std::make_pair(SMITHY_METRICS_SSL_DURATION, MICROSECOND_METRIC_TYPE);
case Aws::Monitoring::HttpClientMetricsType::DownloadSpeed:
return std::make_pair(SMITHY_METRICS_DOWNLOAD_SPEED_METRIC, BYTES_PER_SECOND_METRIC_TYPE);
case Aws::Monitoring::HttpClientMetricsType::UploadSpeed:
return std::make_pair(SMITHY_METRICS_UPLOAD_SPEED_METRIC, BYTES_PER_SECOND_METRIC_TYPE);
default:
return std::make_pair(SMITHY_METRICS_UNKNOWN_METRIC, "unknown");
}
}
};
}
}
}