Skip to content

Commit d3a6d57

Browse files
daschlMichael Nitschinger
authored and
Michael Nitschinger
committed
SPY-133: Add additional metrics support.
This changeset adds basic metrics for performance measurement and debug support. It is optional and can be enabled through the factory or via a system property. The collected values can be exported through: - The Console - JMX - CSV - SLF4J These output reporters can also be configured through properties, by default the console reporter will be used. Change-Id: If206f0be0c0bd6c6ff11d63f1be25debab2e524e Reviewed-on: http://review.couchbase.org/28575 Reviewed-by: Michael Nitschinger <[email protected]> Tested-by: Michael Nitschinger <[email protected]>
1 parent 903a453 commit d3a6d57

18 files changed

+906
-7
lines changed

build.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@
505505
<dependency group="log4j" artifact="log4j" version="${log4j.version}" optional="true" />
506506
<dependency group="org.slf4j" artifact="slf4j-api" version="${slf4j.version}" optional="true" />
507507
<dependency group="org.springframework" artifact="spring-beans" version="${spring-beans.version}" optional="true" />
508+
<dependency group="com.codahale.metrics" artifact="metrics-core" version="${codahale.metrics.version}" optional="true" />
508509
</ivy:makepom>
509510

510511
<ivy:makepom ivyfile="ivy/spymemcached-test.xml"
@@ -516,6 +517,7 @@
516517
<dependency group="log4j" artifact="log4j" version="${log4j.version}" optional="true" />
517518
<dependency group="org.slf4j" artifact="slf4j-api" version="${slf4j.version}" optional="true" />
518519
<dependency group="org.springframework" artifact="spring-beans" version="${spring-beans.version}" optional="true" />
520+
<dependency group="com.codahale.metrics" artifact="metrics-core" version="${codahale.metrics.version}" />
519521
</ivy:makepom>
520522

521523
<!-- Change the version in the pom file to reflect our claimed version. -->
@@ -700,8 +702,8 @@
700702
<exclude name="LICENSE.txt" />
701703
<exclude name="README.markdown" />
702704
<exclude name=".gitreview" />
703-
<exclude name=".git/** />
704-
<exclude name=".idea/** />
705+
<exclude name=".git/**" />
706+
<exclude name=".idea/**" />
705707
</fileset>
706708
</delete>
707709
</target>

ivy.xml

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ SOFTWARE.
5858
conf="common->master" />
5959
<dependency org="org.springframework" name="spring-beans"
6060
rev="${spring-beans.version}" conf="common->master" />
61+
<dependency org="com.codahale.metrics" name="metrics-core"
62+
rev="${codahale.metrics.version}" conf="common->master" />
6163
<dependency org="checkstyle" name="checkstyle" rev="${checkstyle.version}"
6264
conf="checkstyle->default" />
6365
<dependency org="com.google.code.findbugs" name="findbugs"

ivy/libraries.properties

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ junit.version=4.7
3030
log4j.version=1.2.16
3131
slf4j.version=1.7.5
3232
spring-beans.version=3.0.3.RELEASE
33+
codahale.metrics.version=3.0.1

src/main/java/net/spy/memcached/ConnectionFactory.java

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright (C) 2006-2009 Dustin Sallings
3+
* Copyright (C) 2009-2013 Couchbase, Inc.
34
*
45
* Permission is hereby granted, free of charge, to any person obtaining a copy
56
* of this software and associated documentation files (the "Software"), to deal
@@ -31,6 +32,8 @@
3132
import java.util.concurrent.BlockingQueue;
3233

3334
import net.spy.memcached.auth.AuthDescriptor;
35+
import net.spy.memcached.metrics.MetricCollector;
36+
import net.spy.memcached.metrics.MetricType;
3437
import net.spy.memcached.ops.Operation;
3538
import net.spy.memcached.transcoders.Transcoder;
3639

@@ -158,4 +161,14 @@ MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
158161
* Maximum number of timeout exception for shutdown connection.
159162
*/
160163
int getTimeoutExceptionThreshold();
164+
165+
/**
166+
* If true, metric collections are enabled.
167+
*/
168+
MetricType enableMetrics();
169+
170+
/**
171+
* The currently active {@link MetricCollector}.
172+
*/
173+
MetricCollector getMetricCollector();
161174
}

src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Copyright (C) 2006-2009 Dustin Sallings
3-
* Copyright (C) 2009-2011 Couchbase, Inc.
3+
* Copyright (C) 2009-2013 Couchbase, Inc.
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -29,6 +29,8 @@
2929
import java.util.concurrent.BlockingQueue;
3030

3131
import net.spy.memcached.auth.AuthDescriptor;
32+
import net.spy.memcached.metrics.MetricCollector;
33+
import net.spy.memcached.metrics.MetricType;
3234
import net.spy.memcached.ops.Operation;
3335
import net.spy.memcached.ops.OperationQueueFactory;
3436
import net.spy.memcached.protocol.ascii.AsciiOperationFactory;
@@ -69,6 +71,9 @@ public class ConnectionFactoryBuilder {
6971
protected int timeoutExceptionThreshold =
7072
DefaultConnectionFactory.DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD;
7173

74+
protected MetricType metricType = null;
75+
protected MetricCollector collector = null;
76+
7277
/**
7378
* Set the operation queue factory.
7479
*/
@@ -91,6 +96,7 @@ public ConnectionFactoryBuilder(ConnectionFactory cf) {
9196
setTimeoutExceptionThreshold(cf.getTimeoutExceptionThreshold());
9297
setTranscoder(cf.getDefaultTranscoder());
9398
setUseNagleAlgorithm(cf.useNagleAlgorithm());
99+
setEnableMetrics(cf.enableMetrics());
94100
}
95101

96102
public ConnectionFactoryBuilder setOpQueueFactory(OperationQueueFactory q) {
@@ -264,6 +270,26 @@ public ConnectionFactoryBuilder setTimeoutExceptionThreshold(int to) {
264270
return this;
265271
}
266272

273+
/**
274+
* Enable or disable metric collection.
275+
*
276+
* @param type the metric type to use (or disable).
277+
*/
278+
public ConnectionFactoryBuilder setEnableMetrics(MetricType type) {
279+
metricType = type;
280+
return this;
281+
}
282+
283+
/**
284+
* Set a custom {@link MetricCollector}.
285+
*
286+
* @param collector the metric collector to use.
287+
*/
288+
public ConnectionFactoryBuilder setMetricCollector(MetricCollector collector) {
289+
this.collector = collector;
290+
return this;
291+
}
292+
267293
/**
268294
* Get the ConnectionFactory set up with the provided parameters.
269295
*/
@@ -370,6 +396,16 @@ public int getTimeoutExceptionThreshold() {
370396
return timeoutExceptionThreshold;
371397
}
372398

399+
@Override
400+
public MetricType enableMetrics() {
401+
return metricType == null ? super.enableMetrics() : metricType;
402+
}
403+
404+
@Override
405+
public MetricCollector getMetricCollector() {
406+
return collector == null ? super.getMetricCollector() : collector;
407+
}
408+
373409
};
374410

375411
}

src/main/java/net/spy/memcached/DefaultConnectionFactory.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Copyright (C) 2006-2009 Dustin Sallings
3-
* Copyright (C) 2009-2011 Couchbase, Inc.
3+
* Copyright (C) 2009-2013 Couchbase, Inc.
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -37,6 +37,10 @@
3737

3838
import net.spy.memcached.auth.AuthDescriptor;
3939
import net.spy.memcached.compat.SpyObject;
40+
import net.spy.memcached.metrics.DefaultMetricCollector;
41+
import net.spy.memcached.metrics.MetricCollector;
42+
import net.spy.memcached.metrics.MetricType;
43+
import net.spy.memcached.metrics.NoopMetricCollector;
4044
import net.spy.memcached.ops.Operation;
4145
import net.spy.memcached.protocol.ascii.AsciiMemcachedNodeImpl;
4246
import net.spy.memcached.protocol.ascii.AsciiOperationFactory;
@@ -105,10 +109,17 @@ public class DefaultConnectionFactory extends SpyObject implements
105109
*/
106110
public static final int DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD = 998;
107111

112+
/**
113+
* Turn off metric collection by default.
114+
*/
115+
public static final MetricType DEFAULT_METRIC_TYPE = MetricType.OFF;
116+
108117
protected final int opQueueLen;
109118
private final int readBufSize;
110119
private final HashAlgorithm hashAlg;
111120

121+
private MetricCollector metrics;
122+
112123
/**
113124
* Construct a DefaultConnectionFactory with the given parameters.
114125
*
@@ -121,6 +132,7 @@ public DefaultConnectionFactory(int qLen, int bufSize, HashAlgorithm hash) {
121132
opQueueLen = qLen;
122133
readBufSize = bufSize;
123134
hashAlg = hash;
135+
metrics = null;
124136
}
125137

126138
/**
@@ -345,6 +357,31 @@ public int getTimeoutExceptionThreshold() {
345357
return DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD;
346358
}
347359

360+
@Override
361+
public MetricType enableMetrics() {
362+
String metricType = System.getProperty("net.spy.metrics.type");
363+
return metricType == null
364+
? DEFAULT_METRIC_TYPE : MetricType.valueOf(metricType.toUpperCase());
365+
}
366+
367+
@Override
368+
public MetricCollector getMetricCollector() {
369+
if (metrics != null) {
370+
return metrics;
371+
}
372+
373+
String enableMetrics = System.getProperty("net.spy.metrics.enable");
374+
if (enableMetrics().equals(MetricType.OFF) || enableMetrics == "false") {
375+
getLogger().debug("Metric collection disabled.");
376+
metrics = new NoopMetricCollector();
377+
} else {
378+
getLogger().info("Metric collection enabled (Profile " + enableMetrics() + ").");
379+
metrics = new DefaultMetricCollector();
380+
}
381+
382+
return metrics;
383+
}
384+
348385
protected String getName() {
349386
return "DefaultConnectionFactory";
350387
}

0 commit comments

Comments
 (0)