1
1
# spectator-py Usage
2
2
3
+ [ ![ PyPI version] ( https://badge.fury.io/py/netflix-spectator-py.svg )] ( https://badge.fury.io/py/netflix-spectator-py )
4
+
3
5
Python thin-client [ metrics library] for use with [ Atlas] and [ SpectatorD] .
4
6
5
7
[ metrics library ] : https://github.com/Netflix/spectator-py
@@ -8,7 +10,7 @@ Python thin-client [metrics library] for use with [Atlas] and [SpectatorD].
8
10
9
11
## Supported Python Versions
10
12
11
- This library currently targets the Python >= 3.8.
13
+ This library currently targets Python >= 3.8.
12
14
13
15
## Installing
14
16
@@ -27,7 +29,7 @@ import logging
27
29
28
30
from flask import Flask, request, Response
29
31
from flask.logging import default_handler
30
- from spectator.registry import Registry
32
+ from spectator import Registry
31
33
32
34
root_logger = logging.getLogger()
33
35
root_logger.setLevel(logging.DEBUG )
@@ -41,12 +43,21 @@ app = Flask(__name__)
41
43
def root ():
42
44
return Response(" Usage: /api/v1/play?country=foo&title=bar" )
43
45
44
- @app.route (" /api/v1/play" , methods = [ " GET " , " POST " ] )
46
+ @app.route (" /api/v1/play" )
45
47
def play ():
46
- country = request.args.get(" country" , default = " none" )
47
- title = request.args.get(" title" , default = " none" )
48
- registry.counter(" server.requestCount" , {" version" : " v1" }).increment()
49
- return Response(f " requested play for country= { country} title= { title} " )
48
+ country = request.args.get(" country" , default = " unknown" )
49
+ title = request.args.get(" title" , default = " unknown" )
50
+
51
+ if country == " unknown" or title == " unknown" :
52
+ status = 404
53
+ message = f " invalid play request for country= { country} title= { title} "
54
+ else :
55
+ status = 200
56
+ message = f " requested play for country= { country} title= { title} "
57
+
58
+ tags = {" path" : " v1_play" , " country" : country, " title" : title, " status" : str (status)}
59
+ registry.counter(" server.requestCount" , tags).increment()
60
+ return Response(message, status = status)
50
61
```
51
62
52
63
Save this snippet as ` app.py ` , then ` flask --app app run ` .
@@ -58,52 +69,45 @@ import logging
58
69
59
70
from flask import Flask, request, Response
60
71
from flask.logging import default_handler
61
- from spectator.config import Config
62
- from spectator.registry import Registry
63
- from spectator.stopwatch import StopWatch
72
+ from spectator import Config, Registry, StopWatch
64
73
65
74
root_logger = logging.getLogger()
66
75
root_logger.setLevel(logging.DEBUG )
67
76
root_logger.addHandler(default_handler)
68
77
69
- config = Config(extra_common_tags = {" nf. platform" : " my_platform " })
78
+ config = Config(extra_common_tags = {" platform" : " flask-demo " })
70
79
registry = Registry(config)
71
80
72
- request_count_id = registry.new_id(" server.requestCount" , {" version " : " v1 " })
73
- request_latency = registry.timer(" server.requestLatency" )
74
- response_size = registry.distribution_summary(" server.responseSize" )
81
+ request_count_id = registry.new_id(" server.requestCount" , {" path " : " v1_play " })
82
+ request_latency = registry.timer(" server.requestLatency" , { " path " : " v1_play " } )
83
+ response_size = registry.distribution_summary(" server.responseSize" , { " path " : " v1_play " } )
75
84
76
85
app = Flask(__name__ )
77
86
78
87
@app.route (" /" )
79
88
def root ():
80
89
return Response(" Usage: /api/v1/play?country=foo&title=bar" )
81
90
82
- @app.route (" /api/v1/play" , methods = [ " GET " , " POST " ] )
91
+ @app.route (" /api/v1/play" )
83
92
def play ():
84
- if request.method == " GET" :
85
- with StopWatch(request_latency):
86
- status_code = 200
87
- country = request.args.get(" country" , default = " none" )
88
- title = request.args.get(" title" , default = " none" )
89
-
90
- tags = {" country" : country, " title" : title, " status" : str (status_code)}
91
- request_count_with_tags = request_count_id.with_tags(tags)
92
- counter = registry.counter_with_id(request_count_with_tags)
93
- counter.increment()
94
-
93
+ with StopWatch(request_latency):
94
+ country = request.args.get(" country" , default = " unknown" )
95
+ title = request.args.get(" title" , default = " unknown" )
96
+
97
+ if country == " unknown" or title == " unknown" :
98
+ status = 404
99
+ message = f " invalid play request for country= { country} title= { title} "
100
+ else :
101
+ status = 200
95
102
message = f " requested play for country= { country} title= { title} "
96
- response_size.record(len (message))
97
- return Response(message, status = status_code)
98
- else :
99
- status_code = 405
100
103
101
- tags = {" status" : str (status_code)}
102
- request_count_with_tags = request_count_id.with_tags(tags)
103
- counter = registry.counter_with_id(request_count_with_tags)
104
- counter.increment()
104
+ tags = {" country" : country, " title" : title, " status" : str (status)}
105
+ request_count = registry.counter_with_id(request_count_id.with_tags(tags))
106
+
107
+ request_count.increment()
108
+ response_size.record(len (message))
105
109
106
- return Response(" unsupported request method " , status = status_code )
110
+ return Response(message , status = status )
107
111
```
108
112
109
113
Save this snippet as ` app.py ` , then ` flask --app app run ` .
@@ -116,7 +120,7 @@ Instantiate a `Registry` object, with either a default or custom `Config`, and u
116
120
manage ` MeterId ` and ` Meter ` objects.
117
121
118
122
``` python
119
- from spectator.registry import Registry
123
+ from spectator import Registry
120
124
121
125
registry = Registry()
122
126
registry.counter(" server.requestCount" ).increment()
@@ -130,10 +134,10 @@ implement when adopting the thin client version of the library. It existed as a
130
134
thick client because it was stateful, and required starting background threads. The thin client
131
135
version is stateless.
132
136
133
- Importing the ` GlobalRegistry ` instantiates a ` Registry ` with a default ` Config ` that applies
134
- process-specific common tags based on environment variables and opens a UDP socket to the local
135
- [ SpectatorD] agent. The remainder of the instance-specific common tags are provided by [ SpectatorD ] .
136
- Once imported, the ` GlobalRegistry ` can be used to create and manage Meters.
137
+ Importing the ` GlobalRegistry ` instantiates a ` Registry ` with a default ` Config ` , and opens a UDP
138
+ socket to the local [ SpectatorD ] agent when publishing metrics. The instance-specific common tags
139
+ are provided by [ SpectatorD] . Once imported, the ` GlobalRegistry ` can be used to create and manage
140
+ Meters.
137
141
138
142
``` python
139
143
from spectator import GlobalRegistry
@@ -187,7 +191,7 @@ number of successful requests, then you must cast integers to strings. The `Mete
187
191
validate these values, dropping or changing any that are not valid, and reporting a warning log.
188
192
189
193
``` python
190
- from spectator.registry import Registry
194
+ from spectator import Registry
191
195
192
196
registry = Registry()
193
197
registry.counter(" server.numRequests" , {" statusCode" : str (200 )}).increment()
@@ -247,8 +251,7 @@ export SPECTATOR_OUTPUT_LOCATION="udp://[::1]:1234"
247
251
* Provide a custom ` Config ` for the ` Registry ` :
248
252
249
253
``` python
250
- from spectator.config import Config
251
- from spectator.registry import Registry
254
+ from spectator import Config, Registry
252
255
253
256
config = Config(location = " udp://[::1]:1234" )
254
257
registry = Registry(config)
@@ -318,8 +321,7 @@ updates.
318
321
``` python
319
322
import unittest
320
323
321
- from spectator.config import Config
322
- from spectator.registry import Registry
324
+ from spectator import Config, Registry
323
325
324
326
class MetricsTest (unittest .TestCase ):
325
327
@@ -335,14 +337,13 @@ class MetricsTest(unittest.TestCase):
335
337
336
338
### Protocol Parser
337
339
338
- A [ SpectatorD] line protocol parser is available, which ca be used for validating the results
340
+ A [ SpectatorD] line protocol parser is available, which ca be used for validating the results
339
341
captured by a ` MemoryWriter ` .
340
342
341
343
``` python
342
344
import unittest
343
345
344
- from spectator.meter.counter import Counter
345
- from spectator.protocol_parser import get_meter_class, parse_protocol_line
346
+ from spectator import Counter, get_meter_class, parse_protocol_line
346
347
347
348
class ProtocolParserTest (unittest .TestCase ):
348
349
0 commit comments