Reland of Trace the stats report as JSON instead of each stat separately. (patchset #1 id:1 of https://codereview.webrtc.org/3001683002/ )

Reason for revert:
Fixed

Original issue's description:
> Revert of Trace the stats report as JSON instead of each stat separately. (patchset #3 id:100001 of https://codereview.webrtc.org/2986453002/ )
>
> Reason for revert:
> It breaks a downstream project.
>
> Original issue's description:
> > Trace the stats report as JSON instead of each stat separately.
> >
> > Trace the whole report as a string instead of each field on it's own. And test that the traces collected are valid.
> >
> > R=tommi@webrtc.org, hbos@webrtc.org
> > BUG=chromium:653087
> >
> > Review-Url: https://codereview.webrtc.org/2986453002
> > Cr-Commit-Position: refs/heads/master@{#19341}
> > Committed: https://chromium.googlesource.com/external/webrtc/+/80c65780e618f90369295fe5daf8075e9b374c26
>
> TBR=hbos@webrtc.org,tommi@webrtc.org,ehmaldonado@webrtc.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=chromium:653087
>
> Review-Url: https://codereview.webrtc.org/3001683002
> Cr-Commit-Position: refs/heads/master@{#19344}
> Committed: https://chromium.googlesource.com/external/webrtc/+/3439c893585e0958502ef397056a372be1777688

BUG=chromium:653087

Review-Url: https://codereview.webrtc.org/3000943002
Cr-Original-Commit-Position: refs/heads/master@{#19673}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: 8ab0fd80ad4af302d8f72e94e577458084772288
diff --git a/pc/rtcstats_integrationtest.cc b/pc/rtcstats_integrationtest.cc
index b886bcc..7967633 100644
--- a/pc/rtcstats_integrationtest.cc
+++ b/pc/rtcstats_integrationtest.cc
@@ -24,6 +24,7 @@
 #include "webrtc/rtc_base/gunit.h"
 #include "webrtc/rtc_base/refcountedobject.h"
 #include "webrtc/rtc_base/scoped_ref_ptr.h"
+#include "webrtc/rtc_base/trace_event.h"
 #include "webrtc/rtc_base/virtualsocketserver.h"
 
 namespace webrtc {
@@ -33,27 +34,61 @@
 const int64_t kGetStatsTimeoutMs = 10000;
 
 const unsigned char* GetCategoryEnabledHandler(const char* name) {
-  return reinterpret_cast<const unsigned char*>("webrtc_stats");
+  if (strcmp("webrtc_stats", name) != 0) {
+    return reinterpret_cast<const unsigned char*>("");
+  }
+  return reinterpret_cast<const unsigned char*>(name);
 }
 
-void AddTraceEventHandler(char phase,
-                          const unsigned char* category_enabled,
-                          const char* name,
-                          unsigned long long id,
-                          int num_args,
-                          const char** arg_names,
-                          const unsigned char* arg_types,
-                          const unsigned long long* arg_values,
-                          unsigned char flags) {
-  // Do nothing
-}
+class RTCStatsReportTraceListener {
+ public:
+  static void SetUp() {
+    if (!traced_report_)
+      traced_report_ = new RTCStatsReportTraceListener();
+    traced_report_->last_trace_ = "";
+    SetupEventTracer(&GetCategoryEnabledHandler,
+                     &RTCStatsReportTraceListener::AddTraceEventHandler);
+  }
+
+  static const std::string& last_trace() {
+    RTC_DCHECK(traced_report_);
+    return traced_report_->last_trace_;
+  }
+
+ private:
+  static void AddTraceEventHandler(char phase,
+                                   const unsigned char* category_enabled,
+                                   const char* name,
+                                   unsigned long long id,
+                                   int num_args,
+                                   const char** arg_names,
+                                   const unsigned char* arg_types,
+                                   const unsigned long long* arg_values,
+                                   unsigned char flags) {
+    RTC_DCHECK(traced_report_);
+    EXPECT_STREQ("webrtc_stats",
+                 reinterpret_cast<const char*>(category_enabled));
+    EXPECT_STREQ("webrtc_stats", name);
+    EXPECT_EQ(1, num_args);
+    EXPECT_STREQ("report", arg_names[0]);
+    EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, arg_types[0]);
+
+    traced_report_->last_trace_ = reinterpret_cast<const char*>(arg_values[0]);
+  }
+
+  static RTCStatsReportTraceListener* traced_report_;
+  std::string last_trace_;
+};
+
+RTCStatsReportTraceListener* RTCStatsReportTraceListener::traced_report_ =
+    nullptr;
 
 class RTCStatsIntegrationTest : public testing::Test {
  public:
   RTCStatsIntegrationTest()
       : network_thread_(new rtc::Thread(&virtual_socket_server_)),
         worker_thread_(rtc::Thread::Create()) {
-    SetupEventTracer(&GetCategoryEnabledHandler, &AddTraceEventHandler);
+    RTCStatsReportTraceListener::SetUp();
 
     RTC_CHECK(network_thread_->Start());
     RTC_CHECK(worker_thread_->Start());
@@ -662,6 +697,7 @@
 
   rtc::scoped_refptr<const RTCStatsReport> report = GetStatsFromCaller();
   RTCStatsReportVerifier(report.get()).VerifyReport();
+  EXPECT_EQ(report->ToJson(), RTCStatsReportTraceListener::last_trace());
 }
 
 TEST_F(RTCStatsIntegrationTest, GetStatsFromCallee) {
@@ -669,6 +705,7 @@
 
   rtc::scoped_refptr<const RTCStatsReport> report = GetStatsFromCallee();
   RTCStatsReportVerifier(report.get()).VerifyReport();
+  EXPECT_EQ(report->ToJson(), RTCStatsReportTraceListener::last_trace());
 }
 
 TEST_F(RTCStatsIntegrationTest, GetsStatsWhileDestroyingPeerConnections) {
@@ -682,6 +719,8 @@
   // Any pending stats requests should have completed in the act of destroying
   // the peer connection.
   EXPECT_TRUE(stats_obtainer->report());
+  EXPECT_EQ(stats_obtainer->report()->ToJson(),
+            RTCStatsReportTraceListener::last_trace());
 }
 #endif  // HAVE_SCTP
 
diff --git a/pc/rtcstatscollector.cc b/pc/rtcstatscollector.cc
index 5c506e4..5b84ea5 100644
--- a/pc/rtcstatscollector.cc
+++ b/pc/rtcstatscollector.cc
@@ -32,18 +32,6 @@
 
 namespace {
 
-const int kStatTypeMemberNameAndIdMaxLen = 1024;
-
-std::string GetStatTypeMemberNameAndId(const RTCStats& stats,
-                                       const RTCStatsMemberInterface* member) {
-  RTC_DCHECK(strlen(stats.type()) + strlen(member->name())
-             + stats.id().size() + 3 < kStatTypeMemberNameAndIdMaxLen);
-  char buffer[kStatTypeMemberNameAndIdMaxLen];
-  rtc::sprintfn(&buffer[0], sizeof(buffer), "%s.%s.%s", stats.type(),
-                member->name(), stats.id().c_str());
-  return buffer;
-}
-
 std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
   return "RTCCertificate_" + fingerprint;
 }
@@ -778,16 +766,8 @@
     // Trace WebRTC Stats when getStats is called on Javascript.
     // This allows access to WebRTC stats from trace logs. To enable them,
     // select the "webrtc_stats" category when recording traces.
-    for (const RTCStats& stats : *cached_report_) {
-      for (const RTCStatsMemberInterface* member : stats.Members()) {
-        if (member->is_defined()) {
-          TRACE_EVENT_INSTANT2("webrtc_stats", "webrtc_stats",
-                               "value", member->ValueToString(),
-                               "type.name.id", GetStatTypeMemberNameAndId(
-                                   stats, member));
-        }
-      }
-    }
+    TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
+                         cached_report_->ToJson());
     DeliverCachedReport();
   }
 }