Java Code Examples for com.alipay.lookout.api.composite.MixinMetric#gauge()

The following examples show how to use com.alipay.lookout.api.composite.MixinMetric#gauge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JvmInfoMetricsImporterTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Test
public void testGcInfo() {
    Registry registry = new DefaultRegistry();
    JvmGcMetricsImporter jvmGcMetricsImporter = new JvmGcMetricsImporter();
    jvmGcMetricsImporter.register(registry);

    Id id = registry.createId("jvm.gc");
    MixinMetric mixin = registry.mixinMetric(id);
    Gauge gauge = mixin.gauge("young.count", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("young.time", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("old.count", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("old.time", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
}
 
Example 2
Source File: JvmInfoMetricsImporterTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Test
public void testJvmClassInfo() {
    Registry registry = new DefaultRegistry();
    JvmClassesMetricsImporter importer = new JvmClassesMetricsImporter();
    importer.register(registry);

    Id id = registry.createId("jvm.classes");
    MixinMetric mixin = registry.mixinMetric(id);

    Gauge gauge = mixin.gauge("unloaded", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("total", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("loaded", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
}
 
Example 3
Source File: NetTrafficMetricsImporter.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    for (final Map.Entry<String, Long[]> entry : statByFace.entrySet()) {
        final String face = entry.getKey();
        Id id = registry.createId("os.net.stats").withTag("intfc", face);
        MixinMetric mixin = registry.mixinMetric(id);

        for (int i = 0; i < entry.getValue().length; i++) {
            final int index = i;
            mixin.gauge(FIELDS[index], new Gauge<Long>() {
                @Override
                public Long value() {
                    loadIfNessesary();
                    return statByFace.get(face)[index];
                }
            });
        }
    }
}
 
Example 4
Source File: JvmClassesMetricsImporter.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
public void register(Registry registry) {
    Id id = registry.createId("jvm.classes");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("unloaded", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getClassLoadingMXBean().getUnloadedClassCount();
        }
    });
    mixin.gauge("total", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getClassLoadingMXBean().getTotalLoadedClassCount();
        }
    });
    mixin.gauge("loaded", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return ManagementFactory.getClassLoadingMXBean().getLoadedClassCount();
        }
    });

}
 
Example 5
Source File: JvmInfoMetricsImporterTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreadsInfo() {
    Registry registry = new DefaultRegistry();
    JvmThreadsMetricsImporter importer = new JvmThreadsMetricsImporter();
    importer.register(registry);

    Id id = registry.createId("jvm.threads");
    MixinMetric mixin = registry.mixinMetric(id);
    Gauge gauge = mixin.gauge("peak", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("daemon", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("totalStarted", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("active", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
}
 
Example 6
Source File: JvmInfoMetricsImporterTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Test
public void testBaseInfo() {
    Registry registry = new DefaultRegistry();
    BasicMetricsImporter jvmGcMetricsImporter = new BasicMetricsImporter();
    jvmGcMetricsImporter.register(registry);

    Id id = registry.createId("instance");
    MixinMetric mixin = registry.mixinMetric(id);

    Gauge gauge = mixin.gauge("mem.total", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("mem.free", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("processors", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("uptime", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("systemload.average", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
}
 
Example 7
Source File: JvmMemoryMetricsImporter.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
private void heapImport(MixinMetric mixin) {
    mixin.gauge("heap.init", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getInit();
        }
    });
    mixin.gauge("heap.used", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
        }
    });
    mixin.gauge("heap.committed", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted();
        }
    });
    mixin.gauge("heap.max", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
        }
    });
}
 
Example 8
Source File: JvmThreadsMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {
    Id id = registry.createId("jvm.threads");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("peak", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return ManagementFactory.getThreadMXBean().getPeakThreadCount();
        }
    });
    mixin.gauge("daemon", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return ManagementFactory.getThreadMXBean().getDaemonThreadCount();
        }
    });
    mixin.gauge("totalStarted", new Gauge<Long>() {
        @Override
        public Long value() {
            return ManagementFactory.getThreadMXBean().getTotalStartedThreadCount();
        }
    });
    mixin.gauge("active", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return ManagementFactory.getThreadMXBean().getThreadCount();
        }
    });

}
 
Example 9
Source File: JvmInfoMetricsImporterTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Test
public void testMemInfo() {
    Registry registry = new DefaultRegistry();
    JvmMemoryMetricsImporter jvmMemoryMetricsImporter = new JvmMemoryMetricsImporter();
    jvmMemoryMetricsImporter.register(registry);

    Id id = registry.createId("jvm.memory");
    MixinMetric mixin = registry.mixinMetric(id);
    Gauge gauge = mixin.gauge("nonheap.init", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("nonheap.used", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("nonheap.committed", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("nonheap.max", null);
    System.out.println(gauge.value().longValue());
    Assert.assertTrue(gauge.value().longValue() >= -1);

    gauge = mixin.gauge("heap.init", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("heap.used", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("heap.committed", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("heap.max", null);
    Assert.assertTrue(gauge.value().longValue() >= -1);

    gauge = mixin.gauge("codecache.init", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("codecache.used", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("codecache.committed", null);
    Assert.assertTrue(gauge.value().longValue() >= 0);
    gauge = mixin.gauge("codecache.max", null);
    Assert.assertTrue(gauge.value().longValue() >= -1);
}
 
Example 10
Source File: BasicMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {
    MixinMetric mixin = registry.mixinMetric(registry.createId("instance"));
    mixin.gauge("mem.total", new Gauge<Long>() {
        @Override
        public Long value() {
            return Runtime.getRuntime().totalMemory();
        }
    });

    mixin.gauge("mem.free", new Gauge<Long>() {
        @Override
        public Long value() {
            return Runtime.getRuntime().freeMemory();
        }
    });

    mixin.gauge("processors", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return Integer.valueOf(Runtime.getRuntime().availableProcessors());
        }
    });

    mixin.gauge("uptime", new Gauge<Long>() {
        @Override
        public Long value() {
            return Long.valueOf(System.currentTimeMillis() - timestamp);
        }
    });

    mixin.gauge("systemload.average", new Gauge<Double>() {
        @Override
        public Double value() {
            return Double.valueOf(ManagementFactory.getOperatingSystemMXBean()
                .getSystemLoadAverage());
        }
    });
}
 
Example 11
Source File: FileSystemSpaceMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {

    Id basicId = registry.createId("instance.file.system");

    File[] roots = File.listRoots();
    // For each filesystem root;
    //TODO dynamic  File.listRoots() and for each;
    for (final File root : roots) {
        Id id = basicId.withTag("root", root.getAbsolutePath()).withTag(
            LookoutConstants.LOW_PRIORITY_TAG);
        MixinMetric mixin = registry.mixinMetric(id);
        mixin.gauge("total.space", new Gauge<Long>() {
            @Override
            public Long value() {
                return root.getTotalSpace();
            }
        });

        mixin.gauge("free.space", new Gauge<Long>() {
            @Override
            public Long value() {
                return root.getFreeSpace();
            }
        });
        mixin.gauge("usabe.space", new Gauge<Long>() {
            @Override
            public Long value() {
                return root.getUsableSpace();
            }
        });
    }
}
 
Example 12
Source File: JvmGcMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registry registry) {
    Id id = registry.createId("jvm.gc");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("young.count", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.youngGCCount;
        }
    });
    mixin.gauge("young.time", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.youngGCTime;
        }
    });
    mixin.gauge("old.count", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.oldGCCount;
        }
    });
    mixin.gauge("old.time", new Gauge<Long>() {
        @Override
        public Long value() {
            GcInfo.refresh();
            return GcInfo.oldGCTime;
        }
    });
}
 
Example 13
Source File: JvmMemoryMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
private void codeCacheImport(MixinMetric mixin) {
    final MemoryPoolMXBean codeCacheMXBean = getCodeCacheMXBean();
    if (codeCacheMXBean == null) {
        LOGGER.info("can't get code cache MemoryPoolMXBean, won't report code cache usage.");
        return;
    }

    mixin.gauge("codecache.init", new Gauge<Long>() {
        @Override
        public Long value() {
            return codeCacheMXBean.getUsage().getInit();
        }
    });
    mixin.gauge("codecache.used", new Gauge<Long>() {
        @Override
        public Long value() {
            return codeCacheMXBean.getUsage().getUsed();
        }
    });
    mixin.gauge("codecache.committed", new Gauge<Long>() {
        @Override
        public Long value() {
            return codeCacheMXBean.getUsage().getCommitted();
        }
    });
    mixin.gauge("codecache.max", new Gauge<Long>() {
        @Override
        public Long value() {
            return codeCacheMXBean.getUsage().getMax();
        }
    });
}
 
Example 14
Source File: JvmMemoryMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
private void nonheapImport(MixinMetric mixin) {

        mixin.gauge("nonheap.init", new Gauge<Long>() {
            @Override
            public Long value() {
                return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getInit();
            }
        });
        mixin.gauge("nonheap.used", new Gauge<Long>() {
            @Override
            public Long value() {
                return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();
            }
        });
        mixin.gauge("nonheap.committed", new Gauge<Long>() {
            @Override
            public Long value() {
                return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getCommitted();
            }
        });
        mixin.gauge("nonheap.max", new Gauge<Long>() {
            @Override
            public Long value() {
                return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getMax();
            }
        });
    }
 
Example 15
Source File: SystemLoadMetricsImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    Id id = registry.createId("os.systemload.average");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("1min", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return loadAvg[LoadAvg.ONE_MIN.ordinal()];
        }
    });

    mixin.gauge("5min", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return loadAvg[LoadAvg.FIVE_MIN.ordinal()];
        }
    });

    mixin.gauge("15min", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return loadAvg[LoadAvg.FIFTEEN_MIN.ordinal()];
        }
    });
}
 
Example 16
Source File: DiskUsageMetricsImporter.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    for (Map.Entry<String, DiskUsage> entry : diskUsageByDevice.entrySet()) {
        final String device = entry.getKey();
        Id id = registry.createId("os.disk.usage");
        id = id.withTag("device", device);
        id = id.withTag("root", diskUsageByDevice.get(device).fsFile);
        id = id.withTag("type", diskUsageByDevice.get(device).fsVfType);
        MixinMetric mixin = registry.mixinMetric(id);

        mixin.gauge("total.bytes", new Gauge<Long>() {
            @Override
            public Long value() {
                loadIfNessesary();
                return diskUsageByDevice.get(device).totalBytes;
            }
        });

        mixin.gauge("used.bytes", new Gauge<Long>() {
            @Override
            public Long value() {
                loadIfNessesary();
                return diskUsageByDevice.get(device).usedBytes;
            }
        });

        mixin.gauge("percent.used", new Gauge<Float>() {
            @Override
            public Float value() {
                loadIfNessesary();
                if (diskUsageByDevice.get(device).totalBytes == 0) {
                    return 0.0f;
                } else {
                    float ratio = (float) diskUsageByDevice.get(device).usedBytes
                                  / diskUsageByDevice.get(device).totalBytes;
                    return NumFormatUtils.formatFloat(ratio);
                }
            }
        });

    }
}
 
Example 17
Source File: IOStatsMetricsImporter.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    for (Map.Entry<String, Float[]> entry : statsByDevice.entrySet()) {
        final String device = entry.getKey();
        Id id = registry.createId("os.io.stats");
        id = id.withTag("device", device);
        MixinMetric mixin = registry.mixinMetric(id);

        mixin.gauge("svctm", new Gauge<Float>() {
            @Override
            public Float value() {
                loadIfNessesary();
                return statsByDevice.get(device)[IOStats.SVCTM.ordinal()];
            }
        });

        mixin.gauge("r_await", new Gauge<Float>() {
            @Override
            public Float value() {
                loadIfNessesary();
                return statsByDevice.get(device)[IOStats.R_AWAIT.ordinal()];
            }
        });

        mixin.gauge("w_await", new Gauge<Float>() {
            @Override
            public Float value() {
                loadIfNessesary();
                return statsByDevice.get(device)[IOStats.W_AWAIT.ordinal()];
            }
        });

        mixin.gauge("await", new Gauge<Float>() {
            @Override
            public Float value() {
                loadIfNessesary();
                return statsByDevice.get(device)[IOStats.AWAIT.ordinal()];
            }
        });

        mixin.gauge("util", new Gauge<Float>() {
            @Override
            public Float value() {
                loadIfNessesary();
                return statsByDevice.get(device)[IOStats.UTIL.ordinal()];
            }
        });
    }
}
 
Example 18
Source File: CpuUsageMetricsImporter.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRegister(Registry registry) {
    Id id = registry.createId("os.cpu");
    MixinMetric mixin = registry.mixinMetric(id);

    mixin.gauge("user", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.USER.ordinal()];
        }
    });

    mixin.gauge("nice", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.NICE.ordinal()];
        }
    });

    mixin.gauge("system", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.SYSTEM.ordinal()];
        }
    });

    mixin.gauge("idle", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.IDLE.ordinal()];
        }
    });

    mixin.gauge("iowait", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.IOWAIT.ordinal()];
        }
    });

    mixin.gauge("irq", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.IRQ.ordinal()];
        }
    });

    mixin.gauge("softirq", new Gauge<Float>() {
        @Override
        public Float value() {
            loadIfNessesary();
            return cpuUsage[CpuUsage.SOFTIRQ.ordinal()];
        }
    });
}
 
Example 19
Source File: RegistryTest.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoopRegistry() throws Exception {
    final Registry registry = NoopRegistry.INSTANCE;
    //counter
    Counter counter = registry.counter(registry.createId("counter"));
    counter.inc();
    counter.dec();
    counter.count();
    //timer
    Timer timer = registry.timer(registry.createId("timer"));
    timer.record(2, TimeUnit.SECONDS);
    final AtomicInteger x = new AtomicInteger(0);

    timer.record(new Runnable() {
        @Override
        public void run() {
            x.incrementAndGet();
        }
    });
    timer.record(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            x.incrementAndGet();
            return null;
        }
    });
    Assert.assertEquals(2, x.intValue());

    //ds
    DistributionSummary ds = registry.distributionSummary(registry.createId("ds"));
    ds.record(1);
    ds.count();
    ds.totalAmount();

    //gauge
    registry.gauge(registry.createId("gauge"), new Gauge<Integer>() {
        @Override
        public Integer value() {
            return 3;
        }
    });
    registry.removeMetric(registry.createId("gauge"));

    //info

    registry.info(registry.createId("jvm.system.properties"), new Info<Map>() {
        @Override
        public Map value() {
            return System.getProperties();
        }
    });

    registry.info(registry.createId("sofa.config"),
        new AutoPollFriendlyInfo<Map<String, String>>() {

            @Override
            public AutoPollSuggestion autoPollSuggest() {
                // AutoPollSuggestion.NEVEL_AUTO_POLL;
                //AutoPollSuggestion.POLL_WHEN_UPDATED;
                //new AutoPollSuggestion(24, TimeUnit.DAYS);
                return AutoPollSuggestion.POLL_WHEN_UPDATED;
            }

            @Override
            public long lastModifiedTime() {
                return -1L;
            }

            @Override
            public Map<String, String> value() {
                return new HashMap();
            }
        });

    //minxin

    MixinMetric mixinMetric = registry.mixinMetric(registry.createId("mixinMetric"));
    Counter compCounter = mixinMetric.counter("xx");
    Timer compTimer = mixinMetric.timer("time");
    DistributionSummary compDs = mixinMetric.distributionSummary("ds");
    mixinMetric.gauge("compGauge", new Gauge<Long>() {
        @Override
        public Long value() {
            return 1l;
        }
    });
}
 
Example 20
Source File: AbstractRegistry.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
@Override
public void registerExtendedMetrics() {
    for (MetricsImporter metricsImporter : MetricsImporterLocator.locate()) {
        metricsImporter.register(this);
    }

    MixinMetric mixinMetric = mixinMetric(createId("lookout.reg"));
    mixinMetric.gauge("size", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return metrics.size();
        }
    });
    mixinMetric.gauge("max.size", new Gauge<Integer>() {
        @Override
        public Integer value() {
            return config.getInt(LOOKOUT_MAX_METRICS_NUMBER,
                MetricConfig.DEFAULT_MAX_METRICS_NUM);
        }
    });

    // register config info
    info(createId("lookout.config"), new AutoPollFriendlyInfo<Map<String, Object>>() {
        @Override
        public AutoPollSuggestion autoPollSuggest() {
            return AutoPollSuggestion.POLL_WHEN_UPDATED;
        }

        @Override
        public long lastModifiedTime() {
            return -1;
        }

        @Override
        public Map<String, Object> value() {
            if (config instanceof MapConfiguration) {
                return ((MapConfiguration) config).getMap();
            }
            return null;
        }
    });
}