TensorFlow Serving C++ API Documentation
machine_learning_metadata.cc
1 /* Copyright 2020 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_serving/servables/tensorflow/machine_learning_metadata.h"
17 
18 #include "tensorflow/cc/saved_model/constants.h"
19 #include "tensorflow/core/lib/io/path.h"
20 #include "tensorflow/core/lib/monitoring/gauge.h"
21 #include "tensorflow/core/lib/strings/strcat.h"
22 
23 namespace tensorflow {
24 namespace serving {
25 
26 namespace {
27 
28 static constexpr char kMLMDKeyFile[] = "mlmd_uuid";
29 
30 auto* mlmd_map = monitoring::Gauge<string, 2>::New(
31  "/tensorflow/serving/mlmd_map",
32  "Mapping for ML Metadata UUID to model_name and version.", "model_name",
33  "version");
34 
35 } // namespace
36 
37 void MaybePublishMLMDStreamz(const string& export_dir, const string& model_name,
38  int64_t version) {
39  const string mlmd_path = tensorflow::io::JoinPath(
40  export_dir, tensorflow::kSavedModelAssetsExtraDirectory, kMLMDKeyFile);
41  if (tsl::Env::Default()->FileExists(mlmd_path).ok()) {
42  string mlmd_key;
43  auto status = ReadFileToString(tsl::Env::Default(), mlmd_path, &mlmd_key);
44  if (!status.ok()) {
45  LOG(WARNING) << "ML Metadata Key Found But couldn't be read.";
46  } else {
47  mlmd_map->GetCell(model_name, strings::StrCat(version))->Set(mlmd_key);
48  }
49  }
50 }
51 
52 } // namespace serving
53 } // namespace tensorflow