org.finos.legend.sdlc.server.resources.BaseResource Maven / Gradle / Ivy
// Copyright 2020 Goldman Sachs
//
// 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.finos.legend.sdlc.server.resources;
import org.finos.legend.sdlc.server.error.LegendSDLCServerException;
import org.finos.legend.sdlc.server.monitoring.SDLCMetricsHandler;
import org.finos.legend.sdlc.server.tools.StringTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.Status.Family;
public abstract class BaseResource
{
protected T execute(String descriptionForLogging, String metricName, Supplier supplier)
{
Logger logger = getLogger();
SDLCMetricsHandler.operationStart();
boolean isInfoLogging = logger.isInfoEnabled();
String sanitizedDescription = isInfoLogging ? StringTools.sanitizeForLogging(descriptionForLogging, "_", false) : null;
long startTime = System.nanoTime();
if (isInfoLogging)
{
logger.info("Starting {}", sanitizedDescription);
}
try
{
T result = supplier.get();
long endTime = System.nanoTime();
SDLCMetricsHandler.operationComplete(startTime, endTime, metricName);
if (isInfoLogging)
{
long duration = endTime - startTime;
StringBuilder builder = new StringBuilder(sanitizedDescription.length() + 32).append("Finished ").append(sanitizedDescription).append(" (");
StringTools.formatDurationInNanos(builder, duration);
builder.append("s)");
logger.info(builder.toString());
}
return result;
}
catch (LegendSDLCServerException e)
{
long endTime = System.nanoTime();
Status status = e.getStatus();
if ((status != null) && (status.getFamily() == Family.REDIRECTION))
{
SDLCMetricsHandler.operationRedirect(startTime, endTime, metricName);
if (isInfoLogging)
{
long duration = endTime - startTime;
String redirectLocation = String.valueOf(e.getMessage());
StringBuilder builder = new StringBuilder(sanitizedDescription.length() + redirectLocation.length() + 39).append("Redirected ").append(sanitizedDescription).append(" to: ").append(redirectLocation).append(" (");
StringTools.formatDurationInNanos(builder, duration);
builder.append("s)");
logger.info(builder.toString());
}
}
else
{
SDLCMetricsHandler.operationError(startTime, endTime, metricName);
if (logger.isErrorEnabled())
{
long duration = endTime - startTime;
if (sanitizedDescription == null)
{
sanitizedDescription = StringTools.sanitizeForLogging(descriptionForLogging, "_", false);
}
logger.error(buildLoggingErrorMessage(e, sanitizedDescription, duration), e);
}
}
throw e;
}
catch (Throwable t)
{
long endTime = System.nanoTime();
SDLCMetricsHandler.operationError(startTime, endTime, metricName);
if (logger.isErrorEnabled())
{
long duration = endTime - startTime;
if (sanitizedDescription == null)
{
sanitizedDescription = StringTools.sanitizeForLogging(descriptionForLogging, "_", false);
}
logger.error(buildLoggingErrorMessage(t, sanitizedDescription, duration), t);
}
throw t;
}
}
protected R execute(String descriptionForLogging, String metricName, Function super T, R> function, T arg)
{
return execute(descriptionForLogging, metricName, () -> function.apply(arg));
}
protected R execute(String descriptionForLogging, String metricName, BiFunction super T, ? super U, R> function, T arg1, U arg2)
{
return execute(descriptionForLogging, metricName, () -> function.apply(arg1, arg2));
}
protected void execute(String descriptionForLogging, String metricName, Runnable runnable)
{
execute(descriptionForLogging, metricName, () ->
{
runnable.run();
return null;
});
}
protected void execute(String descriptionForLogging, String metricName, Consumer super T> consumer, T arg)
{
execute(descriptionForLogging, metricName, () ->
{
consumer.accept(arg);
return null;
});
}
protected void execute(String descriptionForLogging, String metricName, BiConsumer super T, ? super U> consumer, T arg1, U arg2)
{
execute(descriptionForLogging, metricName, () ->
{
consumer.accept(arg1, arg2);
return null;
});
}
protected T executeWithLogging(String description, Supplier supplier)
{
return execute(description, null, supplier);
}
protected R executeWithLogging(String description, Function super T, R> function, T arg)
{
return executeWithLogging(description, () -> function.apply(arg));
}
protected R executeWithLogging(String description, BiFunction super T, ? super U, R> function, T arg1, U arg2)
{
return executeWithLogging(description, () -> function.apply(arg1, arg2));
}
protected void executeWithLogging(String description, Runnable runnable)
{
executeWithLogging(description, () ->
{
runnable.run();
return null;
});
}
protected void executeWithLogging(String description, Consumer super T> consumer, T arg)
{
executeWithLogging(description, () ->
{
consumer.accept(arg);
return null;
});
}
protected void executeWithLogging(String description, BiConsumer super T, ? super U> consumer, T arg1, U arg2)
{
executeWithLogging(description, () ->
{
consumer.accept(arg1, arg2);
return null;
});
}
protected Logger getLogger()
{
return LoggerFactory.getLogger(getClass());
}
private String buildLoggingErrorMessage(Throwable t, String description, long durationNanos)
{
StringBuilder builder = new StringBuilder(description.length() + 29).append("Error ").append(description).append(" (");
StringTools.formatDurationInNanos(builder, durationNanos);
builder.append("s)");
String message = t.getMessage();
if (message != null)
{
builder.append(": ").append(StringTools.replaceVerticalWhitespace(message, " ", true));
}
return builder.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy