├── .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 | ![Graphite Screenshot](https://raw.github.com/wiki/jmxtrans/embedded-jmxtrans/img/graphite-screenshot-basic.png) 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 | * 37 | * 38 | * Output: Graphite's 39 | * Carbon Plan Text protocol 40 | *
41 |  *     <metric path> <metric value> <metric timestamp>.
42 |  * 
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 | * 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}.
  • 39 | *
40 | * 41 | * Output: Graphite's 42 | * Carbon Plan Text protocol 43 | *
44 |  *     <metric path> <metric value> <metric timestamp>.
45 |  * 
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.

73 | * 74 | * @param bigDecimalExact see description 75 | * @see java.math.BigDecimal 76 | */ 77 | public PlaceholderEnabledJsonNodeFactory(boolean bigDecimalExact) { 78 | super(bigDecimalExact); 79 | } 80 | 81 | /** 82 | * Default constructor 83 | * 84 | *

This calls {@link #PlaceholderEnabledJsonNodeFactory(boolean)} with {@code false} 85 | * as an argument.

86 | */ 87 | public PlaceholderEnabledJsonNodeFactory() { 88 | super(); 89 | } 90 | 91 | @Override 92 | public TextNode textNode(String text) { 93 | String resolvedText = (text == null) ? null : resolver.resolveString(text); 94 | if (logger.isInfoEnabled()) { 95 | if (text == null || text.equals(resolvedText)) { 96 | logger.debug("Resolve '{}' into '{}'", text, resolvedText); 97 | } else { 98 | logger.info("Resolve '{}' into '{}'", text, resolvedText); 99 | } 100 | } 101 | return super.textNode(resolvedText); 102 | } 103 | 104 | private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { 105 | this.logger = LoggerFactory.getLogger(getClass()); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/org/jmxtrans/embedded/util/net/HostAndPort.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.net; 25 | 26 | import org.jmxtrans.embedded.util.Preconditions; 27 | import org.jmxtrans.embedded.util.pool.SocketWriterPoolFactory; 28 | 29 | import javax.annotation.Nonnull; 30 | 31 | /** 32 | * Holder for network host and port. 33 | * 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class HostAndPort { 37 | private final String host; 38 | private final int port; 39 | 40 | public HostAndPort(@Nonnull String host, int port) { 41 | this.host = Preconditions.checkNotEmpty(host, "'host' can not be empty"); 42 | this.port = port; 43 | } 44 | 45 | public String getHost() { 46 | return host; 47 | } 48 | 49 | public int getPort() { 50 | return port; 51 | } 52 | 53 | @Override 54 | public boolean equals(Object o) { 55 | if (this == o) return true; 56 | if (!(o instanceof HostAndPort)) return false; 57 | 58 | HostAndPort that = (HostAndPort) o; 59 | 60 | if (port != that.port) return false; 61 | if (!host.equals(that.host)) return false; 62 | 63 | return true; 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | int result = host.hashCode(); 69 | result = 31 * result + port; 70 | return result; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return "HostAndPort{" + 76 | "host='" + host + '\'' + 77 | ", port=" + port + 78 | '}'; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/jmxtrans/embedded/util/net/SocketOutputStream.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.net; 25 | 26 | import java.io.FilterOutputStream; 27 | import java.io.IOException; 28 | import java.net.InetSocketAddress; 29 | import java.net.Socket; 30 | 31 | /** 32 | * Convenience class for writing bytes to a {@linkplain java.net.Socket}. 33 | * 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class SocketOutputStream extends FilterOutputStream { 37 | 38 | private final Socket socket; 39 | 40 | public SocketOutputStream(InetSocketAddress inetSocketAddress) throws IOException { 41 | this(new Socket(inetSocketAddress.getAddress(), inetSocketAddress.getPort())); 42 | } 43 | 44 | public SocketOutputStream(Socket socket) throws IOException { 45 | super(socket.getOutputStream()); 46 | this.socket = socket; 47 | } 48 | 49 | /** 50 | * Return the underlying {@linkplain java.net.Socket} 51 | */ 52 | public Socket getSocket() { 53 | return socket; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "SocketWriter{" + 59 | "socket=" + socket + 60 | '}'; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/jmxtrans/embedded/util/net/SocketWriter.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.net; 25 | 26 | import java.io.BufferedWriter; 27 | import java.io.FilterWriter; 28 | import java.io.IOException; 29 | import java.io.OutputStreamWriter; 30 | import java.net.DatagramSocket; 31 | import java.net.InetSocketAddress; 32 | import java.net.Socket; 33 | import java.nio.charset.Charset; 34 | 35 | /** 36 | * Convenience class for writing characters to a {@linkplain Socket}. 37 | * 38 | * @author Cyrille Le Clerc 39 | * @author Patrick Brühlmann 40 | */ 41 | public class SocketWriter extends FilterWriter { 42 | 43 | private final Socket socket; 44 | private final DatagramSocket datagramSocket; 45 | 46 | /** 47 | * @param inetSocketAddress host and port of the underlying {@linkplain Socket} 48 | * @param charset charset of the {@linkplain java.io.OutputStream} underlying {@linkplain Socket} 49 | * @throws IOException 50 | */ 51 | public SocketWriter(InetSocketAddress inetSocketAddress, Charset charset) throws IOException { 52 | this(new Socket(inetSocketAddress.getAddress(), inetSocketAddress.getPort()), charset); 53 | } 54 | 55 | public SocketWriter(Socket socket, Charset charset) throws IOException { 56 | super(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), charset))); 57 | this.socket = socket; 58 | this.datagramSocket = null; 59 | } 60 | 61 | public SocketWriter(DatagramSocket datagramSocket, Charset charset) throws IOException { 62 | super(new UDPDatagramWriter(datagramSocket, charset)); 63 | this.datagramSocket = datagramSocket; 64 | this.socket = null; 65 | } 66 | 67 | /** 68 | * Return the underlying {@linkplain java.net.Socket} 69 | */ 70 | public Socket getSocket() { 71 | return socket; 72 | } 73 | 74 | /** 75 | * Return the underlying {@linkplain java.net.DatagramSocket} 76 | */ 77 | public DatagramSocket getDatagramSocket() { 78 | return datagramSocket; 79 | } 80 | 81 | @Override 82 | public String toString() { 83 | return "SocketWriter{" + 84 | "socket=" + ((socket != null) ? socket : datagramSocket) + 85 | '}'; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/jmxtrans/embedded/util/net/UDPDatagramWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2015 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.net; 25 | 26 | import java.io.IOException; 27 | import java.io.Writer; 28 | import java.net.DatagramPacket; 29 | import java.net.DatagramSocket; 30 | import java.nio.charset.Charset; 31 | 32 | /** 33 | * Convenience class for writing characters to a UDP {@linkplain java.nio.channels.DatagramChannel}. 34 | * 35 | * @author Patrick Brühlmann 36 | */ 37 | public class UDPDatagramWriter extends Writer { 38 | 39 | 40 | private final DatagramSocket datagramSocket; 41 | private final Charset charset; 42 | 43 | public UDPDatagramWriter(DatagramSocket datagramSocket, Charset charset) { 44 | this.datagramSocket = datagramSocket; 45 | this.charset = charset; 46 | } 47 | 48 | @Override 49 | public void write(char[] cbuf, int off, int len) throws IOException { 50 | 51 | DatagramPacket datagramPacket = new DatagramPacket(new String(cbuf).getBytes(charset), off, len); 52 | datagramSocket.send(datagramPacket); 53 | } 54 | 55 | @Override 56 | public void flush() throws IOException { 57 | // nothing to do here 58 | } 59 | 60 | @Override 61 | public void close() throws IOException { 62 | // nothing to do here 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/jmxtrans/embedded/util/pool/SocketOutputStreamPoolFactory.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.pool; 25 | 26 | import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; 27 | import org.apache.commons.pool2.KeyedPooledObjectFactory; 28 | import org.apache.commons.pool2.PooledObject; 29 | import org.apache.commons.pool2.impl.DefaultPooledObject; 30 | import org.jmxtrans.embedded.util.net.HostAndPort; 31 | import org.jmxtrans.embedded.util.net.SocketOutputStream; 32 | 33 | import java.net.InetSocketAddress; 34 | import java.net.Socket; 35 | 36 | /** 37 | * Factory for {@linkplain org.jmxtrans.embedded.util.net.SocketOutputStream} instances created from {@linkplain HostAndPort}. 38 | * 39 | * @author Cyrille Le Clerc 40 | */ 41 | public class SocketOutputStreamPoolFactory extends BaseKeyedPooledObjectFactory implements KeyedPooledObjectFactory { 42 | 43 | public static final int DEFAULT_SOCKET_CONNECT_TIMEOUT_IN_MILLIS = 500; 44 | private final int socketConnectTimeoutInMillis; 45 | 46 | public SocketOutputStreamPoolFactory(int socketConnectTimeoutInMillis) { 47 | this.socketConnectTimeoutInMillis = socketConnectTimeoutInMillis; 48 | } 49 | 50 | @Override 51 | public SocketOutputStream create(HostAndPort hostAndPort) throws Exception { 52 | Socket socket = new Socket(); 53 | socket.setKeepAlive(true); 54 | socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()), socketConnectTimeoutInMillis); 55 | return new SocketOutputStream(socket); 56 | } 57 | 58 | @Override 59 | public PooledObject wrap(SocketOutputStream outputStream) { 60 | return new DefaultPooledObject(outputStream); 61 | } 62 | 63 | @Override 64 | public void destroyObject(HostAndPort hostAndPort, PooledObject socketOutputStreamRef) throws Exception { 65 | SocketOutputStream socketOutputStream = socketOutputStreamRef.getObject(); 66 | socketOutputStream.close(); 67 | socketOutputStream.getSocket().close(); 68 | } 69 | 70 | /** 71 | * Defensive approach: we test all the "Socket.isXXX()" flags. 72 | */ 73 | @Override 74 | public boolean validateObject(HostAndPort hostAndPort, PooledObject socketOutputStreamRef) { 75 | Socket socket = socketOutputStreamRef.getObject().getSocket(); 76 | return socket.isConnected() 77 | && socket.isBound() 78 | && !socket.isClosed() 79 | && !socket.isInputShutdown() 80 | && !socket.isOutputShutdown(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/jmxtrans/embedded/util/pool/UDPSocketWriterPoolFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2015 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.pool; 25 | 26 | import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; 27 | import org.apache.commons.pool2.KeyedPooledObjectFactory; 28 | import org.apache.commons.pool2.PooledObject; 29 | import org.apache.commons.pool2.impl.DefaultPooledObject; 30 | import org.jmxtrans.embedded.util.net.HostAndPort; 31 | import org.jmxtrans.embedded.util.net.SocketWriter; 32 | 33 | import java.net.DatagramSocket; 34 | import java.net.InetSocketAddress; 35 | import java.nio.charset.Charset; 36 | 37 | /** 38 | * Factory for UDP {@linkplain SocketWriter} instances. 39 | * 40 | * @author Patrick Brühlmann 41 | * 42 | */ 43 | public class UDPSocketWriterPoolFactory extends BaseKeyedPooledObjectFactory implements KeyedPooledObjectFactory { 44 | 45 | private final Charset charset; 46 | 47 | public UDPSocketWriterPoolFactory(String charset) { 48 | this.charset = Charset.forName(charset); 49 | } 50 | 51 | public UDPSocketWriterPoolFactory(Charset charset) { 52 | this.charset = charset; 53 | } 54 | 55 | @Override 56 | public SocketWriter create(HostAndPort hostAndPort) throws Exception { 57 | DatagramSocket datagramSocket = new DatagramSocket(null); 58 | datagramSocket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort())); 59 | return new SocketWriter(datagramSocket, charset); 60 | } 61 | 62 | @Override 63 | public PooledObject wrap(SocketWriter socketWriter) { 64 | return new DefaultPooledObject(socketWriter); 65 | } 66 | 67 | @Override 68 | public boolean validateObject(HostAndPort hostAndPort, PooledObject socketWriterRef) { 69 | DatagramSocket datagramSocket = socketWriterRef.getObject().getDatagramSocket(); 70 | return datagramSocket.isConnected() 71 | && !datagramSocket.isClosed(); 72 | } 73 | 74 | @Override 75 | public void destroyObject(HostAndPort hostAndPort, PooledObject socketWriterRef) throws Exception { 76 | super.destroyObject(hostAndPort, socketWriterRef); 77 | SocketWriter socketWriter = socketWriterRef.getObject(); 78 | socketWriter.close(); 79 | socketWriter.getDatagramSocket().close(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.handlers: -------------------------------------------------------------------------------- 1 | http\://www.jmxtrans.org/schema/embedded=org.jmxtrans.embedded.spring.EmbeddedJmxTransNamespaceHandler 2 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.schemas: -------------------------------------------------------------------------------- 1 | http\://www.jmxtrans.org/schema/embedded/jmxtrans.xsd=org/jmxtrans/embedded/spring/jmxtrans-1.0.xsd 2 | http\://www.jmxtrans.org/schema/embedded/jmxtrans-1.0.xsd=org/jmxtrans/embedded/spring/jmxtrans-1.0.xsd 3 | http\://www.jmxtrans.org/schema/embedded/jmxtrans-1.1.xsd=org/jmxtrans/embedded/spring/jmxtrans-1.1.xsd 4 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.tooling: -------------------------------------------------------------------------------- 1 | # Tooling related information for the management namespace 2 | http\://www.jmxtrans.org/schema/embedded@name=jmxtrans namespace 3 | http\://www.jmxtrans.org/schema/embedded@prefix=jmxtrans 4 | http\://www.jmxtrans.org/schema/embedded@icon=org/springframework/beans/factory/xml/spring-util.gif 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/config/cloudbees-tomcat-6.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "localEngine:type=ThreadPool,name=\"http-nio-*\"", 5 | "resultAlias": "tomcat.thread-pool.http-nio", 6 | "attributes": [ 7 | "currentThreadCount", 8 | "currentThreadsBusy" 9 | ] 10 | 11 | }, 12 | { 13 | "objectName": "localEngine:type=ThreadPool,name=\"ajp-nio-*\"", 14 | "resultAlias": "tomcat.thread-pool.ajp-nio", 15 | "attributes": [ 16 | "currentThreadCount", 17 | "currentThreadsBusy" 18 | ] 19 | 20 | }, 21 | { 22 | "objectName": "localEngine:type=Manager,path=*,host=*", 23 | "resultAlias": "tomcat.manager.%host%.%path%", 24 | "attributes": [ 25 | "activeSessions" 26 | ] 27 | }, 28 | { 29 | "objectName": "localEngine:type=GlobalRequestProcessor,name=\"http-nio-*\"", 30 | "resultAlias": "tomcat.global-request-processor.http-nio", 31 | "attributes": [ 32 | "bytesReceived", 33 | "bytesSent", 34 | "errorCount", 35 | "processingTime", 36 | "requestCount" 37 | 38 | ] 39 | }, 40 | { 41 | "objectName": "localEngine:type=GlobalRequestProcessor,name=\"ajp-nio-*\"", 42 | "resultAlias": "tomcat.global-request-processor.ajp-nio", 43 | "attributes": [ 44 | "bytesReceived", 45 | "bytesSent", 46 | "errorCount", 47 | "processingTime", 48 | "requestCount" 49 | 50 | ] 51 | }, 52 | { 53 | "objectName": "localEngine:type=DataSource,path=*,host=*,class=javax.sql.DataSource,name=*", 54 | "resultAlias": "tomcat.data-source.%host%.%path%.%name%", 55 | "attributes": [ 56 | "numActive", 57 | "numIdle" 58 | ] 59 | }, 60 | { 61 | "objectName": "localEngine:type=DataSource,class=javax.sql.DataSource,name=*", 62 | "resultAlias": "tomcat.data-source.%name%", 63 | "attributes": [ 64 | "numActive", 65 | "numIdle" 66 | ] 67 | }, 68 | { 69 | "objectName": "localEngine:j2eeType=Servlet,name=*,WebModule=*,J2EEApplication=none,J2EEServer=none", 70 | "resultAlias": "tomcat.servlet.%WebModule%.%name%", 71 | "attributes": [ 72 | "errorCount", 73 | "processingTime", 74 | "requestCount" 75 | ] 76 | } 77 | ] 78 | } -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/config/jmxtrans-internals-servlet-container.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "org.jmxtrans.embedded:name=*,type=EmbeddedJmxTrans,path=*", 5 | "resultAlias": "jmxtrans.%name%.%path%", 6 | "attributes": [ 7 | "CollectedMetricsCount", 8 | "CollectionCount", 9 | "CollectionDurationInNanos", 10 | "ExportCount", 11 | "ExportDurationInNanos", 12 | "ExportedMetricsCount" 13 | ] 14 | 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/config/jmxtrans-internals.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "org.jmxtrans.embedded:name=*,type=EmbeddedJmxTrans", 5 | "resultAlias": "jmxtrans.%name%", 6 | "attributes": [ 7 | "CollectedMetricsCount", 8 | "CollectionCount", 9 | "CollectionDurationInNanos", 10 | "ExportCount", 11 | "ExportDurationInNanos", 12 | "ExportedMetricsCount" 13 | ] 14 | 15 | } 16 | ] 17 | } -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/config/jvm-sun-hotspot.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "java.lang:type=Memory", 5 | "resultAlias": "jvm.memory", 6 | "attributes": [ 7 | { 8 | "name": "HeapMemoryUsage", 9 | "keys": ["committed", "used"] 10 | }, 11 | { 12 | "name": "NonHeapMemoryUsage", 13 | "keys": ["committed", "used"] 14 | } 15 | ] 16 | 17 | }, 18 | { 19 | "objectName": "java.lang:type=Runtime", 20 | "resultAlias": "jvm.runtime", 21 | "attributes": [ 22 | "Uptime" 23 | ] 24 | 25 | }, 26 | { 27 | "objectName": "java.lang:type=GarbageCollector,name=*", 28 | "resultAlias": "jvm.gc.%name%", 29 | "attributes": [ 30 | "CollectionCount", 31 | "CollectionTime" 32 | ] 33 | }, 34 | { 35 | "objectName": "java.lang:type=Threading", 36 | "resultAlias": "jvm.thread", 37 | "attributes": [ 38 | "ThreadCount" 39 | ] 40 | 41 | }, 42 | { 43 | "objectName": "java.lang:type=OperatingSystem", 44 | "resultAlias": "jvm.os", 45 | "attributes": [ 46 | "CommittedVirtualMemorySize", 47 | "FreePhysicalMemorySize", 48 | "FreeSwapSpaceSize", 49 | "OpenFileDescriptorCount", 50 | "ProcessCpuTime", 51 | "SystemLoadAverage" 52 | ] 53 | 54 | } 55 | ] 56 | } -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/config/tomcat-6.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "Catalina:type=ThreadPool,name=\"http-nio-*\"", 5 | "resultAlias": "tomcat.thread-pool.http-nio", 6 | "attributes": [ 7 | "currentThreadCount", 8 | "currentThreadsBusy" 9 | ] 10 | 11 | }, 12 | { 13 | "objectName": "Catalina:type=ThreadPool,name=\"ajp-nio-*\"", 14 | "resultAlias": "tomcat.thread-pool.ajp-nio", 15 | "attributes": [ 16 | "currentThreadCount", 17 | "currentThreadsBusy" 18 | ] 19 | 20 | }, 21 | { 22 | "objectName": "Catalina:type=Manager,path=*,host=*", 23 | "resultAlias": "tomcat.manager.%host%.%path%", 24 | "attributes": [ 25 | "activeSessions" 26 | ] 27 | }, 28 | { 29 | "objectName": "Catalina:type=GlobalRequestProcessor,name=\"http-nio-*\"", 30 | "resultAlias": "tomcat.global-request-processor.http-nio", 31 | "attributes": [ 32 | "bytesReceived", 33 | "bytesSent", 34 | "errorCount", 35 | "processingTime", 36 | "requestCount" 37 | 38 | ] 39 | }, 40 | { 41 | "objectName": "Catalina:type=GlobalRequestProcessor,name=\"ajp-nio-*\"", 42 | "resultAlias": "tomcat.global-request-processor.ajp-nio", 43 | "attributes": [ 44 | "bytesReceived", 45 | "bytesSent", 46 | "errorCount", 47 | "processingTime", 48 | "requestCount" 49 | 50 | ] 51 | }, 52 | { 53 | "objectName": "Catalina:type=DataSource,path=*,host=*,class=javax.sql.DataSource,name=*", 54 | "resultAlias": "tomcat.data-source.%host%.%path%.%name%", 55 | "attributes": [ 56 | "numActive", 57 | "numIdle" 58 | ] 59 | }, 60 | { 61 | "objectName": "Catalina:type=DataSource,class=javax.sql.DataSource,name=*", 62 | "resultAlias": "tomcat.data-source.%name%", 63 | "attributes": [ 64 | "numActive", 65 | "numIdle" 66 | ] 67 | }, 68 | { 69 | "objectName": "Catalina:j2eeType=Servlet,name=*,WebModule=*,J2EEApplication=none,J2EEServer=none", 70 | "resultAlias": "tomcat.servlet.%WebModule%.%name%", 71 | "attributes": [ 72 | "errorCount", 73 | "processingTime", 74 | "requestCount" 75 | ] 76 | } 77 | ] 78 | } -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/config/tomcat-7.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "Catalina:type=ThreadPool,name=\"http-nio-*\"", 5 | "resultAlias": "tomcat.thread-pool.http-nio", 6 | "attributes": [ 7 | "currentThreadCount", 8 | "currentThreadsBusy" 9 | ] 10 | 11 | }, 12 | { 13 | "objectName": "Catalina:type=ThreadPool,name=\"ajp-nio-*\"", 14 | "resultAlias": "tomcat.thread-pool.ajp-nio", 15 | "attributes": [ 16 | "currentThreadCount", 17 | "currentThreadsBusy" 18 | ] 19 | 20 | }, 21 | { 22 | "objectName": "Catalina:type=Manager,context=*,host=*", 23 | "resultAlias": "tomcat.manager.%host%.%context%", 24 | "attributes": [ 25 | "activeSessions" 26 | ] 27 | }, 28 | { 29 | "objectName": "Catalina:type=GlobalRequestProcessor,name=\"http-nio-*\"", 30 | "resultAlias": "tomcat.global-request-processor.http-nio", 31 | "attributes": [ 32 | "bytesReceived", 33 | "bytesSent", 34 | "errorCount", 35 | "processingTime", 36 | "requestCount" 37 | 38 | ] 39 | }, 40 | { 41 | "objectName": "Catalina:type=GlobalRequestProcessor,name=\"ajp-nio-*\"", 42 | "resultAlias": "tomcat.global-request-processor.ajp-nio", 43 | "attributes": [ 44 | "bytesReceived", 45 | "bytesSent", 46 | "errorCount", 47 | "processingTime", 48 | "requestCount" 49 | 50 | ] 51 | }, 52 | { 53 | "objectName": "Catalina:type=DataSource,context=*,host=*,class=javax.sql.DataSource,name=*", 54 | "resultAlias": "tomcat.data-source.%host%.%context%.%name%", 55 | "attributes": [ 56 | "numActive", 57 | "numIdle" 58 | ] 59 | }, 60 | { 61 | "objectName": "Catalina:type=DataSource,class=javax.sql.DataSource,name=*", 62 | "resultAlias": "tomcat.data-source.%name%", 63 | "attributes": [ 64 | "numActive", 65 | "numIdle" 66 | ] 67 | }, 68 | { 69 | "objectName": "Catalina:j2eeType=Servlet,name=*,WebModule=*,J2EEApplication=none,J2EEServer=none", 70 | "resultAlias": "tomcat.servlet.%WebModule%.%name%", 71 | "attributes": [ 72 | "errorCount", 73 | "processingTime", 74 | "requestCount" 75 | ] 76 | } 77 | ] 78 | } -------------------------------------------------------------------------------- /src/main/resources/org/jmxtrans/embedded/spring/jmxtrans-1.0.xsd: -------------------------------------------------------------------------------- 1 | 2 | 25 | 30 | 31 | 32 | 35 | 36 | 37 | 39 | 40 | 41 | 42 | 45 | 46 | 47 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 64 | element. Optional. If not defined, defaults to 'jmxtrans'. 67 | ]]> 68 | 69 | 70 | 71 | 72 | 77 | 78 | 79 | 81 | 82 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /src/site/resources/CNAME: -------------------------------------------------------------------------------- 1 | embedded-jmxtrans.jmxtrans.org 2 | 3 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/EmbeddedJmxTransIntegrationTest.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.hamcrest.Matchers; 27 | import org.jmxtrans.embedded.config.ConfigurationParser; 28 | import org.junit.Assert; 29 | 30 | import java.util.Random; 31 | import java.util.concurrent.TimeUnit; 32 | 33 | /** 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class EmbeddedJmxTransIntegrationTest { 37 | public static void main(String[] args) throws Exception { 38 | EmbeddedJmxTransIntegrationTest integrationTest = new EmbeddedJmxTransIntegrationTest(); 39 | integrationTest.integrationTest(); 40 | } 41 | 42 | public void integrationTest() throws Exception { 43 | EmbeddedJmxTrans embeddedJmxTrans = new ConfigurationParser().newEmbeddedJmxTrans("classpath:org/jmxtrans/embedded/jmxtrans-integ-test.json"); 44 | embeddedJmxTrans.start(); 45 | 46 | Assert.assertThat(embeddedJmxTrans.getState(), Matchers.equalTo(EmbeddedJmxTrans.State.STARTED.toString())); 47 | 48 | TestUtils.generateJvmActivity(); 49 | 50 | System.out.println("bye"); 51 | embeddedJmxTrans.stop(); 52 | Assert.assertThat(embeddedJmxTrans.getState(), Matchers.equalTo(EmbeddedJmxTrans.State.STOPPED.toString())); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/EmbeddedJmxTransShutdownHookIntegrationTest.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.hamcrest.Matchers; 27 | import org.jmxtrans.embedded.config.ConfigurationParser; 28 | import org.junit.Assert; 29 | 30 | import java.util.Random; 31 | import java.util.concurrent.TimeUnit; 32 | 33 | /** 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class EmbeddedJmxTransShutdownHookIntegrationTest { 37 | 38 | public static void main(String[] args) throws Exception { 39 | EmbeddedJmxTransShutdownHookIntegrationTest integrationTest = new EmbeddedJmxTransShutdownHookIntegrationTest(); 40 | integrationTest.integrationTest(); 41 | } 42 | 43 | public void integrationTest() throws Exception { 44 | 45 | EmbeddedJmxTrans embeddedJmxTrans = new ConfigurationParser().newEmbeddedJmxTrans("classpath:org/jmxtrans/embedded/jmxtrans-integ-test.json"); 46 | embeddedJmxTrans.start(); 47 | Assert.assertThat(embeddedJmxTrans.getState(), Matchers.equalTo(EmbeddedJmxTrans.State.STARTED.toString())); 48 | 49 | TestUtils.generateJvmActivity(); 50 | 51 | System.out.println("bye"); 52 | } 53 | 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/MockMemoryPool.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.openmbean.*; 27 | 28 | /** 29 | * @author Cyrille Le Clerc 30 | * @see java.lang.management.MemoryPoolMXBean 31 | */ 32 | public class MockMemoryPool implements MockMemoryPoolMBean { 33 | 34 | /** 35 | * @see java.lang.management.MemoryUsage 36 | */ 37 | private CompositeData usage; 38 | 39 | private String name; 40 | 41 | /** 42 | * @param name e.g. PS Eden Space 43 | * @param committed e.g. 87359488L 44 | */ 45 | public MockMemoryPool(String name, long committed) { 46 | this.name = name; 47 | try { 48 | CompositeType type = new CompositeType( 49 | "java.lang.management.MemoryUsage", "My Type Desc", 50 | new String[]{"committed", "init", "max", "used"}, 51 | new String[]{"committed", "init", "max", "used"}, 52 | new OpenType[]{SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG}); 53 | usage = new CompositeDataSupport( 54 | type, 55 | new String[]{"committed", "init", "max", "used"}, 56 | new Long[]{committed, 16318464L, 88997888L, 87359488L}); 57 | } catch (Exception e) { 58 | throw new RuntimeException(e); 59 | } 60 | } 61 | 62 | @Override 63 | public CompositeData getUsage() { 64 | return usage; 65 | } 66 | 67 | @Override 68 | public long getCollectionUsageThreshold() { 69 | return 0; 70 | } 71 | 72 | @Override 73 | public String getName() { 74 | return name; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/MockMemoryPoolMBean.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.openmbean.CompositeData; 27 | 28 | /** 29 | * Mock {@link MockMemoryPoolMBean} 30 | * 31 | * @author Cyrille Le Clerc 32 | */ 33 | public interface MockMemoryPoolMBean { 34 | 35 | public long getCollectionUsageThreshold(); 36 | 37 | /** 38 | * @see java.lang.management.MemoryPoolMXBean#getName() 39 | */ 40 | public String getName(); 41 | 42 | /** 43 | * @see java.lang.management.MemoryPoolMXBean#getUsage() 44 | */ 45 | public CompositeData getUsage(); 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/ResultNameStrategyTest.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.junit.BeforeClass; 27 | import org.junit.Test; 28 | 29 | import javax.management.MalformedObjectNameException; 30 | import javax.management.ObjectName; 31 | 32 | import static org.hamcrest.CoreMatchers.is; 33 | import static org.junit.Assert.*; 34 | 35 | /** 36 | * @author Cyrille Le Clerc 37 | */ 38 | public class ResultNameStrategyTest { 39 | 40 | static ResultNameStrategy strategy = new ResultNameStrategy(); 41 | 42 | @BeforeClass 43 | public static void beforeClass() throws Exception { 44 | strategy.registerExpressionEvaluator("hostname","tomcat1"); 45 | strategy.registerExpressionEvaluator("canonical_hostname","tomcat1.www.private.mycompany.com"); 46 | strategy.registerExpressionEvaluator("escaped_canonical_hostname","tomcat1_www_private_mycompany_com"); 47 | strategy.registerExpressionEvaluator("hostaddress","10.0.0.81"); 48 | strategy.registerExpressionEvaluator("escaped_hostaddress","10_0_0_81"); 49 | } 50 | 51 | 52 | /** 53 | * Test an expression with '#' based keywords (#hostname#) and with '%' based variables mapped to objectname properties. 54 | * @throws Exception 55 | */ 56 | @Test 57 | public void testResolveExpression() throws Exception { 58 | // prepare 59 | String expression = "#hostname#.tomcat.datasource.%host%.%path%.%name%"; 60 | String objectName = "Catalina:type=Resource,resourcetype=Context,path=/,host=localhost,class=javax.sql.DataSource,name=\"jdbc/my-datasource\""; 61 | 62 | // test 63 | String actual = strategy.resolveExpression(expression, new ObjectName(objectName)); 64 | 65 | // verify 66 | assertThat(actual, is("tomcat1.tomcat.datasource.localhost._.jdbc_my-datasource")); 67 | } 68 | 69 | @Test 70 | public void testEscapeObjectName1() throws Exception { 71 | String objectName = "java.lang:type=GarbageCollector,name=PS Scavenge"; 72 | String actual = strategy.escapeObjectName(new ObjectName(objectName)); 73 | assertThat(actual, is("java_lang.name__PS_Scavenge.type__GarbageCollector")); 74 | } 75 | 76 | @Test 77 | public void testDontReplaceObjectNames() 78 | throws MalformedObjectNameException 79 | { 80 | String objectName = "java.lang:type=GarbageCollector,name=org.jmxtrans.embedded"; 81 | ResultNameStrategy strategy = ResultNameStrategy.builder().replaceDotsInObjectNames(false).build(); 82 | String actual = strategy.resolveExpression( "%name%",new ObjectName( objectName ) ); 83 | assertThat( actual, is ("org.jmxtrans.embedded") ); 84 | } 85 | 86 | @Test 87 | public void testEscapeObjectName2() throws Exception { 88 | String objectName = "Catalina:type=Resource,resourcetype=Context,path=/,host=localhost,class=javax.sql.DataSource,name=\"jdbc/my-datasource\""; 89 | String actual = strategy.escapeObjectName(new ObjectName(objectName)); 90 | assertThat(actual, is("Catalina.class__javax_sql_DataSource.host__localhost.name__jdbc_my-datasource.path___.resourcetype__Context.type__Resource")); 91 | } 92 | 93 | @Test 94 | public void testCanonicalHostNameDotsAreNotEscaped() throws Exception { 95 | ResultNameStrategy resultNameStrategy = new ResultNameStrategy(); 96 | resultNameStrategy.registerExpressionEvaluator("canonical_hostname", "server1.mycompany.com"); 97 | String actual = resultNameStrategy.resolveExpression("#canonical_hostname#"); 98 | assertThat(actual, is("server1.mycompany.com")); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/config/ConfigurationResultNameStrategyTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.config; 2 | 3 | import javax.management.MalformedObjectNameException; 4 | import javax.management.ObjectName; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.ArrayBlockingQueue; 8 | import java.util.concurrent.BlockingQueue; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | import org.jmxtrans.embedded.EmbeddedJmxTrans; 12 | import org.jmxtrans.embedded.Query; 13 | import org.jmxtrans.embedded.QueryResult; 14 | import org.jmxtrans.embedded.TestUtils; 15 | import org.junit.BeforeClass; 16 | import org.junit.Test; 17 | 18 | import static org.hamcrest.CoreMatchers.is; 19 | import static org.junit.Assert.assertThat; 20 | 21 | public class ConfigurationResultNameStrategyTest 22 | { 23 | static Map queriesByResultName; 24 | 25 | static EmbeddedJmxTrans embeddedJmxTrans; 26 | 27 | private static final String ALIAS_NAME = "%name%"; 28 | 29 | @BeforeClass 30 | public static void beforeClass() throws Exception { 31 | ConfigurationParser configurationParser = new ConfigurationParser(); 32 | embeddedJmxTrans = configurationParser.newEmbeddedJmxTrans("classpath:org/jmxtrans/embedded/jmxtrans-config-resultnamestrategy.json"); 33 | queriesByResultName = TestUtils.indexQueriesByName(embeddedJmxTrans.getQueries()); 34 | } 35 | 36 | @Test 37 | public void validateDefaultReplaceDotsInObjectName() 38 | throws MalformedObjectNameException, InterruptedException 39 | { 40 | ObjectName objectName = new ObjectName("org:name=org.jmxtrans.embedded.default"); 41 | Query query = queriesByResultName.get(objectName.toString()); 42 | assertThat(query.getObjectName(), is(objectName)); 43 | assertThat(query.getResultAlias(), is (ALIAS_NAME)); 44 | BlockingQueue queue = new ArrayBlockingQueue(1 ); 45 | query.getQueryAttributes().iterator().next().collectMetrics(query.getObjectName(),1,0, queue); 46 | 47 | QueryResult result = queue.poll( 1, TimeUnit.SECONDS ); 48 | 49 | assertThat (result.getName(), is ("org_jmxtrans_embedded_default.Value")); 50 | } 51 | 52 | 53 | @Test 54 | public void validateReplaceDotsInObjectName() 55 | throws MalformedObjectNameException, InterruptedException 56 | { 57 | ObjectName objectName = new ObjectName("org:name=org.jmxtrans.embedded.replace"); 58 | Query query = queriesByResultName.get(objectName.toString()); 59 | assertThat(query.getObjectName(), is(objectName)); 60 | assertThat(query.getResultAlias(), is (ALIAS_NAME)); 61 | BlockingQueue queue = new ArrayBlockingQueue(1 ); 62 | query.getQueryAttributes().iterator().next().collectMetrics(query.getObjectName(),1,0, queue); 63 | 64 | QueryResult result = queue.poll( 1, TimeUnit.SECONDS ); 65 | 66 | assertThat (result.getName(), is ("org_jmxtrans_embedded_replace.Value")); 67 | } 68 | 69 | 70 | 71 | @Test 72 | public void validateNoReplaceDotsInObjectName() 73 | throws MalformedObjectNameException, InterruptedException 74 | { 75 | ObjectName objectName = new ObjectName("org:name=org.jmxtrans.embedded.noreplace"); 76 | Query query = queriesByResultName.get(objectName.toString()); 77 | assertThat(query.getObjectName(), is(objectName)); 78 | assertThat(query.getResultAlias(), is (ALIAS_NAME)); 79 | BlockingQueue queue = new ArrayBlockingQueue(1 ); 80 | query.getQueryAttributes().iterator().next().collectMetrics(query.getObjectName(),1,0, queue); 81 | 82 | QueryResult result = queue.poll( 1, TimeUnit.SECONDS ); 83 | 84 | assertThat (result.getName(), is ("org.jmxtrans.embedded.noreplace.Value")); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/config/ConfigurationTemplatesTest.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.config; 25 | 26 | import org.junit.Test; 27 | 28 | import java.util.Arrays; 29 | import java.util.List; 30 | 31 | /** 32 | * @author Cyrille Le Clerc 33 | */ 34 | public class ConfigurationTemplatesTest { 35 | 36 | @Test 37 | public void loadAllTemplates() { 38 | List configurationUrls = Arrays.asList( 39 | "classpath:org/jmxtrans/embedded/config/jmxtrans-internals.json", 40 | "classpath:org/jmxtrans/embedded/config/jvm-sun-hotspot.json", 41 | "classpath:org/jmxtrans/embedded/config/tomcat-6.json" 42 | ); 43 | new ConfigurationParser().newEmbeddedJmxTrans(configurationUrls); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/AbstractOutputWriterTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.output; 2 | 3 | import org.jmxtrans.embedded.QueryResult; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class AbstractOutputWriterTest { 10 | 11 | @Test 12 | public void test_toString_should_start_with_fully_qualified_classname() { 13 | AbstractOutputWriter abstractOutputWriter = new AbstractOutputWriter() { 14 | @Override 15 | public void write(Iterable results) { 16 | 17 | } 18 | }; 19 | String toStringValue = abstractOutputWriter.toString(); 20 | String expectedStartString = AbstractOutputWriterTest.class.getName(); 21 | assertTrue("toString() value should start with " + expectedStartString, toStringValue.startsWith(expectedStartString)); 22 | } 23 | 24 | @Test 25 | public void test_toString_exact() { 26 | AbstractOutputWriter abstractOutputWriter = new AbstractOutputWriter() { 27 | @Override 28 | public void write(Iterable results) { 29 | 30 | } 31 | }; 32 | String toStringValue = abstractOutputWriter.toString(); 33 | assertEquals("org.jmxtrans.embedded.output.AbstractOutputWriterTest$2{enabled=true, settings={}}", toStringValue); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/CopperEggWriterTest.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.junit.Test; 28 | 29 | import java.io.ByteArrayOutputStream; 30 | import java.util.Arrays; 31 | import java.util.List; 32 | 33 | /** 34 | * @author Cyrille Le Clerc 35 | */ 36 | /** 37 | * This code is LibratoWriterTest.java, modified to provide the same 38 | * functionality for testing CopperEggWriter.java. 39 | */ 40 | 41 | public class CopperEggWriterTest { 42 | @Test 43 | public void testCUESerialize() throws Exception { 44 | 45 | List counters = Arrays.asList( 46 | new QueryResult("counter1", "counter", 10, System.currentTimeMillis()), 47 | new QueryResult("counter2", "counter", 11.11, System.currentTimeMillis() - 1000), 48 | new QueryResult("counter2", "counter", 12.12, System.currentTimeMillis()) 49 | 50 | ); 51 | 52 | List gauges = Arrays.asList( 53 | new QueryResult("gauge1", "gauge", 9.9, System.currentTimeMillis()), 54 | new QueryResult("gauge2", "gauge", 12.12, System.currentTimeMillis() - 1000), 55 | new QueryResult("gauge2", "gauge", 12.12, System.currentTimeMillis()) 56 | ); 57 | 58 | CopperEggWriter writer = new CopperEggWriter(); 59 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 60 | 61 | writer.cue_serialize(counters, baos); 62 | baos.flush(); 63 | 64 | System.out.println(new String(baos.toByteArray())); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/CsvWriterTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.output; 2 | 3 | import org.jmxtrans.embedded.QueryResult; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | import java.io.BufferedReader; 9 | import java.io.File; 10 | import java.io.FileInputStream; 11 | import java.io.InputStreamReader; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | /** 18 | * @author Ryan Larson 19 | */ 20 | public class CsvWriterTest { 21 | 22 | CsvWriter csvWriter; 23 | static File temp; 24 | 25 | @Before 26 | public void setUp() throws Exception { 27 | temp = new File("csvWriterTest.txt"); 28 | csvWriter = new CsvWriter(); 29 | csvWriter.outputFilePath = temp.getPath(); 30 | csvWriter.start(); 31 | } 32 | 33 | @After 34 | public void tearDown() throws Exception { 35 | csvWriter.stop(); 36 | temp.delete(); 37 | } 38 | 39 | @Test 40 | public void testCreatesCorrectHeader() { 41 | csvWriter.write( 42 | makeQueryResults("first:1", "second:2", "third:3", "fourth:4") 43 | ); 44 | 45 | assertEquals(csvWriter.header[0], "time"); 46 | assertEquals(csvWriter.header[1], "first"); 47 | assertEquals(csvWriter.header[2], "second"); 48 | assertEquals(csvWriter.header[3], "third"); 49 | assertEquals(csvWriter.header[4], "fourth"); 50 | } 51 | 52 | @Test 53 | public void testAlignResultsRealignsScrambledResults() { 54 | csvWriter.write( 55 | makeQueryResults("first:1", "second:2", "third:3", "fourth:4") 56 | ); 57 | 58 | List alignedResults = csvWriter.alignResults( 59 | makeQueryResults("second:2", "first:1", "fourth:4", "third:3"), "123456" 60 | ); 61 | 62 | assertEquals(alignedResults.get(0), "123456"); 63 | assertEquals(alignedResults.get(1), "1"); 64 | assertEquals(alignedResults.get(2), "2"); 65 | assertEquals(alignedResults.get(3), "3"); 66 | assertEquals(alignedResults.get(4), "4"); 67 | } 68 | 69 | @Test 70 | public void testAlignResultsPreservesCorrectlyAlignedResults() { 71 | csvWriter.write( 72 | makeQueryResults("first:1", "second:2", "third:3", "fourth:4") 73 | ); 74 | 75 | List alignedResults = csvWriter.alignResults( 76 | makeQueryResults("first:1", "second:2", "third:3", "fourth:4"), "123456" 77 | ); 78 | 79 | assertEquals(alignedResults.get(0), "123456"); 80 | assertEquals(alignedResults.get(1), "1"); 81 | assertEquals(alignedResults.get(2), "2"); 82 | assertEquals(alignedResults.get(3), "3"); 83 | assertEquals(alignedResults.get(4), "4"); 84 | } 85 | 86 | @Test 87 | public void testCsvFileIsWrittenOutCorrectly() throws Exception { 88 | csvWriter.write( 89 | makeQueryResults("first:1", "second:2", "third:3", "fourth:4") 90 | ); 91 | 92 | csvWriter.write( 93 | makeQueryResults("second:2", "first:1", "fourth:4", "third:3") 94 | ); 95 | 96 | csvWriter.write( 97 | makeQueryResults("first:1", "second:2", "third:3", "fourth:4") 98 | ); 99 | 100 | 101 | BufferedReader reader = new BufferedReader( 102 | new InputStreamReader( 103 | new FileInputStream(temp))); 104 | try { 105 | assertEquals(reader.readLine(), "time,first,second,third,fourth"); 106 | assertEquals(reader.readLine(), "123456,1,2,3,4"); 107 | assertEquals(reader.readLine(), "123456,1,2,3,4"); 108 | assertEquals(reader.readLine(), "123456,1,2,3,4"); 109 | } finally { 110 | reader.close(); 111 | } 112 | } 113 | 114 | List makeQueryResults(String... keyValues) { 115 | List results = new ArrayList(); 116 | 117 | for (String keyValue : keyValues) { 118 | results.add(makeQueryResult(keyValue.split(":")[0], keyValue.split(":")[1])); 119 | } 120 | 121 | return results; 122 | } 123 | 124 | private QueryResult makeQueryResult(String name, String value) { 125 | return new QueryResult(name, value, 123456789L); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/GraphiteHttpWriterIntegrationTest.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.junit.After; 28 | import org.junit.Before; 29 | import org.junit.Test; 30 | 31 | import java.util.*; 32 | 33 | /** 34 | * @author Simon Mazas 35 | */ 36 | public class GraphiteHttpWriterIntegrationTest { 37 | 38 | GraphiteHttpWriter graphiteHttpWriter; 39 | 40 | @Before 41 | public void before() { 42 | graphiteHttpWriter = new GraphiteHttpWriter(); 43 | Map settings = new HashMap(); 44 | settings.put(AbstractOutputWriter.SETTING_URL, "http://10.1.4.33:2013/upload"); 45 | 46 | graphiteHttpWriter.setSettings(settings); 47 | graphiteHttpWriter.start(); 48 | } 49 | 50 | @After 51 | public void after() throws Exception { 52 | graphiteHttpWriter.stop(); 53 | } 54 | 55 | @Test(timeout = 5000) 56 | public void testWithOneResult() throws Exception { 57 | QueryResult queryResult = new QueryResult("testwithoneresult.singleresult", 10, System.currentTimeMillis()); 58 | graphiteHttpWriter.write(Collections.singleton(queryResult)); 59 | } 60 | 61 | @Test(timeout = 5000) 62 | public void testWithTwoResult() throws Exception { 63 | 64 | QueryResult queryResult1 = new QueryResult("testwithtworesult.first", 10, System.currentTimeMillis()); 65 | QueryResult queryResult2 = new QueryResult("testwithtworesult.second", 20, System.currentTimeMillis()); 66 | List results = Arrays.asList(queryResult1, queryResult2); 67 | 68 | graphiteHttpWriter.write(results); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/GraphitePickleWriterIntegrationTest.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.junit.After; 28 | import org.junit.Before; 29 | import org.junit.Test; 30 | 31 | import java.util.*; 32 | 33 | /** 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class GraphitePickleWriterIntegrationTest { 37 | 38 | GraphitePickleWriter graphitePickleWriter; 39 | 40 | @Before 41 | public void before() { 42 | graphitePickleWriter = new GraphitePickleWriter(); 43 | Map settings = new HashMap(); 44 | settings.put(AbstractOutputWriter.SETTING_HOST, "localhost"); 45 | settings.put(AbstractOutputWriter.SETTING_PORT, 2004); 46 | 47 | graphitePickleWriter.setSettings(settings); 48 | graphitePickleWriter.start(); 49 | } 50 | 51 | @After 52 | public void after() throws Exception { 53 | graphitePickleWriter.stop(); 54 | } 55 | 56 | @Test 57 | public void testWithOneResult() throws Exception { 58 | QueryResult queryResult = new QueryResult("testwithoneresult.singleresult", 10, System.currentTimeMillis()); 59 | graphitePickleWriter.write(Collections.singleton(queryResult)); 60 | } 61 | 62 | @Test 63 | public void testWithTwoResult() throws Exception { 64 | 65 | QueryResult queryResult1 = new QueryResult("testwithtworesult.first", 10, System.currentTimeMillis()); 66 | QueryResult queryResult2 = new QueryResult("testwithtworesult.second", 20, System.currentTimeMillis()); 67 | List results = Arrays.asList(queryResult1, queryResult2); 68 | 69 | graphitePickleWriter.write(results); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/GraphiteWriterTcpIntegrationTest.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.jmxtrans.embedded.TestUtils; 28 | import org.junit.After; 29 | import org.junit.Assert; 30 | import org.junit.Before; 31 | import org.junit.Test; 32 | 33 | import java.io.InputStream; 34 | import java.util.*; 35 | 36 | /** 37 | * @author Cyrille Le Clerc 38 | */ 39 | public class GraphiteWriterTcpIntegrationTest { 40 | 41 | GraphiteWriter graphiteWriter; 42 | 43 | @Before 44 | public void before() throws Exception { 45 | Map settings = TestUtils.loadSettingsFromClasspath("my-graphite-tcp-config.properties"); 46 | graphiteWriter = new GraphiteWriter(); 47 | graphiteWriter.setSettings(settings); 48 | graphiteWriter.start(); 49 | } 50 | 51 | @After 52 | public void after() throws Exception { 53 | graphiteWriter.stop(); 54 | } 55 | 56 | @Test 57 | public void testWithOneResult() throws Exception { 58 | QueryResult queryResult = new QueryResult("tcp_no_tls.testwithoneresult.singleresult", 10, System.currentTimeMillis()); 59 | graphiteWriter.write(Collections.singleton(queryResult)); 60 | } 61 | 62 | @Test 63 | public void testWithTwoResult() throws Exception { 64 | 65 | QueryResult queryResult1 = new QueryResult("tcp_no_tls.testwithtworesult.first", 10, System.currentTimeMillis()); 66 | QueryResult queryResult2 = new QueryResult("tcp_no_tls.testwithtworesult.second", 20, System.currentTimeMillis()); 67 | List results = Arrays.asList(queryResult1, queryResult2); 68 | 69 | graphiteWriter.write(results); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/GraphiteWriterTcpTlsIntegrationTest.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.jmxtrans.embedded.TestUtils; 28 | import org.junit.After; 29 | import org.junit.Before; 30 | import org.junit.Test; 31 | 32 | import java.util.Arrays; 33 | import java.util.Collections; 34 | import java.util.List; 35 | import java.util.Map; 36 | 37 | /** 38 | * @author Cyrille Le Clerc 39 | */ 40 | public class GraphiteWriterTcpTlsIntegrationTest { 41 | 42 | GraphiteWriter graphiteWriter; 43 | 44 | @Before 45 | public void before() throws Exception { 46 | Map settings = TestUtils.loadSettingsFromClasspath("my-graphite-tcp-tls-config.properties"); 47 | graphiteWriter = new GraphiteWriter(); 48 | graphiteWriter.setSettings(settings); 49 | graphiteWriter.start(); 50 | } 51 | 52 | @After 53 | public void after() throws Exception { 54 | graphiteWriter.stop(); 55 | } 56 | 57 | @Test 58 | public void testWithOneResult() throws Exception { 59 | QueryResult queryResult = new QueryResult("tcp_tls.testwithoneresult.singleresult", 10, System.currentTimeMillis()); 60 | graphiteWriter.write(Collections.singleton(queryResult)); 61 | } 62 | 63 | @Test 64 | public void testWithTwoResult() throws Exception { 65 | 66 | QueryResult queryResult1 = new QueryResult("tcp_tls.testwithtworesult.first", 10, System.currentTimeMillis()); 67 | QueryResult queryResult2 = new QueryResult("tcp_tls.testwithtworesult.second", 20, System.currentTimeMillis()); 68 | List results = Arrays.asList(queryResult1, queryResult2); 69 | 70 | graphiteWriter.write(results); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/GraphiteWriterUdpIntegrationTest.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.jmxtrans.embedded.TestUtils; 28 | import org.junit.After; 29 | import org.junit.Assert; 30 | import org.junit.Before; 31 | import org.junit.Test; 32 | 33 | import java.io.InputStream; 34 | import java.util.*; 35 | 36 | /** 37 | * @author Cyrille Le Clerc 38 | */ 39 | public class GraphiteWriterUdpIntegrationTest { 40 | 41 | GraphiteWriter graphiteWriter; 42 | 43 | @Before 44 | public void before() throws Exception { 45 | Map settings = TestUtils.loadSettingsFromClasspath("my-graphite-udp-config.properties"); 46 | graphiteWriter = new GraphiteWriter(); 47 | graphiteWriter.setSettings(settings); 48 | graphiteWriter.start(); 49 | } 50 | 51 | @After 52 | public void after() throws Exception { 53 | graphiteWriter.stop(); 54 | } 55 | 56 | @Test 57 | public void testWithOneResult() throws Exception { 58 | QueryResult queryResult = new QueryResult("udp.testwithoneresult.singleresult", 10, System.currentTimeMillis()); 59 | graphiteWriter.write(Collections.singleton(queryResult)); 60 | } 61 | 62 | @Test 63 | public void testWithTwoResult() throws Exception { 64 | 65 | QueryResult queryResult1 = new QueryResult("udp.testwithtworesult.first", 10, System.currentTimeMillis()); 66 | QueryResult queryResult2 = new QueryResult("udp.testwithtworesult.second", 20, System.currentTimeMillis()); 67 | List results = Arrays.asList(queryResult1, queryResult2); 68 | 69 | graphiteWriter.write(results); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/LibratoWriterTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.output; 2 | 3 | import org.hamcrest.Matchers; 4 | import org.jmxtrans.embedded.QueryResult; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.io.ByteArrayOutputStream; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | /** 13 | * @author Cyrille Le Clerc 14 | */ 15 | public class LibratoWriterTest { 16 | @Test 17 | public void testSerialize() throws Exception { 18 | 19 | List counters = Arrays.asList( 20 | new QueryResult("counter1", "counter", 10, System.currentTimeMillis()), 21 | new QueryResult("counter2", "counter", 11.11, System.currentTimeMillis() - 1000), 22 | new QueryResult("counter2", "counter", 12.12, System.currentTimeMillis()) 23 | 24 | ); 25 | 26 | List gauges = Arrays.asList( 27 | new QueryResult("gauge1", "gauge", 9.9, System.currentTimeMillis()), 28 | new QueryResult("gauge2", "gauge", 12.12, System.currentTimeMillis() - 1000), 29 | new QueryResult("gauge2", "gauge", 12.12, System.currentTimeMillis()) 30 | ); 31 | 32 | LibratoWriter writer = new LibratoWriter(); 33 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 34 | 35 | writer.serialize(counters, gauges, baos); 36 | baos.flush(); 37 | 38 | System.out.println(new String(baos.toByteArray())); 39 | 40 | } 41 | @Test 42 | public void testHttpUserAgent(){ 43 | String actual = new LibratoWriter().httpUserAgent; 44 | 45 | Assert.assertThat(actual, Matchers.startsWith("embedded-jmxtrans/")); 46 | Assert.assertThat(actual, Matchers.containsString(System.getProperty("os.name"))); 47 | Assert.assertThat(actual, Matchers.containsString(System.getProperty("os.arch"))); 48 | Assert.assertThat(actual, Matchers.containsString(System.getProperty("os.version"))); 49 | 50 | Assert.assertThat(actual, Matchers.containsString(System.getProperty("java.vm.name"))); 51 | Assert.assertThat(actual, Matchers.containsString(System.getProperty("java.version"))); 52 | 53 | System.out.println(actual); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/StackdriverWriterTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.output; 2 | 3 | import org.jmxtrans.embedded.QueryResult; 4 | import org.junit.Test; 5 | 6 | import java.io.ByteArrayOutputStream; 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | /** 11 | * Basic test for the Stackdriver writer 12 | */ 13 | public class StackdriverWriterTest { 14 | @Test 15 | public void testSerialize() throws Exception { 16 | 17 | List metrics = Arrays.asList( 18 | new QueryResult("metric1", "counter", 10, System.currentTimeMillis()), 19 | new QueryResult("metric2", "counter", 11.11, System.currentTimeMillis() - 1000), 20 | new QueryResult("metric2", "counter", 12.12, System.currentTimeMillis()), 21 | new QueryResult("metric3", "gauge", 9.9, System.currentTimeMillis()), 22 | new QueryResult("metric3", "gauge", 12.12, System.currentTimeMillis() - 1000), 23 | new QueryResult("metric4", "gauge", 12.12, System.currentTimeMillis()) 24 | 25 | ); 26 | 27 | StackdriverWriter writer = new StackdriverWriter(); 28 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 29 | 30 | writer.serialize(metrics, baos); 31 | baos.flush(); 32 | 33 | System.out.println(new String(baos.toByteArray())); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/StatsDIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.output; 2 | 3 | import org.jmxtrans.embedded.EmbeddedJmxTrans; 4 | import org.jmxtrans.embedded.config.ConfigurationParser; 5 | import org.junit.Ignore; 6 | import org.junit.Test; 7 | 8 | import java.util.Arrays; 9 | import java.util.List; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | /** 13 | * @author Cyrille Le Clerc 14 | */ 15 | @Ignore 16 | public class StatsDIntegrationTest { 17 | public static void main(String[] args) throws Exception { 18 | StatsDIntegrationTest test = new StatsDIntegrationTest(); 19 | test.test(); 20 | } 21 | 22 | @Test 23 | public void test() throws Exception { 24 | List configurationFiles = Arrays.asList( 25 | "classpath:org/jmxtrans/embedded/config/jvm-sun-hotspot.json", 26 | "classpath:org/jmxtrans/embedded/config/jmxtrans-internals.json", 27 | "classpath:org/jmxtrans/embedded/output/statsd-writer.json" 28 | ); 29 | 30 | ConfigurationParser configurationParser = new ConfigurationParser(); 31 | EmbeddedJmxTrans embeddedJmxTrans = configurationParser.newEmbeddedJmxTrans(configurationFiles); 32 | embeddedJmxTrans.setQueryIntervalInSeconds(5); 33 | embeddedJmxTrans.setExportIntervalInSeconds(10); 34 | embeddedJmxTrans.start(); 35 | Thread.sleep(TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES)); 36 | embeddedJmxTrans.stop(); 37 | System.out.println("bye"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/StatsDWriterIntegrationTest.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.junit.After; 28 | import org.junit.Before; 29 | import org.junit.Test; 30 | 31 | import java.util.*; 32 | 33 | /** 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class StatsDWriterIntegrationTest { 37 | 38 | StatsDWriter statsdWriter; 39 | 40 | @Before 41 | public void before() { 42 | statsdWriter = new StatsDWriter(); 43 | Map settings = new HashMap(); 44 | settings.put(AbstractOutputWriter.SETTING_HOST, "localhost"); 45 | settings.put(AbstractOutputWriter.SETTING_PORT, 8125); 46 | 47 | statsdWriter.setSettings(settings); 48 | statsdWriter.start(); 49 | } 50 | 51 | @After 52 | public void after() throws Exception { 53 | statsdWriter.stop(); 54 | } 55 | 56 | @Test 57 | public void testWithOneResult() throws Exception { 58 | QueryResult queryResult = new QueryResult("testwithoneresult.singleresult", 10, System.currentTimeMillis()); 59 | statsdWriter.write(Collections.singleton(queryResult)); 60 | } 61 | 62 | @Test 63 | public void testWithTwoResult() throws Exception { 64 | 65 | QueryResult queryResult1 = new QueryResult("testwithtworesult.first", 10, System.currentTimeMillis()); 66 | QueryResult queryResult2 = new QueryResult("testwithtworesult.second", 20, System.currentTimeMillis()); 67 | List results = Arrays.asList(queryResult1, queryResult2); 68 | 69 | statsdWriter.write(results); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/TestWriter1.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 | /** 27 | * @author Cyrille Le Clerc 28 | */ 29 | public class TestWriter1 extends NoOpWriter { 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/TestWriter2.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 | /** 27 | * @author Cyrille Le Clerc 28 | */ 29 | public class TestWriter2 extends NoOpWriter { 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/TestWriter3.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 | /** 27 | * @author Cyrille Le Clerc 28 | */ 29 | public class TestWriter3 extends NoOpWriter { 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/output/TestWriter4.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 | /** 27 | * @author Cyrille Le Clerc 28 | */ 29 | public class TestWriter4 extends NoOpWriter { 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/spring/EmbeddedJmxTransBeanDefinitionParser1Test.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.jmxtrans.embedded.Query; 28 | import org.jmxtrans.embedded.TestUtils; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | import org.springframework.beans.factory.annotation.Autowired; 32 | import org.springframework.test.context.ContextConfiguration; 33 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 34 | 35 | import java.util.Map; 36 | 37 | import static org.junit.Assert.assertNotNull; 38 | 39 | /** 40 | * @author Cyrille Le Clerc 41 | */ 42 | @RunWith(SpringJUnit4ClassRunner.class) 43 | @ContextConfiguration(locations = "test-spring-configuration-1.xml") 44 | public class EmbeddedJmxTransBeanDefinitionParser1Test { 45 | 46 | @Autowired 47 | EmbeddedJmxTrans embeddedJmxTrans; 48 | 49 | @Test 50 | public void test() { 51 | Map queries = TestUtils.indexQueriesByAliasOrName(embeddedJmxTrans.getQueries()); 52 | Query tomcatProcessorQuery = queries.get("tomcat.global-request-processor.http-nio"); 53 | assertNotNull(tomcatProcessorQuery); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/spring/EmbeddedJmxTransBeanDefinitionParser2Test.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 static org.junit.Assert.*; 27 | import org.jmxtrans.embedded.EmbeddedJmxTrans; 28 | import org.jmxtrans.embedded.Query; 29 | import org.jmxtrans.embedded.TestUtils; 30 | import org.junit.Test; 31 | import org.junit.runner.RunWith; 32 | import org.springframework.beans.factory.annotation.Autowired; 33 | import org.springframework.test.context.ContextConfiguration; 34 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 35 | 36 | import java.util.Map; 37 | 38 | /** 39 | * @author Cyrille Le Clerc 40 | */ 41 | @RunWith(SpringJUnit4ClassRunner.class) 42 | @ContextConfiguration(locations = "test-spring-configuration-2.xml") 43 | public class EmbeddedJmxTransBeanDefinitionParser2Test { 44 | 45 | @Autowired 46 | EmbeddedJmxTrans embeddedJmxTrans; 47 | 48 | @Test 49 | public void test() { 50 | Map queries = TestUtils.indexQueriesByAliasOrName(embeddedJmxTrans.getQueries()); 51 | Query tomcatProcessorQuery = queries.get("tomcat.global-request-processor.http-nio"); 52 | assertNotNull(tomcatProcessorQuery); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/spring/EmbeddedJmxTransBeanDefinitionParser3Test.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.jmxtrans.embedded.Query; 28 | import org.jmxtrans.embedded.TestUtils; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | import org.springframework.beans.factory.annotation.Autowired; 32 | import org.springframework.test.context.ContextConfiguration; 33 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 34 | 35 | import java.util.Map; 36 | 37 | import static org.junit.Assert.assertNotNull; 38 | 39 | /** 40 | * @author Cyrille Le Clerc 41 | */ 42 | @RunWith(SpringJUnit4ClassRunner.class) 43 | @ContextConfiguration(locations = "test-spring-configuration-3.xml") 44 | public class EmbeddedJmxTransBeanDefinitionParser3Test { 45 | 46 | @Autowired 47 | EmbeddedJmxTrans embeddedJmxTrans; 48 | 49 | @Test 50 | public void test() { 51 | Map queries = TestUtils.indexQueriesByAliasOrName(embeddedJmxTrans.getQueries()); 52 | Query tomcatProcessorQuery = queries.get("tomcat.global-request-processor.http-nio"); 53 | assertNotNull(tomcatProcessorQuery); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/spring/EmbeddedJmxTransFactoryTest.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.junit.Test; 28 | import org.springframework.core.io.DefaultResourceLoader; 29 | 30 | import static org.hamcrest.CoreMatchers.*; 31 | import static org.junit.Assert.*; 32 | 33 | /** 34 | * @author Cyrille Le Clerc 35 | */ 36 | public class EmbeddedJmxTransFactoryTest { 37 | 38 | 39 | @Test 40 | public void testGetObject() throws Exception { 41 | String configuration = "classpath:org/jmxtrans/embedded/jmxtrans-factory-test.json"; 42 | EmbeddedJmxTransFactory factory = new EmbeddedJmxTransFactory(new DefaultResourceLoader()); 43 | factory.setConfigurationUrl(configuration); 44 | 45 | EmbeddedJmxTrans embeddedJmxTrans = factory.getObject(); 46 | assertThat(embeddedJmxTrans, notNullValue()); 47 | assertThat(embeddedJmxTrans.getQueries().size(), is(8)); 48 | assertThat(embeddedJmxTrans.getOutputWriters().size(), is(1)); 49 | 50 | embeddedJmxTrans.stop(); 51 | 52 | } 53 | 54 | @Test 55 | public void testGetObjectMultipleTimes() throws Exception { 56 | String configuration = "classpath:org/jmxtrans/embedded/jmxtrans-factory-test.json"; 57 | EmbeddedJmxTransFactory factory = new EmbeddedJmxTransFactory(new DefaultResourceLoader()); 58 | factory.setConfigurationUrl(configuration); 59 | 60 | EmbeddedJmxTrans embeddedJmxTrans = factory.getObject(); 61 | assertThat(embeddedJmxTrans, notNullValue()); 62 | assertThat(embeddedJmxTrans.getQueries().size(), is(8)); 63 | assertThat(embeddedJmxTrans.getOutputWriters().size(), is(1)); 64 | 65 | embeddedJmxTrans.stop(); 66 | 67 | embeddedJmxTrans.start(); 68 | embeddedJmxTrans.stop(); 69 | 70 | embeddedJmxTrans.start(); 71 | embeddedJmxTrans.stop(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/StringUtils2Test.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 org.hamcrest.collection.IsIterableContainingInOrder; 27 | import org.junit.Test; 28 | 29 | import java.util.Arrays; 30 | import java.util.List; 31 | 32 | import static org.hamcrest.Matchers.is; 33 | import static org.junit.Assert.assertThat; 34 | 35 | /** 36 | * @author Cyrille Le Clerc 37 | */ 38 | public class StringUtils2Test { 39 | 40 | @Test 41 | public void testDelimitedStringToList() { 42 | List actual = StringUtils2.delimitedStringToList("a,b;c\nd,,e,;f"); 43 | System.out.println(actual); 44 | assertThat(actual, IsIterableContainingInOrder.contains("a", "b", "c", "d", "e", "f")); 45 | } 46 | 47 | @Test 48 | public void testJoin() { 49 | List tokens = Arrays.asList("com", "mycompany", "ecommerce", "server1"); 50 | String actual = StringUtils2.join(tokens, "."); 51 | assertThat(actual, is("com.mycompany.ecommerce.server1")); 52 | } 53 | 54 | @Test 55 | public void testReverseTokens() { 56 | String in = "server1.ecommerce.mycompany.com"; 57 | String actual = StringUtils2.reverseTokens(in, "."); 58 | assertThat(actual, is("com.mycompany.ecommerce.server1")); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/concurrent/DiscardingBlockingQueueTest.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 org.junit.Before; 27 | import org.junit.Test; 28 | 29 | import java.util.Arrays; 30 | import java.util.List; 31 | import java.util.concurrent.TimeUnit; 32 | 33 | import static org.hamcrest.CoreMatchers.*; 34 | import static org.junit.Assert.*; 35 | 36 | /** 37 | * @author Cyrille Le Clerc 38 | */ 39 | public class DiscardingBlockingQueueTest { 40 | DiscardingBlockingQueue queue; 41 | 42 | @Before 43 | public void before() { 44 | queue = new DiscardingBlockingQueue(5); 45 | } 46 | 47 | @Test 48 | public void testAdd() throws Exception { 49 | for (int i = 0; i < 10; i++) { 50 | queue.add(i); 51 | } 52 | verifyQueueElements(); 53 | } 54 | 55 | private void verifyQueueElements() { 56 | assertThat(queue.remainingCapacity(), is(0)); 57 | assertThat(queue.poll(), is(5)); 58 | assertThat(queue.poll(), is(6)); 59 | assertThat(queue.poll(), is(7)); 60 | assertThat(queue.poll(), is(8)); 61 | assertThat(queue.poll(), is(9)); 62 | } 63 | 64 | @Test 65 | public void testOffer() throws Exception { 66 | for (int i = 0; i < 10; i++) { 67 | queue.offer(i); 68 | } 69 | verifyQueueElements(); 70 | } 71 | 72 | @Test 73 | public void testPut() throws Exception { 74 | for (int i = 0; i < 10; i++) { 75 | queue.put(i); 76 | } 77 | verifyQueueElements(); 78 | } 79 | 80 | @Test 81 | public void testOfferWithTimeout() throws Exception { 82 | for (int i = 0; i < 10; i++) { 83 | queue.offer(i, 100, TimeUnit.MILLISECONDS); 84 | } 85 | verifyQueueElements(); 86 | } 87 | 88 | @Test 89 | public void testAddAll() throws Exception { 90 | List integers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); 91 | queue.addAll(integers); 92 | 93 | verifyQueueElements(); 94 | } 95 | 96 | @Test 97 | public void testRemainingCapacity() throws Exception { 98 | List integers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); 99 | queue.addAll(integers); 100 | 101 | verifyQueueElements(); 102 | queue.put(10); 103 | queue.put(11); 104 | assertThat(queue.size(), is(2)); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/json/PlaceholderEnabledJsonNodeFactoryTest.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.JsonNode; 27 | import com.fasterxml.jackson.databind.ObjectMapper; 28 | import org.junit.Test; 29 | 30 | import java.io.InputStream; 31 | 32 | import static org.hamcrest.CoreMatchers.*; 33 | import static org.junit.Assert.*; 34 | 35 | /** 36 | * @author Cyrille Le Clerc 37 | */ 38 | public class PlaceholderEnabledJsonNodeFactoryTest { 39 | 40 | @Test 41 | public void testWithPlaceholders() throws Exception { 42 | System.setProperty("graphite.host", "graphite.www.private.mycompany.com"); 43 | System.setProperty("server.name", "tomcat1"); 44 | try { 45 | String configurationUrl = "org/jmxtrans/embedded/util/json/jmxtrans-placeholder-test.json"; 46 | InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(configurationUrl); 47 | ObjectMapper objectMapper = new ObjectMapper(); 48 | objectMapper.setNodeFactory(new PlaceholderEnabledJsonNodeFactory()); 49 | JsonNode rootNode = objectMapper.readValue(in, JsonNode.class); 50 | JsonNode outputWritersNode = rootNode.path("outputWriters"); 51 | JsonNode outputWriterNode = outputWritersNode.get(1); 52 | assertThat(outputWriterNode.path("@class").asText(), is("org.jmxtrans.embedded.output.GraphiteWriter")); 53 | JsonNode settingsNode = outputWriterNode.path("settings"); 54 | assertThat(settingsNode.path("host").asText(), is("graphite.www.private.mycompany.com")); 55 | assertThat(settingsNode.path("port").asInt(), is(2003)); 56 | assertThat(settingsNode.path("namePrefix").asText(), is("servers.tomcat1.")); 57 | 58 | } finally { 59 | System.getProperties().remove("graphite.host"); 60 | System.getProperties().remove("server.name"); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/json/PropertyPlaceholderResolverTest.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 org.junit.Test; 27 | 28 | import static org.hamcrest.CoreMatchers.*; 29 | import static org.junit.Assert.*; 30 | 31 | /** 32 | * @author Cyrille Le Clerc 33 | */ 34 | public class PropertyPlaceholderResolverTest { 35 | 36 | private PropertyPlaceholderResolver resolver = new PropertyPlaceholderResolver(); 37 | 38 | @Test 39 | public void testResolveStringWithSystemProperty() { 40 | System.setProperty("graphite.host", "graphite-server.private.mycompany.com"); 41 | try { 42 | String actual = resolver.resolveString("${graphite.host:localhost}"); 43 | assertThat(actual, is("graphite-server.private.mycompany.com")); 44 | } finally { 45 | System.getProperties().remove("graphite.host"); 46 | } 47 | } 48 | 49 | @Test 50 | public void testResolveComplexStringWithSystemProperty() { 51 | System.setProperty("graphite.host", "graphite-server.private.mycompany.com"); 52 | try { 53 | String actual = resolver.resolveString("${graphite.host:localhost}:${graphite.port:2003}"); 54 | assertThat(actual, is("graphite-server.private.mycompany.com:2003")); 55 | } finally { 56 | System.getProperties().remove("graphite.host"); 57 | } 58 | } 59 | 60 | @Test 61 | public void testResolveStringWithDefaultValue() { 62 | String actual = resolver.resolveString("${graphite.host:localhost}"); 63 | assertThat(actual, is("localhost")); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/net/ssl/SslUtilsIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.util.net.ssl; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.net.ssl.HostnameVerifier; 6 | import javax.net.ssl.HttpsURLConnection; 7 | import javax.net.ssl.SSLSession; 8 | import javax.net.ssl.SSLSocketFactory; 9 | import java.net.URL; 10 | 11 | /** 12 | * @author Cyrille Le Clerc 13 | */ 14 | public class SslUtilsIntegrationTest { 15 | 16 | @Test 17 | public void test_trust_invalid_ssl_certificate_with_google_ip() throws Exception { 18 | String url = "https://216.58.211.68"; // 216.58.211.68 is google.com 19 | HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); 20 | SSLSocketFactory socketFactory = SslUtils.getTrustAllSSLSocketFactory(); 21 | 22 | connection.setSSLSocketFactory(socketFactory); 23 | 24 | connection.setHostnameVerifier(new HostnameVerifier() { 25 | @Override 26 | public boolean verify(String hostname, SSLSession sslSession) { 27 | return true; 28 | } 29 | }); 30 | int responseCode = connection.getResponseCode(); 31 | 32 | System.out.println(url + "\t->\t" + responseCode); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/net/ssl/SslUtilsTest.java: -------------------------------------------------------------------------------- 1 | package org.jmxtrans.embedded.util.net.ssl; 2 | 3 | import com.sun.net.httpserver.*; 4 | import org.hamcrest.Matchers; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.annotation.Nonnull; 9 | import javax.net.ssl.HttpsURLConnection; 10 | import javax.net.ssl.SSLContext; 11 | import javax.net.ssl.SSLParameters; 12 | import javax.net.ssl.SSLSocketFactory; 13 | import java.io.File; 14 | import java.io.IOException; 15 | import java.io.OutputStream; 16 | import java.net.InetSocketAddress; 17 | import java.net.URL; 18 | import java.util.concurrent.Executors; 19 | 20 | /** 21 | * @author Cyrille Le Clerc 22 | */ 23 | public class SslUtilsTest { 24 | 25 | @Test 26 | public void test_keystore_in_classpath_google_dot_com() throws Exception { 27 | 28 | String keyStore = "classpath:org/jmxtrans/embedded/util/net/ssl/cacerts"; 29 | String passphrase = "changeit"; 30 | SSLSocketFactory sslSocketFactory = SslUtils.getSSLSocketFactory(keyStore, passphrase, keyStore, passphrase); 31 | 32 | URL url = new URL("https://www.google.com"); 33 | 34 | HttpsURLConnection cnn = (HttpsURLConnection) url.openConnection(); 35 | cnn.setSSLSocketFactory(sslSocketFactory); 36 | 37 | int responseCode = cnn.getResponseCode(); 38 | 39 | Assert.assertThat(responseCode, Matchers.is(200)); 40 | } 41 | 42 | @Test 43 | public void test_keystore_on_file_system_google_dot_com() throws Exception { 44 | 45 | String keyStore = "org/jmxtrans/embedded/util/net/ssl/cacerts"; 46 | URL keyStoreUrl = Thread.currentThread().getContextClassLoader().getResource(keyStore); 47 | File keyStoreFile = new File(keyStoreUrl.toURI()); 48 | String keystorePassword = "changeit"; 49 | String keyStoreFilePath = keyStoreFile.getAbsolutePath(); 50 | 51 | SSLSocketFactory sslSocketFactory = SslUtils.getSSLSocketFactory(keyStoreFilePath, keystorePassword, keyStoreFilePath, keystorePassword); 52 | 53 | URL url = new URL("https://www.google.com"); 54 | HttpsURLConnection cnn = (HttpsURLConnection) url.openConnection(); 55 | cnn.setSSLSocketFactory(sslSocketFactory); 56 | 57 | int responseCode = cnn.getResponseCode(); 58 | 59 | Assert.assertThat(responseCode, Matchers.is(200)); 60 | } 61 | 62 | 63 | @Test 64 | public void test_selfsigned_certificate() throws Exception { 65 | String password = "password"; 66 | String keyStore = "classpath:org/jmxtrans/embedded/util/net/ssl/keystore.jks"; 67 | String trustStore = "classpath:org/jmxtrans/embedded/util/net/ssl/truststore.jks"; 68 | 69 | HttpsServer server = getHttpsServer(keyStore, password); 70 | 71 | URL testUrl = new URL("https://127.0.0.1:" + server.getAddress().getPort() + "/"); 72 | 73 | HttpsURLConnection cnn = (HttpsURLConnection) testUrl.openConnection(); 74 | cnn.setSSLSocketFactory(SslUtils.getSSLSocketFactory(null, null, trustStore, password)); 75 | 76 | int responseCode = cnn.getResponseCode(); 77 | 78 | Assert.assertThat(responseCode, Matchers.is(200)); 79 | server.stop(5); // seconds 80 | } 81 | 82 | /** 83 | * @param keyStore keystore containing the certificate used by the HTTPS server 84 | * @param password password of the keystore 85 | * @return HTTP server listening on a random port 86 | * @throws IOException 87 | */ 88 | @Nonnull 89 | private HttpsServer getHttpsServer(String keyStore, String password) throws IOException { 90 | HttpsServer server; 91 | SSLContext sslContext = SslUtils.getSslContext(keyStore, password, null, null); 92 | server = HttpsServer.create(new InetSocketAddress(0), 5); 93 | server.createContext("/", new HttpHandler() { 94 | @Override 95 | public void handle(HttpExchange exchange) throws IOException { 96 | String response = "Hello world"; 97 | exchange.sendResponseHeaders(200, response.length()); 98 | OutputStream os = exchange.getResponseBody(); 99 | os.write(response.getBytes()); 100 | os.close(); 101 | } 102 | }); 103 | server.setHttpsConfigurator(new HttpsConfigurator(sslContext) { 104 | public void configure(HttpsParameters params) { 105 | SSLContext c = getSSLContext(); 106 | SSLParameters sslParams = c.getDefaultSSLParameters(); 107 | params.setSSLParameters(sslParams); 108 | 109 | 110 | } 111 | }); 112 | server.setExecutor(Executors.newCachedThreadPool()); 113 | server.start(); 114 | return server; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/test/java/org/jmxtrans/embedded/util/pool/ManagedGenericKeyedObjectPoolTest.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.pool; 25 | 26 | import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; 27 | import org.apache.commons.pool2.PooledObject; 28 | import org.apache.commons.pool2.impl.DefaultPooledObject; 29 | import org.apache.commons.pool2.impl.GenericKeyedObjectPool; 30 | import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; 31 | import org.junit.Test; 32 | 33 | import javax.management.MBeanServer; 34 | import javax.management.ObjectName; 35 | import java.lang.management.ManagementFactory; 36 | 37 | 38 | import static org.hamcrest.CoreMatchers.*; 39 | import static org.junit.Assert.*; 40 | 41 | /** 42 | * @author Cyrille Le Clerc 43 | */ 44 | public class ManagedGenericKeyedObjectPoolTest { 45 | 46 | @Test 47 | public void testMbeanAttributeAccess() throws Exception { 48 | BaseKeyedPooledObjectFactory factory = new BaseKeyedPooledObjectFactory() { 49 | @Override 50 | public String create(String key) throws Exception { 51 | return key; 52 | } 53 | 54 | @Override 55 | public PooledObject wrap(String value) { 56 | return new DefaultPooledObject(value); 57 | } 58 | }; 59 | GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig(); 60 | config.setJmxNameBase("org.jmxtrans.embedded:type=GenericKeyedObjectPool,writer=MyWriter,name="); 61 | config.setJmxNamePrefix("my-host_1234"); 62 | GenericKeyedObjectPool objectPool = new GenericKeyedObjectPool(factory, config); 63 | 64 | ObjectName objectName = new ObjectName("org.jmxtrans.embedded:type=GenericKeyedObjectPool,writer=MyWriter,name=my-host_1234"); 65 | MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); 66 | try { 67 | Object numIdle = mbeanServer.getAttribute(objectName, "NumIdle"); 68 | assertThat(numIdle, instanceOf(Number.class)); 69 | 70 | } finally { 71 | mbeanServer.unregisterMBean(objectName); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/resources/.gitignore: -------------------------------------------------------------------------------- 1 | my-*.properties -------------------------------------------------------------------------------- /src/test/resources/graphite-tcp-config.template.properties: -------------------------------------------------------------------------------- 1 | host=graphite.example.com 2 | port=2003 3 | namePrefix=servers.#hostname#. 4 | -------------------------------------------------------------------------------- /src/test/resources/graphite-tcp-tls-config.template.properties: -------------------------------------------------------------------------------- 1 | host=graphite.example.com 2 | port=20030 3 | namePrefix=servers.#hostname#. 4 | useTls=true 5 | -------------------------------------------------------------------------------- /src/test/resources/graphite-udp-config.template.properties: -------------------------------------------------------------------------------- 1 | host=graphite.example.com 2 | port=2003 3 | namePrefix=servers.#hostname#. 4 | protocol=udp 5 | -------------------------------------------------------------------------------- /src/test/resources/librato-config.template.properties: -------------------------------------------------------------------------------- 1 | # Create a file my-librato-config.properties with the following properties 2 | 3 | LIBRATO_USER=YOUR_TOKEN_USER 4 | LIBRATO_TOKEN=YOUR_TOKEN_TOKEN -------------------------------------------------------------------------------- /src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 24 | 25 | 26 | 27 | 28 | %date{yyyy/MM/dd HH:mm:ss.SSS,Europe/Paris} %5level [%thread] %logger{16} - %message%n 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/jmxtrans-config-merge-1-test.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 5 | "attribute": "CollectionUsageThreshold" 6 | 7 | }, 8 | { 9 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 10 | "resultAlias": "test-aliased-query", 11 | "attribute": "CollectionUsageThresholdCount" 12 | }, 13 | { 14 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 15 | "resultAlias": "test-attribute-with-alias", 16 | "attribute": { 17 | "name": "CollectionUsageThresholdCount", 18 | "resultAlias": "test-alias" 19 | } 20 | }, 21 | { 22 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 23 | "resultAlias": "test-attributes", 24 | "attributes": ["CollectionUsageThresholdExceeded", "CollectionUsageThresholdSupported"] 25 | }, 26 | { 27 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 28 | "resultAlias": "test-attributes-with-alias", 29 | "attributes": [ 30 | { 31 | "name": "CollectionUsageThresholdExceeded", 32 | "resultAlias": "collection-usage-threshold-exceeded" 33 | }, 34 | { 35 | "name": "CollectionUsageThresholdSupported", 36 | "resultAlias": "collection-usage-threshold-supported" 37 | }, 38 | "CollectionUsageThresholdCount" 39 | ] 40 | }, 41 | { 42 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 43 | "resultAlias": "test-attribute-with-key", 44 | "attribute": { 45 | "name": "PeakUsage", 46 | "key": "committed" 47 | } 48 | }, 49 | { 50 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 51 | "resultAlias": "test-attribute-with-keys", 52 | "attribute": { 53 | "name": "PeakUsage", 54 | "keys": ["committed", "init", "max", "used"] 55 | } 56 | }, 57 | { 58 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 59 | "resultAlias": "test-with-outputwriter", 60 | "attribute": "CollectionUsageThresholdCount", 61 | "outputWriters": [ 62 | { 63 | "@class": "org.jmxtrans.embedded.output.NoOpWriter" 64 | } 65 | ] 66 | } 67 | ] 68 | } -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/jmxtrans-config-merge-2-test.json: -------------------------------------------------------------------------------- 1 | { 2 | "outputWriters": [ 3 | { 4 | "@class": "org.jmxtrans.embedded.output.ConsoleWriter" 5 | }, 6 | { 7 | "@class": "org.jmxtrans.embedded.output.GraphiteWriter", 8 | "settings": { 9 | "host": "${graphite.host:localhost}", 10 | "port": "${graphite.port:2003}", 11 | "namePrefix": "servers.${server.name:localhost}." 12 | } 13 | } 14 | ], 15 | "queryIntervalInSeconds": 5, 16 | "numQueryThreads": 3, 17 | "exportIntervalInSeconds": 10, 18 | "numExportThreads": 2, 19 | "exportBatchSize": 30 20 | } -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/jmxtrans-config-resultnamestrategy.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "org:name=org.jmxtrans.embedded.replace", 5 | "attribute": "Value", 6 | "resultAlias": "%name%", 7 | "resultNameStrategy": { 8 | "replaceDotsInObjectName": true 9 | } 10 | 11 | }, 12 | { 13 | "objectName": "org:name=org.jmxtrans.embedded.noreplace", 14 | "resultAlias": "%name%", 15 | "attribute": "Value", 16 | "resultNameStrategy": { 17 | "replaceDotsInObjectName": false 18 | } 19 | }, 20 | { 21 | "objectName": "org:name=org.jmxtrans.embedded.default", 22 | "resultAlias": "%name%", 23 | "attribute": "Value" 24 | } 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/jmxtrans-config-test.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 5 | "attribute": "CollectionUsageThreshold" 6 | 7 | }, 8 | { 9 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 10 | "resultAlias": "test-aliased-query", 11 | "attribute": "CollectionUsageThresholdCount" 12 | }, 13 | { 14 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 15 | "resultAlias": "test-attribute-with-alias", 16 | "attribute": { 17 | "name": "CollectionUsageThresholdCount", 18 | "resultAlias": "test-alias" 19 | } 20 | }, 21 | { 22 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 23 | "resultAlias": "test-attributes", 24 | "attributes": ["CollectionUsageThresholdExceeded", "CollectionUsageThresholdSupported"] 25 | }, 26 | { 27 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 28 | "resultAlias": "test-attributes-with-alias", 29 | "attributes": [ 30 | { 31 | "name": "CollectionUsageThresholdExceeded", 32 | "resultAlias": "collection-usage-threshold-exceeded" 33 | }, 34 | { 35 | "name": "CollectionUsageThresholdSupported", 36 | "resultAlias": "collection-usage-threshold-supported" 37 | }, 38 | "CollectionUsageThresholdCount" 39 | ] 40 | }, 41 | { 42 | "objectName": "java.lang:type=OperatingSystem", 43 | "resultAlias": "test-gauge-and-counter-attributes", 44 | "attributes": [ 45 | { 46 | "name": "SystemLoadAverage", 47 | "type":"gauge" 48 | }, 49 | { 50 | "name": "ProcessCpuTime", 51 | "type":"counter" 52 | } 53 | ] 54 | }, 55 | { 56 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 57 | "resultAlias": "test-attribute-with-key", 58 | "attribute": { 59 | "name": "PeakUsage", 60 | "key": "committed" 61 | } 62 | }, 63 | { 64 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 65 | "resultAlias": "test-attribute-with-keys", 66 | "attribute": { 67 | "name": "PeakUsage", 68 | "keys": ["committed", "init", "max", "used"] 69 | } 70 | }, 71 | { 72 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 73 | "resultAlias": "test-with-outputwriter", 74 | "attribute": "CollectionUsageThresholdCount", 75 | "outputWriters": [ 76 | { 77 | "@class": "org.jmxtrans.embedded.output.NoOpWriter" 78 | } 79 | ] 80 | }, 81 | { 82 | "objectName": "java.lang:type=MemoryPool,name=*", 83 | "resultAlias": "test-with-capacity.%name%", 84 | "attribute": "CollectionUsageThresholdCount", 85 | "capacity": 500 86 | } 87 | ], 88 | "outputWriters": [ 89 | { 90 | "@class": "org.jmxtrans.embedded.output.ConsoleWriter" 91 | }, 92 | { 93 | "@class": "org.jmxtrans.embedded.output.TestWriter1", 94 | "settings": { 95 | "enabled": false 96 | } 97 | }, 98 | { 99 | "@class": "org.jmxtrans.embedded.output.TestWriter2", 100 | "settings": { 101 | "enabled": true 102 | } 103 | }, 104 | { 105 | "@class": "org.jmxtrans.embedded.output.TestWriter3", 106 | "settings": { 107 | "enabled": "${jmxtrans.writer.enabled:false}" 108 | } 109 | }, 110 | { 111 | "@class": "org.jmxtrans.embedded.output.TestWriter4", 112 | "settings": { 113 | "enabled": "${jmxtrans.writer.enabled:true}" 114 | } 115 | }, 116 | { 117 | "@class": "org.jmxtrans.embedded.output.GraphiteWriter", 118 | "settings": { 119 | "host": "${graphite.host:localhost}", 120 | "port": "${graphite.port:2003}", 121 | "namePrefix": "servers.${server.name:localhost}." 122 | } 123 | } 124 | ], 125 | "queryIntervalInSeconds": 5, 126 | "numQueryThreads": 3, 127 | "exportIntervalInSeconds": 10, 128 | "numExportThreads": 2, 129 | "exportBatchSize": 30 130 | } 131 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/jmxtrans-factory-test.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 5 | "attribute": "CollectionUsageThreshold" 6 | 7 | }, 8 | { 9 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 10 | "resultAlias": "test-aliased-query", 11 | "attribute": "CollectionUsageThresholdCount" 12 | }, 13 | { 14 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 15 | "resultAlias": "test-attribute-with-alias", 16 | "attribute": { 17 | "name": "CollectionUsageThresholdCount", 18 | "resultAlias": "test-alias" 19 | } 20 | }, 21 | { 22 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 23 | "resultAlias": "test-attributes", 24 | "attributes": ["CollectionUsageThresholdExceeded", "CollectionUsageThresholdSupported"] 25 | }, 26 | { 27 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 28 | "resultAlias": "test-attributes-with-alias", 29 | "attributes": [ 30 | { 31 | "name": "CollectionUsageThresholdExceeded", 32 | "resultAlias": "collection-usage-threshold-exceeded" 33 | }, 34 | { 35 | "name": "CollectionUsageThresholdSupported", 36 | "resultAlias": "collection-usage-threshold-supported" 37 | }, 38 | "CollectionUsageThresholdCount" 39 | ] 40 | }, 41 | { 42 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 43 | "resultAlias": "test-attribute-with-key", 44 | "attribute": { 45 | "name": "PeakUsage", 46 | "key": "committed" 47 | } 48 | }, 49 | { 50 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 51 | "resultAlias": "test-attribute-with-keys", 52 | "attribute": { 53 | "name": "PeakUsage", 54 | "keys": ["committed", "init", "max", "used"] 55 | } 56 | }, 57 | { 58 | "objectName": "java.lang:type=MemoryPool,name=PS Eden Space", 59 | "resultAlias": "test-with-outputwriter", 60 | "attribute": "CollectionUsageThresholdCount", 61 | "outputWriters": [ 62 | { 63 | "@class": "org.jmxtrans.embedded.output.NoOpWriter" 64 | } 65 | ] 66 | } 67 | ], 68 | "outputWriters": [ 69 | { 70 | "@class": "org.jmxtrans.embedded.output.NoOpWriter" 71 | } 72 | ], 73 | "queryIntervalInSeconds": 5, 74 | "numQueryThreads": 3, 75 | "exportIntervalInSeconds": 10, 76 | "numExportThreads": 2, 77 | "exportBatchSize": 30 78 | } -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/jmxtrans-integ-test.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "java.lang:type=GarbageCollector,name=PS MarkSweep", 5 | "resultAlias": "jvm.gc.ps-marksweep", 6 | "attributes": ["CollectionCount", "CollectionTime"] 7 | }, 8 | { 9 | "objectName": "java.lang:type=GarbageCollector,name=PS Scavenge", 10 | "resultAlias": "jvm.gc.ps-scavenge", 11 | "attributes": ["CollectionCount", "CollectionTime"] 12 | }, 13 | { 14 | "objectName": "java.lang:type=Memory", 15 | "resultAlias": "jvm.memory", 16 | "attributes": [ 17 | { 18 | "name": "HeapMemoryUsage", 19 | "keys": ["committed", "init", "max", "used"] 20 | }, 21 | { 22 | "name": "NonHeapMemoryUsage", 23 | "keys": ["committed", "init", "max", "used"] 24 | } 25 | ] 26 | }, 27 | { 28 | "objectName": "java.lang:type=Threading", 29 | "resultAlias": "jvm.thread", 30 | "attributes": ["ThreadCount", "TotalStartedThreadCount", "DaemonThreadCount"] 31 | }, 32 | { 33 | "objectName": "java.lang:type=Runtime", 34 | "resultAlias": "jvm.runtime", 35 | "attribute": "Uptime" 36 | }, 37 | { 38 | "objectName": "java.lang:type=OperatingSystem", 39 | "resultAlias": "jvm.operatingsystem", 40 | "attributes": ["SystemLoadAverage", "ProcessCpuTime", "FreePhysicalMemorySize", "FreeSwapSpaceSize"] 41 | } 42 | ], 43 | "outputWriters": [ 44 | { 45 | "@class": "org.jmxtrans.embedded.output.ConsoleWriter" 46 | }, 47 | { 48 | "@class": "org.jmxtrans.embedded.output.GraphiteWriter", 49 | "settings": { 50 | "host": "${graphite.host:localhost}", 51 | "port": "${graphite.port:2003}", 52 | "namePrefix": "servers.${server.name:localhost}.", 53 | "enabled": false 54 | } 55 | } 56 | ], 57 | "queryIntervalInSeconds": 1, 58 | "numQueryThreads": 1, 59 | "exportIntervalInSeconds": 20 60 | } -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/output/statsd-writer.json: -------------------------------------------------------------------------------- 1 | { 2 | "outputWriters": [ 3 | { 4 | "@class": "org.jmxtrans.embedded.output.ConsoleWriter" 5 | }, 6 | { 7 | "@class": "org.jmxtrans.embedded.output.StatsDWriter", 8 | "settings": { 9 | "host": "${statsd.host:localhost}", 10 | "port": "${statsd.port:8125}" 11 | } 12 | } 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/spring/test-spring-configuration-1.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 30 | 31 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/spring/test-spring-configuration-2.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 30 | 31 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/spring/test-spring-configuration-3.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 30 | 31 | 32 | classpath:org/jmxtrans/embedded/config/jmxtrans-internals.json 33 | classpath:org/jmxtrans/embedded/config/jvm-sun-hotspot.json 34 | classpath:org/jmxtrans/embedded/config/tomcat-6.json 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/util/json/jmxtrans-placeholder-test.json: -------------------------------------------------------------------------------- 1 | { 2 | "queries": [ 3 | { 4 | "objectName": "java.lang:type=GarbageCollector,name=PS MarkSweep", 5 | "resultAlias": "jvm.gc.ps-marksweep", 6 | "attributes": ["CollectionCount", "CollectionTime"] 7 | }, 8 | { 9 | "objectName": "java.lang:type=GarbageCollector,name=PS Scavenge", 10 | "resultAlias": "jvm.gc.ps-scavenge", 11 | "attributes": ["CollectionCount", "CollectionTime"] 12 | }, 13 | { 14 | "objectName": "java.lang:type=Memory", 15 | "resultAlias": "jvm.memory", 16 | "attributes": [ 17 | { 18 | "name": "HeapMemoryUsage", 19 | "keys": ["committed", "init", "max", "used"] 20 | }, 21 | { 22 | "name": "NonHeapMemoryUsage", 23 | "keys": ["committed", "init", "max", "used"] 24 | } 25 | ] 26 | }, 27 | { 28 | "objectName": "java.lang:type=Threading", 29 | "resultAlias": "jvm.thread", 30 | "attributes": ["ThreadCount", "TotalStartedThreadCount", "DaemonThreadCount"] 31 | }, 32 | { 33 | "objectName": "java.lang:type=Runtime", 34 | "resultAlias": "jvm.runtime", 35 | "attribute": "Uptime" 36 | }, 37 | { 38 | "objectName": "java.lang:type=OperatingSystem", 39 | "resultAlias": "jvm.operatingsystem", 40 | "attributes": ["SystemLoadAverage", "ProcessCpuTime", "FreePhysicalMemorySize", "FreeSwapSpaceSize"] 41 | } 42 | ], 43 | "outputWriters": [ 44 | { 45 | "@class": "org.jmxtrans.embedded.output.NoOpWriter" 46 | }, 47 | { 48 | "@class": "org.jmxtrans.embedded.output.GraphiteWriter", 49 | "settings": { 50 | "host": "${graphite.host}", 51 | "port": "${graphite.port:2003}", 52 | "namePrefix": "servers.${server.name:localhost}." 53 | } 54 | } 55 | ], 56 | "queryIntervalInSeconds": 1, 57 | "numQueryThreads": 1, 58 | "exportIntervalInSeconds": 20 59 | } -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/util/net/ssl/README.txt: -------------------------------------------------------------------------------- 1 | 2 | keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 3600 -keysize 2048 3 | 4 | keytool -genkeypair -keystore keystore.jks -validity 3600 -dname "CN=jmxtrans.org, OU=jmxtrans, O=Unknown, L=Unknown, ST=Unknown, C=Unknown" -keypass password -storepass password -keyalg RSA -alias selfsigned -ext SAN=dns:www.jmxtrans.org,ip:127.0.0.1 5 | 6 | keytool -export -keystore keystore.jks -alias selfsigned -file selfsigned.cer 7 | 8 | keytool -import -file selfsigned.cer -alias selfsigned -keystore truststore.jks 9 | -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/util/net/ssl/cacerts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmxtrans/embedded-jmxtrans/3b5761341ba892bee4aad13c376b732e5f912d2f/src/test/resources/org/jmxtrans/embedded/util/net/ssl/cacerts -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/util/net/ssl/keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmxtrans/embedded-jmxtrans/3b5761341ba892bee4aad13c376b732e5f912d2f/src/test/resources/org/jmxtrans/embedded/util/net/ssl/keystore.jks -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/util/net/ssl/selfsigned.cer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmxtrans/embedded-jmxtrans/3b5761341ba892bee4aad13c376b732e5f912d2f/src/test/resources/org/jmxtrans/embedded/util/net/ssl/selfsigned.cer -------------------------------------------------------------------------------- /src/test/resources/org/jmxtrans/embedded/util/net/ssl/truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmxtrans/embedded-jmxtrans/3b5761341ba892bee4aad13c376b732e5f912d2f/src/test/resources/org/jmxtrans/embedded/util/net/ssl/truststore.jks -------------------------------------------------------------------------------- /src/test/statsd/README.md: -------------------------------------------------------------------------------- 1 | Script `start.sh` to start a testing [statsd](https://github.com/etsy/statsd) server outputting metrics to the console. 2 | 3 | The environment variable `STATSD_HOME` must be defined. 4 | 5 | A file`.envrc` can be used to source the `STATSD_HOME` variable. 6 | -------------------------------------------------------------------------------- /src/test/statsd/start.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # set -x 4 | set -e 5 | 6 | # resolve links - $0 may be a softlink 7 | PRG="$0" 8 | 9 | while [ -h "$PRG" ]; do 10 | ls=`ls -ld "$PRG"` 11 | link=`expr "$ls" : '.*-> \(.*\)$'` 12 | if expr "$link" : '/.*' > /dev/null; then 13 | PRG="$link" 14 | else 15 | PRG=`dirname "$PRG"`/"$link" 16 | fi 17 | done 18 | 19 | # Get standard environment variables 20 | PRGDIR=`dirname "$PRG"` 21 | pushd $PRGDIR 22 | PRGDIR=`pwd` 23 | popd 24 | 25 | 26 | if [ -r "$PRGDIR/.envrc" ]; then 27 | echo "Source file $PRGDIR/.envrc" 28 | . "$PRGDIR/.envrc" 29 | else 30 | echo "No file $PRGDIR/.envrc found to source environment variables (e.g. STATSD_HOME)" 31 | fi 32 | 33 | if [ -z "$STATSD_HOME" ]; then 34 | echo "Unable to start as STATSD_HOME is not defined" 35 | exit 1 36 | fi 37 | 38 | 39 | CONFIG_FILE="$PRGDIR/statsd-config-console.js" 40 | 41 | echo "START STATSD ..." 42 | echo "STATSD_HOME: $STATSD_HOME" 43 | echo "Statsd configuration file: $CONFIG_FILE" 44 | 45 | pushd $STATSD_HOME 46 | node stats.js $CONFIG_FILE 47 | -------------------------------------------------------------------------------- /src/test/statsd/statsd-config-console.js: -------------------------------------------------------------------------------- 1 | { 2 | graphitePort: 2003, 3 | graphiteHost: "graphite.example.com", 4 | port: 8125, 5 | debug : true, 6 | backends: ["./backends/console"] 7 | } --------------------------------------------------------------------------------