@@ -20,6 +20,39 @@ pip install netflix-spectator-py
2020
2121## Instrumenting Code
2222
23+ ### Simple Example
24+
25+ ``` python
26+ import logging
27+
28+ from flask import Flask, request, Response
29+ from flask.logging import default_handler
30+ from spectator.registry import Registry
31+
32+ root_logger = logging.getLogger()
33+ root_logger.setLevel(logging.DEBUG )
34+ root_logger.addHandler(default_handler)
35+
36+ registry = Registry()
37+
38+ app = Flask(__name__ )
39+
40+ @app.route (" /" )
41+ def root ():
42+ return Response(" Usage: /api/v1/play?country=foo&title=bar" )
43+
44+ @app.route (" /api/v1/play" , methods = [" GET" , " POST" ])
45+ 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} " )
50+ ```
51+
52+ Save this snippet as ` app.py ` , then ` flask --app app run ` .
53+
54+ ### Complex Example
55+
2356``` python
2457import logging
2558
@@ -33,7 +66,7 @@ root_logger = logging.getLogger()
3366root_logger.setLevel(logging.DEBUG )
3467root_logger.addHandler(default_handler)
3568
36- config = Config(location = " none " , extra_common_tags = {" nf.platform" : " my_platform" })
69+ config = Config(extra_common_tags = {" nf.platform" : " my_platform" })
3770registry = Registry(config)
3871
3972request_count_id = registry.new_id(" server.requestCount" , {" version" : " v1" })
@@ -42,12 +75,10 @@ response_size = registry.distribution_summary("server.responseSize")
4275
4376app = Flask(__name__ )
4477
45-
4678@app.route (" /" )
4779def root ():
4880 return Response(" Usage: /api/v1/play?country=foo&title=bar" )
4981
50-
5182@app.route (" /api/v1/play" , methods = [" GET" , " POST" ])
5283def play ():
5384 if request.method == " GET" :
0 commit comments