[Serve] Document Metric Infrastructure (#9389)

This commit is contained in:
Simon Mo
2020-07-21 14:52:18 -07:00
committed by GitHub
parent 4f470c3fc1
commit d8fd74d528
4 changed files with 130 additions and 20 deletions
+8
View File
@@ -193,6 +193,14 @@ py_test(
deps = [":serve_lib"]
)
py_test(
name = "snippet_metric_export",
size = "small",
srcs = glob(["examples/doc/*.py"]),
tags = ["exclusive"],
deps = [":serve_lib"]
)
# Disable the deployment tutorial test because it requires
# ray start --head in the background.
# py_test(
@@ -0,0 +1,55 @@
import json
import time
import requests
from ray import serve
from ray.serve.metric.exporter import ExporterInterface
class FileExporter(ExporterInterface):
def __init__(self):
self.file = open("/tmp/serve_metrics.log", "w")
def export(self, metric_metadata, metric_batch):
for metric_item in metric_batch:
data = metric_metadata[metric_item.key].__dict__
data["labels"] = metric_item.labels
data["values"] = metric_item.value
self.file.write(json.dumps(data))
self.file.write("\n")
self.file.flush()
def inspect_metrics(self):
return "Metric is located at /tmp/serve_metrics.log"
serve.init(metric_exporter=FileExporter)
def echo(flask_request):
return "hello " + flask_request.args.get("name", "serve!")
serve.create_backend("hello", echo)
serve.create_endpoint("hello", backend="hello", route="/hello")
for _ in range(5):
requests.get("http://127.0.0.1:8000/hello").text
time.sleep(0.2)
print("Retrieving metrics from file...")
with open("/tmp/serve_metrics.log") as metric_log:
for line in metric_log:
print(line)
# Retrieving metrics from file...
# {"name": "backend_worker_starts",
# "type": 1,
# "description": "The number of time this replica workers ...",
# "label_names": ["replica_tag"],
# "default_labels": {"backend": "hello"}, "
# labels": {"replica_tag": "hello#XwzPQn"},
# "values": 1
# }
# ...
+3 -20
View File
@@ -1,30 +1,13 @@
"""
Full example of ray.serve module
"""
import json
import time
from pygments import formatters, highlight, lexers
import requests
import ray
import ray.serve as serve
def pformat_color_json(d):
"""Use pygments to pretty format and colorize dictionary"""
formatted_json = json.dumps(d, sort_keys=True, indent=4)
colorful_json = highlight(formatted_json, lexers.JsonLexer(),
formatters.TerminalFormatter())
return colorful_json
from ray.serve.metric import PrometheusExporter
# initialize ray serve system.
serve.init()
serve.init(metric_exporter=PrometheusExporter)
# a backend can be a function or class.
@@ -70,4 +53,4 @@ serve.update_backend_config("echo:v1", {"num_replicas": 2})
serve.update_backend_config("echo:v2", {"num_replicas": 2})
# As well as retrieving relevant system metrics
print(pformat_color_json(serve.stat()))
print(serve.stat().decode())