/kpis metrics endpoint
This commit is contained in:
parent
e0a3e6a14f
commit
e127a7e73c
|
@ -4,6 +4,7 @@ import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||||
import org.springframework.data.elasticsearch.client.RestClients;
|
import org.springframework.data.elasticsearch.client.RestClients;
|
||||||
|
@ -25,6 +26,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||||
@EnableElasticsearchRepositories(basePackageClasses = {
|
@EnableElasticsearchRepositories(basePackageClasses = {
|
||||||
Event.class, Notification.class
|
Event.class, Notification.class
|
||||||
})
|
})
|
||||||
|
@ComponentScan(basePackages = "eu.dnetlib")
|
||||||
public class LiteratureBrokerServiceConfiguration extends AbstractElasticsearchConfiguration {
|
public class LiteratureBrokerServiceConfiguration extends AbstractElasticsearchConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||||
import org.springframework.data.elasticsearch.client.RestClients;
|
import org.springframework.data.elasticsearch.client.RestClients;
|
||||||
|
@ -25,6 +26,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||||
@EnableElasticsearchRepositories(basePackageClasses = {
|
@EnableElasticsearchRepositories(basePackageClasses = {
|
||||||
Event.class, Notification.class
|
Event.class, Notification.class
|
||||||
})
|
})
|
||||||
|
@ComponentScan(basePackages = "eu.dnetlib")
|
||||||
public class BrokerConfiguration extends AbstractElasticsearchConfiguration {
|
public class BrokerConfiguration extends AbstractElasticsearchConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -3,6 +3,7 @@ package eu.dnetlib.organizations;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
import eu.dnetlib.common.app.AbstractDnetApp;
|
import eu.dnetlib.common.app.AbstractDnetApp;
|
||||||
|
@ -12,10 +13,11 @@ import springfox.documentation.service.ApiInfo;
|
||||||
import springfox.documentation.spring.web.plugins.Docket;
|
import springfox.documentation.spring.web.plugins.Docket;
|
||||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||||
|
|
||||||
@SpringBootApplication(scanBasePackages = "eu.dnetlib")
|
@SpringBootApplication
|
||||||
@EnableSwagger2
|
@EnableSwagger2
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
|
@ComponentScan(basePackages = "eu.dnetlib")
|
||||||
public class MainApplication extends AbstractDnetApp {
|
public class MainApplication extends AbstractDnetApp {
|
||||||
|
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
|
|
|
@ -8,16 +8,13 @@ import eu.dnetlib.organizations.repository.OrganizationRepository;
|
||||||
import eu.dnetlib.organizations.utils.OrganizationStatus;
|
import eu.dnetlib.organizations.utils.OrganizationStatus;
|
||||||
|
|
||||||
@Component("valid_organizations_total")
|
@Component("valid_organizations_total")
|
||||||
public class ValidOrganizationsMetric extends MetricInfo {
|
public class ValidOrganizationsMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private OrganizationRepository organizationRepository;
|
private OrganizationRepository organizationRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double obtainValue() {
|
public double obtainValue() {
|
||||||
|
|
||||||
System.out.println("xxxxxxxxxxxxxx");
|
|
||||||
|
|
||||||
return organizationRepository.countByStatus(OrganizationStatus.approved.toString());
|
return organizationRepository.countByStatus(OrganizationStatus.approved.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,31 +1,30 @@
|
||||||
package eu.dnetlib.organizations.controller;
|
package eu.dnetlib.common.metrics;
|
||||||
|
|
||||||
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
|
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
|
||||||
|
|
||||||
import java.util.Set;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
import eu.dnetlib.common.metrics.MetricUtils;
|
|
||||||
import io.prometheus.client.exporter.common.TextFormat;
|
import io.prometheus.client.exporter.common.TextFormat;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class KpiMetricsController {
|
public class KpiMetricsController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MetricUtils metricUtils;
|
||||||
|
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@RequestMapping(value = "/kpis", method = RequestMethod.GET, produces = "*/*")
|
@RequestMapping(value = "/kpis", method = RequestMethod.GET, produces = "*/*")
|
||||||
public ResponseEntity<String> kpiMetrics(
|
public ResponseEntity<String> kpiMetrics(@RequestHeader(value = "Accept", required = false, defaultValue = "") final String accept) {
|
||||||
@RequestParam(value = "name[]", required = false, defaultValue = "") final Set<String> name,
|
|
||||||
@RequestHeader(value = "Accept", required = false, defaultValue = "") final String accept) {
|
|
||||||
|
|
||||||
final String contentType = TextFormat.chooseContentType(accept);
|
final String contentType = TextFormat.chooseContentType(accept);
|
||||||
final String result = MetricUtils.output(name, contentType);
|
final String result = metricUtils.output(contentType);
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(CONTENT_TYPE, contentType)
|
.header(CONTENT_TYPE, contentType)
|
||||||
.body(result);
|
.body(result);
|
|
@ -1,32 +1,7 @@
|
||||||
package eu.dnetlib.common.metrics;
|
package eu.dnetlib.common.metrics;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
public abstract interface MetricInfo {
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
double obtainValue();
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
|
||||||
|
|
||||||
public abstract class MetricInfo implements BeanNameAware {
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(MetricInfo.class);
|
|
||||||
|
|
||||||
private String beanName;
|
|
||||||
|
|
||||||
abstract public double obtainValue();
|
|
||||||
|
|
||||||
public String getBeanName() {
|
|
||||||
return beanName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setBeanName(final String beanName) {
|
|
||||||
this.beanName = beanName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void register() {
|
|
||||||
log.info("Prometheus - new metric registered: " + getBeanName());
|
|
||||||
MetricUtils.register(beanName, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,28 +3,43 @@ package eu.dnetlib.common.metrics;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Set;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
|
import javax.annotation.PostConstruct;
|
||||||
import io.prometheus.client.CollectorRegistry;
|
|
||||||
import io.prometheus.client.Gauge;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import io.micrometer.core.instrument.Metrics;
|
||||||
|
import io.prometheus.client.Collector.MetricFamilySamples;
|
||||||
|
import io.prometheus.client.GaugeMetricFamily;
|
||||||
import io.prometheus.client.exporter.common.TextFormat;
|
import io.prometheus.client.exporter.common.TextFormat;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class MetricUtils {
|
public class MetricUtils {
|
||||||
|
|
||||||
private static final CompositeMeterRegistry meterRegistry = new CompositeMeterRegistry();
|
@Autowired(required = false)
|
||||||
private static final CollectorRegistry collectorRegistry = new CollectorRegistry(true);
|
private Map<String, MetricInfo> kpiMetrics;
|
||||||
|
|
||||||
public static MetricInfo register(final String name, final MetricInfo metric) {
|
@PostConstruct
|
||||||
final MetricInfo meter = meterRegistry.gauge(name, metric, MetricInfo::obtainValue);
|
public void registerToMainEndpoint() {
|
||||||
Gauge.build(name, name).register(collectorRegistry);
|
if (kpiMetrics != null) {
|
||||||
return meter;
|
kpiMetrics.forEach((k, v) -> Metrics.gauge(k, v, o -> o.obtainValue()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String output(final Set<String> metricsToInclude, final String contentType) {
|
public String output(final String contentType) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
final List<MetricFamilySamples> samples = new ArrayList<>();
|
||||||
|
if (kpiMetrics != null) {
|
||||||
|
kpiMetrics.forEach((k, v) -> samples.add(new GaugeMetricFamily(k, "", v.obtainValue())));
|
||||||
|
}
|
||||||
final Writer writer = new StringWriter();
|
final Writer writer = new StringWriter();
|
||||||
TextFormat.writeFormat(contentType, writer, collectorRegistry.filteredMetricFamilySamples(metricsToInclude));
|
TextFormat.writeFormat(contentType, writer, Collections.enumeration(samples));
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
// This actually never happens since StringWriter::write() doesn't throw any IOException
|
// This actually never happens since StringWriter::write() doesn't throw any IOException
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_last_metrics_updater_run_timestamp_seconds")
|
@Component("provide_broker_last_metrics_updater_run_timestamp_seconds")
|
||||||
public class LastUpdateMetric extends MetricInfo {
|
public class LastUpdateMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_datasources_with_events")
|
@Component("provide_broker_datasources_with_events")
|
||||||
public class TotalDatasourcesWithEventsMetric extends MetricInfo {
|
public class TotalDatasourcesWithEventsMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_datasources_with_subscriptions")
|
@Component("provide_broker_datasources_with_subscriptions")
|
||||||
public class TotalDatasourcesWithSubscriptionsMetric extends MetricInfo {
|
public class TotalDatasourcesWithSubscriptionsMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_events")
|
@Component("provide_broker_events")
|
||||||
public class TotalEventsMetric extends MetricInfo {
|
public class TotalEventsMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_notifications")
|
@Component("provide_broker_notifications")
|
||||||
public class TotalNotificationsMetric extends MetricInfo {
|
public class TotalNotificationsMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_subscribers")
|
@Component("provide_broker_subscribers")
|
||||||
public class TotalSubscribersMetric extends MetricInfo {
|
public class TotalSubscribersMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.stereotype.Component;
|
||||||
import eu.dnetlib.common.metrics.MetricInfo;
|
import eu.dnetlib.common.metrics.MetricInfo;
|
||||||
|
|
||||||
@Component("provide_broker_subscriptions")
|
@Component("provide_broker_subscriptions")
|
||||||
public class TotalSubscriptionsMetric extends MetricInfo {
|
public class TotalSubscriptionsMetric implements MetricInfo {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetricsCacheUtils metricsCacheUtils;
|
private MetricsCacheUtils metricsCacheUtils;
|
||||||
|
|
Loading…
Reference in New Issue