Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 14a4740

Browse files
vcasadeiliyanhui1228
authored andcommitted
Prometheus system tests (#299)
1 parent 890c19e commit 14a4740

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,4 @@ Apache 2.0 - See `LICENSE <LICENSE>`__ for more information.
600600
Disclaimer
601601
----------
602602

603-
This is not an official Google product.
603+
This is not an official Google product.

opencensus/stats/aggregation_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ def increment_bucket_count(self, value):
230230
return i
231231

232232
self._counts_per_bucket[(len(self._bounds))-1] += 1
233-
234233
return i
235234

236235

opencensus/stats/exporters/prometheus_exporter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ def describe(self):
237237
registered = {}
238238
for sign in self.registered_views:
239239
registered[sign] = self.registered_views[sign]
240-
241240
for v_data in list(self.view_data): # pragma: NO COVER
242-
signature = view_signature(self.options.namespace, v_data.view)
243-
desc = self.registered_views[signature]
244-
metric = self.to_metric(desc,
245-
self.view_data[v_data].view)
246-
yield metric
241+
if not isinstance(v_data, str):
242+
signature = view_signature(self.options.namespace, v_data.view)
243+
desc = self.registered_views[signature]
244+
metric = self.to_metric(desc,
245+
self.view_data[v_data].view)
246+
yield metric
247247

248248

249249
class PrometheusStatsExporter(base.StatsExporter):
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2018, OpenCensus Authors
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+
import unittest
16+
17+
18+
class TestPrometheusStats(unittest.TestCase):
19+
20+
def test_prometheus_stats(self):
21+
import random
22+
import time
23+
import sys
24+
25+
from opencensus.stats import aggregation as aggregation_module
26+
from opencensus.stats.exporters import prometheus_exporter as prometheus
27+
from opencensus.stats import measure as measure_module
28+
from opencensus.stats import stats as stats_module
29+
from opencensus.stats import view as view_module
30+
from opencensus.tags import tag_key as tag_key_module
31+
from opencensus.tags import tag_map as tag_map_module
32+
from opencensus.tags import tag_value as tag_value_module
33+
34+
MiB = 1 << 20
35+
FRONTEND_KEY = tag_key_module.TagKey("my.org/keys/frontend")
36+
VIDEO_SIZE_MEASURE = measure_module.MeasureInt(
37+
"my.org/measures/video_size", "size of processed videos", "By")
38+
VIDEO_SIZE_VIEW_NAME = "my.org/views/video_size"
39+
VIDEO_SIZE_DISTRIBUTION = aggregation_module.CountAggregation(
40+
256.0 * MiB)
41+
VIDEO_SIZE_VIEW = view_module.View(VIDEO_SIZE_VIEW_NAME,
42+
"processed video size over time",
43+
[FRONTEND_KEY],
44+
VIDEO_SIZE_MEASURE,
45+
VIDEO_SIZE_DISTRIBUTION)
46+
stats = stats_module.Stats()
47+
view_manager = stats.view_manager
48+
stats_recorder = stats.stats_recorder
49+
50+
exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="opencensus", port=9303))
51+
view_manager.register_exporter(exporter)
52+
53+
view_manager.register_view(VIDEO_SIZE_VIEW)
54+
55+
time.sleep(random.randint(1, 10) / 1000.0)
56+
57+
tag_value = tag_value_module.TagValue(str(random.randint(1, 10000)))
58+
tag_map = tag_map_module.TagMap()
59+
tag_map.insert(FRONTEND_KEY, tag_value)
60+
measure_map = stats_recorder.new_measurement_map()
61+
measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB)
62+
measure_map.record(tag_map)
63+
64+
if sys.version_info > (3, 0):
65+
import urllib.request
66+
contents = urllib.request.urlopen("http://localhost:9303/metrics").read()
67+
else:
68+
import urllib2
69+
contents = urllib2.urlopen("http://localhost:9303/metrics").read()
70+
71+
self.assertIn(b'# TYPE opencensus_my.org/views/video_size counter', contents)
72+
self.assertIn(b'opencensus_my.org/views/video_size 268435456.0', contents)

0 commit comments

Comments
 (0)