Search in sources :

Example 21 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class MetricsEndpointMetricReader method findAll.

@Override
public Iterable<Metric<?>> findAll() {
    List<Metric<?>> metrics = new ArrayList<>();
    Map<String, Object> values = this.endpoint.invoke();
    Date timestamp = new Date();
    for (Entry<String, Object> entry : values.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        metrics.add(new Metric<>(name, (Number) value, timestamp));
    }
    return metrics;
}
Also used : ArrayList(java.util.ArrayList) Metric(org.springframework.boot.actuate.metrics.Metric) Date(java.util.Date)

Example 22 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class MetricRegistryMetricReader method findOne.

@Override
public Metric<?> findOne(String metricName) {
    String name = this.names.get(metricName);
    if (name == null) {
        return null;
    }
    com.codahale.metrics.Metric metric = this.registry.getMetrics().get(name);
    if (metric == null) {
        return null;
    }
    if (metric instanceof Counter) {
        Counter counter = (Counter) metric;
        return new Metric<Number>(metricName, counter.getCount());
    }
    if (metric instanceof Gauge) {
        Object value = ((Gauge<?>) metric).getValue();
        if (value instanceof Number) {
            return new Metric<>(metricName, (Number) value);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Ignoring gauge '" + name + "' (" + metric + ") as its value is not a Number");
        }
        return null;
    }
    if (metric instanceof Sampling) {
        if (metricName.contains(".snapshot.")) {
            Number value = getMetric(((Sampling) metric).getSnapshot(), metricName);
            if (metric instanceof Timer) {
                // convert back to MILLISEC
                value = TimeUnit.MILLISECONDS.convert(value.longValue(), TimeUnit.NANOSECONDS);
            }
            return new Metric<>(metricName, value);
        }
    }
    return new Metric<>(metricName, getMetric(metric, metricName));
}
Also used : Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Metric(org.springframework.boot.actuate.metrics.Metric) Sampling(com.codahale.metrics.Sampling) Gauge(com.codahale.metrics.Gauge)

Example 23 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class DataSourcePublicMetrics method metrics.

@Override
public Collection<Metric<?>> metrics() {
    Set<Metric<?>> metrics = new LinkedHashSet<>();
    for (Map.Entry<String, DataSourcePoolMetadata> entry : this.metadataByPrefix.entrySet()) {
        String prefix = entry.getKey();
        prefix = (prefix.endsWith(".") ? prefix : prefix + ".");
        DataSourcePoolMetadata metadata = entry.getValue();
        addMetric(metrics, prefix + "active", metadata.getActive());
        addMetric(metrics, prefix + "usage", metadata.getUsage());
    }
    return metrics;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataSourcePoolMetadata(org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata) Metric(org.springframework.boot.actuate.metrics.Metric) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project cas by apereo.

the class CasMetricsRepositoryConfiguration method mongoDbMetricWriter.

@ConditionalOnProperty(prefix = "cas.metrics.mongo", name = "collection")
@Bean
@ExportMetricWriter
public GaugeWriter mongoDbMetricWriter() {
    final MetricsProperties.MongoDb prop = casProperties.getMetrics().getMongo();
    final MongoDbConnectionFactory factory = new MongoDbConnectionFactory();
    final MongoTemplate mongoTemplate = factory.buildMongoTemplate(prop);
    return metric -> {
        final MongoDbMetric metrics = new MongoDbMetric(metric);
        mongoTemplate.save(metrics, prop.getCollection());
    };
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Getter(lombok.Getter) MongoDbConnectionFactory(org.apereo.cas.mongo.MongoDbConnectionFactory) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) MetricsProperties(org.apereo.cas.configuration.model.core.metrics.MetricsProperties) StatsdMetricWriter(org.springframework.boot.actuate.metrics.statsd.StatsdMetricWriter) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ToString(lombok.ToString) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) MetricWriter(org.springframework.boot.actuate.metrics.writer.MetricWriter) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) GaugeWriter(org.springframework.boot.actuate.metrics.writer.GaugeWriter) RedisObjectFactory(org.apereo.cas.redis.core.RedisObjectFactory) RedisMetricRepository(org.springframework.boot.actuate.metrics.repository.redis.RedisMetricRepository) InfluxDbConnectionFactory(org.apereo.cas.influxdb.InfluxDbConnectionFactory) Serializable(java.io.Serializable) ExportMetricWriter(org.springframework.boot.actuate.autoconfigure.ExportMetricWriter) Configuration(org.springframework.context.annotation.Configuration) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Metric(org.springframework.boot.actuate.metrics.Metric) OpenTsdbGaugeWriter(org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbGaugeWriter) Point(org.influxdb.dto.Point) Bean(org.springframework.context.annotation.Bean) MongoDbConnectionFactory(org.apereo.cas.mongo.MongoDbConnectionFactory) MetricsProperties(org.apereo.cas.configuration.model.core.metrics.MetricsProperties) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) ExportMetricWriter(org.springframework.boot.actuate.autoconfigure.ExportMetricWriter) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) Bean(org.springframework.context.annotation.Bean)

Aggregations

Metric (org.springframework.boot.actuate.metrics.Metric)24 Test (org.junit.Test)11 CachePublicMetrics (org.springframework.boot.actuate.endpoint.CachePublicMetrics)7 RichGaugeReaderPublicMetrics (org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics)6 LongAdder (java.util.concurrent.atomic.LongAdder)5 Theory (org.junit.experimental.theories.Theory)5 DataSourcePublicMetrics (org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics)5 MetricReaderPublicMetrics (org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics)5 PublicMetrics (org.springframework.boot.actuate.endpoint.PublicMetrics)5 SystemPublicMetrics (org.springframework.boot.actuate.endpoint.SystemPublicMetrics)5 TomcatPublicMetrics (org.springframework.boot.actuate.endpoint.TomcatPublicMetrics)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)3 Future (java.util.concurrent.Future)3 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 RichGauge (org.springframework.boot.actuate.metrics.rich.RichGauge)2 Counter (com.codahale.metrics.Counter)1