├── .gitignore
├── .mvn
└── wrapper
│ ├── MavenWrapperDownloader.java
│ └── maven-wrapper.properties
├── LICENSE
├── NOTICE
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
├── java
│ └── org
│ │ └── jmxtrans
│ │ └── embedded
│ │ ├── DeferredResultNameStrategyExpressionEvaluatorInitializer.java
│ │ ├── EmbeddedJmxTrans.java
│ │ ├── EmbeddedJmxTransException.java
│ │ ├── EmbeddedJmxTransMBean.java
│ │ ├── Query.java
│ │ ├── QueryAttribute.java
│ │ ├── QueryMBean.java
│ │ ├── QueryResult.java
│ │ ├── ResultNameStrategy.java
│ │ ├── config
│ │ ├── ConfigurationParser.java
│ │ ├── EtcdKVStore.java
│ │ ├── KVStore.java
│ │ └── KeyValue.java
│ │ ├── output
│ │ ├── AbstractOutputWriter.java
│ │ ├── ConsoleWriter.java
│ │ ├── CopperEggWriter.java
│ │ ├── CsvWriter.java
│ │ ├── GraphiteHttpWriter.java
│ │ ├── GraphitePickleWriter.java
│ │ ├── GraphiteWriter.java
│ │ ├── LibratoWriter.java
│ │ ├── NoOpWriter.java
│ │ ├── OutputWriter.java
│ │ ├── Slf4jWriter.java
│ │ ├── StackdriverWriter.java
│ │ └── StatsDWriter.java
│ │ ├── servlet
│ │ └── EmbeddedJmxTransLoaderListener.java
│ │ ├── spring
│ │ ├── EmbeddedJmxTransBeanDefinitionParser.java
│ │ ├── EmbeddedJmxTransFactory.java
│ │ ├── EmbeddedJmxTransNamespaceHandler.java
│ │ ├── SpringEmbeddedJmxTrans.java
│ │ └── SpringEmbeddedJmxTransMBean.java
│ │ └── util
│ │ ├── CachingReference.java
│ │ ├── Preconditions.java
│ │ ├── StringUtils2.java
│ │ ├── concurrent
│ │ ├── DiscardingBlockingQueue.java
│ │ ├── DiscardingBlockingQueueMBean.java
│ │ └── NamedThreadFactory.java
│ │ ├── io
│ │ └── IoUtils2.java
│ │ ├── jmx
│ │ └── JmxUtils2.java
│ │ ├── json
│ │ ├── PlaceholderEnabledJsonNodeFactory.java
│ │ └── PropertyPlaceholderResolver.java
│ │ ├── net
│ │ ├── HostAndPort.java
│ │ ├── SocketOutputStream.java
│ │ ├── SocketWriter.java
│ │ ├── UDPDatagramWriter.java
│ │ └── ssl
│ │ │ └── SslUtils.java
│ │ └── pool
│ │ ├── SocketOutputStreamPoolFactory.java
│ │ ├── SocketWriterPoolFactory.java
│ │ └── UDPSocketWriterPoolFactory.java
└── resources
│ ├── META-INF
│ ├── spring.handlers
│ ├── spring.schemas
│ └── spring.tooling
│ └── org
│ └── jmxtrans
│ └── embedded
│ ├── config
│ ├── cloudbees-tomcat-6.json
│ ├── jmxtrans-internals-servlet-container.json
│ ├── jmxtrans-internals.json
│ ├── jvm-sun-hotspot.json
│ ├── tomcat-6.json
│ └── tomcat-7.json
│ └── spring
│ ├── jmxtrans-1.0.xsd
│ └── jmxtrans-1.1.xsd
├── site
└── resources
│ └── CNAME
└── test
├── java
└── org
│ └── jmxtrans
│ └── embedded
│ ├── EmbeddedJmxTransIntegrationTest.java
│ ├── EmbeddedJmxTransShutdownHookIntegrationTest.java
│ ├── MockMemoryPool.java
│ ├── MockMemoryPoolMBean.java
│ ├── QueryTest.java
│ ├── ResultNameStrategyTest.java
│ ├── TestUtils.java
│ ├── config
│ ├── ConfigurationMergeTest.java
│ ├── ConfigurationResultNameStrategyTest.java
│ ├── ConfigurationTemplatesTest.java
│ └── ConfigurationTest.java
│ ├── output
│ ├── AbstractOutputWriterTest.java
│ ├── CopperEggWriterTest.java
│ ├── CsvWriterTest.java
│ ├── GraphiteHttpWriterIntegrationTest.java
│ ├── GraphitePickleWriterIntegrationTest.java
│ ├── GraphiteWriterTcpIntegrationTest.java
│ ├── GraphiteWriterTcpTlsIntegrationTest.java
│ ├── GraphiteWriterUdpIntegrationTest.java
│ ├── LibratoMetricsIntegrationTest.java
│ ├── LibratoWriterTest.java
│ ├── StackdriverWriterTest.java
│ ├── StatsDIntegrationTest.java
│ ├── StatsDWriterChannelFailureTest.java
│ ├── StatsDWriterIntegrationTest.java
│ ├── TestWriter1.java
│ ├── TestWriter2.java
│ ├── TestWriter3.java
│ └── TestWriter4.java
│ ├── spring
│ ├── EmbeddedJmxTransBeanDefinitionParser1Test.java
│ ├── EmbeddedJmxTransBeanDefinitionParser2Test.java
│ ├── EmbeddedJmxTransBeanDefinitionParser3Test.java
│ └── EmbeddedJmxTransFactoryTest.java
│ └── util
│ ├── StringUtils2Test.java
│ ├── concurrent
│ └── DiscardingBlockingQueueTest.java
│ ├── json
│ ├── PlaceholderEnabledJsonNodeFactoryTest.java
│ └── PropertyPlaceholderResolverTest.java
│ ├── net
│ └── ssl
│ │ ├── SslUtilsIntegrationTest.java
│ │ └── SslUtilsTest.java
│ └── pool
│ └── ManagedGenericKeyedObjectPoolTest.java
├── resources
├── .gitignore
├── graphite-tcp-config.template.properties
├── graphite-tcp-tls-config.template.properties
├── graphite-udp-config.template.properties
├── librato-config.template.properties
├── logback-test.xml
└── org
│ └── jmxtrans
│ └── embedded
│ ├── jmxtrans-config-merge-1-test.json
│ ├── jmxtrans-config-merge-2-test.json
│ ├── jmxtrans-config-resultnamestrategy.json
│ ├── jmxtrans-config-test.json
│ ├── jmxtrans-factory-test.json
│ ├── jmxtrans-integ-test.json
│ ├── output
│ └── statsd-writer.json
│ ├── spring
│ ├── test-spring-configuration-1.xml
│ ├── test-spring-configuration-2.xml
│ └── test-spring-configuration-3.xml
│ └── util
│ ├── json
│ └── jmxtrans-placeholder-test.json
│ └── net
│ └── ssl
│ ├── README.txt
│ ├── cacerts
│ ├── keystore.jks
│ ├── selfsigned.cer
│ └── truststore.jks
└── statsd
├── README.md
├── start.sh
└── statsd-config-console.js
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Package Files #
4 | *.jar
5 | *.war
6 | *.ear
7 |
8 | # Maven
9 | target
10 |
11 | # Intellij
12 | .idea
13 | *.iml
14 |
15 | **/.envrc
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2010-2013 the original author or authors
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | This product includes/uses software, Apache Commons : Pool (http://commons.apache.org/),
2 | developed by The Apache Software Foundation (http://www.apache.org)
3 | License: The Apache Software License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.txt)
4 |
5 |
6 | This product includes/uses software, Logback (http://logback.qos.ch/),
7 | developed by QOS.ch (http://www.qos.ch/)
8 | License: Eclipse Public License v1.0 (http://www.eclipse.org/legal/epl-v10.html)
9 | or (per the licensee's choosing)
10 | GNU Lesser General Public License, version 2.1 (http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html)
11 |
12 | This product includes/uses software, SLF4J (http://www.slf4j.org/),
13 | developed by QOS.ch (http://www.qos.ch/)
14 | License: MIT License (http://www.slf4j.org/license.html)
15 |
16 | JUnit (http://junit.org)
17 | developped by Harmcrest (www.hamcrest.org)
18 | License: Common Public License - v 1.0 (http://junit.sourceforge.net/cpl-v10.html)
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # embedded-jmxtrans
2 |
3 | In process JMX metrics exporter. Inspired by the standalone version of jmxtrans but embedded inside your java process (e.g. Tomcat).
4 |
5 | An in process JMX Exporter will solve the problem of remote JMX access in cloud-style and elastic environments where the IP address of the Java servers is unknown and where RMI-IIOP is disabled (e.g. Amazon Elastic Beanstalk, Cloudbees, ...).
6 |
7 |
8 | * [Documentation](https://github.com/jmxtrans/embedded-jmxtrans/wiki)
9 | * [Google Group](https://groups.google.com/forum/#!forum/jmxtrans) if you have anything to discuss
10 | * [Latest javadocs](http://jmxtrans.github.com/embedded-jmxtrans/apidocs/)
11 | * [Sample](https://github.com/jmxtrans/embedded-jmxtrans-samples)
12 |
13 | ## Getting started
14 |
15 | Getting started guide for Spring Framework enabled web applications.
16 |
17 | ### Maven
18 |
19 | Add `embedded-jmxtrans` dependency
20 |
21 | ```xml
22 |
23 | org.jmxtrans.embedded
24 | embedded-jmxtrans
25 | 1.0.12
26 |
27 | ```
28 |
29 | ### Spring Framework
30 |
31 | Declare `` in your Spring configuration :
32 | ```xml
33 |
37 |
38 |
39 | classpath:jmxtrans.json
40 | classpath:org/jmxtrans/embedded/config/tomcat-6.json
41 | classpath:org/jmxtrans/embedded/config/jmxtrans-internals.json
42 | classpath:org/jmxtrans/embedded/config/jvm-sun-hotspot.json
43 |
44 |
45 | ```
46 |
47 | ### Configure writers
48 |
49 | Create `src/main/resources/jmxtrans.json`, add your mbeans and declare both `ConsoleWriter` (output to `stdout`) and `GraphiteWriter`
50 |
51 | ```json
52 | {
53 | "queries": [
54 | {
55 | "objectName": "cocktail:type=ShoppingCartController,name=ShoppingCartController",
56 | "resultAlias": "",
57 | "attributes": [
58 | {
59 | "name": "SalesRevenueInCentsCounter",
60 | "resultAlias": "sales.revenueInCentsCounter"
61 | }
62 | ]
63 | },
64 | {
65 | "objectName": "com.cocktail:type=CocktailService,name=cocktailService",
66 | "resultAlias": "cocktail.controller",
67 | "attributes": ["SearchedCocktailCount", "DisplayedCocktailCount", "SendCocktailRecipeCount"]
68 | }
69 | ],
70 | "outputWriters": [
71 | {
72 | "@class": "org.jmxtrans.embedded.output.ConsoleWriter"
73 | },
74 | {
75 | "@class": "org.jmxtrans.embedded.output.GraphiteWriter",
76 | "settings": {
77 | "host": "${graphite.host:localhost}",
78 | "port": "${graphite.port:2003}"
79 | }
80 | }
81 | ]
82 | }
83 | ```
84 |
85 | In this sample, Graphite host & port are defaulted to `localhost:2003` and can be overwritten with system properties or environment variables, for example in `$CATALINA_BASE/conf/catalina.properties`.
86 |
87 | #### Dynamic MBeans
88 |
89 | If metrics are provided by a Dynamic MBean all attributes can be collected by declaring an empty attributes array, for example :
90 |
91 | ```json
92 | {
93 | "queries": [
94 | {
95 | "objectName": "com.cocktail:type=CocktailService,name=cocktailService",
96 | "resultAlias": "cocktail.controller",
97 | "attributes": [ ]
98 | }
99 | ]
100 | }
101 | ```
102 |
103 | ### Start application and check metrics
104 |
105 | #### Check metrics in the Console
106 |
107 | ```
108 | ...
109 | jvm.os.SystemLoadAverage 2.97265625 1358242428
110 | tomcat.thread-pool.http-8080.currentThreadsBusy 0 1358242458
111 | tomcat.manager.localhost._.activeSessions 0 1358242458
112 | tomcat.servlet.__localhost_.jsp.processingTime 0 1358242458
113 | tomcat.servlet.__localhost_.jsp.errorCount 0 1358242458
114 | tomcat.servlet.__localhost_.jsp.requestCount 0 1358242458
115 | cocktail.controller.SearchedCocktailCount 12 1358242458
116 | ...
117 | ```
118 |
119 | #### Check metrics in Graphite
120 |
121 | 
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/EmbeddedJmxTransException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded;
25 |
26 | /**
27 | * Generic exception of the EmbeddedJmxTrans component.
28 | *
29 | * @author Cyrille Le Clerc
30 | */
31 | public class EmbeddedJmxTransException extends RuntimeException {
32 | public EmbeddedJmxTransException() {
33 | super();
34 | }
35 |
36 | public EmbeddedJmxTransException(String message) {
37 | super(message);
38 | }
39 |
40 | public EmbeddedJmxTransException(String message, Throwable cause) {
41 | super(message, cause);
42 | }
43 |
44 | public EmbeddedJmxTransException(Throwable cause) {
45 | super(cause);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/EmbeddedJmxTransMBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded;
25 |
26 | /**
27 | * JMX MBean interface of the {@link EmbeddedJmxTrans}.
28 | *
29 | * @author Cyrille Le Clerc
30 | */
31 | public interface EmbeddedJmxTransMBean {
32 |
33 | int getNumQueryThreads();
34 |
35 | int getQueryIntervalInSeconds();
36 |
37 | int getExportIntervalInSeconds();
38 |
39 | int getNumExportThreads();
40 |
41 | void collectMetrics();
42 |
43 | void exportCollectedMetrics();
44 |
45 | int getCollectedMetricsCount();
46 |
47 | long getCollectionDurationInNanos();
48 |
49 | long getCollectionDurationInMillis();
50 |
51 | int getCollectionCount();
52 |
53 | int getExportedMetricsCount();
54 |
55 | long getExportDurationInNanos();
56 |
57 | long getExportDurationInMillis();
58 |
59 | int getExportCount();
60 |
61 | int getDiscardedResultsCount();
62 |
63 | String getState();
64 |
65 | void stop() throws Exception;
66 |
67 | void start() throws Exception;
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/QueryMBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded;
25 |
26 | import javax.management.ObjectName;
27 |
28 | /**
29 | * @author Cyrille Le Clerc
30 | */
31 | public interface QueryMBean {
32 | void collectMetrics();
33 |
34 | int exportCollectedMetrics();
35 |
36 | int getCollectedMetricsCount();
37 |
38 | long getCollectionDurationInNanos();
39 |
40 | int getCollectionCount();
41 |
42 | int getExportedMetricsCount();
43 |
44 | long getExportDurationInNanos();
45 |
46 | int getExportCount();
47 |
48 | String getResultAlias();
49 |
50 | ObjectName getObjectName();
51 |
52 | String getId();
53 |
54 | int getDiscardedResultsCount();
55 | }
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/QueryResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded;
25 |
26 | import org.jmxtrans.embedded.util.Preconditions;
27 |
28 | import javax.annotation.Nonnull;
29 | import javax.annotation.Nullable;
30 | import java.sql.Timestamp;
31 | import java.util.concurrent.TimeUnit;
32 |
33 | /**
34 | * Value of a collected metric.
35 | *
36 | * @author Cyrille Le Clerc
37 | * @author Jon Stevens
38 | */
39 | public class QueryResult {
40 |
41 | @Nonnull
42 | private final String name;
43 |
44 | private final long epochInMillis;
45 |
46 | @Nullable
47 | private final Object value;
48 |
49 | @Nullable
50 | private final String type;
51 |
52 | /**
53 | * @param name plain name of the metric (variables (e.g. %my-jmx-attr%) must have been resolved).
54 | * @param value value of the collected metric
55 | * @param epochInMillis collect time in millis (see {@link System#currentTimeMillis()})
56 | */
57 | public QueryResult(@Nonnull String name, @Nullable Object value, long epochInMillis) {
58 | this(name, null, value, epochInMillis);
59 | }
60 |
61 | /**
62 | * @param name plain name of the metric (variables (e.g. %my-jmx-attr%) must have been resolved).
63 | * @param type type of the metric (e.g. "{@code counter}", "{@code gauge}", ...)
64 | * @param value value of the collected metric
65 | * @param epochInMillis collect time in millis (see {@link System#currentTimeMillis()})
66 | */
67 | public QueryResult(@Nonnull String name, @Nullable String type, @Nullable Object value, long epochInMillis) {
68 | this.name = Preconditions.checkNotEmpty(name);
69 | this.value = value;
70 | this.epochInMillis = epochInMillis;
71 | this.type = type;
72 | }
73 |
74 | @Nonnull
75 | public String getName() {
76 | return name;
77 | }
78 |
79 | @Nullable
80 | public String getType() {
81 | return type;
82 | }
83 |
84 | public long getEpochInMillis() {
85 | return epochInMillis;
86 | }
87 |
88 | public long getEpoch(TimeUnit timeUnit) {
89 | return timeUnit.convert(epochInMillis, TimeUnit.MILLISECONDS);
90 | }
91 |
92 | @Nullable
93 | public Object getValue() {
94 | return value;
95 | }
96 |
97 | @Override
98 | public String toString() {
99 | return "QueryResult{" +
100 | " epoch=" + new Timestamp(epochInMillis) +
101 | ", name='" + name + '\'' +
102 | ", value=" + value +
103 | ", type=" + type +
104 | '}';
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/config/KVStore.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 | * associated documentation files (the "Software"), to deal in the Software without restriction,
6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 | * furnished to do so, subject to the following conditions:
9 | *
10 | * The above copyright notice and this permission notice shall be included in all copies or
11 | * substantial portions of the Software.
12 | *
13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 | * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 | */
19 |
20 |
21 | package org.jmxtrans.embedded.config;
22 |
23 | import org.jmxtrans.embedded.EmbeddedJmxTransException;
24 |
25 | /**
26 | * This interface represents a super simplified key value store from which you can only read keys.
27 | * You get back the key value and a version id
28 | *
29 | * @author Simone Zorzetti
30 | */
31 | public interface KVStore {
32 |
33 | /**
34 | * Retrieves the value of a key from the kv store. The version can be used to determine if a key
35 | * was changed since the last read
36 | *
37 | * @param KeyURI: uri of the key eg: etcd://127.0.0.1:123/level1/config for a cluster you can use
38 | * etcd://[ipaddr1:port1, ipaddr:port2,...]:/path
39 | * @return a KeyValue object which hold the key value and the version id (modification index or
40 | * trx id)
41 | * @throws EmbeddedJmxTransException
42 | */
43 | public KeyValue getKeyValue(String KeyURI) throws EmbeddedJmxTransException;
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/config/KeyValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 | * associated documentation files (the "Software"), to deal in the Software without restriction,
6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 | * furnished to do so, subject to the following conditions:
9 | *
10 | * The above copyright notice and this permission notice shall be included in all copies or
11 | * substantial portions of the Software.
12 | *
13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 | * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 | */
19 |
20 | package org.jmxtrans.embedded.config;
21 |
22 | /**
23 | * This class represents a value retrieved from a key value store. Associated with the value comes
24 | * the version. The version is a unique for the value of the key. It can be used to determine if a
25 | * key has been changed. Etcd and Consul use an "index", a monotonically incrementing number,
26 | * Zookeeper uses transaction ids
27 | *
28 | * @author Simone Zorzetti
29 | */
30 | public class KeyValue {
31 |
32 | private final String value;
33 | private final String version;
34 |
35 | /**
36 | * Constructor
37 | */
38 | public KeyValue(String val, String idx) {
39 | value = val;
40 | version = idx;
41 | }
42 |
43 | /**
44 | * The value of the key
45 | *
46 | * @return
47 | */
48 | public String getValue() {
49 | return value;
50 | }
51 |
52 | /**
53 | * The version of the key
54 | *
55 | * @return
56 | */
57 | public String getVersion() {
58 | return version;
59 | }
60 |
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/output/ConsoleWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.output;
25 |
26 | import org.jmxtrans.embedded.QueryResult;
27 |
28 | import java.util.concurrent.TimeUnit;
29 |
30 | /**
31 | * Output results to stdout.
32 | *
33 | * Settings:
34 | *
35 | *
"enabled": flag to enable/disable the writer. Optional, default value: {$code true}.
43 | * With timestamp in seconds.
44 | *
45 | * @author Cyrille Le Clerc
46 | */
47 | public class ConsoleWriter extends AbstractOutputWriter implements OutputWriter {
48 |
49 | /**
50 | * Write metrics to stdout.
51 | */
52 | @Override
53 | public void write(Iterable results) {
54 | for (QueryResult result : results) {
55 | String msg = result.getName() + " " + result.getValue() + " " + result.getEpoch(TimeUnit.SECONDS);
56 | System.out.println(msg);
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/output/NoOpWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.output;
25 |
26 | import org.jmxtrans.embedded.QueryResult;
27 |
28 | import java.io.IOException;
29 | import java.io.Writer;
30 |
31 | /**
32 | * Used for testing. Send results to "/dev/null".
33 | *
34 | * Settings:
35 | *
36 | *
"enabled": flag to enable/disable the writer. Optional, default value: {$code true}.
37 | *
38 | *
39 | *
40 | * @author Cyrille Le Clerc
41 | */
42 | public class NoOpWriter extends AbstractOutputWriter {
43 |
44 | static class NullWriter extends Writer {
45 |
46 | @Override
47 | public void write(char[] buffer, int off, int len) throws IOException {
48 | }
49 |
50 | @Override
51 | public void flush() throws IOException {
52 | }
53 |
54 | @Override
55 | public void close() throws IOException {
56 | }
57 | }
58 |
59 | private Writer out = new NullWriter();
60 |
61 | /**
62 | * Write metrics to /dev/null
63 | */
64 | @Override
65 | public void write(Iterable results) {
66 | for (QueryResult result : results) {
67 | try {
68 | out.write(result.toString());
69 | } catch (IOException e) {
70 | // never occurs
71 | }
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/output/OutputWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.output;
25 |
26 | import org.jmxtrans.embedded.QueryResult;
27 |
28 | import javax.annotation.PostConstruct;
29 | import javax.annotation.PreDestroy;
30 | import java.util.Map;
31 |
32 | /**
33 | * Interface of define a writer that will convert the collected JMX metrics to a given format (e.g. file, Graphite, ...)
34 | *
35 | * @author Cyrille Le Clerc
36 | * @author Jon Stevens
37 | */
38 | public interface OutputWriter {
39 |
40 | /**
41 | * Configuration settings of the {@linkplain OutputWriter}.
42 | */
43 | Map getSettings();
44 |
45 | /**
46 | * Sets the configuration setting of the {@linkplain OutputWriter}.
47 | */
48 | void setSettings(Map settings);
49 |
50 | /**
51 | * Write all the given {@linkplain QueryResult} to the target system.
52 | */
53 | void write(Iterable results);
54 |
55 | /**
56 | * Initialize the {@linkplain OutputWriter}. Called at the startup of the {@linkplain org.jmxtrans.embedded.EmbeddedJmxTrans}.
57 | *
58 | * This is the place to load the configuration (from the injected settings) and to initialize writer's resource like object pools.
59 | *
60 | * Writer are started even if {@link #isEnabled()} is false.
61 | *
62 | * @throws Exception
63 | */
64 | @PostConstruct
65 | void start() throws Exception;
66 |
67 | /**
68 | * Stops the underlying resources of the {@linkplain OutputWriter}. Called at the shutdown of the {@linkplain org.jmxtrans.embedded.EmbeddedJmxTrans}.
69 | *
70 | * This is the place to stop the writer's resources like object pools, threads and sockets.
71 | *
72 | * @throws Exception
73 | */
74 | @PreDestroy
75 | void stop() throws Exception;
76 |
77 | boolean isEnabled();
78 |
79 | void setEnabled(boolean enabled);
80 | }
81 |
82 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/output/Slf4jWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.output;
25 |
26 | import org.jmxtrans.embedded.QueryResult;
27 | import org.slf4j.Logger;
28 | import org.slf4j.LoggerFactory;
29 |
30 | import java.util.concurrent.TimeUnit;
31 |
32 | /**
33 | * SLF4J based {@linkplain OutputWriter} implementation.
34 | *
35 | * Settings:
36 | *
37 | *
"logger": Name of the logger. Optional, default value: "org.jmxtrans.embedded.output.Slf4jWriter"
38 | *
"enabled": flag to enable/disable the writer. Optional, default value: {$code true}.
46 | * With timestamp in seconds.
47 | *
48 | * @author Cyrille Le Clerc
49 | */
50 | public class Slf4jWriter extends AbstractOutputWriter {
51 |
52 | private Logger logger = LoggerFactory.getLogger(getClass());
53 |
54 | /**
55 | * Initialize the logger.
56 | */
57 | @Override
58 | public void start() {
59 | super.start();
60 | logger = LoggerFactory.getLogger(getStringSetting("logger", getClass().getName()));
61 | }
62 |
63 | @Override
64 | public void write(Iterable results) {
65 | for (QueryResult result : results) {
66 | String msg = result.getName() + " " + result.getValue() + " " + result.getEpoch(TimeUnit.SECONDS);
67 | logger.info(msg);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/spring/EmbeddedJmxTransBeanDefinitionParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2012-2013 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.jmxtrans.embedded.spring;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | import org.jmxtrans.embedded.EmbeddedJmxTransException;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 | import org.springframework.beans.factory.config.BeanDefinition;
25 | import org.springframework.beans.factory.support.AbstractBeanDefinition;
26 | import org.springframework.beans.factory.support.BeanDefinitionBuilder;
27 | import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
28 | import org.springframework.beans.factory.xml.ParserContext;
29 | import org.springframework.util.StringUtils;
30 | import org.w3c.dom.Element;
31 | import org.w3c.dom.Node;
32 | import org.w3c.dom.NodeList;
33 |
34 | /**
35 | * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} for an {@link EmbeddedJmxTransFactory}.
36 | *
37 | * @author Cyrille Le Clerc
38 | */
39 | public class EmbeddedJmxTransBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
40 |
41 | private static final String CONFIGURATION_ATTRIBUTE = "configuration";
42 | private static final String CONFIGURATION_SCAN_PERIOD_IN_SECONDS = "configuration-scan-period-in-seconds";
43 | private static final String IGNORE_CONFIGURATION_NOT_FOUND_ATTRIBUTE = "ignore-configuration-not-found";
44 |
45 | private final Logger logger = LoggerFactory.getLogger(getClass());
46 |
47 | @Override
48 | protected Class getBeanClass(Element element) {
49 | return EmbeddedJmxTransFactory.class;
50 | }
51 |
52 | @Override
53 | protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
54 | String id = element.getAttribute(ID_ATTRIBUTE);
55 | return (StringUtils.hasText(id) ? id : "jmxtrans");
56 | }
57 |
58 | @Override
59 | protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
60 | builder.setRole(BeanDefinition.ROLE_APPLICATION);
61 | builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
62 |
63 | if (element.hasAttribute(CONFIGURATION_SCAN_PERIOD_IN_SECONDS)) {
64 | builder.addPropertyValue("configurationScanPeriodInSeconds", element.getAttribute(CONFIGURATION_SCAN_PERIOD_IN_SECONDS));
65 | }
66 |
67 | if (element.hasAttribute(IGNORE_CONFIGURATION_NOT_FOUND_ATTRIBUTE)) {
68 | builder.addPropertyValue("ignoreConfigurationNotFound", element.getAttribute(IGNORE_CONFIGURATION_NOT_FOUND_ATTRIBUTE));
69 | }
70 | List configurationUrls = new ArrayList();
71 | if (element.hasAttribute(CONFIGURATION_ATTRIBUTE)) {
72 | String configurationUrl = element.getAttribute(CONFIGURATION_ATTRIBUTE);
73 | logger.debug("Add configuration from attribute {}", configurationUrl);
74 | configurationUrls.add(configurationUrl);
75 | }
76 |
77 | NodeList configurationNodeList = element.getElementsByTagNameNS(element.getNamespaceURI(), CONFIGURATION_ATTRIBUTE);
78 | for (int i = 0; i < configurationNodeList.getLength(); i++) {
79 | Node node = configurationNodeList.item(i);
80 | if (node instanceof Element) {
81 | String configurationUrl = node.getTextContent();
82 | logger.debug("Add configuration from attribute {}", configurationUrl);
83 | configurationUrls.add(configurationUrl);
84 | } else {
85 | throw new EmbeddedJmxTransException("Invalid configuration child element " + node);
86 | }
87 |
88 | }
89 | builder.addPropertyValue("configurationUrls", configurationUrls);
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/spring/EmbeddedJmxTransNamespaceHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2012-2013 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.jmxtrans.embedded.spring;
17 |
18 | import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
19 |
20 | /**
21 | * {@link org.springframework.beans.factory.xml.NamespaceHandler} for the '
22 | * jmxtrans' namespace.
23 | *
24 | * @author Cyrille Le Clerc
25 | */
26 | public class EmbeddedJmxTransNamespaceHandler extends NamespaceHandlerSupport {
27 |
28 | public void init() {
29 | registerBeanDefinitionParser("jmxtrans", new EmbeddedJmxTransBeanDefinitionParser());
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/spring/SpringEmbeddedJmxTrans.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.spring;
25 |
26 | import org.jmxtrans.embedded.EmbeddedJmxTrans;
27 | import org.springframework.beans.factory.DisposableBean;
28 | import org.springframework.beans.factory.InitializingBean;
29 | import org.springframework.jmx.export.naming.SelfNaming;
30 |
31 | import javax.management.MalformedObjectNameException;
32 | import javax.management.ObjectName;
33 |
34 | /**
35 | * @author Cyrille Le Clerc
36 | */
37 | public class SpringEmbeddedJmxTrans extends EmbeddedJmxTrans implements SpringEmbeddedJmxTransMBean, InitializingBean, DisposableBean, SelfNaming {
38 |
39 | private String objectName;
40 |
41 | @Override
42 | public void afterPropertiesSet() throws Exception {
43 | super.start();
44 | }
45 |
46 | @Override
47 | public void destroy() throws Exception {
48 | super.stop();
49 | }
50 |
51 | @Override
52 | public ObjectName getObjectName() throws MalformedObjectNameException {
53 | return new ObjectName(objectName);
54 | }
55 |
56 | public void setObjectName(String objectName) {
57 | this.objectName = objectName;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/spring/SpringEmbeddedJmxTransMBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.spring;
25 |
26 | import org.jmxtrans.embedded.EmbeddedJmxTransMBean;
27 |
28 | /**
29 | * JMX MBean interface for {@link SpringEmbeddedJmxTrans}.
30 | *
31 | * @author Cyrille Le Clerc
32 | */
33 | public interface SpringEmbeddedJmxTransMBean extends EmbeddedJmxTransMBean {
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/util/Preconditions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.util;
25 |
26 | import javax.annotation.Nullable;
27 |
28 | /**
29 | * Inspired by {@code com.google.common.base.Preconditions}
30 | *
31 | * @author Cyrille Le Clerc
32 | */
33 | public class Preconditions {
34 | private Preconditions() {
35 | }
36 |
37 | /**
38 | * Check the nullity of the given reference.
39 | *
40 | * @param reference reference to check
41 | * @return the given reference
42 | * @throws NullPointerException if the given reference is null
43 | */
44 | public static T checkNotNull(T reference) throws NullPointerException {
45 | return checkNotNull(reference, null);
46 | }
47 |
48 | /**
49 | * Check the nullity of the given reference.
50 | *
51 | * @param reference reference to check
52 | * @param message exception message, can be null
53 | * @return the given reference
54 | * @throws NullPointerException if the given reference is null
55 | */
56 | public static T checkNotNull(T reference, @Nullable String message) throws NullPointerException {
57 | if (reference == null) {
58 | throw new NullPointerException(message == null ? "" : message);
59 | }
60 | return reference;
61 | }
62 |
63 | /**
64 | * Check the nullity and emptiness of the given reference.
65 | *
66 | * @param reference reference to check
67 | * @return the given reference
68 | * @throws IllegalArgumentException if the given reference is null
69 | */
70 | public static String checkNotEmpty(String reference) throws IllegalArgumentException {
71 | return checkNotEmpty(reference, null);
72 | }
73 |
74 | /**
75 | * Check the nullity and emptiness of the given reference.
76 | *
77 | * @param reference reference to check
78 | * @param message exception message, can be null
79 | * @return the given reference
80 | * @throws IllegalArgumentException if the given reference is null
81 | */
82 | public static String checkNotEmpty(String reference, @Nullable String message) throws IllegalArgumentException {
83 | if (reference == null || reference.isEmpty()) {
84 | throw new IllegalArgumentException(message == null ? "Null or empty value" : message);
85 | }
86 | return reference;
87 | }
88 |
89 | /**
90 | * Check the given {@code state}.
91 | *
92 | * @param state the state top check
93 | * @param message exception message, can be null
94 | * @throws IllegalStateException if the given {@code state} is {@code false}
95 | */
96 | public static void checkState(boolean state, @Nullable String message) throws IllegalStateException {
97 | if (!state)
98 | throw new IllegalStateException(message);
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/util/concurrent/DiscardingBlockingQueueMBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.util.concurrent;
25 |
26 | /**
27 | * @author Cyrille Le Clerc
28 | */
29 | public interface DiscardingBlockingQueueMBean {
30 | int getDiscardedElementCount();
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/util/concurrent/NamedThreadFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.util.concurrent;
25 |
26 | import javax.annotation.Nonnull;
27 | import java.util.concurrent.Executors;
28 | import java.util.concurrent.ThreadFactory;
29 | import java.util.concurrent.atomic.AtomicLong;
30 |
31 | /**
32 | * Basic {@linkplain ThreadFactory} to redefine the name of the created thread.
33 | *
34 | * Inspired by Google Guava's {@code com.google.common.util.concurrent.ThreadFactoryBuilder}
35 | *
36 | * @author Cyrille Le Clerc
37 | */
38 | public class NamedThreadFactory implements ThreadFactory {
39 |
40 | private final ThreadFactory backingThreadFactory = Executors.defaultThreadFactory();
41 |
42 | private boolean daemon;
43 |
44 | private String threadNamePrefix;
45 |
46 | private AtomicLong increment = new AtomicLong();
47 |
48 | public NamedThreadFactory(String threadNamePrefix, boolean daemon) {
49 | this.threadNamePrefix = threadNamePrefix;
50 | this.daemon = daemon;
51 | }
52 |
53 |
54 | @Override
55 | @Nonnull
56 | public Thread newThread(Runnable r) {
57 | Thread thread = backingThreadFactory.newThread(r);
58 | thread.setName(threadNamePrefix + increment.incrementAndGet());
59 | thread.setDaemon(daemon);
60 | return thread;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/util/io/IoUtils2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.util.io;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import javax.annotation.Nonnull;
30 | import java.io.*;
31 |
32 | /**
33 | * IO utils.
34 | *
35 | * @author Cyrille Le Clerc
36 | */
37 | public class IoUtils2 {
38 | private static final Logger logger = LoggerFactory.getLogger(IoUtils2.class);
39 |
40 | private IoUtils2() {
41 | }
42 |
43 | public static OutputStream nullOutputStream() {
44 | return new OutputStream() {
45 | @Override
46 | public void write(int b) throws IOException {
47 | }
48 | };
49 | }
50 |
51 | public static void copy(InputStream in, OutputStream out) throws IOException {
52 | byte[] buffer = new byte[512];
53 | int len;
54 | while ((len = in.read(buffer)) != -1) {
55 | out.write(buffer, 0, len);
56 | }
57 | }
58 |
59 | public static void closeQuietly(Closeable closeable) {
60 | try {
61 | closeable.close();
62 | } catch (IOException e) {
63 | logger.debug("Exception closing quietly", e);
64 | }
65 | }
66 |
67 | public static InputStream getInputStream(@Nonnull String url) throws FileNotFoundException {
68 |
69 | if (url.startsWith("classpath:")) {
70 | InputStream result = Thread.currentThread().getContextClassLoader().getResourceAsStream(url.substring("classpath:".length()));
71 | if (result == null)
72 | throw new FileNotFoundException("File '" + url + "' not found");
73 | return result;
74 | } else {
75 | return new FileInputStream(url);
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/util/jmx/JmxUtils2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.util.jmx;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import javax.annotation.Nullable;
30 | import javax.management.MBeanServer;
31 | import javax.management.ObjectName;
32 |
33 | /**
34 | * JMX utils.
35 | *
36 | * @author Cyrille Le Clerc
37 | */
38 | public class JmxUtils2 {
39 | private final static Logger logger = LoggerFactory.getLogger(JmxUtils2.class);
40 |
41 | private JmxUtils2() {
42 |
43 | }
44 |
45 | /**
46 | * Try to register given object with objectName.
47 | *
48 | * If registration fails, a {@link Logger#warn(String)} message is emitted and null is returned.
49 | *
50 | * @param object
51 | * @param objectName
52 | * @param mbeanServer
53 | * @return the ObjectName of the registered object or null if registration failed.
54 | */
55 | @Nullable
56 | public static ObjectName registerObject(Object object, String objectName, MBeanServer mbeanServer) {
57 | try {
58 | return mbeanServer.registerMBean(object, new ObjectName(objectName)).getObjectName();
59 | } catch (Exception e) {
60 | logger.warn("Failure to register {}:{}", objectName, object, e);
61 | return null;
62 | }
63 | }
64 |
65 | /**
66 | * Try to unregister given objectName.
67 | *
68 | * If given objectName is null, nothing is done.
69 | * If registration fails, a {@link Logger#warn(String)} message is emitted and null is returned.
70 | *
71 | * @param objectName objectName to unregister
72 | * @param mbeanServer MBeanServer to which the objectName is unregistered
73 | */
74 | public static void unregisterObject(ObjectName objectName, MBeanServer mbeanServer) {
75 | if (objectName == null) {
76 | return;
77 | }
78 | try {
79 | mbeanServer.unregisterMBean(objectName);
80 | } catch (Exception e) {
81 | logger.warn("Failure to unregister {}", objectName, e);
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/org/jmxtrans/embedded/util/json/PlaceholderEnabledJsonNodeFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010-2013 the original author or authors
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining
5 | * a copy of this software and associated documentation files (the
6 | * "Software"), to deal in the Software without restriction, including
7 | * without limitation the rights to use, copy, modify, merge, publish,
8 | * distribute, sublicense, and/or sell copies of the Software, and to
9 | * permit persons to whom the Software is furnished to do so, subject to
10 | * the following conditions:
11 | *
12 | * The above copyright notice and this permission notice shall be
13 | * included in all copies or substantial portions of the Software.
14 | *
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | *
23 | */
24 | package org.jmxtrans.embedded.util.json;
25 |
26 | import com.fasterxml.jackson.databind.node.JsonNodeFactory;
27 | import com.fasterxml.jackson.databind.node.TextNode;
28 | import org.slf4j.Logger;
29 | import org.slf4j.LoggerFactory;
30 |
31 | import java.io.IOException;
32 | import java.io.ObjectInputStream;
33 |
34 | /**
35 | * Property placeholder / substitution enabled {@link JsonNodeFactory}.
36 | *
37 | * Each JSON text value is processed by the {@link PropertyPlaceholderResolver} before building a {@linkplain TextNode}.
38 | *
39 | * Inspired by
40 | * Spring Framework PropertyPlaceHolder and by
41 | * Jakarta Digester Substitution.
42 | *
43 | * @author Cyrille Le Clerc
44 | */
45 | public class PlaceholderEnabledJsonNodeFactory extends JsonNodeFactory {
46 |
47 | private static final long serialVersionUID = 1L;
48 |
49 | private Logger logger = LoggerFactory.getLogger(getClass());
50 |
51 | private PropertyPlaceholderResolver resolver = new PropertyPlaceholderResolver();
52 |
53 | /**
54 | * Main constructor
55 | *
56 | *
The only argument to this constructor is a boolean telling whether
57 | * {@link com.fasterxml.jackson.databind.node.DecimalNode} instances must be built with exact representations of
58 | * {@link java.math.BigDecimal} instances.
59 | *
60 | *
This has quite an influence since, for instance, a BigDecimal (and,
61 | * therefore, a DecimalNode) constructed from input string {@code "1.0"} and
62 | * another constructed with input string {@code "1.00"} will not be
63 | * equal, since their scale differs (1 in the first case, 2 in the second
64 | * case).
65 | *
66 | *
Note that setting the argument to {@code true} does not
67 | * guarantee a strict inequality between JSON representations: input texts
68 | * {@code "0.1"} and {@code "1e-1"}, for instance, yield two equivalent
69 | * BigDecimal instances since they have the same scale (1).
70 | *
71 | *
The no-arg constructor (and the default {@link #instance}) calls this
72 | * constructor with {@code false} as an argument.