Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HWKMETRICS-125 Add JBoss Logging as in all Hawkular components #352

Merged
merged 2 commits into from Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -43,7 +43,7 @@ before_install:
install:
- mvn -version -B
script:
- mvn verify -Dwildfly.logging.console.level=INFO -Djboss-as.logging.console.level=INFO -Dterminal-event.timeout=25 -B
- mvn verify -Dtest.logging.console.level=INFO -Dwildfly.logging.console.level=INFO -Djboss-as.logging.console.level=INFO -Dterminal-event.timeout=25 -B
after_failure: bash .travis.diagnose.sh
after_success:
- PROJECT_VERSION=`mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '\['`
Expand Down
201 changes: 96 additions & 105 deletions api/diff.txt

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions api/metrics-api-common/pom.xml
Expand Up @@ -114,5 +114,19 @@
<scope>provided</scope>
</dependency>

<!-- JBoss Logging Annotations Processor -->

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>
</project>
Expand Up @@ -46,6 +46,8 @@

import org.hawkular.metrics.api.jaxrs.config.Configurable;
import org.hawkular.metrics.api.jaxrs.config.ConfigurationProperty;
import org.hawkular.metrics.api.jaxrs.log.RestLogger;
import org.hawkular.metrics.api.jaxrs.log.RestLogging;
import org.hawkular.metrics.api.jaxrs.util.Eager;
import org.hawkular.metrics.api.jaxrs.util.TestClock;
import org.hawkular.metrics.api.jaxrs.util.VirtualClock;
Expand All @@ -62,8 +64,6 @@
import org.hawkular.metrics.tasks.impl.TaskSchedulerImpl;
import org.hawkular.rx.cassandra.driver.RxSessionImpl;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.Cluster;
Expand All @@ -86,7 +86,7 @@
@ApplicationScoped
@Eager
public class MetricsServiceLifecycle {
private static final Logger LOG = LoggerFactory.getLogger(MetricsServiceLifecycle.class);
private static final RestLogger log = RestLogging.getRestLogger(MetricsServiceLifecycle.class);

/**
* @see #getState()
Expand Down Expand Up @@ -190,17 +190,16 @@ private void startMetricsService() {
if (state != State.STARTING) {
return;
}
LOG.info("Initializing metrics service");
log.infoInitializing();
connectionAttempts++;
try {
session = createSession();
} catch (Exception t) {
Throwable rootCause = Throwables.getRootCause(t);
LOG.warn("Could not connect to Cassandra cluster - assuming its not up yet: " + rootCause
.getLocalizedMessage());
log.warnCouldNotConnectToCassandra(rootCause.getLocalizedMessage());
// cycle between original and more wait time - avoid waiting huge amounts of time
long delay = 1L + ((connectionAttempts - 1L) % 4L);
LOG.warn("[{}] Retrying connecting to Cassandra cluster in [{}]s...", connectionAttempts, delay);
log.warnRetryingConnectingToCassandra(connectionAttempts, delay);
lifecycleExecutor.schedule(this::startMetricsService, delay, SECONDS);
return;
}
Expand All @@ -226,20 +225,20 @@ private void startMetricsService() {
// the registered metrics in various ways such as new REST endpoints, JMX, or via different
// com.codahale.metrics.Reporter instances.
metricsService.startUp(session, keyspace, false, false, new MetricRegistry());
LOG.info("Metrics service started");
log.infoServiceStarted();

initJobs();

state = State.STARTED;
} catch (Exception t) {
LOG.error("An error occurred trying to connect to the Cassandra cluster", t);
} catch (Exception e) {
log.fatalCannotConnectToCassandra(e);
state = State.FAILED;
} finally {
if (state != State.STARTED) {
try {
metricsService.shutdown();
} catch (Exception ignore) {
LOG.error("Could not shutdown the metricsService instance: ", ignore);
} catch (Exception e) {
log.errorCouldNotCloseServiceInstance(e);
}
}
}
Expand All @@ -252,7 +251,7 @@ private Session createSession() {
port = Integer.parseInt(cqlPort);
} catch (NumberFormatException nfe) {
String defaultPort = CASSANDRA_CQL_PORT.defaultValue();
LOG.warn("Invalid CQL port '{}', not a number. Will use a default of {}", cqlPort, defaultPort);
log.warnInvalidCqlPort(cqlPort, defaultPort);
port = Integer.parseInt(defaultPort);
}
clusterBuilder.withPort(port);
Expand Down Expand Up @@ -342,8 +341,8 @@ void destroy() {
Future stopFuture = lifecycleExecutor.submit(this::stopMetricsService);
try {
Futures.get(stopFuture, 1, MINUTES, Exception.class);
} catch (Exception ignore) {
LOG.error("Unexcepted exception while shutting down, ", ignore);
} catch (Exception e) {
log.errorShutdownProblem(e);
}
lifecycleExecutor.shutdown();
}
Expand All @@ -362,4 +361,4 @@ private void stopMetricsService() {
}
}
}
}
}
@@ -0,0 +1,80 @@
/*
* 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.log;

import static org.jboss.logging.Logger.Level.ERROR;
import static org.jboss.logging.Logger.Level.FATAL;
import static org.jboss.logging.Logger.Level.INFO;
import static org.jboss.logging.Logger.Level.WARN;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;
import org.jboss.logging.annotations.ValidIdRange;

/**
* REST interface logging messages.
*
* @author Thomas Segismont
*/
@MessageLogger(projectCode = "HAWKMETRICS")
@ValidIdRange(min = 200000, max = 209999)
public interface RestLogger extends BasicLogger {

@LogMessage(level = INFO)
@Message(id = 200001, value = "Hawkular Metrics starting")
void infoAppStarting();

@LogMessage(level = INFO)
@Message(id = 200002, value = "Initializing metrics service")
void infoInitializing();

@LogMessage(level = WARN)
@Message(id = 200003, value = "Could not connect to Cassandra cluster - assuming its not up yet: %s")
void warnCouldNotConnectToCassandra(String msg);

@LogMessage(level = WARN)
@Message(id = 200004, value = "[%d] Retrying connecting to Cassandra cluster in [%d]s...")
void warnRetryingConnectingToCassandra(Integer connectionAttempts, Long delay);

@LogMessage(level = INFO)
@Message(id = 200005, value = "Metrics service started")
void infoServiceStarted();

@LogMessage(level = FATAL)
@Message(id = 200006, value = "An error occurred trying to connect to the Cassandra cluster")
void fatalCannotConnectToCassandra(@Cause Exception e);

@LogMessage(level = ERROR)
@Message(id = 200007, value = "Could not shutdown the Metrics service instance")
void errorCouldNotCloseServiceInstance(@Cause Exception e);

@LogMessage(level = WARN)
@Message(id = 200008, value = "Invalid CQL port %s, not a number. Will use a default of %s")
void warnInvalidCqlPort(String port, String defaultPort);

@LogMessage(level = ERROR)
@Message(id = 200009, value = "Unexcepted exception while shutting down")
void errorShutdownProblem(@Cause Exception e);

@LogMessage(level = ERROR)
@Message(id = 200010, value = "Failed to process request")
void errorRequestProblem(@Cause Throwable t);
}
@@ -0,0 +1,35 @@
/*
* 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.log;

import org.jboss.logging.Logger;

/**
* Simplify logger lookup.
*
* @author Thomas Segismont
*/
public class RestLogging {

public static RestLogger getRestLogger(Class<?> clazz) {
return Logger.getMessageLogger(RestLogger.class, clazz.getName());
}

private RestLogging() {
// Utility class
}
}
Expand Up @@ -22,27 +22,24 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.hawkular.metrics.api.jaxrs.log.RestLogger;
import org.hawkular.metrics.api.jaxrs.log.RestLogging;
import org.hawkular.metrics.api.jaxrs.model.ApiError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;

/**
* @author jsanda
*/
public class ApiUtils {
private static final Logger LOG = LoggerFactory.getLogger(ApiUtils.class);

private ApiUtils() {
}
private static final RestLogger log = RestLogging.getRestLogger(ApiUtils.class);

public static Response collectionToResponse(Collection<?> collection) {
return collection.isEmpty() ? noContent() : Response.ok(collection).type(MediaType.APPLICATION_JSON).build();
}

public static Response serverError(Throwable t, String message) {
LOG.trace("Server error response", t);
log.errorRequestProblem(t);
String errorMsg = message + ": " + Throwables.getRootCause(t).getMessage();
return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ApiError(errorMsg)).build();
}
Expand Down Expand Up @@ -71,4 +68,8 @@ public static Response badRequest(Throwable t) {
ApiError error = new ApiError(t.getLocalizedMessage());
return badRequest(error);
}

private ApiUtils() {
// Utility class
}
}
10 changes: 10 additions & 0 deletions api/metrics-api-jaxrs-1.1/pom.xml
Expand Up @@ -66,6 +66,16 @@
</dependency>

<!-- AS7 provided -->
<!-- Set the jboss-logging scope here since EAP6.4 BOM does not include jboss-logging -->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<!--
This version is "almost" compatible with the version included in EAP6.4:
we can't use logger methods with primitive arguments
-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
Expand Down
Expand Up @@ -42,23 +42,23 @@
import org.hawkular.metrics.api.jaxrs.handler.TenantsHandler;
import org.hawkular.metrics.api.jaxrs.handler.VirtualClockHandler;
import org.hawkular.metrics.api.jaxrs.interceptor.EmptyPayloadInterceptor;
import org.hawkular.metrics.api.jaxrs.log.RestLogger;
import org.hawkular.metrics.api.jaxrs.log.RestLogging;
import org.hawkular.metrics.api.jaxrs.param.DurationConverter;
import org.hawkular.metrics.api.jaxrs.param.MetricTypeConverter;
import org.hawkular.metrics.api.jaxrs.param.TagsConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Rest app initialization
* Rest app initialization.
*
* @author Heiko W. Rupp
*/
@ApplicationPath("/")
public class HawkularMetricsRestApp extends Application {

private static final Logger logger = LoggerFactory.getLogger(HawkularMetricsRestApp.class);
private static final RestLogger log = RestLogging.getRestLogger(HawkularMetricsRestApp.class);

public HawkularMetricsRestApp() {
logger.info("Hawkular Metrics starting ..");
log.infoAppStarting();
}

/**
Expand Down Expand Up @@ -91,10 +91,10 @@ public Set<Class<?>> getClasses() {
boolean useVirtualClock = Boolean.valueOf(System.getProperty("hawkular.metrics.use-virtual-clock", "false"));

if (useVirtualClock) {
logger.info("Deploying {}", VirtualClockHandler.class);
log.infof("Deploying %s", VirtualClockHandler.class.getCanonicalName());
classes.add(VirtualClockHandler.class);
} else {
logger.info("Virtual clock is disabled");
log.info("Virtual clock is disabled");
}

// Add exception mapper providers
Expand Down
Expand Up @@ -22,16 +22,15 @@
import javax.ws.rs.core.Response.Status;

import org.hawkular.metrics.api.jaxrs.model.ApiError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jboss.logging.Logger;

import com.google.common.base.Throwables;

/**
* @author Jeeva Kandasamy
*/
public class ExceptionMapperUtils {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionMapperUtils.class);
private static final Logger log = Logger.getLogger(ExceptionMapperUtils.class);

public static Response buildResponse(Throwable exception, int statusCode) {
ResponseBuilder responseBuilder = Response.status(statusCode);
Expand All @@ -48,8 +47,8 @@ private static Response buildErrorResponse(Throwable exception, ResponseBuilder
.entity(new ApiError(Throwables.getRootCause(exception).getMessage()))
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
if (LOG.isTraceEnabled()) {
LOG.trace("Turning " + exception.getClass().getCanonicalName() + " into a " + response.getStatus() + " " +
if (log.isTraceEnabled()) {
log.trace("Turning " + exception.getClass().getCanonicalName() + " into a " + response.getStatus() + " " +
"response", exception);
}
return response;
Expand Down