@Override public Flux<Metric> getMetrics(Mono<Empty> request) { return request .flatMapIterable(empty -> publicMetrics) .flatMapIterable(PublicMetrics::metrics) .map(metric -> { Metric.Builder builder = Metric.newBuilder() .setName(metric.getName()); if (metric.getTimestamp() != null) { builder.setTimestamp(ProtobufMappers.dateToTimestamp(metric.getTimestamp())); } if (metric.getValue() instanceof Long || metric.getValue() instanceof Integer) { builder.setLongValue(metric.getValue().longValue()); } else if (metric.getValue() instanceof Float || metric.getValue() instanceof Double) { builder.setDoubleValue((metric.getValue()).doubleValue()); } else { builder.setStringValue(metric.getValue().toString()); } return builder.build(); }); }
@RequestMapping(method = RequestMethod.GET) public ResponseEntity<String> prometheusMetrics() { StringBuilder sb = new StringBuilder(); for (PublicMetrics publicMetrics : this.publicMetrics) { for (Metric<?> metric : publicMetrics.metrics()) { final String sanitizedName = sanitizeMetricName(metric.getName()); final String type = typeForName(sanitizedName); final String metricName = metricName(sanitizedName, type); double value = metric.getValue().doubleValue(); sb.append(String.format("#TYPE %s %s\n", metricName, type)); sb.append(String.format("#HELP %s %s\n", metricName, metricName)); sb.append(String.format("%s %s\n", metricName, prometheusDouble(value))); } } return ResponseEntity.ok() .contentType(MediaType.parseMediaType("text/plain; version=0.0.4; charset=utf-8")) .body(sb.toString()); }
@Test public void multipleDataSources() { load(MultipleDataSourcesConfig.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); // Hikari won't work unless a first connection has been retrieved JdbcTemplate jdbcTemplate = new JdbcTemplate( this.context.getBean("hikariDS", DataSource.class)); jdbcTemplate.execute(new ConnectionCallback<Void>() { @Override public Void doInConnection(Connection connection) throws SQLException, DataAccessException { return null; } }); Collection<Metric<?>> anotherMetrics = bean.metrics(); assertMetrics(anotherMetrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.hikariDS.active", "datasource.hikariDS.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); }
@Test public void correctHttpResponse() throws Exception { PublicMetrics publicMetrics = () -> Collections.singleton(new Metric<Number>("mem.free", 1024)); ResponseEntity<String> response = responseForMetrics(publicMetrics); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); assertThat(response.getHeaders().getContentType().toString(), equalTo("text/plain;version=0.0.4;charset=utf-8")); }
@Test public void defaultsToGauge() throws Exception { PublicMetrics publicMetrics = () -> Collections.singleton(new Metric<Number>("mem.free", 1024)); ResponseEntity<String> response = responseForMetrics(publicMetrics); String body = response.getBody(); assertThat(body, equalTo( "#TYPE mem_free gauge\n" + "#HELP mem_free mem_free\n" + "mem_free 1024.0\n")); }
@Test public void detectsCounters() throws Exception { PublicMetrics publicMetrics = () -> Collections.singleton(new Metric<Number>("counter_mem.free", 1024)); ResponseEntity<String> response = responseForMetrics(publicMetrics); String body = response.getBody(); assertThat(body, equalTo( "#TYPE mem_free counter\n" + "#HELP mem_free mem_free\n" + "mem_free 1024.0\n")); }
@Test public void detectsGauges() throws Exception { PublicMetrics publicMetrics = () -> Collections.singleton(new Metric<Number>("gauge_mem.free", 1024)); ResponseEntity<String> response = responseForMetrics(publicMetrics); String body = response.getBody(); assertThat(body, equalTo( "#TYPE mem_free gauge\n" + "#HELP mem_free mem_free\n" + "mem_free 1024.0\n")); }
@Override public List<MetricFamilySamples> collect() { Map<String, MetricFamilySamples> samples = new HashMap<String, 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.put(name, metricFamilySamples); } } return new ArrayList<MetricFamilySamples>(samples.values()); }
public EndpointAutoConfiguration( ObjectProvider<HealthAggregator> healthAggregatorProvider, ObjectProvider<Map<String, HealthIndicator>> healthIndicatorsProvider, ObjectProvider<List<InfoContributor>> infoContributorsProvider, ObjectProvider<Collection<PublicMetrics>> publicMetricsProvider, ObjectProvider<TraceRepository> traceRepositoryProvider) { this.healthAggregator = healthAggregatorProvider.getIfAvailable(); this.healthIndicators = healthIndicatorsProvider.getIfAvailable(); this.infoContributors = infoContributorsProvider.getIfAvailable(); this.publicMetrics = publicMetricsProvider.getIfAvailable(); this.traceRepository = traceRepositoryProvider.getIfAvailable(); }
@Bean @ConditionalOnMissingBean public MetricsEndpoint metricsEndpoint() { List<PublicMetrics> publicMetrics = new ArrayList<PublicMetrics>(); if (this.publicMetrics != null) { publicMetrics.addAll(this.publicMetrics); } Collections.sort(publicMetrics, AnnotationAwareOrderComparator.INSTANCE); return new MetricsEndpoint(publicMetrics); }
@Test public void autoDataSource() { load(DataSourceAutoConfiguration.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage"); }
@Test public void multipleDataSourcesWithPrimary() { load(MultipleDataSourcesWithPrimaryConfig.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); }
@Test public void multipleDataSourcesWithCustomPrimary() { load(MultipleDataSourcesWithCustomPrimaryConfig.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage", "datasource.dataSource.active", "datasource.dataSource.usage"); }
@Test public void customPrefix() { load(MultipleDataSourcesWithPrimaryConfig.class, CustomDataSourcePublicMetrics.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "ds.first.active", "ds.first.usage", "ds.second.active", "ds.second.usage"); }
@Bean PublicMetrics customPublicMetrics() { return new PublicMetrics() { @Override public Collection<Metric<?>> metrics() { Metric<Integer> metric = new Metric<Integer>("foo", 1); return Collections.<Metric<?>>singleton(metric); } }; }
public PrometheusEndpoint(Collection<PublicMetrics> publicMetrics, PrometeusMetricNameConverter prometeusMetricNameConverter, Map<String, HealthIndicator> healthIndicators, HealthAggregator healthAggregator) { super("prometheus", false, true); this.publicMetrics = publicMetrics; this.prometeusMetricNameConverter = prometeusMetricNameConverter; this.healthIndicators = healthIndicators; this.healthAggregator = healthAggregator; }
@Bean @ConditionalOnMissingBean @Autowired public PrometheusEndpoint prometheusEndpoint(Collection<PublicMetrics> publicMetrics, PrometeusMetricNameConverter prometeusMetricNameConverter, Map<String, HealthIndicator> healthIndicators, HealthAggregator healthAggregator) { return new PrometheusEndpoint(publicMetrics, prometeusMetricNameConverter, healthIndicators, healthAggregator); }
private MetricsServlet createMetricsServlet(PublicMetrics publicMetrics) { CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry; MetricRegistry metricRegistry = new MetricRegistry(); for (Metric<?> metric : publicMetrics.metrics()) { Counter counter = metricRegistry.counter(metric.getName()); counter.dec(counter.getCount()); counter.inc(Double.valueOf(metric.getValue().toString()).longValue()); } DropwizardExports dropwizardExports = new DropwizardExports(metricRegistry); // List<Collector.MetricFamilySamples> metricFamilySamples = dropwizardExports.collect(); collectorRegistry.register(dropwizardExports); return new MetricsServlet(collectorRegistry); }
@Bean public PublicMetrics threadPoolMetrics() { return new PublicMetricsAggregator( new ThreadPoolMetrics(ctx), new MetricReaderPublicMetrics(metricRepository()) ); }
@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; }
/** * Create a new {@link MetricService} instance. * @param publicMetrics the metrics to expose. The collection will be sorted using the * {@link AnnotationAwareOrderComparator}. */ public MetricService(Collection<PublicMetrics> publicMetrics) { Assert.notNull(publicMetrics, "PublicMetrics must not be null"); this.publicMetrics = new ArrayList<>(publicMetrics); AnnotationAwareOrderComparator.sort(this.publicMetrics); }
@Autowired public PrometheusMetricsAutoConfiguration(Collection<PublicMetrics> publicMetrics) { this.publicMetrics = publicMetrics; }
private ResponseEntity<String> responseForMetrics(PublicMetrics publicMetrics) { PrometheusMetricsAutoConfiguration pmc = new PrometheusMetricsAutoConfiguration(Collections.singleton(publicMetrics)); return pmc.prometheusMetrics(); }
@Bean public PublicMetrics metricsAggregate() { return new MetricReaderPublicMetrics(aggregatesMetricReader()); }
SpringBootMetricsCollector(Collection<PublicMetrics> publicMetrics) { this.publicMetrics = publicMetrics; }