Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #79 from librato/feature/get_composite
Browse files Browse the repository at this point in the history
Add basic GET support for composite metrics
  • Loading branch information
jderrett committed Jan 21, 2015
2 parents cae4cda + 4831a56 commit 59ee265
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ To retrieve a specific metric:
counter = api.get("connections")
```

To retrieve a composite metric:

```python
# Get average temperature in all cities for last 8 hours
compose = 'mean(s("temperature", "*", {function: "mean", period: "3600"}))'
start_time = int(time.time()) - 8 * 3600
resp = api.get_composite(compose, start_time=start_time)
resp['measurements'][0]['series']
# [
# {u'measure_time': 1421744400, u'value': 41.23944444444444},
# {u'measure_time': 1421748000, u'value': 40.07611111111111},
# {u'measure_time': 1421751600, u'value': 38.77444444444445},
# {u'measure_time': 1421755200, u'value': 38.05833333333333},
# {u'measure_time': 1421758800, u'value': 37.983333333333334},
# {u'measure_time': 1421762400, u'value': 38.93333333333333},
# {u'measure_time': 1421766000, u'value': 40.556666666666665}
# ]
```

For sending more measurements:

```python
Expand Down
9 changes: 9 additions & 0 deletions librato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ def get(self, name, **query_props):
else:
raise Exception('The server sent me something that is not a Gauge nor a Counter.')

def get_composite(self, compose, **query_props):
if 'resolution' not in query_props:
# Default to raw resolution
query_props['resolution'] = 1
if 'start_time' not in query_props:
raise Exception("You must provide a 'start_time'")
query_props['compose'] = compose
return self._mexe("metrics", method="GET", query_props=query_props)

def update(self, name, **query_props):
resp = self._mexe("metrics/%s" % self.sanitize(name), method="PUT", query_props=query_props)

Expand Down

0 comments on commit 59ee265

Please sign in to comment.