Commit 9d178a15 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #6588 from Nick Pillitteri

* gh-6588:
  Polish “Allow injection of StatsDClient into StatsdMetricWriter”
  Allow injection of StatsDClient into StatsdMetricWriter
parents 49202570 13a9bc05
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,6 +19,7 @@ package org.springframework.boot.actuate.metrics.statsd; ...@@ -19,6 +19,7 @@ package org.springframework.boot.actuate.metrics.statsd;
import java.io.Closeable; import java.io.Closeable;
import com.timgroup.statsd.NonBlockingStatsDClient; import com.timgroup.statsd.NonBlockingStatsDClient;
import com.timgroup.statsd.StatsDClient;
import com.timgroup.statsd.StatsDClientErrorHandler; import com.timgroup.statsd.StatsDClientErrorHandler;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -26,6 +27,7 @@ import org.apache.commons.logging.LogFactory; ...@@ -26,6 +27,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.writer.Delta; import org.springframework.boot.actuate.metrics.writer.Delta;
import org.springframework.boot.actuate.metrics.writer.MetricWriter; import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -43,7 +45,7 @@ public class StatsdMetricWriter implements MetricWriter, Closeable { ...@@ -43,7 +45,7 @@ public class StatsdMetricWriter implements MetricWriter, Closeable {
private static final Log logger = LogFactory.getLog(StatsdMetricWriter.class); private static final Log logger = LogFactory.getLog(StatsdMetricWriter.class);
private final NonBlockingStatsDClient client; private final StatsDClient client;
/** /**
* Create a new writer instance with the given parameters. * Create a new writer instance with the given parameters.
...@@ -61,12 +63,26 @@ public class StatsdMetricWriter implements MetricWriter, Closeable { ...@@ -61,12 +63,26 @@ public class StatsdMetricWriter implements MetricWriter, Closeable {
* @param port the port for the statsd server * @param port the port for the statsd server
*/ */
public StatsdMetricWriter(String prefix, String host, int port) { public StatsdMetricWriter(String prefix, String host, int port) {
prefix = StringUtils.hasText(prefix) ? prefix : null; this(new NonBlockingStatsDClient(trimPrefix(prefix), host, port,
while (prefix != null && prefix.endsWith(".")) { new LoggingStatsdErrorHandler()));
prefix = prefix.substring(0, prefix.length() - 1); }
/**
* Create a new writer with the given client.
* @param client StatsD client to write metrics with
*/
public StatsdMetricWriter(StatsDClient client) {
Assert.notNull(client);
this.client = client;
}
private static String trimPrefix(String prefix) {
String trimmedPrefix = StringUtils.hasText(prefix) ? prefix : null;
while (trimmedPrefix != null && trimmedPrefix.endsWith(".")) {
trimmedPrefix = trimmedPrefix.substring(0, trimmedPrefix.length() - 1);
} }
this.client = new NonBlockingStatsDClient(prefix, host, port,
new LoggingStatsdErrorHandler()); return trimmedPrefix;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment