Skip to content

Commit

Permalink
Cleaning up Metrics Manager and adding some warnings/exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
FieldFlux committed Jun 18, 2015
1 parent 764c26d commit 2ab2664
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -56,7 +56,12 @@ public class MetricsManager
private boolean isInitialized = false;

@Config("orbit.metrics.reporters")
private List<ReporterConfig> reporterConfigs;
private List<ReporterConfig> reporterConfigs = new ArrayList<ReporterConfig>();

public static String sanitizeMetricName(String name)
{
return name.replaceAll("[\\[\\]\\.\\\\/]", ""); //strip illegal characters
}


public synchronized void initializeMetrics(String uniqueId)
Expand Down Expand Up @@ -87,15 +92,14 @@ public void registerExportedMetrics(Object obj)
{
if (field.isAnnotationPresent(ExportMetric.class))
{
final ExportMetric annotation = field.getAnnotation(ExportMetric.class);
//check to see if the field is accessible.
if (!field.isAccessible())
{
continue;
throw new IllegalStateException("Field " + field.getName() + " in object " + obj.getClass().getName() + " is marked for Metrics Export but the field is not accessible");
}
final ExportMetric annotation = field.getAnnotation(ExportMetric.class);
final String gaugeName = MetricRegistry.name(obj.getClass(), annotation.name());

registry.register(gaugeName, (Gauge<Object>) () -> {
registerGauge(annotation, obj, () -> {
try
{
Object value = field.get(obj);
Expand All @@ -106,8 +110,6 @@ public void registerExportedMetrics(Object obj)
throw new IllegalStateException("Field " + field.getName() + " was inaccessible: " + iae.getMessage());
}
});

logger.debug("Registered new metric for field " + field.getName() + " in class " + obj.getClass());
}
}

Expand All @@ -118,14 +120,12 @@ public void registerExportedMetrics(Object obj)
//methods declared as metrics must not accept parameters.
if (method.getParameterCount() > 0)
{
continue;
throw new IllegalArgumentException("Method " + method.getName() + " in object " + obj.getClass().getName() + " is marked for Metrics Export but the method definition contains parameters.");
}

final ExportMetric annotation = method.getAnnotation(ExportMetric.class);
final String gaugeName = MetricRegistry.name(obj.getClass(), annotation.name());

registry.register(gaugeName, (Gauge<Object>) () -> {

registerGauge(annotation, obj, () -> {
try
{
Object value = method.invoke(obj, new Object[0]);
Expand All @@ -140,9 +140,23 @@ public void registerExportedMetrics(Object obj)
throw new UncheckedException("Invocation of method " + method.getName() + " failed", ite.getTargetException());
}
});

logger.debug("Registered new metric for method " + method.getName() + " in class " + obj.getClass());
}
}
}

private void registerGauge(ExportMetric annotation, Object obj, Supplier<Object> metricSupplier)
{
final String gaugeName = MetricRegistry.name(obj.getClass(), annotation.name());

registry.register(gaugeName, (Gauge<Object>) () -> {

Object value = metricSupplier.get();
return value;
});

if (logger.isDebugEnabled())
{
logger.debug("Registered new metric " + annotation.name());
}
}
}
5 changes: 2 additions & 3 deletions actors/stage/src/main/java/com/ea/orbit/actors/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,8 @@ public Task<?> start()

if (metricsManager != null)
{
String cleanRuntimeId = runtimeIdentity().replaceAll("[\\[\\]\\.\\\\/]", ""); //strip illegal characters
cleanRuntimeId = cleanRuntimeId.replace("Orbit","");
cleanRuntimeId = getClusterName() + "." + getNodeName() + "." + cleanRuntimeId;
String cleanRuntimeId = runtimeIdentity().replace("Orbit", "");
cleanRuntimeId = MetricsManager.sanitizeMetricName(getClusterName()) + "." + MetricsManager.sanitizeMetricName(getNodeName()) + "." + MetricsManager.sanitizeMetricName(cleanRuntimeId);
metricsManager.initializeMetrics(cleanRuntimeId);
metricsManager.registerExportedMetrics(execution);
}
Expand Down

0 comments on commit 2ab2664

Please sign in to comment.