Skip to content

Commit

Permalink
Merge pull request #327 from hawkular/HWKMETRICS-128
Browse files Browse the repository at this point in the history
HWKMETRICS-128
  • Loading branch information
mwringe committed Aug 27, 2015
2 parents 245debd + 2ec3abd commit ed0c216
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -29,6 +32,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.wordnik.swagger.annotations.ApiOperation;
Expand All @@ -45,17 +49,14 @@ public class BaseHandler {
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Returns some basic information about the Hawkular Metrics service.",
response = String.class, responseContainer = "Map")
public Response baseJSON(@Context ServletContext context) {

String version = context.getInitParameter("hawkular.metrics.version");
if (version == null) {
version = "undefined";
}
public Response baseJSON(@Context ServletContext servletContext) {
Map<String, Object> hawkularMetricsProperties = new HashMap<>();
hawkularMetricsProperties.put("name", "Hawkular-Metrics");

HawkularMetricsBase hawkularMetrics = new HawkularMetricsBase();
hawkularMetrics.version = version;
hawkularMetricsProperties.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

return Response.ok(hawkularMetrics).build();
return Response.ok(hawkularMetricsProperties).build();
}

@GET
Expand All @@ -66,25 +67,4 @@ public void baseHTML(@Context ServletContext context) throws Exception {
HttpServletResponse response = ResteasyProviderFactory.getContextData(HttpServletResponse.class);
request.getRequestDispatcher("/static/index.html").forward(request,response);
}

private class HawkularMetricsBase {

String name = "Hawkular-Metrics";
String version;

@SuppressWarnings("unused")
public String getName() {
return name;
}

@SuppressWarnings("unused")
public void setVersion(String version) {
this.version = version;
}

@SuppressWarnings("unused")
public String getVersion() {
return version;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle.State;
import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;

import com.wordnik.swagger.annotations.ApiOperation;

Expand All @@ -49,12 +52,15 @@ public class StatusHandler {
@GET
@ApiOperation(value = "Returns the current status for various components.",
response = String.class, responseContainer = "Map")
public Response status() {
public Response status(@Context ServletContext servletContext) {
Map<String, Object> status = new HashMap<>();

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

status.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

return Response.ok(status).build();
}
}
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.util;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.servlet.ServletContext;

public class ManifestInformation {

public static final List<String> VERSION_ATTRIBUTES = Collections
.unmodifiableList(Arrays.asList("Implementation-Version", "Built-From-Git-SHA1"));

public static Map<String, String> getManifestInformation(ServletContext servletContext, String... attributes) {
return getManifestInformation(servletContext, Arrays.asList(attributes));
}

public static Map<String, String> getManifestInformation(ServletContext servletContext, List<String> attributes) {
Map<String, String> manifestAttributes = new HashMap<>();
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
for (String attribute : attributes) {
manifestAttributes.put(attribute, attr.getValue(attribute));
}
} catch (Exception e) {
for (String attribute : attributes) {
if (manifestAttributes.get(attribute) == null) {
manifestAttributes.put(attribute, "Unknown");
}
}
}

return manifestAttributes;
}
}
5 changes: 0 additions & 5 deletions api/metrics-api-jaxrs-1.1/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@

<display-name>Hawkular Metrics Rest interface - JAX-RS 1.1</display-name>

<context-param>
<param-name>hawkular.metrics.version</param-name>
<param-value>${project.version}</param-value>
</context-param>

<context-param>
<param-name>resteasy.providers</param-name>
<param-value>org.hawkular.metrics.api.jaxrs.util.JacksonContextResolver</param-value>
Expand Down
19 changes: 10 additions & 9 deletions api/metrics-api-jaxrs-1.1/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
</head>

<body>
<div class="content">
<div class="logo"/>
<img src="/hawkular/metrics/static/hawkular_logo.png" alt="Hawkular Logo"/>
</div>
<h1>Hawkular Metrics</h1>
<div class="content">
<div class="logo">
<img src="/hawkular/metrics/static/hawkular_logo.png" alt="Hawkular Logo"/>
</div>
<h1>Hawkular Metrics</h1>

<h3>A time series metrics engine based on Cassandra</h3>
<h3>A time series metrics engine based on Cassandra</h3>

<p>${project.version}</p>
<p id="implementation-version"></p>
<p id="built-from-git-sha1" style="font-size:12px;"></p>

<p id="status">Status: <i>Checking...</i></p>
</div>
<p id="status">Status: <i>Checking...</i></p>
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions api/metrics-api-jaxrs-1.1/src/main/webapp/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function updateStatus() {
if (httpRequest.status === 200) {
statusJson = JSON.parse(httpRequest.responseText);
document.getElementById("status").innerHTML = "Metrics Service :" + statusJson.MetricsService;
document.getElementById("implementation-version").innerHTML = statusJson["Implementation-Version"];
document.getElementById("built-from-git-sha1").innerHTML = "(Git SHA1 - " + statusJson["Built-From-Git-SHA1"] + ")";
} else if (httpRequest.status === 404 || httpRequest.status === 503) {
document.getElementById("status").innerHTML = "The server is not available";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -29,6 +32,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.wordnik.swagger.annotations.ApiOperation;
Expand All @@ -45,17 +49,14 @@ public class BaseHandler {
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Returns some basic information about the Hawkular Metrics service.",
response = String.class, responseContainer = "Map")
public Response baseJSON(@Context ServletContext context) {

String version = context.getInitParameter("hawkular.metrics.version");
if (version == null) {
version = "undefined";
}
public Response baseJSON(@Context ServletContext servletContext) {
Map<String, Object> hawkularMetricsProperties = new HashMap<>();
hawkularMetricsProperties.put("name", "Hawkular-Metrics");

HawkularMetricsBase hawkularMetrics = new HawkularMetricsBase();
hawkularMetrics.version = version;
hawkularMetricsProperties.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

return Response.ok(hawkularMetrics).build();
return Response.ok(hawkularMetricsProperties).build();
}

@GET
Expand All @@ -66,22 +67,4 @@ public void baseHTML(@Context ServletContext context) throws Exception {
HttpServletResponse response = ResteasyProviderFactory.getContextData(HttpServletResponse.class);
request.getRequestDispatcher("/static/index.html").forward(request,response);
}

private class HawkularMetricsBase {

String name = "Hawkular-Metrics";
String version;

public String getName() {
return name;
}

public void setVersion(String version) {
this.version = version;
}

public String getVersion() {
return version;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle.State;
import org.hawkular.metrics.api.jaxrs.util.ManifestInformation;

import com.wordnik.swagger.annotations.ApiOperation;

Expand All @@ -49,12 +52,17 @@ public class StatusHandler {
@GET
@ApiOperation(value = "Returns the current status for various components.",
response = String.class, responseContainer = "Map")
public Response status() {
public Response status(@Context ServletContext servletContext) {
Map<String, Object> status = new HashMap<>();

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

status.putAll(
ManifestInformation.getManifestInformation(servletContext, ManifestInformation.VERSION_ATTRIBUTES));

return Response.ok(status).build();
}


}
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.util;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.servlet.ServletContext;

public class ManifestInformation {

public static final List<String> VERSION_ATTRIBUTES = Collections
.unmodifiableList(Arrays.asList("Implementation-Version", "Built-From-Git-SHA1"));

public static Map<String, String> getManifestInformation(ServletContext servletContext, String... attributes) {
return getManifestInformation(servletContext, Arrays.asList(attributes));
}

public static Map<String, String> getManifestInformation(ServletContext servletContext, List<String> attributes) {
Map<String, String> manifestAttributes = new HashMap<>();
try (InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF")) {
Manifest manifest = new Manifest(inputStream);
Attributes attr = manifest.getMainAttributes();
for (String attribute : attributes) {
manifestAttributes.put(attribute, attr.getValue(attribute));
}
} catch (Exception e) {
for (String attribute : attributes) {
if (manifestAttributes.get(attribute) == null) {
manifestAttributes.put(attribute, "Unknown");
}
}
}

return manifestAttributes;
}
}
5 changes: 0 additions & 5 deletions api/metrics-api-jaxrs/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@

<display-name>Hawkular Metrics Rest interface</display-name>

<context-param>
<param-name>hawkular.metrics.version</param-name>
<param-value>${project.version}</param-value>
</context-param>

<context-param>
<param-name>resteasy.providers</param-name>
<param-value>org.hawkular.metrics.api.jaxrs.util.JacksonConfig</param-value>
Expand Down
7 changes: 5 additions & 2 deletions api/metrics-api-jaxrs/src/main/webapp/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@

<body>
<div class="content">
<div class="logo"/>
<div class="logo">
<img src="/hawkular/metrics/static/hawkular_logo.png" alt="Hawkular Logo"/>
</div>
<h1>Hawkular Metrics</h1>
<h3>A time series metrics engine based on Cassandra</h3>
<p>${project.version}</p>

<p id="implementation-version"></p>
<p id="built-from-git-sha1" style="font-size:12px;"></p>

<p id="status">Status: <i>Checking...</i></p>
</div>
</body>
Expand Down

0 comments on commit ed0c216

Please sign in to comment.