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

Refactor sdlc metrics handler - first phase #343

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.prometheus.client.Collector;
import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;
import io.prometheus.client.SimpleTimer;
import io.prometheus.client.Summary;
import org.eclipse.collections.api.map.ConcurrentMutableMap;
Expand Down Expand Up @@ -53,26 +54,77 @@ protected Counter createNewMetric(String name, String help)
}
};

// ----------------------------------------- NEW IMPLEMENTATION -----------------------------------------

private static final Counter OPERATION_COUNTER = Counter
.build("sdlc_operations_count", "Counter of SDLC operations started")
.labelNames("operation_name")
.register();

private static final Histogram OPERATION_HISTOGRAM = Histogram
.build("sdlc_operations_duration", "Duration histogram for SDLC operations terminating with an error, success or redirect")
.labelNames("operation_name", "status")
.register();

public static void observeOperationComplete(long startNanos, long endNanos, String operationName)
{
operationTermination(startNanos, endNanos, operationName, "COMPLETE");
}

public static void observeOperationRedirect(long startNanos, long endNanos, String operationName)
{
operationTermination(startNanos, endNanos, operationName, "REDIRECT");
}

public static void observeOperationError(long startNanos, long endNanos, String operationName)
{
operationTermination(startNanos, endNanos, operationName, "ERROR");
}

private static void operationTermination(long startNanos, long endNanos, String operationName, String status)
{
double duration = SimpleTimer.elapsedSecondsFromNanos(startNanos, endNanos);
OPERATION_HISTOGRAM.labels(operationName != null ? operationName : "unknown", status).observe(duration);
}

public static void observeOperationStart()
{
incrementOperationCounter("sdlc operation start");
}

public static void incrementOperationCounter(String name)
{
OPERATION_COUNTER.labels(name != null ? name : "unknown").inc();
}


// -------------------------------------- END OF NEW IMPLEMENTATION -------------------------------------

@Deprecated
public static void operationStart()
{
OPERATION_START_COUNTER.inc();
}

@Deprecated
public static void operationComplete(long startNanos, long endNanos, String durationMetricName)
{
operationTermination(startNanos, endNanos, OPERATION_COMPLETE_SUMMARY, durationMetricName);
}

@Deprecated
public static void operationRedirect(long startNanos, long endNanos, String durationMetricName)
{
operationTermination(startNanos, endNanos, OPERATION_REDIRECT_SUMMARY, durationMetricName);
}

@Deprecated
public static void operationError(long startNanos, long endNanos, String durationMetricName)
{
operationTermination(startNanos, endNanos, OPERATION_ERROR_SUMMARY, durationMetricName);
}

@Deprecated
private static void operationTermination(long startNanos, long endNanos, Summary durationSummary, String durationMetricName)
{
double duration = SimpleTimer.elapsedSecondsFromNanos(startNanos, endNanos);
Expand All @@ -87,6 +139,7 @@ private static void operationTermination(long startNanos, long endNanos, Summary
}
}

@Deprecated
public static void incrementCounter(String name)
{
Counter counter = ADDITIONAL_COUNTERS.getOrCreate(name);
Expand All @@ -96,18 +149,21 @@ public static void incrementCounter(String name)
}
}

@Deprecated
private static Summary createDurationSummary(String name, String help)
{
return Summary.build(name, help)
.quantile(0.5, 0.05).quantile(0.9, 0.01).quantile(0.99, 0.001)
.register();
}

@Deprecated
private static Counter createCounter(String name, String help)
{
return Counter.build(name, help).register();
}

@Deprecated
private abstract static class MetricsRegistry<T extends Collector>
{
private static final String METRIC_PREFIX = "sdlc_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public abstract class BaseResource
protected <T> T execute(String descriptionForLogging, String metricName, Supplier<T> supplier)
{
Logger logger = getLogger();
SDLCMetricsHandler.observeOperationStart();
SDLCMetricsHandler.operationStart();
boolean isInfoLogging = logger.isInfoEnabled();
String sanitizedDescription = isInfoLogging ? StringTools.sanitizeForLogging(descriptionForLogging, "_", false) : null;
Expand All @@ -45,6 +46,7 @@ protected <T> T execute(String descriptionForLogging, String metricName, Supplie
{
T result = supplier.get();
long endTime = System.nanoTime();
SDLCMetricsHandler.observeOperationComplete(startTime, endTime, metricName);
SDLCMetricsHandler.operationComplete(startTime, endTime, metricName);
if (isInfoLogging)
{
Expand All @@ -62,6 +64,7 @@ protected <T> T execute(String descriptionForLogging, String metricName, Supplie
Status status = e.getStatus();
if ((status != null) && (status.getFamily() == Family.REDIRECTION))
{
SDLCMetricsHandler.observeOperationRedirect(startTime, endTime, metricName);
SDLCMetricsHandler.operationRedirect(startTime, endTime, metricName);
if (isInfoLogging)
{
Expand All @@ -75,6 +78,7 @@ protected <T> T execute(String descriptionForLogging, String metricName, Supplie
}
else
{
SDLCMetricsHandler.observeOperationError(startTime, endTime, metricName);
SDLCMetricsHandler.operationError(startTime, endTime, metricName);
if (logger.isErrorEnabled())
{
Expand All @@ -91,6 +95,7 @@ protected <T> T execute(String descriptionForLogging, String metricName, Supplie
catch (Throwable t)
{
long endTime = System.nanoTime();
SDLCMetricsHandler.observeOperationError(startTime, endTime, metricName);
SDLCMetricsHandler.operationError(startTime, endTime, metricName);
if (logger.isErrorEnabled())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ private static String getRetryableExceptionLogMessage(GitLabApiException e, int
private static void noteRetryableException()
{
SDLCMetricsHandler.incrementCounter(RETRY_METRIC);
SDLCMetricsHandler.incrementOperationCounter(RETRY_METRIC);
}

public static boolean branchExists(GitLabApi api, Object projectIdOrPath, String branchName) throws GitLabApiException
Expand Down