001package io.prometheus.client.spring.boot;
002
003import io.prometheus.client.exporter.common.TextFormat;
004import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
005import org.springframework.boot.context.properties.ConfigurationProperties;
006import org.springframework.http.ResponseEntity;
007import org.springframework.web.bind.annotation.RequestMapping;
008import org.springframework.web.bind.annotation.RequestMethod;
009import org.springframework.web.bind.annotation.RequestParam;
010import org.springframework.web.bind.annotation.ResponseBody;
011
012import java.util.Set;
013
014import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
015
016@ConfigurationProperties("endpoints.prometheus")
017public class PrometheusMvcEndpoint extends EndpointMvcAdapter {
018
019  private final PrometheusEndpoint delgate;
020
021  public PrometheusMvcEndpoint(PrometheusEndpoint delegate) {
022    super(delegate);
023    this.delgate = delegate;
024  }
025
026  @RequestMapping(
027          method = {RequestMethod.GET},
028          produces = { "*/*" }
029  )
030  @ResponseBody
031  public ResponseEntity value(
032          @RequestParam(value = "name[]", required = false, defaultValue = "") Set<String> name) {
033    if (!getDelegate().isEnabled()) {
034      // Shouldn't happen - MVC endpoint shouldn't be registered when delegate's
035      // disabled
036      return getDisabledResponse();
037    }
038
039    String result = delgate.writeRegistry(name);
040    return ResponseEntity.ok()
041            .header(CONTENT_TYPE, TextFormat.CONTENT_TYPE_004)
042            .body(result);
043  }
044}