Skip to content

Commit

Permalink
HWKMETRICS-104: Create a status endpoint to display the state of thin…
Browse files Browse the repository at this point in the history
…gs such as the MetricsService
  • Loading branch information
mwringe committed May 27, 2015
1 parent 4140256 commit ac6476e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.hawkular.metrics.api.jaxrs.filter;

import javax.ws.rs.core.UriInfo;
import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.StatusHandler;
import org.hawkular.metrics.core.api.MetricsService;

import javax.inject.Inject;
Expand Down Expand Up @@ -44,6 +46,14 @@ public class MetricsServiceStateFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
UriInfo uriInfo = containerRequestContext.getUriInfo();
String path = uriInfo.getPath();

if (path.startsWith(StatusHandler.PATH)) {
// The status page does not require the MetricsService to be up and running.
return;
}

if (metricsService.getState() == MetricsService.State.STARTING) {
// Fail since the Cassandra cluster is not yet up yet.
Response response = Response.status(Response.Status.SERVICE_UNAVAILABLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.ws.rs.ext.Provider;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.StatusHandler;

/**
* @author Stefan Negrea
Expand All @@ -52,8 +53,8 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
UriInfo uriInfo = requestContext.getUriInfo();
String path = uriInfo.getPath();

if (path.startsWith("/tenants") || path.startsWith("/db")) {
// Tenants and Influx handlers do not check the tenant header
if (path.startsWith("/tenants") || path.startsWith("/db") || path.startsWith(StatusHandler.PATH)) {
// Tenants, Influx and status handlers do not check the tenant header
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.api.jaxrs.handler;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import com.wordnik.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.hawkular.metrics.core.api.MetricsService;

/**
* @author mwringe
*/
@Path("/status")
@Produces(APPLICATION_JSON)
public class StatusHandler {

public static final String PATH = "/status";

@Inject
private MetricsService metricsService;

private static final String METRICSSERVICE_NAME = "MetricsService";

@GET
@ApiOperation(value = "Returns the current status for various components.",
response = String.class, responseContainer = "Map")
public Response status() {
Map<String, Object> status = new HashMap();

MetricsService.State metricState = metricsService.getState();
status.put(METRICSSERVICE_NAME, metricState.toString());

return Response.ok(status).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.rest

import org.junit.Test
import static org.junit.Assert.assertEquals

/**
* @author mwringe
*/
class StatusITest extends RESTTest {
@Test
void getStatusTest() {
def response = hawkularMetrics.get(path: "status");

assertEquals(200, response.status);

println("response: " + response.data);

def expectedData = ["MetricsService" : "STARTED"];

assertEquals(expectedData, response.data);
}
}

0 comments on commit ac6476e

Please sign in to comment.