Skip to content

Commit c22df47

Browse files
authored
update spectator-py docs for 1.0.3 (#194)
Top-level imports were added, which simplifies usage, so relevant examples were updated. The main code examples were simplified a bit.
1 parent f1aa0bb commit c22df47

12 files changed

+66
-68
lines changed

Diff for: docs/spectator/lang/py/meters/age-gauge.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Time Since Last Success alerting pattern.
77
To set a specific time as the last success:
88

99
```python
10-
from spectator.registry import Registry
10+
from spectator import Registry
1111

1212
registry = Registry()
1313
registry.age_gauge("time.sinceLastSuccess").set(1611081000)
@@ -19,7 +19,7 @@ registry.age_gauge_with_id(last_success).set(1611081000)
1919
To set `now()` as the last success:
2020

2121
```python
22-
from spectator.registry import Registry
22+
from spectator import Registry
2323

2424
registry = Registry()
2525
registry.age_gauge("time.sinceLastSuccess").now()

Diff for: docs/spectator/lang/py/meters/counter.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ be used to convert them back into a value-per-step on a graph.
77
Call `increment()` when an event occurs:
88

99
```python
10-
from spectator.registry import Registry
10+
from spectator import Registry
1111

1212
registry = Registry()
1313
registry.counter("server.numRequests").increment()
@@ -20,7 +20,7 @@ You can also pass a value to `increment()`. This is useful when a collection of
2020
together:
2121

2222
```python
23-
from spectator.registry import Registry
23+
from spectator import Registry
2424

2525
registry = Registry()
2626
registry.counter("queue.itemsAdded").increment(10)

Diff for: docs/spectator/lang/py/meters/dist-summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This means that a `4K` tick label will represent 4 kilobytes, rather than 4 kilo
99
Call `record()` with a value:
1010

1111
```python
12-
from spectator.registry import Registry
12+
from spectator import Registry
1313

1414
registry = Registry()
1515
registry.distribution_summary("server.requestSize").record(10)

Diff for: docs/spectator/lang/py/meters/gauge.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ higher or lower at some point during interval, but that is not known.
99
Call `set()` with a value:
1010

1111
```python
12-
from spectator.registry import Registry
12+
from spectator import Registry
1313

1414
registry = Registry()
1515
registry.gauge("server.queueSize").set(10)
@@ -23,7 +23,7 @@ not need to be collected on a tight 1-minute schedule to ensure that Atlas shows
2323
graphs. A custom TTL may be configured for gauges. SpectatorD enforces a minimum TTL of 5 seconds.
2424

2525
```python
26-
from spectator.registry import Registry
26+
from spectator import Registry
2727

2828
registry = Registry()
2929
registry.gauge("server.queueSize", ttl_seconds=120).set(10)

Diff for: docs/spectator/lang/py/meters/max-gauge.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ standard Gauges, Max Gauges do not continue to report to the backend, and there
66
Call `set()` with a value:
77

88
```python
9-
from spectator.registry import Registry
9+
from spectator import Registry
1010

1111
registry = Registry()
1212
registry.max_gauge("server.queueSize").set(10)

Diff for: docs/spectator/lang/py/meters/monotonic-counter-uint.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Call `set()` when an event occurs:
88

99
```python
1010
from ctypes import c_uint64
11-
from spectator.registry import Registry
11+
from spectator import Registry
1212

1313
registry = Registry()
1414
registry.monotonic_counter_uint("iface.bytes").set(c_uint64(1))

Diff for: docs/spectator/lang/py/meters/monotonic-counter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ these values, at the expense of a slower time-to-first metric.
77
Call `set()` when an event occurs:
88

99
```python
10-
from spectator.registry import Registry
10+
from spectator import Registry
1111

1212
registry = Registry()
1313
registry.monotonic_counter("iface.bytes").set(10)

Diff for: docs/spectator/lang/py/meters/percentile-dist-summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ added to Percentile Distribution Summaries and ensure that they have a small bou
1111
Call `record()` with a value:
1212

1313
```python
14-
from spectator.registry import Registry
14+
from spectator import Registry
1515

1616
registry = Registry()
1717
registry.pct_distribution_summary("server.requestSize").record(10)

Diff for: docs/spectator/lang/py/meters/percentile-timer.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Timers and ensure that they have a small bounded cardinality.
1111
Call `record()` with a value:
1212

1313
```python
14-
from spectator.registry import Registry
14+
from spectator import Registry
1515

1616
registry = Registry()
1717
registry.pct_timer("server.requestLatency").record(0.01)
@@ -25,8 +25,7 @@ the number of seconds that have elapsed while executing a block of code:
2525

2626
```python
2727
import time
28-
from spectator.registry import Registry
29-
from spectator.stopwatch import StopWatch
28+
from spectator import Registry, StopWatch
3029

3130
registry = Registry()
3231
thread_sleep = registry.pct_timer("thread.sleep")

Diff for: docs/spectator/lang/py/meters/timer.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ A Timer is used to measure how long (in seconds) some event is taking.
33
Call `record()` with a value:
44

55
```python
6-
from spectator.registry import Registry
6+
from spectator import Registry
77

88
registry = Registry()
99
registry.timer("server.requestLatency").record(0.01)
@@ -17,8 +17,7 @@ the number of seconds that have elapsed while executing a block of code:
1717

1818
```python
1919
import time
20-
from spectator.registry import Registry
21-
from spectator.stopwatch import StopWatch
20+
from spectator import Registry, StopWatch
2221

2322
registry = Registry()
2423
thread_sleep = registry.timer("thread.sleep")

Diff for: docs/spectator/lang/py/migrations.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ After:
6363

6464
```python
6565
import time
66-
from spectator.registry import Registry
67-
from spectator.stopwatch import StopWatch
66+
from spectator import Registry, StopWatch
6867

6968
registry = Registry()
7069
server_latency = registry.pct_timer("serverLatency")
@@ -99,7 +98,7 @@ GlobalRegistry.gauge("server.queueSize", ttl_seconds=120).set(10)
9998
After:
10099

101100
```python
102-
from spectator.registry import Registry
101+
from spectator import Registry
103102

104103
registry = Registry()
105104
registry.gauge("server.queueSize", ttl_seconds=120).set(10)

Diff for: docs/spectator/lang/py/usage.md

+49-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# spectator-py Usage
22

3+
[![PyPI version](https://badge.fury.io/py/netflix-spectator-py.svg)](https://badge.fury.io/py/netflix-spectator-py)
4+
35
Python thin-client [metrics library] for use with [Atlas] and [SpectatorD].
46

57
[metrics library]: https://github.com/Netflix/spectator-py
@@ -8,7 +10,7 @@ Python thin-client [metrics library] for use with [Atlas] and [SpectatorD].
810

911
## Supported Python Versions
1012

11-
This library currently targets the Python >= 3.8.
13+
This library currently targets Python >= 3.8.
1214

1315
## Installing
1416

@@ -27,7 +29,7 @@ import logging
2729

2830
from flask import Flask, request, Response
2931
from flask.logging import default_handler
30-
from spectator.registry import Registry
32+
from spectator import Registry
3133

3234
root_logger = logging.getLogger()
3335
root_logger.setLevel(logging.DEBUG)
@@ -41,12 +43,21 @@ app = Flask(__name__)
4143
def root():
4244
return Response("Usage: /api/v1/play?country=foo&title=bar")
4345

44-
@app.route("/api/v1/play", methods=["GET", "POST"])
46+
@app.route("/api/v1/play")
4547
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)
5061
```
5162

5263
Save this snippet as `app.py`, then `flask --app app run`.
@@ -58,52 +69,45 @@ import logging
5869

5970
from flask import Flask, request, Response
6071
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
6473

6574
root_logger = logging.getLogger()
6675
root_logger.setLevel(logging.DEBUG)
6776
root_logger.addHandler(default_handler)
6877

69-
config = Config(extra_common_tags={"nf.platform": "my_platform"})
78+
config = Config(extra_common_tags={"platform": "flask-demo"})
7079
registry = Registry(config)
7180

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"})
7584

7685
app = Flask(__name__)
7786

7887
@app.route("/")
7988
def root():
8089
return Response("Usage: /api/v1/play?country=foo&title=bar")
8190

82-
@app.route("/api/v1/play", methods=["GET", "POST"])
91+
@app.route("/api/v1/play")
8392
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
95102
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
100103

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))
105109

106-
return Response("unsupported request method", status=status_code)
110+
return Response(message, status=status)
107111
```
108112

109113
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
116120
manage `MeterId` and `Meter` objects.
117121

118122
```python
119-
from spectator.registry import Registry
123+
from spectator import Registry
120124

121125
registry = Registry()
122126
registry.counter("server.requestCount").increment()
@@ -130,10 +134,10 @@ implement when adopting the thin client version of the library. It existed as a
130134
thick client because it was stateful, and required starting background threads. The thin client
131135
version is stateless.
132136

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.
137141

138142
```python
139143
from spectator import GlobalRegistry
@@ -187,7 +191,7 @@ number of successful requests, then you must cast integers to strings. The `Mete
187191
validate these values, dropping or changing any that are not valid, and reporting a warning log.
188192

189193
```python
190-
from spectator.registry import Registry
194+
from spectator import Registry
191195

192196
registry = Registry()
193197
registry.counter("server.numRequests", {"statusCode": str(200)}).increment()
@@ -247,8 +251,7 @@ export SPECTATOR_OUTPUT_LOCATION="udp://[::1]:1234"
247251
* Provide a custom `Config` for the `Registry`:
248252

249253
```python
250-
from spectator.config import Config
251-
from spectator.registry import Registry
254+
from spectator import Config, Registry
252255

253256
config = Config(location="udp://[::1]:1234")
254257
registry = Registry(config)
@@ -318,8 +321,7 @@ updates.
318321
```python
319322
import unittest
320323

321-
from spectator.config import Config
322-
from spectator.registry import Registry
324+
from spectator import Config, Registry
323325

324326
class MetricsTest(unittest.TestCase):
325327

@@ -335,14 +337,13 @@ class MetricsTest(unittest.TestCase):
335337

336338
### Protocol Parser
337339

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
339341
captured by a `MemoryWriter`.
340342

341343
```python
342344
import unittest
343345

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
346347

347348
class ProtocolParserTest(unittest.TestCase):
348349

0 commit comments

Comments
 (0)