Skip to content

Commit

Permalink
batch: allow to pass archive policy/unit
Browse files Browse the repository at this point in the history
This change allow to pass the archive policy/unit when we batch
measures for metrics.
  • Loading branch information
Mehdi Abaakouk committed Jan 18, 2018
1 parent 8282e3b commit de28bec
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
22 changes: 14 additions & 8 deletions doc/source/rest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -584,16 +584,22 @@
{
"{{ scenarios['create-resource-instance-with-dynamic-metrics']['response'].json['id'] }}": {
"cpu.util": [
{ "timestamp": "2014-10-06T14:34:12", "value": 12 },
{ "timestamp": "2014-10-06T14:34:20", "value": 2 }
]
"cpu.util": {
"archive_policy_name": "{{ scenarios['create-archive-policy']['response'].json['name'] }}",
"measures": [
{ "timestamp": "2014-10-06T14:34:12", "value": 12 },
{ "timestamp": "2014-10-06T14:34:20", "value": 2 }
]
}
},
"{{ scenarios['create-resource-instance-with-metrics']['response'].json['id'] }}": {
"cpu.util": [
{ "timestamp": "2014-10-06T14:34:12", "value": 6 },
{ "timestamp": "2014-10-06T14:34:20", "value": 25 }
]
"cpu.util": {
"archive_policy_name": "{{ scenarios['create-archive-policy']['response'].json['name'] }}",
"measures": [
{ "timestamp": "2014-10-06T14:34:12", "value": 6 },
{ "timestamp": "2014-10-06T14:34:20", "value": 25 }
]
}
}
}
Expand Down
40 changes: 29 additions & 11 deletions gnocchi/rest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,16 +1546,32 @@ def post(self, metric_id, start=None, stop=None, aggregation='mean',


class ResourcesMetricsMeasuresBatchController(rest.RestController):

@staticmethod
def BackwardCompatibleMeasuresList(v):
v = voluptuous.Schema(
voluptuous.Any(MeasuresListSchema,
{voluptuous.Optional("archive_policy_name"):
six.text_type,
voluptuous.Optional("unit"):
six.text_type,
"measures": MeasuresListSchema}),
required=True)(v)
if isinstance(v, dict):
return v
else:
# Old format
return {"measures": v}

@pecan.expose('json')
def post(self, create_metrics=False):
creator = pecan.request.auth_helper.get_current_user(
pecan.request)
MeasuresBatchSchema = voluptuous.Schema(
{functools.partial(ResourceID, creator=creator):
{six.text_type: MeasuresListSchema}}
)

body = deserialize_and_validate(MeasuresBatchSchema)
{six.text_type: self.BackwardCompatibleMeasuresList}})
body = deserialize_and_validate(MeasuresBatchSchema,
detailed_exc=True)

known_metrics = []
unknown_metrics = []
Expand All @@ -1575,19 +1591,21 @@ def post(self, create_metrics=False):
all_metrics[metric.resource_id].append(metric)

for original_resource_id, resource_id in body:
body_by_rid[resource_id] = body[(original_resource_id,
resource_id)]
names = list(body[(original_resource_id, resource_id)].keys())
r = body[(original_resource_id, resource_id)]
body_by_rid[resource_id] = r
names = list(r.keys())
metrics = all_metrics[resource_id]

known_names = [m.name for m in metrics]
if strtobool("create_metrics", create_metrics):
already_exists_names = []
for name in names:
if name not in known_names:
metric = MetricsController.MetricSchema({
"name": name
})
metric_data = {"name": name}
for attr in ["archive_policy_name", "unit"]:
if attr in r[name]:
metric_data[attr] = r[name][attr]
metric = MetricsController.MetricSchema(metric_data)
try:
m = pecan.request.indexer.create_metric(
uuid.uuid4(),
Expand Down Expand Up @@ -1641,7 +1659,7 @@ def post(self, create_metrics=False):

pecan.request.incoming.add_measures_batch(
dict((metric.id,
body_by_rid[metric.resource_id][metric.name])
body_by_rid[metric.resource_id][metric.name]["measures"])
for metric in known_metrics))

pecan.response.status = 202
Expand Down
56 changes: 52 additions & 4 deletions gnocchi/tests/functional/gabbits/batch-measures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ tests:
response_headers:
content-length: 0

- name: push measurements to unknown named metrics and create it with new format
POST: /v1/batch/resources/metrics/measures?create_metrics=true
data:
46c9418d-d63b-4cdd-be89-8f57ffc5952e:
newformat:
archive_policy_name: simple
unit: "ks"
measures:
- timestamp: "2015-03-06T14:33:57"
value: 43.1
- timestamp: "2015-03-06T14:34:12"
value: 42
auto.newformat:
measures:
- timestamp: "2015-03-06T14:33:57"
value: 43.1
- timestamp: "2015-03-06T14:34:12"
value: 24
auto.test:
measures:
- timestamp: "2015-03-06T14:33:57"
value: 43.1
- timestamp: "2015-03-06T14:35:12"
value: 24
status: 202
response_headers:
content-length: 0

- name: get created metric to check creation
GET: /v1/resource/generic/46c9418d-d63b-4cdd-be89-8f57ffc5952e/metric/auto.test

Expand All @@ -188,6 +216,25 @@ tests:
response_json_paths:
$:
- ["2015-03-06T14:34:12+00:00", 1.0, 12.0]
- ["2015-03-06T14:35:12+00:00", 1.0, 24.0]

- name: get created metric to check creation with newformat
GET: /v1/resource/generic/46c9418d-d63b-4cdd-be89-8f57ffc5952e/metric/newformat

- name: get created metric to check creation with newformat2
GET: /v1/resource/generic/46c9418d-d63b-4cdd-be89-8f57ffc5952e/metric/auto.newformat

- name: ensure measure have been posted with newformat
GET: /v1/resource/generic/46c9418d-d63b-4cdd-be89-8f57ffc5952e/metric/newformat/measures?refresh=true&start=2015-03-06T14:34
response_json_paths:
$:
- ["2015-03-06T14:34:12+00:00", 1.0, 42.0]

- name: ensure measure have been posted with newformat2
GET: /v1/resource/generic/46c9418d-d63b-4cdd-be89-8f57ffc5952e/metric/auto.newformat/measures?refresh=true&start=2015-03-06T14:34
response_json_paths:
$:
- ["2015-03-06T14:34:12+00:00", 1.0, 24.0]

- name: push measurements to unknown named metrics and resource with create_metrics with uuid resource id
POST: /v1/batch/resources/metrics/measures?create_metrics=true
Expand All @@ -202,10 +249,11 @@ tests:
value: 12
bbbbbbbb-d63b-4cdd-be89-111111111111:
auto.test:
- timestamp: "2015-03-06T14:33:57"
value: 43.1
- timestamp: "2015-03-06T14:34:12"
value: 12
measures:
- timestamp: "2015-03-06T14:33:57"
value: 43.1
- timestamp: "2015-03-06T14:34:12"
value: 12
status: 400
response_json_paths:
$.description.cause: "Unknown resources"
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/ap-in-batch-d83f6aa163d200e9.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
A new format for the batch payload is available to allow to pass
the archive policy description

0 comments on commit de28bec

Please sign in to comment.