Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
26ee3465
Commit
26ee3465
authored
Mar 19, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish 'Support OpenMetrics text format with Prometheus'
Closes gh-25564
parent
11b4a19d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
26 deletions
+34
-26
PrometheusScrapeEndpoint.java
...e/metrics/export/prometheus/PrometheusScrapeEndpoint.java
+6
-19
TextOutputFormat.java
...t/actuate/metrics/export/prometheus/TextOutputFormat.java
+28
-7
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java
View file @
26ee3465
...
...
@@ -24,14 +24,12 @@ import java.util.Set;
import
io.prometheus.client.Collector.MetricFamilySamples
;
import
io.prometheus.client.CollectorRegistry
;
import
io.prometheus.client.exporter.common.TextFormat
;
import
org.springframework.boot.actuate.endpoint.annotation.Endpoint
;
import
org.springframework.boot.actuate.endpoint.annotation.ReadOperation
;
import
org.springframework.boot.actuate.endpoint.web.WebEndpointResponse
;
import
org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.MimeType
;
/**
* {@link Endpoint @Endpoint} that outputs metrics in a format that can be scraped by the
...
...
@@ -50,30 +48,19 @@ public class PrometheusScrapeEndpoint {
this
.
collectorRegistry
=
collectorRegistry
;
}
@ReadOperation
(
produces
=
{
TextFormat
.
CONTENT_TYPE_004
,
TextFormat
.
CONTENT_TYPE_OPENMETRICS_100
})
public
WebEndpointResponse
<
String
>
scrape
(
ProducibleTextFormat
producibleTextFormat
,
@Nullable
Set
<
String
>
includedNames
)
{
@ReadOperation
(
producesFrom
=
TextOutputFormat
.
class
)
public
WebEndpointResponse
<
String
>
scrape
(
TextOutputFormat
format
,
@Nullable
Set
<
String
>
includedNames
)
{
try
{
Writer
writer
=
new
StringWriter
();
Enumeration
<
MetricFamilySamples
>
samples
=
(
includedNames
!=
null
)
?
this
.
collectorRegistry
.
filteredMetricFamilySamples
(
includedNames
)
:
this
.
collectorRegistry
.
metricFamilySamples
();
MimeType
contentType
=
producibleTextFormat
.
getMimeType
();
if
(
producibleTextFormat
==
ProducibleTextFormat
.
CONTENT_TYPE_004
)
{
TextFormat
.
write004
(
writer
,
samples
);
}
else
if
(
producibleTextFormat
==
ProducibleTextFormat
.
CONTENT_TYPE_OPENMETRICS_100
)
{
TextFormat
.
writeOpenMetrics100
(
writer
,
samples
);
}
else
{
throw
new
RuntimeException
(
"Unsupported text format '"
+
producibleTextFormat
.
getMimeType
()
+
"'"
);
}
return
new
WebEndpointResponse
<>(
writer
.
toString
(),
contentType
);
format
.
write
(
writer
,
samples
);
return
new
WebEndpointResponse
<>(
writer
.
toString
(),
format
);
}
catch
(
IOException
ex
)
{
// This actually never happens since StringWriter::write() doesn't throw any
// IOException
throw
new
RuntimeException
(
"Writing metrics failed"
,
ex
);
// This actually never happens since StringWriter doesn't throw an IOException
throw
new
IllegalStateException
(
"Writing metrics failed"
,
ex
);
}
}
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/
ProducibleTex
tFormat.java
→
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/
TextOutpu
tFormat.java
View file @
26ee3465
...
...
@@ -16,39 +16,60 @@
package
org
.
springframework
.
boot
.
actuate
.
metrics
.
export
.
prometheus
;
import
java.io.IOException
;
import
java.io.Writer
;
import
java.util.Enumeration
;
import
io.prometheus.client.Collector.MetricFamilySamples
;
import
io.prometheus.client.exporter.common.TextFormat
;
import
org.springframework.boot.actuate.endpoint.
http
.Producible
;
import
org.springframework.boot.actuate.endpoint.
annotation
.Producible
;
import
org.springframework.util.MimeType
;
import
org.springframework.util.MimeTypeUtils
;
/**
* A {@link Producible}
for Prometheus'
s {@link TextFormat}.
* A {@link Producible}
enum for supported Prometheu
s {@link TextFormat}.
*
* @author Andy Wilkinson
* @since 2.5.0
*/
public
enum
ProducibleTextFormat
implements
Producible
<
ProducibleTex
tFormat
>
{
public
enum
TextOutputFormat
implements
Producible
<
TextOutpu
tFormat
>
{
/**
* Openmetrics text version 1.0.0.
*/
CONTENT_TYPE_OPENMETRICS_100
(
TextFormat
.
CONTENT_TYPE_OPENMETRICS_100
),
CONTENT_TYPE_OPENMETRICS_100
(
TextFormat
.
CONTENT_TYPE_OPENMETRICS_100
)
{
@Override
void
write
(
Writer
writer
,
Enumeration
<
MetricFamilySamples
>
samples
)
throws
IOException
{
TextFormat
.
writeOpenMetrics100
(
writer
,
samples
);
}
},
/**
* Prometheus text version 0.0.4.
*/
CONTENT_TYPE_004
(
TextFormat
.
CONTENT_TYPE_004
);
CONTENT_TYPE_004
(
TextFormat
.
CONTENT_TYPE_004
)
{
@Override
void
write
(
Writer
writer
,
Enumeration
<
MetricFamilySamples
>
samples
)
throws
IOException
{
TextFormat
.
write004
(
writer
,
samples
);
}
};
private
final
MimeType
mimeType
;
ProducibleTex
tFormat
(
String
mimeType
)
{
TextOutpu
tFormat
(
String
mimeType
)
{
this
.
mimeType
=
MimeTypeUtils
.
parseMimeType
(
mimeType
);
}
@Override
public
MimeType
getMimeType
()
{
public
MimeType
get
Produced
MimeType
()
{
return
this
.
mimeType
;
}
abstract
void
write
(
Writer
writer
,
Enumeration
<
MetricFamilySamples
>
samples
)
throws
IOException
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment