metric element¶
-
class
metrix.element.
MElement
(name: str, value: Union[int, float], *, tags: Optional[Dict] = None)[source]¶ An individual metric element – a single data point – to be sent through a stream and on to one or more sinks.
>>> from metrix import MElement >>> me = MElement("counts", 1, tags={"env": "dev", "foo": "bar"}) >>> print(me) MElement(name='counts', value=1, tags={'env': 'dev', 'foo': 'bar'}) >>> me.key 'env:dev|foo:bar'
- Parameters
name – Base name of the metric to which the element belongs.
value – Numeric value of the metric element.
tags – Optional tags to associate with this (name, value) pair.
Note
In typical usage, users will not directly instantiate this class; instead, they’ll pass (name, value, tags) into
MCoordinator.send()
orMCoordinator.timer()
, which will create a correspondingMElement
under the hood.
Generate a (hashable!) key string from a collection of
tags
, where tags are pipe-delimited and each tag’s field and value are colon-delimited.>>> key_from_tags({"foo": "bar}) "foo:bar" >>> key_from_tags({"foo": "bar", "bat": "baz"}) "bat:baz|foo:bar"
Note
The ordering of items in
tags
doesn’t matter, since the generated key is always ordered alphabetically.
Generate a collection of tags from a key string, where tags are pipe-delimited and each tag’s field and value are colon-delimited.
>>> tags_from_key(None) None >>> tags_from_key("foo:bar") {"foo": "bar"} >>> tags_from_key("foo:bar|bat:baz") {"bat": "baz", "foo": "bar"}
Note
The ordering of items in
key
doesn’t matter, since the generated tags items are always ordered alphabetically.See also