Skip to content

Commit

Permalink
[HWKMETRICS-317] Add @get on root for each metric type to match the e…
Browse files Browse the repository at this point in the history
…nd-point for adding metrics via @post.
  • Loading branch information
Stefan Negrea committed Oct 19, 2015
1 parent 3b437d4 commit 99860ad
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 161 deletions.
401 changes: 263 additions & 138 deletions api/diff.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.PatternSyntaxException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -104,6 +105,29 @@ public Response createAvailabilityMetric(
}
}

@GET
@Path("/")
public Response findMetrics(
@QueryParam("tags") Tags tags) {

Observable<Metric<AvailabilityType>> metricObservable = (tags == null)
? metricsService.findMetrics(tenantId, AVAILABILITY)
: metricsService.findMetricsWithFilters(tenantId, tags.getTags(), AVAILABILITY);

try {
return metricObservable
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.toBlocking()
.lastOrDefault(null);
} catch (PatternSyntaxException e) {
return badRequest(e);
} catch (Exception e) {
return serverError(e);
}
}

@GET
@Path("/{id}")
public Response getAvailabilityMetric(@PathParam("id") String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.PatternSyntaxException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -105,6 +106,29 @@ public Response createCounter(
}
}

@GET
@Path("/")
public <T> Response findMetrics(
@QueryParam("tags") Tags tags) {

Observable<Metric<Long>> metricObservable = (tags == null)
? metricsService.findMetrics(tenantId, COUNTER)
: metricsService.findMetricsWithFilters(tenantId, tags.getTags(), COUNTER);

try {
return metricObservable
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.toBlocking()
.lastOrDefault(null);
} catch (PatternSyntaxException e) {
return badRequest(e);
} catch (Exception e) {
return serverError(e);
}
}

@GET
@Path("/{id}")
public Response getCounter(@PathParam("id") String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.PatternSyntaxException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -106,6 +107,29 @@ public Response createGaugeMetric(
}
}

@GET
@Path("/")
public Response findMetrics(
@QueryParam("tags") Tags tags) {

Observable<Metric<Double>> metricObservable = (tags == null)
? metricsService.findMetrics(tenantId, GAUGE)
: metricsService.findMetricsWithFilters(tenantId, tags.getTags(), GAUGE);

try {
return metricObservable
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.toBlocking()
.lastOrDefault(null);
} catch (PatternSyntaxException e) {
return badRequest(e);
} catch (Exception e) {
return serverError(e);
}
}

@GET
@Produces(APPLICATION_JSON)
@Path("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.regex.PatternSyntaxException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -114,6 +115,39 @@ public void createAvailabilityMetric(
metricsService.createMetric(metric).subscribe(new MetricCreatedObserver(asyncResponse, location));
}

@GET
@Path("/")
@ApiOperation(value = "Find tenant's metric definitions.",
notes = "Does not include any metric values. ",
response = MetricDefinition.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved at least one metric definition."),
@ApiResponse(code = 204, message = "No metrics found."),
@ApiResponse(code = 400, message = "Invalid type parameter type.", response = ApiError.class),
@ApiResponse(code = 500, message = "Failed to retrieve metrics due to unexpected error.",
response = ApiError.class)
})
public void findGuageMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") Tags tags) {

Observable<Metric<AvailabilityType>> metricObservable = (tags == null)
? metricsService.findMetrics(tenantId, AVAILABILITY)
: metricsService.findMetricsWithFilters(tenantId, tags.getTags(), AVAILABILITY);

metricObservable
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
if (t instanceof PatternSyntaxException) {
asyncResponse.resume(badRequest(t));
} else {
asyncResponse.resume(serverError(t));
}
});
}

@GET
@Path("/{id}")
@ApiOperation(value = "Retrieve single metric definition.", response = MetricDefinition.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.PatternSyntaxException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -121,6 +122,39 @@ public void createCounter(
metricsService.createMetric(metric).subscribe(new MetricCreatedObserver(asyncResponse, location));
}

@GET
@Path("/")
@ApiOperation(value = "Find tenant's counter metric definitions.",
notes = "Does not include any metric values. ",
response = MetricDefinition.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved at least one metric definition."),
@ApiResponse(code = 204, message = "No metrics found."),
@ApiResponse(code = 400, message = "Invalid type parameter type.", response = ApiError.class),
@ApiResponse(code = 500, message = "Failed to retrieve metrics due to unexpected error.",
response = ApiError.class)
})
public void findGuageMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") Tags tags) {

Observable<Metric<Long>> metricObservable = (tags == null)
? metricsService.findMetrics(tenantId, COUNTER)
: metricsService.findMetricsWithFilters(tenantId, tags.getTags(), COUNTER);

metricObservable
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
if (t instanceof PatternSyntaxException) {
asyncResponse.resume(badRequest(t));
} else {
asyncResponse.resume(serverError(t));
}
});
}

@GET
@Path("/{id}")
@ApiOperation(value = "Retrieve a counter definition.", response = MetricDefinition.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import static org.hawkular.metrics.api.jaxrs.filter.TenantFilter.TENANT_HEADER_NAME;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.badRequest;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.serverError;
import static org.hawkular.metrics.core.api.MetricType.GAUGE;

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.PatternSyntaxException;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -116,6 +118,39 @@ public void createGaugeMetric(
metricsService.createMetric(metric).subscribe(new MetricCreatedObserver(asyncResponse, location));
}

@GET
@Path("/")
@ApiOperation(value = "Find tenant's metric definitions.",
notes = "Does not include any metric values. ",
response = MetricDefinition.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved at least one metric definition."),
@ApiResponse(code = 204, message = "No metrics found."),
@ApiResponse(code = 400, message = "Invalid type parameter type.", response = ApiError.class),
@ApiResponse(code = 500, message = "Failed to retrieve metrics due to unexpected error.",
response = ApiError.class)
})
public void findGuageMetrics(
@Suspended AsyncResponse asyncResponse,
@ApiParam(value = "List of tags filters", required = false) @QueryParam("tags") Tags tags) {

Observable<Metric<Double>> metricObservable = (tags == null)
? metricsService.findMetrics(tenantId, GAUGE)
: metricsService.findMetricsWithFilters(tenantId, tags.getTags(), GAUGE);

metricObservable
.map(MetricDefinition::new)
.toList()
.map(ApiUtils::collectionToResponse)
.subscribe(asyncResponse::resume, t -> {
if (t instanceof PatternSyntaxException) {
asyncResponse.resume(badRequest(t));
} else {
asyncResponse.resume(serverError(t));
}
});
}

@GET
@Path("/{id}")
@ApiOperation(value = "Retrieve single metric definition.", response = MetricDefinition.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class TagsITest extends RESTTest {
metricTypes.each {
def tenantId = nextTenantId()

// Create a metrics
// Create metric
def response = hawkularMetrics.post(path: it.path, body: [
id : 'N1',
tags: ['a1': 'A', 'd1': 'B']
Expand All @@ -158,33 +158,46 @@ class TagsITest extends RESTTest {
query: [tags: "a1:*"],
headers: [(tenantHeaderName): tenantId])

assertEquals(200, response.status)
assertTrue(response.data instanceof List)
assertEquals(2, response.data.size())
assertTrue((response.data ?: []).contains([
tenantId: tenantId,
id : 'N1',
tags : ['a1': 'A', 'd1': 'B'],
type: it.type
]))
assertTrue((response.data ?: []).contains([
tenantId: tenantId,
id : 'N2',
tags : ['a1': 'A2'],
type: it.type
]))
def alternateResponse = hawkularMetrics.get(path: it.path,
query: [tags: "a1:*"],
headers: [(tenantHeaderName): tenantId])

[response, alternateResponse].each { lresponse ->
assertEquals(200, lresponse.status)
assertTrue(lresponse.data instanceof List)
assertEquals(2, lresponse.data.size())
assertTrue((lresponse.data ?: []).contains([
tenantId: tenantId,
id : 'N1',
tags : ['a1': 'A', 'd1': 'B'],
type: it.type
]))
assertTrue((lresponse.data ?: []).contains([
tenantId: tenantId,
id : 'N2',
tags : ['a1': 'A2'],
type: it.type
]))
}

// Fetch with tags & type
response = hawkularMetrics.get(path: "metrics",
query: [tags: "a1:A,d1:B", type: it.type],
headers: [(tenantHeaderName): tenantId])
assertEquals(200, response.status)
assertEquals([[
tenantId: tenantId,
id : 'N1',
tags : ['a1': 'A', 'd1': 'B'],
type: it.type
]], response.data)

alternateResponse = hawkularMetrics.get(path: it.path,
query: [tags: "a1:A,d1:B"],
headers: [(tenantHeaderName): tenantId])

[response, alternateResponse].each { lresponse ->
assertEquals(200, lresponse.status)
assertEquals([[
tenantId: tenantId,
id : 'N1',
tags : ['a1': 'A', 'd1': 'B'],
type: it.type
]], lresponse.data)
}

// Fetch with incorrect regexp
badGet(path: "metrics", query: [tags: "a1:**", type: it.type],
Expand Down

0 comments on commit 99860ad

Please sign in to comment.