@@ -103,6 +103,9 @@ void TracerTelemetry::capture_metrics() {
103103 .count ();
104104 for (auto & m : metrics_snapshots_) {
105105 auto value = m.first .get ().capture_and_reset_value ();
106+ if (value == 0 ) {
107+ continue ;
108+ }
106109 m.second .emplace_back (timepoint, value);
107110 }
108111}
@@ -111,35 +114,40 @@ std::string TracerTelemetry::heartbeat_and_telemetry() {
111114 time_t tracer_time = std::chrono::duration_cast<std::chrono::seconds>(
112115 clock_ ().wall .time_since_epoch ())
113116 .count ();
117+ auto batch_payloads = nlohmann::json::array ();
118+
114119 auto heartbeat = nlohmann::json::object ({
115120 {" request_type" , " app-heartbeat" },
116121 });
122+ batch_payloads.emplace_back (std::move (heartbeat));
117123
118124 auto metrics = nlohmann::json::array ();
119125 for (auto & m : metrics_snapshots_) {
120126 auto & metric = m.first .get ();
121127 auto & points = m.second ;
122- if (points.empty ()) {
123- continue ;
128+ if (!points.empty ()) {
129+ metrics.emplace_back (nlohmann::json::object ({
130+ {" metric" , metric.name ()},
131+ {" tags" , metric.tags ()},
132+ {" type" , metric.type ()},
133+ {" interval" , 60 },
134+ {" points" , points},
135+ {" common" , metric.common ()},
136+ }));
124137 }
125-
126- metrics.emplace_back (nlohmann::json::object ({
127- {" metric" , metric.name ()},
128- {" tags" , metric.tags ()},
129- {" type" , metric.type ()},
130- {" interval" , 60 },
131- {" points" , points},
132- {" common" , metric.common ()},
133- }));
138+ points.clear ();
134139 }
135140
136- auto generate_metrics = nlohmann::json::object ({
137- {" request_type" , " generate-metrics" },
138- {" payload" , nlohmann::json::object ({
139- {" namespace" , " tracers" },
140- {" series" , metrics},
141- })},
142- });
141+ if (!metrics.empty ()) {
142+ auto generate_metrics = nlohmann::json::object ({
143+ {" request_type" , " generate-metrics" },
144+ {" payload" , nlohmann::json::object ({
145+ {" namespace" , " tracers" },
146+ {" series" , metrics},
147+ })},
148+ });
149+ batch_payloads.emplace_back (std::move (generate_metrics));
150+ }
143151
144152 seq_id++;
145153 auto payload =
@@ -163,10 +171,7 @@ std::string TracerTelemetry::heartbeat_and_telemetry() {
163171 {" host" , nlohmann::json::object ({
164172 {" hostname" , hostname_},
165173 })},
166- {" payload" , nlohmann::json::array ({
167- heartbeat,
168- generate_metrics,
169- })},
174+ {" payload" , batch_payloads},
170175 })
171176 .dump ();
172177 return payload;
0 commit comments