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