Search in sources :

Example 1 with Metrics

use of io.vertigo.commons.analytics.metric.Metrics in project vertigo by KleeGroup.

the class MetricAnalyticsUtil method createMetricDefinitions.

/**
 * Registers all methods annotated with @Metrics
 */
public static List<MetricDefinition> createMetricDefinitions(final String componentId, final Component component, final AopPlugin aopPlugin) {
    Assertion.checkNotNull(component);
    // -- we construct a map of feature by componentId
    final Map<String, String> featureByComponentId = new HashMap<>();
    Home.getApp().getConfig().getModuleConfigs().forEach(moduleConfig -> moduleConfig.getComponentConfigs().forEach(componentConfig -> featureByComponentId.put(componentConfig.getId(), moduleConfig.getName())));
    // 1. search all methods
    return Stream.of(aopPlugin.unwrap(component).getClass().getMethods()).filter(method -> method.isAnnotationPresent(Metrics.class)).map(method -> {
        Assertion.checkArgument(List.class.isAssignableFrom(method.getReturnType()), "metrics supplier methods of class {0} must return a List of Metric instead of {1}", component.getClass(), method.getReturnType());
        Assertion.checkArgument(method.getParameterTypes().length == 0, "metrics supplier methods of class {0} must not have any parameter", component.getClass());
        // -----
        // 2. For each method register a listener
        // we remove # because it doesn't comply with definition naming rule
        final String metricDefinitionName = "MET_" + StringUtil.camelToConstCase(componentId.replaceAll("#", "")) + "$" + StringUtil.camelToConstCase(method.getName());
        return new MetricDefinition(metricDefinitionName, () -> (List<Metric>) ClassUtil.invoke(component, method));
    }).collect(Collectors.toList());
}
Also used : HashMap(java.util.HashMap) ClassUtil(io.vertigo.util.ClassUtil) Collectors(java.util.stream.Collectors) Home(io.vertigo.app.Home) List(java.util.List) Stream(java.util.stream.Stream) StringUtil(io.vertigo.util.StringUtil) Map(java.util.Map) Assertion(io.vertigo.lang.Assertion) Component(io.vertigo.core.component.Component) Metrics(io.vertigo.commons.analytics.metric.Metrics) AopPlugin(io.vertigo.core.component.AopPlugin) MetricDefinition(io.vertigo.commons.analytics.metric.MetricDefinition) Metric(io.vertigo.commons.analytics.metric.Metric) Metrics(io.vertigo.commons.analytics.metric.Metrics) MetricDefinition(io.vertigo.commons.analytics.metric.MetricDefinition) HashMap(java.util.HashMap) List(java.util.List)

Example 2 with Metrics

use of io.vertigo.commons.analytics.metric.Metrics in project vertigo by KleeGroup.

the class SystemMetricsProvider method getSystemMetrics.

@Metrics
public List<Metric> getSystemMetrics() {
    final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    final Runtime runtime = Runtime.getRuntime();
    // ---
    final Metric memoryUsed = buildMetric("memoryUsed", () -> Long.valueOf(runtime.totalMemory() / 1024 / 1024).doubleValue());
    final Metric memoryUsedPercent = buildMetric("memoryUsedPercent", () -> Long.valueOf(runtime.totalMemory()).doubleValue() / runtime.maxMemory() * 100);
    final Metric cpuUsage = buildMetric("cpuUsage", () -> operatingSystemMXBean.getSystemLoadAverage());
    // ---
    return Arrays.asList(memoryUsed, memoryUsedPercent, cpuUsage);
}
Also used : Metric(io.vertigo.commons.analytics.metric.Metric) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Metrics(io.vertigo.commons.analytics.metric.Metrics)

Aggregations

Metric (io.vertigo.commons.analytics.metric.Metric)2 Metrics (io.vertigo.commons.analytics.metric.Metrics)2 Home (io.vertigo.app.Home)1 MetricDefinition (io.vertigo.commons.analytics.metric.MetricDefinition)1 AopPlugin (io.vertigo.core.component.AopPlugin)1 Component (io.vertigo.core.component.Component)1 Assertion (io.vertigo.lang.Assertion)1 ClassUtil (io.vertigo.util.ClassUtil)1 StringUtil (io.vertigo.util.StringUtil)1 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1