Polish "Auto-config support for latest Prometheus client and simpleclient"

See gh-40023
This commit is contained in:
Moritz Halbritter
2024-04-05 10:36:53 +02:00
parent 7f26b67e61
commit ce358c601b
12 changed files with 45 additions and 39 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.lang.Nullable;
*
* @author Jon Schneider
* @author Johnny Lim
* @author Moritz Halbritter
* @since 2.0.0
*/
@WebEndpoint(id = "prometheus")
@@ -51,17 +52,15 @@ public class PrometheusScrapeEndpoint {
}
@ReadOperation(producesFrom = PrometheusOutputFormat.class)
public WebEndpointResponse<String> scrape(PrometheusOutputFormat format, @Nullable Set<String> includedNames) {
public WebEndpointResponse<byte[]> scrape(PrometheusOutputFormat format, @Nullable Set<String> includedNames) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(this.nextMetricsScrapeSize);
MetricSnapshots metricSnapshots = (includedNames != null)
? this.prometheusRegistry.scrape(includedNames::contains) : this.prometheusRegistry.scrape();
format.write(outputStream, metricSnapshots);
String scrapePage = outputStream.toString();
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;
return new WebEndpointResponse<>(scrapePage, format);
byte[] content = outputStream.toByteArray();
this.nextMetricsScrapeSize = content.length + METRICS_SCRAPE_CHARS_EXTRA;
return new WebEndpointResponse<>(content, format);
}
catch (IOException ex) {
throw new IllegalStateException("Writing metrics failed", ex);

View File

@@ -38,7 +38,8 @@ import org.springframework.lang.Nullable;
* @author Jon Schneider
* @author Johnny Lim
* @since 2.0.0
* @deprecated in favor of {@link PrometheusScrapeEndpoint}
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of
* {@link PrometheusScrapeEndpoint}
*/
@Deprecated(since = "3.3.0", forRemoval = true)
@WebEndpoint(id = "prometheus")
@@ -63,10 +64,8 @@ public class PrometheusSimpleclientScrapeEndpoint {
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
: this.collectorRegistry.metricFamilySamples();
format.write(writer, samples);
String scrapePage = writer.toString();
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;
return new WebEndpointResponse<>(scrapePage, format);
}
catch (IOException ex) {

View File

@@ -32,7 +32,7 @@ import org.springframework.util.MimeTypeUtils;
*
* @author Andy Wilkinson
* @since 2.5.0
* @deprecated in favor of {@link PrometheusOutputFormat}
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of {@link PrometheusOutputFormat}
*/
@Deprecated(since = "3.3.0", forRemoval = true)
public enum TextOutputFormat implements Producible<TextOutputFormat> {