Skip to content

Commit 10649bc

Browse files
yuriatgoogleTakashi Matsuo
andauthored
Added Prometheus sample for monitoring (GoogleCloudPlatform#5433)
* initial creation of Prometheus sample - /monitoring * cleaning up for linting * tests pass * adding region tags * cleaning up nox config * cleaning up region tags * updating copyright to get tests to pass * cleaning up license header Co-authored-by: Takashi Matsuo <tmatsuo@google.com>
1 parent d922abb commit 10649bc

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
/ml_engine/**/*.py @alecglassford @GoogleCloudPlatform/python-samples-owners
5151
/monitoring/**/*.py @GoogleCloudPlatform/python-samples-owners
5252
/monitoring/opencensus @yuriatgoogle @GoogleCloudPlatform/python-samples-owners
53+
/monitoring/prometheus @yuriatgoogle @GoogleCloudPlatform/python-samples-owners
5354
/notebooks/**/*.py @alixhami @sirtorry @GoogleCloudPlatform/python-samples-owners
5455
/opencensus/**/*.py @GoogleCloudPlatform/python-samples-owners
5556
/profiler/**/*.py @kalyanac @GoogleCloudPlatform/python-samples-owners

monitoring/prometheus/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This section contains a sample of using [Prometheus](https://prometheus.io) to instrument a Flask application to emit Service Level Indicator metrics.
2+
3+
## Running the samples locally
4+
5+
1. Many samples require extra libraries to be installed. If there is a `requirements.txt`, you will need to install the dependencies with [`pip`](pip.readthedocs.org).
6+
7+
pip install -t lib -r requirements.txt
8+
9+
2. Use `main.py` to run the sample:
10+
11+
python main.py
12+
13+
3. Visit `http://localhost:8080` to view your application.
14+
15+
4. Visit `http://localhost:8080/metrics` to view your metrics.
16+
17+
## Additional resources
18+
19+
For more information on Prometheus:
20+
21+
> https://prometheus.io

monitoring/prometheus/main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2021 Google LLC
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+
# https://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+
# [START monitoring_sli_metrics_prometheus_setup]
15+
import random
16+
import time
17+
18+
from flask import Flask
19+
20+
from prometheus_client import (
21+
Counter,
22+
generate_latest,
23+
Histogram,
24+
REGISTRY,
25+
)
26+
27+
# [END monitoring_sli_metrics_prometheus_setup]
28+
29+
app = Flask(__name__)
30+
# [START monitoring_sli_metrics_prometheus_create_metrics]
31+
PYTHON_REQUESTS_COUNTER = Counter("python_requests", "total requests")
32+
PYTHON_FAILED_REQUESTS_COUNTER = Counter("python_failed_requests", "failed requests")
33+
PYTHON_LATENCIES_HISTOGRAM = Histogram(
34+
"python_request_latency", "request latency by path"
35+
)
36+
# [END monitoring_sli_metrics_prometheus_create_metrics]
37+
38+
39+
@app.route("/")
40+
# [START monitoring_sli_metrics_prometheus_latency]
41+
@PYTHON_LATENCIES_HISTOGRAM.time()
42+
# [END monitoring_sli_metrics_prometheus_latency]
43+
def homePage():
44+
# count request
45+
# [START monitoring_sli_metrics_prometheus_counts]
46+
PYTHON_REQUESTS_COUNTER.inc()
47+
# fail 10% of the time
48+
if random.randint(0, 100) > 90:
49+
PYTHON_FAILED_REQUESTS_COUNTER.inc()
50+
# [END monitoring_sli_metrics_prometheus_counts]
51+
return ("error!", 500)
52+
else:
53+
random_delay = random.randint(0, 5000) / 1000
54+
# delay for a bit to vary latency measurement
55+
time.sleep(random_delay)
56+
return "home page"
57+
58+
59+
# [START monitoring_sli_metrics_prometheus_metrics_endpoint]
60+
@app.route("/metrics", methods=["GET"])
61+
def stats():
62+
return generate_latest(REGISTRY), 200
63+
64+
65+
# [END monitoring_sli_metrics_prometheus_metrics_endpoint]
66+
67+
68+
if __name__ == "__main__":
69+
app.run(debug=True, host="0.0.0.0", port=8080)

monitoring/prometheus/main_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2021 Google LLC
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+
import main
15+
16+
17+
def test_index() -> None:
18+
"""
19+
GIVEN the app
20+
WHEN multiple requests to the / endpoint are made
21+
THEN check that most of them succeed
22+
"""
23+
client = main.app.test_client()
24+
success_counter = 0
25+
for x in range(10):
26+
r = client.get("/")
27+
if r.status_code == 200:
28+
success_counter = success_counter + 1
29+
assert success_counter >= 5
30+
31+
32+
def test_metrics() -> None:
33+
"""
34+
GIVEN the app
35+
WHEN a request to the /metrics endpoint is made
36+
THEN check that it's a valid Prometheus endpoint
37+
"""
38+
client = main.app.test_client()
39+
res = client.get("/metrics")
40+
assert res.status_code == 200
41+
assert "HELP" in res.data.decode("utf-8")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==6.2.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Flask==1.1.2
2+
google-api-core==1.25.1
3+
google-auth==1.25.0
4+
googleapis-common-protos==1.53.0
5+
prometheus-client==0.9.0
6+
prometheus-flask-exporter==0.18.1
7+
requests==2.25.1

0 commit comments

Comments
 (0)