Metrics: rename applicationName to key

Fix #879
This commit is contained in:
Vinicius Carvalho
2017-03-27 11:44:21 -04:00
committed by Marius Bogoevici
parent ba18c01602
commit 9fdd7f9fc5
3 changed files with 33 additions and 6 deletions

View File

@@ -1834,6 +1834,11 @@ By default the module is configured to only send Spring Integration message chan
Available properties for customization using the prefix `spring.cloud.stream.metrics`.
key::
The name of the metric being emitted. Should be an unique value per application.
+
Default:: `${spring.application.name:${vcap.application.name:${spring.config.name:application}}}`
+
delay-millis::
The period in which metrics will be posted to the channel
+

View File

@@ -29,7 +29,7 @@ public class StreamMetricsProperties {
private String prefix;
@Value("${spring.application.name:${vcap.application.name:${spring.config.name:application}}}")
private String applicationName;
private String key;
private String metricName;
@@ -73,12 +73,12 @@ public class StreamMetricsProperties {
this.prefix = prefix;
}
public String getApplicationName() {
return applicationName;
public String getKey() {
return key;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
public void setKey(String key) {
this.key = key;
}
public String[] getProperties() {
@@ -97,7 +97,7 @@ public class StreamMetricsProperties {
}
private String resolveMetricName() {
StringBuffer name = new StringBuffer(this.applicationName);
StringBuffer name = new StringBuffer(this.key);
if (!StringUtils.isEmpty(this.prefix)) {
String prefix = this.prefix;
if (prefix.lastIndexOf(".") == -1) {

View File

@@ -168,6 +168,28 @@ public class BinderMetricsEmitterTests {
applicationContext.close();
}
@Test
public void overrideAppName() throws Exception {
ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class,
"--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.metrics.delay-millis=500",
"--spring.application.name=foo", "--spring.cloud.stream.instanceIndex=1",
"--spring.cloud.stream.bindings.streamMetrics.destination=foo",
"--spring.cloud.stream.metrics.key=foobarfoo");
Emitter emitterSource = applicationContext.getBean(Emitter.class);
MessageCollector collector = applicationContext.getBean(MessageCollector.class);
Message message = collector.forChannel(emitterSource.metrics()).poll(1000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(message);
ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class);
ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(),
ApplicationMetrics.class);
Assert.assertTrue(contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics()));
Assert.assertFalse(contains("mem", applicationMetrics.getMetrics()));
Assert.assertEquals("foobarfoo", applicationMetrics.getName());
Assert.assertEquals(1, applicationMetrics.getInstanceIndex());
Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties()));
applicationContext.close();
}
private boolean contains(String metric, Collection<Metric> metrics) {
boolean contains = false;
for (Metric entry : metrics) {