| 1053 | |
| 1054 | |
| 1055 | Future<Response> Http::getMetrics( |
| 1056 | const mesos::agent::Call& call, |
| 1057 | ContentType contentType, |
| 1058 | const Option<Principal>& principal) const |
| 1059 | { |
| 1060 | CHECK_EQ(mesos::agent::Call::GET_METRICS, call.type()); |
| 1061 | CHECK(call.has_get_metrics()); |
| 1062 | |
| 1063 | LOG(INFO) << "Processing GET_METRICS call"; |
| 1064 | |
| 1065 | Option<Duration> timeout; |
| 1066 | if (call.get_metrics().has_timeout()) { |
| 1067 | timeout = Nanoseconds(call.get_metrics().timeout().nanoseconds()); |
| 1068 | } |
| 1069 | |
| 1070 | return process::metrics::snapshot(timeout) |
| 1071 | .then([contentType](const map<string, double>& metrics) -> Response { |
| 1072 | // Serialize the following message: |
| 1073 | // |
| 1074 | // v1::agent::Response response; |
| 1075 | // response.set_type(v1::agent::Response::GET_METRICS); |
| 1076 | // v1::agent::Response::GetMetrics* getMetrics = ...; |
| 1077 | |
| 1078 | switch (contentType) { |
| 1079 | case ContentType::PROTOBUF: { |
| 1080 | string output; |
| 1081 | google::protobuf::io::StringOutputStream stream(&output); |
| 1082 | google::protobuf::io::CodedOutputStream writer(&stream); |
| 1083 | |
| 1084 | WireFormatLite::WriteEnum( |
| 1085 | v1::agent::Response::kTypeFieldNumber, |
| 1086 | v1::agent::Response::GET_METRICS, |
| 1087 | &writer); |
| 1088 | |
| 1089 | WireFormatLite::WriteBytes( |
| 1090 | v1::agent::Response::kGetMetricsFieldNumber, |
| 1091 | serializeGetMetrics<v1::agent::Response::GetMetrics>(metrics), |
| 1092 | &writer); |
| 1093 | |
| 1094 | // We must manually trim the unused buffer space since |
| 1095 | // we use the string before the coded output stream is |
| 1096 | // destructed. |
| 1097 | writer.Trim(); |
| 1098 | |
| 1099 | return OK(std::move(output), stringify(contentType)); |
| 1100 | } |
| 1101 | |
| 1102 | case ContentType::JSON: { |
| 1103 | string body = jsonify([&](JSON::ObjectWriter* writer) { |
| 1104 | const google::protobuf::Descriptor* descriptor = |
| 1105 | v1::agent::Response::descriptor(); |
| 1106 | |
| 1107 | int field; |
| 1108 | |
| 1109 | field = v1::agent::Response::kTypeFieldNumber; |
| 1110 | writer->field( |
| 1111 | descriptor->FindFieldByNumber(field)->name(), |
| 1112 | v1::agent::Response::Type_Name( |