24
24
25
25
import io .prometheus .client .Collector .MetricFamilySamples ;
26
26
import io .prometheus .client .CollectorRegistry ;
27
- import io .prometheus .client .exporter .common .TextFormat ;
28
27
29
28
import org .springframework .boot .actuate .endpoint .annotation .Endpoint ;
30
29
import org .springframework .boot .actuate .endpoint .annotation .ReadOperation ;
31
30
import org .springframework .boot .actuate .endpoint .web .WebEndpointResponse ;
32
31
import org .springframework .boot .actuate .endpoint .web .annotation .WebEndpoint ;
33
32
import org .springframework .lang .Nullable ;
34
- import org .springframework .util .MimeType ;
35
33
36
34
/**
37
35
* {@link Endpoint @Endpoint} that outputs metrics in a format that can be scraped by the
@@ -50,30 +48,19 @@ public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) {
50
48
this .collectorRegistry = collectorRegistry ;
51
49
}
52
50
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 ) {
56
53
try {
57
54
Writer writer = new StringWriter ();
58
55
Enumeration <MetricFamilySamples > samples = (includedNames != null )
59
56
? this .collectorRegistry .filteredMetricFamilySamples (includedNames )
60
57
: 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 );
72
60
}
73
61
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 );
77
64
}
78
65
}
79
66
0 commit comments