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
14eb0410
Commit
14eb0410
authored
Aug 12, 2020
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Add basic auth support for Prometheus pushgateway"
See gh-22548
parent
9ddc97f8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
66 deletions
+96
-66
PrometheusMetricsExportAutoConfiguration.java
.../prometheus/PrometheusMetricsExportAutoConfiguration.java
+23
-21
PrometheusProperties.java
...igure/metrics/export/prometheus/PrometheusProperties.java
+31
-43
PrometheusMetricsExportAutoConfigurationTests.java
...etheus/PrometheusMetricsExportAutoConfigurationTests.java
+42
-2
No files found.
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java
View file @
14eb0410
...
...
@@ -16,6 +16,11 @@
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
metrics
.
export
.
prometheus
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.time.Duration
;
import
java.util.Map
;
import
io.micrometer.core.instrument.Clock
;
import
io.micrometer.prometheus.PrometheusConfig
;
import
io.micrometer.prometheus.PrometheusMeterRegistry
;
...
...
@@ -24,6 +29,7 @@ import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
import
io.prometheus.client.exporter.PushGateway
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint
;
import
org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration
;
import
org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration
;
...
...
@@ -44,21 +50,17 @@ import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.log.LogMessage
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.time.Duration
;
import
java.util.Map
;
import
org.springframework.util.StringUtils
;
/**
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to Prometheus.
*
* @since 2.0.0
* @author Jon Schneider
* @author David J. M. Karlsen
* @since 2.0.0
*/
@Configuration
(
proxyBeanMethods
=
false
)
@AutoConfigureBefore
({
CompositeMeterRegistryAutoConfiguration
.
class
,
SimpleMetricsExportAutoConfiguration
.
class
})
@AutoConfigureBefore
({
CompositeMeterRegistryAutoConfiguration
.
class
,
SimpleMetricsExportAutoConfiguration
.
class
})
@AutoConfigureAfter
(
MetricsAutoConfiguration
.
class
)
@ConditionalOnBean
(
Clock
.
class
)
@ConditionalOnClass
(
PrometheusMeterRegistry
.
class
)
...
...
@@ -75,7 +77,7 @@ public class PrometheusMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public
PrometheusMeterRegistry
prometheusMeterRegistry
(
PrometheusConfig
prometheusConfig
,
CollectorRegistry
collectorRegistry
,
Clock
clock
)
{
CollectorRegistry
collectorRegistry
,
Clock
clock
)
{
return
new
PrometheusMeterRegistry
(
prometheusConfig
,
collectorRegistry
,
clock
);
}
...
...
@@ -118,30 +120,30 @@ public class PrometheusMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public
PrometheusPushGatewayManager
prometheusPushGatewayManager
(
CollectorRegistry
collectorRegistry
,
PrometheusProperties
prometheusProperties
,
Environment
environment
)
{
PrometheusProperties
prometheusProperties
,
Environment
environment
)
{
PrometheusProperties
.
Pushgateway
properties
=
prometheusProperties
.
getPushgateway
();
Duration
pushRate
=
properties
.
getPushRate
();
String
job
=
getJob
(
properties
,
environment
);
Map
<
String
,
String
>
groupingKey
=
properties
.
getGroupingKey
();
ShutdownOperation
shutdownOperation
=
properties
.
getShutdownOperation
();
return
new
PrometheusPushGatewayManager
(
getPushGateway
(
properties
.
getBaseUrl
()),
collectorRegistry
,
pushRate
,
job
,
groupingKey
,
shutdownOperation
);
PushGateway
pushGateway
=
initializePushGateway
(
properties
.
getBaseUrl
());
if
(
StringUtils
.
hasText
(
properties
.
getUsername
()))
{
pushGateway
.
setConnectionFactory
(
new
BasicAuthHttpConnectionFactory
(
properties
.
getUsername
(),
properties
.
getPassword
()));
}
return
new
PrometheusPushGatewayManager
(
pushGateway
,
collectorRegistry
,
pushRate
,
job
,
groupingKey
,
shutdownOperation
);
}
private
PushGateway
getPushGateway
(
String
url
)
{
PushGateway
pushGateway
=
null
;
private
PushGateway
initializePushGateway
(
String
url
)
{
try
{
pushGateway
=
new
PushGateway
(
new
URL
(
url
));
}
catch
(
MalformedURLException
ex
)
{
return
new
PushGateway
(
new
URL
(
url
));
}
catch
(
MalformedURLException
ex
)
{
logger
.
warn
(
LogMessage
.
format
(
"Invalid PushGateway base url '%s': update your configuration to a valid URL"
,
url
));
pushGateway
=
new
PushGateway
(
url
);
}
PrometheusProperties
.
Pushgateway
properties
=
prometheusProperties
.
getPushgateway
();
if
(
properties
.
getAuthEnabled
())
{
pushgateway
.
setConnectionFactory
(
new
BasicAuthHttpConnectionFactory
(
properties
.
getAuthusername
(),
properties
.
getAuthpassword
()));
return
new
PushGateway
(
url
);
}
return
pushGateway
;
}
private
String
getJob
(
PrometheusProperties
.
Pushgateway
properties
,
Environment
environment
)
{
...
...
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusProperties.java
View file @
14eb0410
...
...
@@ -16,14 +16,15 @@
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
metrics
.
export
.
prometheus
;
import
io.micrometer.prometheus.HistogramFlavor
;
import
org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
java.time.Duration
;
import
java.util.HashMap
;
import
java.util.Map
;
import
io.micrometer.prometheus.HistogramFlavor
;
import
org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
/**
* {@link ConfigurationProperties @ConfigurationProperties} for configuring metrics export
* to Prometheus.
...
...
@@ -85,7 +86,6 @@ public class PrometheusProperties {
return
this
.
pushgateway
;
}
/**
* Configuration options for push-based interaction with Prometheus.
*/
...
...
@@ -102,34 +102,29 @@ public class PrometheusProperties {
private
String
baseUrl
=
"http://localhost:9091"
;
/**
* Frequency with which to push metrics.
*/
private
Duration
pushRate
=
Duration
.
ofMinutes
(
1
);
/**
* Job identifier for this application instance.
* Login user of the Prometheus Pushgateway.
*/
private
String
job
;
private
String
username
;
/**
*
Grouping key for the pushed metrics
.
*
Login password of the Prometheus Pushgateway
.
*/
private
Map
<
String
,
String
>
groupingKey
=
new
HashMap
<>()
;
private
String
password
;
/**
*
Enable publishing via a Prometheus Pushgateway with Basic Auth
.
*
Frequency with which to push metrics
.
*/
private
Boolean
authEnabled
=
false
;
private
Duration
pushRate
=
Duration
.
ofMinutes
(
1
)
;
/**
*
Prometheus Pushgateway basic-auth usernam
e.
*
Job identifier for this application instanc
e.
*/
private
String
authusername
;
private
String
job
;
/**
*
Prometheus Pushgateway basic-auth password
.
*
Grouping key for the pushed metrics
.
*/
private
String
authpassword
;
private
Map
<
String
,
String
>
groupingKey
=
new
HashMap
<>()
;
/**
* Operation that should be performed on shutdown.
...
...
@@ -152,6 +147,22 @@ public class PrometheusProperties {
this
.
baseUrl
=
baseUrl
;
}
public
String
getUsername
()
{
return
this
.
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
String
getPassword
()
{
return
this
.
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
Duration
getPushRate
()
{
return
this
.
pushRate
;
}
...
...
@@ -184,29 +195,6 @@ public class PrometheusProperties {
this
.
shutdownOperation
=
shutdownOperation
;
}
public
Boolean
getAuthEnabled
()
{
return
this
.
authEnabled
;
}
public
void
setAuthEnabled
(
Boolean
authEnabled
)
{
this
.
authEnabled
=
authEnabled
;
}
public
String
getAuthusername
()
{
return
authusername
;
}
public
void
setAuthusername
(
String
authusername
)
{
this
.
authusername
=
authusername
;
}
public
String
getAuthpassword
()
{
return
authpassword
;
}
public
void
setAuthpassword
(
String
authpassword
)
{
this
.
authpassword
=
authpassword
;
}
}
}
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java
View file @
14eb0410
...
...
@@ -16,10 +16,16 @@
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
metrics
.
export
.
prometheus
;
import
java.util.function.Consumer
;
import
io.micrometer.core.instrument.Clock
;
import
io.micrometer.prometheus.PrometheusConfig
;
import
io.micrometer.prometheus.PrometheusMeterRegistry
;
import
io.prometheus.client.CollectorRegistry
;
import
io.prometheus.client.exporter.BasicAuthHttpConnectionFactory
;
import
io.prometheus.client.exporter.DefaultHttpConnectionFactory
;
import
io.prometheus.client.exporter.HttpConnectionFactory
;
import
io.prometheus.client.exporter.PushGateway
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
...
...
@@ -29,6 +35,7 @@ import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScra
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.test.context.assertj.AssertableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.test.context.runner.ContextConsumer
;
import
org.springframework.boot.test.system.CapturedOutput
;
import
org.springframework.boot.test.system.OutputCaptureExtension
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -42,6 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link PrometheusMetricsExportAutoConfiguration}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
@ExtendWith
(
OutputCaptureExtension
.
class
)
class
PrometheusMetricsExportAutoConfigurationTests
{
...
...
@@ -142,6 +150,15 @@ class PrometheusMetricsExportAutoConfigurationTests {
});
}
@Test
void
withPushGatewayNoBasicAuth
()
{
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
ManagementContextAutoConfiguration
.
class
))
.
withPropertyValues
(
"management.metrics.export.prometheus.pushgateway.enabled=true"
)
.
withUserConfiguration
(
BaseConfiguration
.
class
)
.
run
(
hasHttpConnectionFactory
((
httpConnectionFactory
)
->
assertThat
(
httpConnectionFactory
)
.
isInstanceOf
(
DefaultHttpConnectionFactory
.
class
)));
}
@Test
@Deprecated
void
withCustomLegacyPushGatewayURL
(
CapturedOutput
output
)
{
...
...
@@ -163,11 +180,34 @@ class PrometheusMetricsExportAutoConfigurationTests {
.
run
((
context
)
->
hasGatewayURL
(
context
,
"https://example.com:8080/metrics/"
));
}
@Test
void
withPushGatewayBasicAuth
()
{
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
ManagementContextAutoConfiguration
.
class
))
.
withPropertyValues
(
"management.metrics.export.prometheus.pushgateway.enabled=true"
,
"management.metrics.export.prometheus.pushgateway.username=admin"
,
"management.metrics.export.prometheus.pushgateway.password=secret"
)
.
withUserConfiguration
(
BaseConfiguration
.
class
)
.
run
(
hasHttpConnectionFactory
((
httpConnectionFactory
)
->
assertThat
(
httpConnectionFactory
)
.
isInstanceOf
(
BasicAuthHttpConnectionFactory
.
class
)));
}
private
void
hasGatewayURL
(
AssertableApplicationContext
context
,
String
url
)
{
assertThat
(
getPushGateway
(
context
)).
hasFieldOrPropertyWithValue
(
"gatewayBaseURL"
,
url
);
}
private
ContextConsumer
<
AssertableApplicationContext
>
hasHttpConnectionFactory
(
Consumer
<
HttpConnectionFactory
>
httpConnectionFactory
)
{
return
(
context
)
->
{
PushGateway
pushGateway
=
getPushGateway
(
context
);
httpConnectionFactory
.
accept
((
HttpConnectionFactory
)
ReflectionTestUtils
.
getField
(
pushGateway
,
"connectionFactory"
));
};
}
private
PushGateway
getPushGateway
(
AssertableApplicationContext
context
)
{
assertThat
(
context
).
hasSingleBean
(
PrometheusPushGatewayManager
.
class
);
PrometheusPushGatewayManager
gatewayManager
=
context
.
getBean
(
PrometheusPushGatewayManager
.
class
);
Object
pushGateway
=
ReflectionTestUtils
.
getField
(
gatewayManager
,
"pushGateway"
);
assertThat
(
pushGateway
).
hasFieldOrPropertyWithValue
(
"gatewayBaseURL"
,
url
);
return
(
PushGateway
)
ReflectionTestUtils
.
getField
(
gatewayManager
,
"pushGateway"
);
}
@Configuration
(
proxyBeanMethods
=
false
)
...
...
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