|
17 | 17 | #include "cuttlefish/host/libs/metrics/metrics_transmitter.h" |
18 | 18 |
|
19 | 19 | #include "cuttlefish/common/libs/utils/result.h" |
20 | | -#include "cuttlefish/host/commands/metrics/send.h" |
21 | | -#include "cuttlefish/host/libs/metrics/metrics_defs.h" |
| 20 | +#include "cuttlefish/host/libs/web/http_client/curl_global_init.h" |
| 21 | +#include "cuttlefish/host/libs/web/http_client/curl_http_client.h" |
| 22 | +#include "cuttlefish/host/libs/web/http_client/http_client.h" |
| 23 | +#include "cuttlefish/host/libs/web/http_client/http_string.h" |
22 | 24 | #include "external_proto/clientanalytics.pb.h" |
23 | 25 |
|
24 | 26 | namespace cuttlefish { |
| 27 | +namespace { |
| 28 | + |
| 29 | +// TODO: chadreynolds - create a compilation or runtime flag to swap |
| 30 | +// environments |
| 31 | +enum class ClearcutEnvironment { |
| 32 | + kLocal = 0, |
| 33 | + kStaging = 1, |
| 34 | + kProd = 2, |
| 35 | +}; |
| 36 | + |
| 37 | +std::string ClearcutEnvironmentUrl(const ClearcutEnvironment environment) { |
| 38 | + switch (environment) { |
| 39 | + case ClearcutEnvironment::kLocal: |
| 40 | + return "http://localhost:27910/log"; |
| 41 | + case ClearcutEnvironment::kStaging: |
| 42 | + return "https://play.googleapis.com:443/staging/log"; |
| 43 | + case ClearcutEnvironment::kProd: |
| 44 | + return "https://play.googleapis.com:443/log"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +Result<void> PostRequest(HttpClient& http_client, const std::string& output, |
| 49 | + const ClearcutEnvironment server) { |
| 50 | + const std::string clearcut_url = ClearcutEnvironmentUrl(server); |
| 51 | + HttpResponse<std::string> response = |
| 52 | + CF_EXPECT(HttpPostToString(http_client, clearcut_url, output)); |
| 53 | + CF_EXPECTF(response.HttpSuccess(), "Metrics POST failed ({}): {}", |
| 54 | + response.http_code, response.data); |
| 55 | + return {}; |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace |
25 | 59 |
|
26 | 60 | Result<void> TransmitMetricsEvent( |
27 | 61 | const wireless_android_play_playlog::LogRequest& log_request) { |
28 | | - int reporting_outcome = metrics::PostRequest(log_request.SerializeAsString(), |
29 | | - metrics::ClearcutServer::kProd); |
30 | | - CF_EXPECTF(reporting_outcome != MetricsExitCodes::kSuccess, |
31 | | - "Issue reporting metrics: {}", reporting_outcome); |
| 62 | + CurlGlobalInit curl_global_init; |
| 63 | + std::unique_ptr<HttpClient> http_client = CurlHttpClient(); |
| 64 | + CF_EXPECT(http_client.get() != nullptr, |
| 65 | + "Unable to create cURL client for metrics transmission"); |
| 66 | + CF_EXPECT(PostRequest(*http_client, log_request.SerializeAsString(), |
| 67 | + ClearcutEnvironment::kProd)); |
32 | 68 | return {}; |
33 | 69 | } |
34 | 70 |
|
|
0 commit comments