Skip to content

Add Levenshtein Distance Check for 'prometheus' Typo in Monitoring Config File #4102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions tensorflow_serving/model_servers/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ void Server::PollFilesystemAndReloadConfig(const string& config_file_path) {
}
}

int LevenshteinDistance(const std::string& s1, const std::string& s2) {
const size_t len1 = s1.size(), len2 = s2.size();
std::vector<std::vector<size_t>> d(len1 + 1, std::vector<size_t>(len2 + 1));

for (size_t i = 0; i <= len1; ++i) d[i][0] = i;
for (size_t j = 0; j <= len2; ++j) d[0][j] = j;

for (size_t i = 1; i <= len1; ++i) {
for (size_t j = 1; j <= len2; ++j) {
d[i][j] = std::min({ d[i - 1][j] + 1,
d[i][j - 1] + 1,
d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1) });
}
}

return static_cast<int>(d[len1][len2]);
}

absl::Status Server::BuildAndStart(const Options& server_options) {
if (server_options.grpc_port == 0 &&
server_options.grpc_socket_path.empty()) {
Expand Down Expand Up @@ -437,10 +455,23 @@ absl::Status Server::BuildAndStart(const Options& server_options) {
const string server_address =
"localhost:" + std::to_string(server_options.http_port);
MonitoringConfig monitoring_config;

if (!server_options.monitoring_config_file.empty()) {
const std::string config_file = server_options.monitoring_config_file;
// Check if the config file name is a typo for "prometheus".
// Levenshtein distance <= 2 is considered a typo.
const std::string target = "prometheus";
int distance = LevenshteinDistance(config_file, target);
if (distance <= 2) {
LOG(ERROR) << absl::StrFormat(
"Did you mean 'prometheus'? The provided monitoring config file '%s' "
"seems like a typo (Levenshtein distance = %d).", config_file, distance);
}

TF_RETURN_IF_ERROR(ParseProtoTextFile<MonitoringConfig>(
server_options.monitoring_config_file, &monitoring_config));
config_file, &monitoring_config));
}

http_server_ = CreateAndStartHttpServer(
server_options.http_port, server_options.http_num_threads,
server_options.http_timeout_in_ms, monitoring_config,
Expand All @@ -457,7 +488,7 @@ absl::Status Server::BuildAndStart(const Options& server_options) {
}
}
return absl::OkStatus();
}
}

void Server::WaitForTermination() {
if (http_server_ != nullptr) {
Expand Down