2424
2525import io .prometheus .client .Collector .MetricFamilySamples ;
2626import io .prometheus .client .CollectorRegistry ;
27- import io .prometheus .client .exporter .common .TextFormat ;
2827
2928import org .springframework .boot .actuate .endpoint .annotation .Endpoint ;
3029import org .springframework .boot .actuate .endpoint .annotation .ReadOperation ;
3130import org .springframework .boot .actuate .endpoint .web .WebEndpointResponse ;
3231import org .springframework .boot .actuate .endpoint .web .annotation .WebEndpoint ;
3332import org .springframework .lang .Nullable ;
34- import org .springframework .util .MimeType ;
3533
3634/**
3735 * {@link Endpoint @Endpoint} that outputs metrics in a format that can be scraped by the
@@ -50,30 +48,19 @@ public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) {
5048 this .collectorRegistry = collectorRegistry ;
5149 }
5250
53- @ ReadOperation (produces = { TextFormat .CONTENT_TYPE_004 , TextFormat .CONTENT_TYPE_OPENMETRICS_100 })
54- public WebEndpointResponse <String > scrape (ProducibleTextFormat producibleTextFormat ,
55- @ Nullable Set <String > includedNames ) {
51+ @ ReadOperation (producesFrom = TextOutputFormat .class )
52+ public WebEndpointResponse <String > scrape (TextOutputFormat format , @ Nullable Set <String > includedNames ) {
5653 try {
5754 Writer writer = new StringWriter ();
5855 Enumeration <MetricFamilySamples > samples = (includedNames != null )
5956 ? this .collectorRegistry .filteredMetricFamilySamples (includedNames )
6057 : this .collectorRegistry .metricFamilySamples ();
61- MimeType contentType = producibleTextFormat .getMimeType ();
62- if (producibleTextFormat == ProducibleTextFormat .CONTENT_TYPE_004 ) {
63- TextFormat .write004 (writer , samples );
64- }
65- else if (producibleTextFormat == ProducibleTextFormat .CONTENT_TYPE_OPENMETRICS_100 ) {
66- TextFormat .writeOpenMetrics100 (writer , samples );
67- }
68- else {
69- throw new RuntimeException ("Unsupported text format '" + producibleTextFormat .getMimeType () + "'" );
70- }
71- return new WebEndpointResponse <>(writer .toString (), contentType );
58+ format .write (writer , samples );
59+ return new WebEndpointResponse <>(writer .toString (), format );
7260 }
7361 catch (IOException ex ) {
74- // This actually never happens since StringWriter::write() doesn't throw any
75- // IOException
76- throw new RuntimeException ("Writing metrics failed" , ex );
62+ // This actually never happens since StringWriter doesn't throw an IOException
63+ throw new IllegalStateException ("Writing metrics failed" , ex );
7764 }
7865 }
7966
0 commit comments