Skip to content

Commit 841bcdd

Browse files
flapjack103Yann
authored andcommitted
Add metric metadata endpoints (#181)
[api] new metadata resource
1 parent 331d9e1 commit 841bcdd

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

datadog/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from datadog.api.timeboards import Timeboard
2323
from datadog.api.events import Event
2424
from datadog.api.infrastructure import Infrastructure
25+
from datadog.api.metadata import Metadata
2526
from datadog.api.metrics import Metric
2627
from datadog.api.monitors import Monitor
2728
from datadog.api.screenboards import Screenboard

datadog/api/metadata.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# datadog
2+
from datadog.api.resources import GetableAPIResource, UpdatableAPIResource
3+
4+
5+
class Metadata(GetableAPIResource, UpdatableAPIResource):
6+
"""
7+
A wrapper around Metric Metadata HTTP API
8+
"""
9+
_class_url = '/metrics'
10+
_json_name = 'metrics'
11+
12+
@classmethod
13+
def get(cls, metric_name):
14+
"""
15+
Get metadata information on an existing Datadog metric
16+
17+
param metric_name: metric name (ex. system.cpu.idle)
18+
19+
:returns: JSON response from HTTP request
20+
"""
21+
if not metric_name:
22+
raise KeyError("'metric_name' parameter is required")
23+
24+
return super(Metadata, cls).get(metric_name)
25+
26+
@classmethod
27+
def update(cls, metric_name, **params):
28+
"""
29+
Update metadata fields for an existing Datadog metric.
30+
If the metadata does not exist for the metric it is created by
31+
the update.
32+
33+
:param type: type of metric (ex. "gauge", "rate", etc.)
34+
see http://docs.datadoghq.com/metrictypes/
35+
:type type: string
36+
37+
:param description: description of the metric
38+
:type description: string
39+
40+
:param short_name: short name of the metric
41+
:type short_name: string
42+
43+
:param unit: unit type associated with the metric (ex. "byte", "operation")
44+
see http://docs.datadoghq.com/units/ for full list
45+
:type unit: string
46+
47+
:param per_unit: per unit type (ex. "second" as in "queries per second")
48+
see http://docs.datadoghq.com/units/ for full list
49+
:type per_unit: string
50+
51+
:param statsd_interval: statsd flush interval for metric in seconds (if applicable)
52+
:type statsd_interval: integer
53+
54+
:return: JSON response from HTTP request
55+
56+
>>> api.Metadata.update(metric_name='api.requests.served', metric_type="counter")
57+
"""
58+
if not metric_name:
59+
raise KeyError("'metric_name' parameter is required")
60+
61+
return super(Metadata, cls).update(id=metric_name, **params)

datadog/dogshell/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ def main():
6868

6969
args.func(args)
7070

71+
7172
if __name__ == '__main__':
7273
main()

datadog/dogshell/event.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from datadog.util.compat import json
1111

1212

13+
time_pat = re.compile(r'(?P<delta>[0-9]*\.?[0-9]+)(?P<unit>[mhd])')
14+
15+
1316
def prettyprint_event(event):
1417
title = event['title'] or ''
1518
text = event.get('text', '') or ''
@@ -34,8 +37,6 @@ def prettyprint_event_details(event):
3437
def print_event_details(event):
3538
prettyprint_event(event)
3639

37-
time_pat = re.compile(r'(?P<delta>[0-9]*\.?[0-9]+)(?P<unit>[mhd])')
38-
3940

4041
def parse_time(timestring):
4142
now = time.mktime(datetime.datetime.now().timetuple())
@@ -44,7 +45,7 @@ def parse_time(timestring):
4445
else:
4546
try:
4647
t = int(timestring)
47-
except:
48+
except Exception:
4849
match = time_pat.match(timestring)
4950
if match is None:
5051
raise Exception

datadog/dogshell/wrap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,6 @@ def main():
321321

322322
sys.exit(returncode)
323323

324+
324325
if __name__ == '__main__':
325326
main()

0 commit comments

Comments
 (0)