| 1 | // Copyright The OpenTelemetry Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #ifndef OPENTELEMETRY_STL_VERSION |
| 5 | // Unfortunately as of 04/27/2021 the fix is NOT in the vcpkg snapshot of Google Test. |
| 6 | // Remove above `#ifdef` once the GMock fix for C++20 is in the mainline. |
| 7 | // |
| 8 | // Please refer to this GitHub issue for additional details: |
| 9 | // https://github.com/google/googletest/issues/2914 |
| 10 | // https://github.com/google/googletest/commit/61f010d703b32de9bfb20ab90ece38ab2f25977f |
| 11 | // |
| 12 | // If we compile using Visual Studio 2019 with `c++latest` (C++20) without the GMock fix, |
| 13 | // then the compilation here fails in `gmock-actions.h` from: |
| 14 | // .\tools\vcpkg\installed\x64-windows\include\gmock\gmock-actions.h(819): |
| 15 | // error C2653: 'result_of': is not a class or namespace name |
| 16 | // |
| 17 | // That is because `std::result_of` has been removed in C++20. |
| 18 | |
| 19 | # include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h" |
| 20 | |
| 21 | # include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" |
| 22 | |
| 23 | // Problematic code that pulls in Gmock and breaks with vs2019/c++latest : |
| 24 | # include "opentelemetry/proto/collector/metrics/v1/metrics_service_mock.grpc.pb.h" |
| 25 | |
| 26 | # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" |
| 27 | |
| 28 | # include "opentelemetry/exporters/otlp/otlp_grpc_client.h" |
| 29 | # include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" |
| 30 | |
| 31 | # include "opentelemetry/sdk/trace/simple_processor.h" |
| 32 | # include "opentelemetry/sdk/trace/tracer_provider.h" |
| 33 | # include "opentelemetry/trace/provider.h" |
| 34 | |
| 35 | # include <grpcpp/grpcpp.h> |
| 36 | # include <gtest/gtest.h> |
| 37 | |
| 38 | # if defined(_MSC_VER) |
| 39 | # include "opentelemetry/sdk/common/env_variables.h" |
| 40 | using opentelemetry::sdk::common::setenv; |
| 41 | using opentelemetry::sdk::common::unsetenv; |
| 42 | # endif |
| 43 | |
| 44 | using namespace testing; |
| 45 | |
| 46 | OPENTELEMETRY_BEGIN_NAMESPACE |
| 47 | namespace exporter |
| 48 | { |
| 49 | namespace otlp |
| 50 | { |
| 51 | |
| 52 | class OtlpGrpcMetricExporterTestPeer : public ::testing::Test |
| 53 | { |
| 54 | public: |
| 55 | std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter( |
| 56 | const OtlpGrpcMetricExporterOptions &options) |
| 57 | { |
| 58 | return std::unique_ptr<sdk::metrics::PushMetricExporter>(new OtlpGrpcMetricExporter(options)); |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter( |
| 62 | std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub_interface) |
| 63 | { |
| 64 | return std::unique_ptr<sdk::metrics::PushMetricExporter>( |
| 65 | new OtlpGrpcMetricExporter(std::move(stub_interface))); |
| 66 | } |
| 67 | |
| 68 | std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter( |
| 69 | std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub_interface, |
| 70 | std::shared_ptr<OtlpGrpcClient> client) |
| 71 | { |
| 72 | return std::unique_ptr<sdk::metrics::PushMetricExporter>( |
| 73 | new OtlpGrpcMetricExporter(std::move(stub_interface), std::move(client))); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter( |
| 77 | const OtlpGrpcMetricExporterOptions &options, |
| 78 | std::shared_ptr<OtlpGrpcClient> client) |
| 79 | { |
| 80 | return std::unique_ptr<sdk::metrics::PushMetricExporter>( |
| 81 | new OtlpGrpcMetricExporter(options, std::move(client))); |
| 82 | } |
| 83 | |
| 84 | // Get the options associated with the given exporter. |
| 85 | const OtlpGrpcMetricExporterOptions &GetOptions(std::unique_ptr<OtlpGrpcMetricExporter> &exporter) |
| 86 | { |
| 87 | return exporter->options_; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // Test exporter configuration options |
| 92 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigTest) |
| 93 | { |
| 94 | OtlpGrpcMetricExporterOptions opts; |
| 95 | opts.endpoint = "localhost:45454"; |
| 96 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter(opts)); |
| 97 | EXPECT_EQ(GetOptions(exporter).endpoint, "localhost:45454"); |
| 98 | } |
| 99 | |
| 100 | // Test exporter configuration options with use_ssl_credentials |
| 101 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigSslCredentialsTest) |
| 102 | { |
| 103 | std::string cacert_str = "--begin and end fake cert--"; |
| 104 | OtlpGrpcMetricExporterOptions opts; |
| 105 | opts.use_ssl_credentials = true; |
| 106 | opts.ssl_credentials_cacert_as_string = cacert_str; |
| 107 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter(opts)); |
| 108 | EXPECT_EQ(GetOptions(exporter).ssl_credentials_cacert_as_string, cacert_str); |
| 109 | EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, true); |
| 110 | } |
| 111 | |
| 112 | # ifndef NO_GETENV |
| 113 | // Test exporter configuration options with use_ssl_credentials |
| 114 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigFromEnv) |
| 115 | { |
| 116 | const std::string cacert_str = "--begin and end fake cert--"; |
| 117 | setenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING", cacert_str.c_str(), 1); |
| 118 | setenv("OTEL_EXPORTER_OTLP_SSL_ENABLE", "True", 1); |
| 119 | const std::string endpoint = "https://localhost:9999"; |
| 120 | setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint.c_str(), 1); |
| 121 | setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20050ms", 1); |
| 122 | setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1); |
| 123 | setenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS", "k1=v3,k1=v4", 1); |
| 124 | |
| 125 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 126 | EXPECT_EQ(GetOptions(exporter).ssl_credentials_cacert_as_string, cacert_str); |
| 127 | EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, true); |
| 128 | EXPECT_EQ(GetOptions(exporter).endpoint, endpoint); |
| 129 | EXPECT_EQ(GetOptions(exporter).timeout.count(), |
| 130 | std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 131 | std::chrono::milliseconds{20050}) |
| 132 | .count()); |
| 133 | EXPECT_EQ(GetOptions(exporter).metadata.size(), 3); |
| 134 | { |
| 135 | // Test k2 |
| 136 | auto range = GetOptions(exporter).metadata.equal_range("k2"); |
| 137 | EXPECT_TRUE(range.first != range.second); |
| 138 | EXPECT_EQ(range.first->second, std::string("v2")); |
| 139 | ++range.first; |
| 140 | EXPECT_TRUE(range.first == range.second); |
| 141 | } |
| 142 | { |
| 143 | // Test k1 |
| 144 | auto range = GetOptions(exporter).metadata.equal_range("k1"); |
| 145 | EXPECT_TRUE(range.first != range.second); |
| 146 | EXPECT_EQ(range.first->second, std::string("v3")); |
| 147 | ++range.first; |
| 148 | EXPECT_EQ(range.first->second, std::string("v4")); |
| 149 | ++range.first; |
| 150 | EXPECT_TRUE(range.first == range.second); |
| 151 | } |
| 152 | |
| 153 | unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); |
| 154 | unsetenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"); |
| 155 | unsetenv("OTEL_EXPORTER_OTLP_SSL_ENABLE"); |
| 156 | unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT"); |
| 157 | unsetenv("OTEL_EXPORTER_OTLP_HEADERS"); |
| 158 | unsetenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS"); |
| 159 | } |
| 160 | |
| 161 | // Test exporter configuration options with use_ssl_credentials |
| 162 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigHttpsSecureFromEnv) |
| 163 | { |
| 164 | // https takes precedence over insecure |
| 165 | const std::string endpoint = "https://localhost:9999"; |
| 166 | setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint.c_str(), 1); |
| 167 | setenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE", "true", 1); |
| 168 | |
| 169 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 170 | EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, true); |
| 171 | EXPECT_EQ(GetOptions(exporter).endpoint, endpoint); |
| 172 | |
| 173 | unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); |
| 174 | unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); |
| 175 | } |
| 176 | |
| 177 | // Test exporter configuration options with use_ssl_credentials |
| 178 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigHttpInsecureFromEnv) |
| 179 | { |
| 180 | // http takes precedence over secure |
| 181 | const std::string endpoint = "http://localhost:9999"; |
| 182 | setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint.c_str(), 1); |
| 183 | setenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE", "false", 1); |
| 184 | |
| 185 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 186 | EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, false); |
| 187 | EXPECT_EQ(GetOptions(exporter).endpoint, endpoint); |
| 188 | |
| 189 | unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); |
| 190 | unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); |
| 191 | } |
| 192 | |
| 193 | // Test exporter configuration options with use_ssl_credentials |
| 194 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownSecureFromEnv) |
| 195 | { |
| 196 | const std::string endpoint = "localhost:9999"; |
| 197 | setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint.c_str(), 1); |
| 198 | setenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE", "false", 1); |
| 199 | |
| 200 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 201 | EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, true); |
| 202 | EXPECT_EQ(GetOptions(exporter).endpoint, endpoint); |
| 203 | |
| 204 | unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); |
| 205 | unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); |
| 206 | } |
| 207 | |
| 208 | // Test exporter configuration options with use_ssl_credentials |
| 209 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownInsecureFromEnv) |
| 210 | { |
| 211 | const std::string endpoint = "localhost:9999"; |
| 212 | setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint.c_str(), 1); |
| 213 | setenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE", "true", 1); |
| 214 | |
| 215 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 216 | EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, false); |
| 217 | EXPECT_EQ(GetOptions(exporter).endpoint, endpoint); |
| 218 | |
| 219 | unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); |
| 220 | unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); |
| 221 | } |
| 222 | |
| 223 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryDefaultValues) |
| 224 | { |
| 225 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 226 | const auto options = GetOptions(exporter); |
| 227 | ASSERT_EQ(options.retry_policy_max_attempts, 5); |
| 228 | ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1.0f); |
| 229 | ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5.0f); |
| 230 | ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5f); |
| 231 | } |
| 232 | |
| 233 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryValuesFromEnv) |
| 234 | { |
| 235 | setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_ATTEMPTS", "123", 1); |
| 236 | setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_INITIAL_BACKOFF", "4.5", 1); |
| 237 | setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_BACKOFF", "6.7", 1); |
| 238 | setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_BACKOFF_MULTIPLIER", "8.9", 1); |
| 239 | |
| 240 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 241 | const auto options = GetOptions(exporter); |
| 242 | ASSERT_EQ(options.retry_policy_max_attempts, 123); |
| 243 | ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5f); |
| 244 | ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7f); |
| 245 | ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9f); |
| 246 | |
| 247 | unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_ATTEMPTS"); |
| 248 | unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_INITIAL_BACKOFF"); |
| 249 | unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_BACKOFF"); |
| 250 | unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_BACKOFF_MULTIPLIER"); |
| 251 | } |
| 252 | |
| 253 | TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryGenericValuesFromEnv) |
| 254 | { |
| 255 | setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1); |
| 256 | setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1); |
| 257 | setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1); |
| 258 | setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1); |
| 259 | |
| 260 | std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter()); |
| 261 | const auto options = GetOptions(exporter); |
| 262 | ASSERT_EQ(options.retry_policy_max_attempts, 321); |
| 263 | ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4f); |
| 264 | ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6f); |
| 265 | ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8f); |
| 266 | |
| 267 | unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS"); |
| 268 | unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF"); |
| 269 | unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF"); |
| 270 | unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER"); |
| 271 | } |
| 272 | # endif // NO_GETENV |
| 273 | |
| 274 | TEST_F(OtlpGrpcMetricExporterTestPeer, CheckGetAggregationTemporality) |
| 275 | { |
| 276 | auto options = OtlpGrpcMetricExporterOptions(); |
| 277 | options.aggregation_temporality = PreferredAggregationTemporality::kCumulative; |
| 278 | |
| 279 | auto client = OtlpGrpcClientFactory::Create(options); |
| 280 | |
| 281 | auto exporter0 = GetExporter(options); |
| 282 | auto exporter1 = GetExporter(client->MakeMetricsServiceStub()); |
| 283 | auto exporter2 = GetExporter(options, client); |
| 284 | auto exporter3 = GetExporter(client->MakeMetricsServiceStub(), client); |
| 285 | |
| 286 | EXPECT_EQ( |
| 287 | opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, |
| 288 | exporter0->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); |
| 289 | |
| 290 | EXPECT_EQ( |
| 291 | opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, |
| 292 | exporter1->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); |
| 293 | |
| 294 | EXPECT_EQ( |
| 295 | opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, |
| 296 | exporter2->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); |
| 297 | |
| 298 | EXPECT_EQ( |
| 299 | opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, |
| 300 | exporter3->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); |
| 301 | } |
| 302 | |
| 303 | } // namespace otlp |
| 304 | } // namespace exporter |
| 305 | OPENTELEMETRY_END_NAMESPACE |
| 306 | #endif /* OPENTELEMETRY_STL_VERSION */ |