Repository: envoyproxy/envoy
File: source/server/admin/server_info_handler.cc (C++)

Code before:
Http::Code ServerInfoHandler::handlerHotRestartVersion(Http::ResponseHeaderMap&,
                                                       Buffer::Instance& response, AdminStream&) {
  response.add(server_.hotRestart().version());
  return Http::Code::OK;
}

// TODO(ambuc): Add more tcmalloc stats, export proto details based on allocator.
Http::Code ServerInfoHandler::handlerMemory(Http::ResponseHeaderMap& response_headers,
                                            Buffer::Instance& response, AdminStream&) {
  response_headers.setReferenceContentType(Http::Headers::get().ContentTypeValues.Json);
  envoy::admin::v3::Memory memory;
  memory.set_allocated(Memory::Stats::totalCurrentlyAllocated());
  memory.set_heap_size(Memory::Stats::totalCurrentlyReserved());
  memory.set_total_thread_cache(Memory::Stats::totalThreadCacheBytes());
  memory.set_pageheap_unmapped(Memory::Stats::totalPageHeapUnmapped());
  memory.set_pageheap_free(Memory::Stats::totalPageHeapFree());
  memory.set_total_physical_bytes(Memory::Stats::totalPhysicalBytes());
  response.add(MessageUtil::getJsonStringFromMessageOrError(memory, true, true)); // pretty-print
  return Http::Code::OK;
}

Http::Code ServerInfoHandler::handleMemoryTcmallocStats(Http::ResponseHeaderMap& response_headers,
                                                        Buffer::Instance& response, AdminStream&) {
  response_headers.setReferenceContentType(Http::Headers::get().ContentTypeValues.Text);
  response.add(Memory::Stats::dumpStats());
#if defined(TCMALLOC) || defined(GPERFTOOLS_TCMALLOC)
  return Http::Code::OK;
#else
  return Http::Code::NotImplemented;
#endif
}

Http::Code ServerInfoHandler::handlerReady(Http::ResponseHeaderMap&, Buffer::Instance& response,
                                           AdminStream&) {
  const envoy::admin::v3::ServerInfo::State state =
      Utility::serverState(server_.initManager().state(), server_.healthCheckFailed());

  response.add(envoy::admin::v3::ServerInfo::State_Name(state) + "\n");
  Http::Code code =
      state == envoy::admin::v3::ServerInfo::LIVE ? Http::Code::OK : Http::Code::ServiceUnavailable;
  return code;
}

Http::Code ServerInfoHandler::handlerServerInfo(Http::ResponseHeaderMap& headers,
                                                Buffer::Instance& response, AdminStream&) {
  const std::time_t current_time =
      std::chrono::system_clock::to_time_t(server_.timeSource().systemTime());
  const std::time_t uptime_current_epoch = current_time - server_.startTimeCurrentEpoch();
  const std::time_t uptime_all_epochs = current_time - server_.startTimeFirstEpoch();

  ASSERT(uptime_current_epoch >= 0);

Review comment (suggestion):
This will work fine, but it would be cleaner if you make `Stats::dumpStats()` return `absl::optional<std::string>` and make it return a nullopt when not compiled with tcmalloc; then you don't need a preprocessor check here, you can just check for a value in the optional.

Code after:
Http::Code ServerInfoHandler::handlerHotRestartVersion(Http::ResponseHeaderMap&,
                                                       Buffer::Instance& response, AdminStream&) {
  response.add(server_.hotRestart().version());
  return Http::Code::OK;
}

// TODO(ambuc): Add more tcmalloc stats, export proto details based on allocator.
Http::Code ServerInfoHandler::handlerMemory(Http::ResponseHeaderMap& response_headers,
                                            Buffer::Instance& response, AdminStream&) {
  response_headers.setReferenceContentType(Http::Headers::get().ContentTypeValues.Json);
  envoy::admin::v3::Memory memory;
  memory.set_allocated(Memory::Stats::totalCurrentlyAllocated());
  memory.set_heap_size(Memory::Stats::totalCurrentlyReserved());
  memory.set_total_thread_cache(Memory::Stats::totalThreadCacheBytes());
  memory.set_pageheap_unmapped(Memory::Stats::totalPageHeapUnmapped());
  memory.set_pageheap_free(Memory::Stats::totalPageHeapFree());
  memory.set_total_physical_bytes(Memory::Stats::totalPhysicalBytes());
  response.add(MessageUtil::getJsonStringFromMessageOrError(memory, true, true)); // pretty-print
  return Http::Code::OK;
}

Http::Code ServerInfoHandler::handleMemoryTcmallocStats(Http::ResponseHeaderMap& response_headers,
                                                        Buffer::Instance& response, AdminStream&) {
  response_headers.setReferenceContentType(Http::Headers::get().ContentTypeValues.Text);
  auto stats = Memory::Stats::dumpStats();

  if (stats.has_value()) {
    response.add(stats.value());
    return Http::Code::OK;
  }

  response.add("Envoy was not built with tcmalloc.\n");
  return Http::Code::NotImplemented;
}

Http::Code ServerInfoHandler::handlerReady(Http::ResponseHeaderMap&, Buffer::Instance& response,
                                           AdminStream&) {
  const envoy::admin::v3::ServerInfo::State state =
      Utility::serverState(server_.initManager().state(), server_.healthCheckFailed());

  response.add(envoy::admin::v3::ServerInfo::State_Name(state) + "\n");
  Http::Code code =
      state == envoy::admin::v3::ServerInfo::LIVE ? Http::Code::OK : Http::Code::ServiceUnavailable;
  return code;
}

Http::Code ServerInfoHandler::handlerServerInfo(Http::ResponseHeaderMap& headers,
                                                Buffer::Instance& response, AdminStream&) {
  const std::time_t current_time =
      std::chrono::system_clock::to_time_t(server_.timeSource().systemTime());
  const std::time_t uptime_current_epoch = current_time - server_.startTimeCurrentEpoch();