下面列出了怎么用com.codahale.metrics.graphite.GraphiteSender的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public GraphiteSender get() {
HostAndPort hostAndPort = configuration.getAddress();
String host = hostAndPort.getHost();
int port = hostAndPort.getPortOrDefault(2003);
switch (configuration.getProtocol()) {
case PICKLE:
return new PickledGraphite(
host,
port,
SocketFactory.getDefault(),
configuration.getCharset(),
configuration.getPickleBatchSize());
case TCP:
return new Graphite(host, port, SocketFactory.getDefault(), configuration.getCharset());
case UDP:
return new GraphiteUDP(host, port);
default:
throw new IllegalArgumentException("Unknown Graphite protocol \"" + configuration.getProtocol() + "\"");
}
}
private GraphiteReporterService(Builder builder) {
super(requireNonNull(builder.serviceName));
MetricRegistry registry = requireNonNull(builder.registry);
GraphiteSender graphiteSender = requireNonNull(builder.graphiteSender);
String prefix = requireNonNull(builder.prefix);
this.reportingIntervalMillis = builder.reportingIntervalMillis;
this.reporter = GraphiteReporter.forRegistry(registry)
.prefixedWith(prefix)
.convertRatesTo(SECONDS)
.convertDurationsTo(MILLISECONDS)
.filter(ALL)
.build(graphiteSender);
}
/**
* Builds a {@link GraphiteReporter} with the given properties, sending metrics using the
* given {@link GraphiteSender}.
*
* @param graphite a {@link GraphiteSender}
* @return a {@link GraphiteReporter}
*/
public GraphiteReporter build(GraphiteSender graphite) {
return new GraphiteReporter(registry,
graphite,
clock,
prefix,
rateUnit,
durationUnit,
filter);
}
private GraphiteReporter(MetricRegistry registry,
GraphiteSender graphite,
Clock clock,
String prefix,
TimeUnit rateUnit,
TimeUnit durationUnit,
MetricFilter filter) {
super(registry, "graphite-reporter", filter, rateUnit, durationUnit);
this.graphite = graphite;
this.clock = clock;
this.prefix = prefix;
}
@Override
protected void configure() {
bind(GraphiteSender.class).toProvider(GraphiteSenderProvider.class);
bind(GraphiteReporter.class).toProvider(GraphiteReporterProvider.class);
addConfigBeans();
addInitializer(MetricsGraphiteReporterService.class);
}
@Inject
public GraphiteReporterProvider(MetricsGraphiteReporterConfiguration configuration,
GraphiteSender graphiteSender,
MetricRegistry metricRegistry) {
this.configuration = requireNonNull(configuration);
this.graphiteSender = requireNonNull(graphiteSender);
this.metricRegistry = requireNonNull(metricRegistry);
}
@Test
public void getReturnsGraphite() throws Exception {
final MetricsGraphiteReporterConfiguration configuration = new MetricsGraphiteReporterConfiguration() {
@Override
public GraphiteProtocol getProtocol() {
return GraphiteProtocol.TCP;
}
};
final GraphiteSenderProvider provider = new GraphiteSenderProvider(configuration);
final GraphiteSender graphiteSender = provider.get();
assertTrue(graphiteSender instanceof Graphite);
assertFalse(graphiteSender.isConnected());
}
@Test
public void getReturnsGraphiteUDP() throws Exception {
final MetricsGraphiteReporterConfiguration configuration = new MetricsGraphiteReporterConfiguration() {
@Override
public GraphiteProtocol getProtocol() {
return GraphiteProtocol.UDP;
}
};
final GraphiteSenderProvider provider = new GraphiteSenderProvider(configuration);
final GraphiteSender graphiteSender = provider.get();
assertTrue(graphiteSender instanceof GraphiteUDP);
assertFalse(graphiteSender.isConnected());
}
@Test
public void getReturnsGraphitePickledGraphite() throws Exception {
final MetricsGraphiteReporterConfiguration configuration = new MetricsGraphiteReporterConfiguration() {
@Override
public GraphiteProtocol getProtocol() {
return GraphiteProtocol.PICKLE;
}
};
final GraphiteSenderProvider provider = new GraphiteSenderProvider(configuration);
final GraphiteSender graphiteSender = provider.get();
assertTrue(graphiteSender instanceof PickledGraphite);
assertFalse(graphiteSender.isConnected());
}
@Test
public void get() throws Exception {
final MetricsGraphiteReporterConfiguration configuration = new MetricsGraphiteReporterConfiguration();
final GraphiteSender graphiteSender = new GraphiteUDP("127.0.0.1", 12345);
final MetricRegistry metricRegistry = new MetricRegistry();
final GraphiteReporterProvider provider = new GraphiteReporterProvider(configuration, graphiteSender, metricRegistry);
final GraphiteReporter reporter = provider.get();
assertNotNull(reporter);
}
private void send(GraphiteSender graphite, TreeMap<Long, Metric> metricValues) {
// do not send the last FLUSH_TIMESTAMP_WAIT timestamps to prevent duplicate timestamps
// being sent to graphite. (only the last will count) and will cause dips in charts
int max = metricValues.size() - FLUSH_TIMESTAMP_WAIT;
if (max < 1) {
// no metrics to send
return;
}
metricValues.entrySet().stream().limit(max).forEach(e -> send(graphite, e.getValue()));
}
private void send(GraphiteSender graphite, Metric m) {
try {
graphite.send(m.getName(), String.valueOf(m.getValue()), m.getTime());
if (m.getSamples() > 0) {
graphite
.send(StringUtils.replace(m.getName(), ".median", ".samples"),
String.valueOf(m.getSamples()), m.getTime());
}
} catch (IOException e) {
log.error("Error while sending metric: {}", m, e);
}
}
private void initialiseMetrics(PublicApiConfig configuration, Environment environment) {
GraphiteSender graphiteUDP = new GraphiteUDP(configuration.getGraphiteHost(), Integer.parseInt(configuration.getGraphitePort()));
GraphiteReporter.forRegistry(environment.metrics())
.prefixedWith(SERVICE_METRICS_NODE)
.build(graphiteUDP)
.start(GRAPHITE_SENDING_PERIOD_SECONDS, TimeUnit.SECONDS);
}
GraphiteMetricSender(GraphiteSender graphite, Clock clock, String prefix) {
this.graphite = graphite;
this.clock = clock;
this.prefix = prefix;
}
/**
* Overrides the actual methods in order to inject the mock {@link #graphiteSenderMock}.
* <p>
* If this method is called with the test property values, it returns the mock. Otherwise operate
* regularly.
*
* @param host the provided hostname.
* @param port the provided port.
* @param charset the provided graphite server charset.
* @return {@link #graphiteSenderMock} if all params were the constant test params, regular result otherwise.
*/
@Override
protected GraphiteSender createSender(String host, int port, Charset charset) {
if (TEST_HOST.equals(host) && TEST_PORT == port && TEST_CHARSET.equals(charset)) {
return graphiteSenderMock;
}
return super.createSender(host, port, charset);
}
/**
* Create a sender.
*
* @param host the hostname of the server to connect to.
* @param port the port on which the server listens.
* @param charset the charset in which the server expects logs.
* @return The created sender.
*/
protected GraphiteSender createSender(String host, int port, Charset charset) {
return new Graphite(host, port, SocketFactory.getDefault(), charset);
}