org.springframework.boot.actuate.metrics.Metric Java Examples

The following examples show how to use org.springframework.boot.actuate.metrics.Metric. 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: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
/***
 * Spring Boot Standard interface Implementation
 * @param metricName name
 * @return Converted Actuator Metric
 */
@Override
public Metric<?> findOne(String metricName) {
    if (StringUtils.isBlank(metricName)) {
        return null;
    }
    //Standard Actuator Implementation
    Id id = this.springBootActuatorRegistry.createId(metricName);
    List<Metric> metricList = findMetricsById(id);
    if (metricList != null && metricList.size() > 0) {
        //Converted to lookout Metrics,default first
        return metricList.get(0);
    } else {
        return null;
    }
}
 
Example #2
Source File: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<Metric<?>> findAll() {
    final Iterator<com.alipay.lookout.api.Metric> lookoutIt = this.springBootActuatorRegistry
        .iterator();
    return new Iterable<Metric<?>>() {
        @Override
        public Iterator<Metric<?>> iterator() {
            List<Metric<?>> metricsRes = new LinkedList<Metric<?>>();
            while (lookoutIt.hasNext()) {
                com.alipay.lookout.api.Metric lookoutMetric = lookoutIt.next();
                Id id = lookoutMetric.id();
                Collection<Metric> metricList = findMetricsById(id);
                if (metricList != null && metricList.size() > 0) {
                    for (Metric metric : metricList) {
                        metricsRes.add(metric);
                    }
                }
            }
            return metricsRes.iterator();
        }
    };
}
 
Example #3
Source File: LookoutRegistryMetricReaderTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
/**
 * Method: findOne(String metricName)
 */
@Test
public void testFindOne() throws Exception {
    assertNull(lookoutRegistryMetricReader.findOne(""));
    assertTrue(this.metricReaderPublicMetrics != null
               && this.metricReaderPublicMetrics.size() > 0);
    assertNotNull(testRestTemplate);
    String endpointId = "mappings";
    String restUrl = urlHttpPrefix + "/" + endpointId;
    ResponseEntity<String> response = testRestTemplate.getForEntity(restUrl, String.class);
    assertTrue(StringUtils.isNotBlank(response.getBody()));
    //
    Metric metric = this.lookoutRegistryMetricReader
        .findOne(LookoutSpringBootMetricsImpl.LOOKOUT_GAUGE_PREFIX + "response." + endpointId);
    assertTrue(metric != null);
}
 
Example #4
Source File: LookoutSpringBootMetricsImplTest.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
/**
 * Method: increment(String metricName)
 * Method: decrement(String metricName)
 */
@Test
public void testIncrement() throws Exception {
    String originName = "metricName";
    String metricName = originName;
    this.counterService.increment(originName);
    metricName = LookoutSpringBootMetricsImpl.LOOKOUT_COUNTER_PREFIX + metricName;
    Metric metric = this.lookoutRegistryMetricReader.findOne(metricName);
    assertTrue(metric != null);
    assertEquals(1L, metric.getValue());
    //decrement
    this.counterService.decrement(originName);
    metric = this.lookoutRegistryMetricReader.findOne(metricName);
    assertTrue(metric != null);
    assertEquals(0L, metric.getValue());
}
 
Example #5
Source File: MossMetricsEndpoint.java    From Moss with Apache License 2.0 5 votes vote down vote up
/**
 * Get GC Info兼容Java 8和Java 10
 * @param haloMetricResponse
 * @param metric
 */
private void getGcInfo(HaloMetricResponse haloMetricResponse, Metric<?> metric) {
    if(GC_PS_MARKSWEEP_COUNT.equals(metric.getName())||"gc.g1_old_generation.count".equals(metric.getName())){
        haloMetricResponse.setGcPsMarksweepCount(String.valueOf(metric.getValue()));
    }
    if(GC_PS_MARKSWEEP_TIME.equals(metric.getName())||"gc.g1_old_generation.time".equals(metric.getName())){
        haloMetricResponse.setGcPsMarksweepTime(String.valueOf(metric.getValue()));
    }
    if(GC_PS_SCAVENGE_TIME.equals(metric.getName())||"gc.g1_young_generation.time".equals(metric.getName())){
        haloMetricResponse.setGcPsScavengeTime(String.valueOf(metric.getValue()));
    }
    if(GC_PS_SCAVENGE_COUNT.equals(metric.getName())||"gc.g1_young_generation.count".equals(metric.getName())){
        haloMetricResponse.setGcPsScavengeCount(String.valueOf(metric.getValue()));
    }
}
 
Example #6
Source File: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
private List<Metric> findMetricsById(Id id) {
    com.alipay.lookout.api.Metric lookoutMetric = this.springBootActuatorRegistry.get(id);
    if (lookoutMetric == null) {
        return null;
    }
    Indicator indicator = lookoutMetric.measure();
    return IndicatorConvert.convertFromIndicator(indicator);
}
 
Example #7
Source File: LookoutRegistryMetricReaderTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * Method: findAll()
 */
@Test
public void testFindAll() throws Exception {
    Iterable<Metric<?>> metricIterable = this.lookoutRegistryMetricReader.findAll();
    List<Metric> metricList = new ArrayList<Metric>();
    for (Metric<?> metric : metricIterable) {
        metricList.add(metric);
    }
    assertTrue(metricList.size() > 0);
}
 
Example #8
Source File: LookoutSpringBootMetricsImplTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * Method: reset(String metricName)
 */
@Test
public void testReset() throws Exception {
    String originName = "metricName1";
    String metricName = originName;
    this.counterService.increment(originName);
    metricName = LookoutSpringBootMetricsImpl.LOOKOUT_COUNTER_PREFIX + metricName;
    Metric metric = this.lookoutRegistryMetricReader.findOne(metricName);
    assertTrue(metric != null);
    this.counterService.reset(originName);
    assertTrue(this.lookoutRegistryMetricReader.findOne(metricName) == null);
}
 
Example #9
Source File: LookoutSpringBootMetricsImplTest.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * Method: submit(String metricName, double value)
 */
@Test
public void testSubmit() throws Exception {
    String gaugeName = "gaugeName";
    double value = 10;
    this.gaugeService.submit(gaugeName, value);
    //get
    gaugeName = LookoutSpringBootMetricsImpl.LOOKOUT_GAUGE_PREFIX + gaugeName;
    Metric metric = this.lookoutRegistryMetricReader.findOne(gaugeName);
    assertEquals(value, metric.getValue());
}
 
Example #10
Source File: SpectatorMetricReader.java    From spring-cloud-netflix-contrib with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Metric<?>> findAll() {
	return stream(registry.spliterator(), false)
			.flatMap(
					metric -> stream(metric.measure().spliterator(), false).map(
							measure -> new Metric<>(asHierarchicalName(measure.id()), measure.value())))
			.sorted((m1, m2) -> m1.getName().compareTo(m2.getName())).collect(Collectors.toList());
}
 
Example #11
Source File: SpringBootMetricsCollector.java    From client_java with Apache License 2.0 5 votes vote down vote up
@Override
public List<MetricFamilySamples> collect() {
  ArrayList<MetricFamilySamples> samples = new ArrayList<MetricFamilySamples>();
  for (PublicMetrics publicMetrics : this.publicMetrics) {
    for (Metric<?> metric : publicMetrics.metrics()) {
      String name = Collector.sanitizeMetricName(metric.getName());
      double value = metric.getValue().doubleValue();
      MetricFamilySamples metricFamilySamples = new MetricFamilySamples(
              name, Type.GAUGE, name, Collections.singletonList(
              new MetricFamilySamples.Sample(name, Collections.<String>emptyList(), Collections.<String>emptyList(), value)));
      samples.add(metricFamilySamples);
    }
  }
  return samples;
}
 
Example #12
Source File: MossMetricsEndpoint.java    From Moss with Apache License 2.0 4 votes vote down vote up
@Override
public HaloMetricResponse invoke() {
    HaloMetricResponse haloMetricResponse=new HaloMetricResponse();
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    List<PublicMetrics> metrics = new ArrayList<PublicMetrics>(this.publicMetrics);
    for (PublicMetrics publicMetric : metrics) {
        try {
            for (Metric<?> metric : publicMetric.metrics()) {
                if(THREADS.equals(metric.getName())){
                    haloMetricResponse.setJvmThreadslive(String.valueOf(metric.getValue()));
                }
                if(NON_HEAP_Used.equals(metric.getName())){
                    haloMetricResponse.setJvmMemoryUsedNonHeap(String.valueOf(metric.getValue()));
                }
                if(HEAP_USED.equals(metric.getName())){
                    haloMetricResponse.setJvmMemoryUsedHeap(String.valueOf(metric.getValue()));
                }
                if(HEAP_COMMITTED.equals(metric.getName())){
                    haloMetricResponse.setHeapCommitted(String.valueOf(metric.getValue()));
                }
                if(HEAP_INIT.equals(metric.getName())){
                    haloMetricResponse.setHeapInit(String.valueOf(metric.getValue()));
                }
                if(HEAP_MAX.equals(metric.getName())){
                    haloMetricResponse.setHeapMax(String.valueOf(metric.getValue()));
                }
                getGcInfo(haloMetricResponse, metric);
                if(NONHEAP_COMMITTED.equals(metric.getName())){
                    haloMetricResponse.setNonheapCommitted(String.valueOf(metric.getValue()));
                }
                if(SYSTEMLOAD_AVERAGE.equals(metric.getName())){
                    haloMetricResponse.setSystemloadAverage(String.valueOf(metric.getValue()));
                }
                if(PROCESSORS.equals(metric.getName())){
                    haloMetricResponse.setProcessors(String.valueOf(metric.getValue()));
                }



            }
        }
        catch (Exception ex) {
            // Could not evaluate metrics
        }
    }
    return haloMetricResponse;
}
 
Example #13
Source File: ExamplePublicMetrics.java    From building-microservices with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Metric<?>> metrics() {
	return Arrays.asList(new Metric<>("example.public", random.nextInt()));
}
 
Example #14
Source File: SpectatorMetricReader.java    From spring-cloud-netflix-contrib with Apache License 2.0 4 votes vote down vote up
@Override
public Metric<?> findOne(String name) {
	throw new UnsupportedOperationException("cannot construct a tag-based Spectator id from a hierarchical name");
}
 
Example #15
Source File: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 2 votes vote down vote up
@Override
public void onRemoved(com.alipay.lookout.api.Metric metric) {

}
 
Example #16
Source File: LookoutRegistryMetricReader.java    From sofa-lookout with Apache License 2.0 2 votes vote down vote up
@Override
public void onAdded(com.alipay.lookout.api.Metric metric) {

}