first temptative

This commit is contained in:
Michele Artini 2021-11-26 16:31:31 +01:00
parent 6c06ad6b9a
commit e0a3e6a14f
7 changed files with 75 additions and 6 deletions

View File

@ -12,7 +12,7 @@ import springfox.documentation.service.ApiInfo;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@SpringBootApplication(scanBasePackages = "eu.dnetlib")
@EnableSwagger2
@EnableCaching
@EnableScheduling

View File

@ -49,7 +49,7 @@ public class MockSecurityConfig extends WebSecurityConfigurerAdapter {
.hasAnyRole(OpenOrgsConstants.VALID_ROLES)
.antMatchers("/registration_api/**")
.hasRole(OpenOrgsConstants.NOT_AUTORIZED_ROLE)
.antMatchers("/common/**", "/resources/**", "/webjars/**", "/metrics", "/health", "/dbmodel/**")
.antMatchers("/common/**", "/resources/**", "/webjars/**", "/metrics", "/health", "/kpis", "/dbmodel/**")
.permitAll()
.antMatchers("/oa_api/**")
.permitAll()

View File

@ -62,7 +62,7 @@ public class OAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
.hasAnyRole(OpenOrgsConstants.VALID_ROLES)
.antMatchers("/registration_api/**")
.hasRole(OpenOrgsConstants.NOT_AUTORIZED_ROLE)
.antMatchers("/", "/common/**", "/resources/**", "/webjars/**", "/metrics", "/health", "/dbmodel/**")
.antMatchers("/", "/common/**", "/resources/**", "/webjars/**", "/metrics", "/health", "/kpis", "/dbmodel/**")
.permitAll()
.antMatchers("/oa_api/**")
.hasIpAddress(openaireApiValidSubnet)

View File

@ -0,0 +1,33 @@
package eu.dnetlib.organizations.controller;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import java.util.Set;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import eu.dnetlib.common.metrics.MetricUtils;
import io.prometheus.client.exporter.common.TextFormat;
@Controller
public class KpiMetricsController {
@ResponseBody
@RequestMapping(value = "/kpis", method = RequestMethod.GET, produces = "*/*")
public ResponseEntity<String> kpiMetrics(
@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 result = MetricUtils.output(name, contentType);
return ResponseEntity.ok()
.header(CONTENT_TYPE, contentType)
.body(result);
}
}

View File

@ -15,6 +15,9 @@ public class ValidOrganizationsMetric extends MetricInfo {
@Override
public double obtainValue() {
System.out.println("xxxxxxxxxxxxxx");
return organizationRepository.countByStatus(OrganizationStatus.approved.toString());
}

View File

@ -6,8 +6,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanNameAware;
import io.micrometer.core.instrument.Metrics;
public abstract class MetricInfo implements BeanNameAware {
private static final Log log = LogFactory.getLog(MetricInfo.class);
@ -28,7 +26,7 @@ public abstract class MetricInfo implements BeanNameAware {
@PostConstruct
public void register() {
log.info("Prometheus - new metric registered: " + getBeanName());
Metrics.gauge(getBeanName(), this, o -> obtainValue());
MetricUtils.register(beanName, this);
}
}

View File

@ -0,0 +1,35 @@
package eu.dnetlib.common.metrics;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Set;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.common.TextFormat;
public class MetricUtils {
private static final CompositeMeterRegistry meterRegistry = new CompositeMeterRegistry();
private static final CollectorRegistry collectorRegistry = new CollectorRegistry(true);
public static MetricInfo register(final String name, final MetricInfo metric) {
final MetricInfo meter = meterRegistry.gauge(name, metric, MetricInfo::obtainValue);
Gauge.build(name, name).register(collectorRegistry);
return meter;
}
public static String output(final Set<String> metricsToInclude, final String contentType) {
try {
final Writer writer = new StringWriter();
TextFormat.writeFormat(contentType, writer, collectorRegistry.filteredMetricFamilySamples(metricsToInclude));
return writer.toString();
} catch (final IOException e) {
// This actually never happens since StringWriter::write() doesn't throw any IOException
throw new RuntimeException("Writing metrics failed", e);
}
}
}