diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6eff375 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +target/ +tmp/ +.idea/ +*.iml +*.ipr +*.iws \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8dada3e --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/NOTICE.md b/NOTICE.md new file mode 100644 index 0000000..4d5864b --- /dev/null +++ b/NOTICE.md @@ -0,0 +1,184 @@ +Dropwizard InfluxDB Reporter + +Copyright (C) 2017 Kickstarter, PBC. + +Dropwizard InfluxDB Reporter uses several third-party OSS libraries listed in pom.xml. + +# Dropwizard Metrics + +Copyright 2010-2013 Coda Hale and Yammer, Inc. + +This product includes software developed by Coda Hale and Yammer, Inc. + +This product includes code derived from the JSR-166 project (ThreadLocalRandom, Striped64, +LongAdder), which was released with the following comments: + + Written by Doug Lea with assistance from members of JCP JSR-166 + Expert Group and released to the public domain, as explained at + http://creativecommons.org/publicdomain/zero/1.0/ + +# Dropwizard + +Copyright 2010-2013 Coda Hale and Yammer, Inc., 2014-2016 Dropwizard Team + +This product includes software developed by Coda Hale and Yammer, Inc. + +# Autovalue + +Copyright 2013 Google, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +# Jackson Modules + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +# JUnit + +A simple framework to write repeatable tests. + +http://junit.org/junit4/ + +The JUnit depends on Java Hamcrest (http://hamcrest.org/JavaHamcrest/). + +Eclipse Public License - v 1.0 +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and + +b) in the case of each subsequent Contributor: + +i) changes to the Program, and + +ii) additions to the Program; + +where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. + +"Program" means the Contributions distributed in accordance with this Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, including all Contributors. + +2. GRANT OF RIGHTS + +a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. + +b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. + +c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. + +d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: + +a) it complies with the terms and conditions of this Agreement; and + +b) its license agreement: + +i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; + +ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; + +iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and + +iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + +a) it must be made available under this Agreement; and + +b) a copy of this Agreement must be included with each copy of the Program. + +Contributors may not remove or alter any copyright notices contained within the Program. + +Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. + +For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. + +# Mockito + +Mocking framework for Java. + +https://github.com/mockito/mockito + +The MIT License + +Copyright (c) 2007 Mockito contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +# Maven Plugins + +Copyright 2002-2009 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/README.md b/README.md new file mode 100644 index 0000000..cda29be --- /dev/null +++ b/README.md @@ -0,0 +1,267 @@ +# Dropwizard InfluxDB Reporter + +[![CircleCI](https://circleci.com/gh/kickstarter/dropwizard-influxdb-reporter.svg?style=svg)](https://circleci.com/gh/kickstarter/dropwizard-influxdb-reporter) + +Sane Dropwizard metrics instrumentation for InfluxDB 1.2+. + +```ruby +> show series +key +--- +client_connections,client=influxdb-http-writer +connections,port=8080 +connections,port=8081 +http_server +http_server,metric=1xx-responses +http_server,metric=2xx-responses +http_server,metric=3xx-responses +http_server,metric=4xx-responses +http_server,metric=5xx-responses +http_server,metric=async-dispatches +http_server,metric=async-timeouts +http_server,metric=connect-requests +http_server,metric=delete-requests +http_server,metric=dispatches +http_server,metric=get-requests +http_server,metric=head-requests +http_server,metric=move-requests +http_server,metric=options-requests +http_server,metric=other-requests +http_server,metric=post-requests +http_server,metric=put-requests +http_server,metric=requests +http_server,metric=trace-requests +jvm +jvm_buffers +jvm_buffers,type=direct +jvm_buffers,type=mapped +jvm_classloader +jvm_gc +jvm_memory,metric=heap +jvm_memory,metric=non-heap +jvm_memory,metric=pools.Code-Cache +jvm_memory,metric=pools.Compressed-Class-Space +jvm_memory,metric=pools.Metaspace +jvm_memory,metric=pools.PS-Eden-Space +jvm_memory,metric=pools.PS-Old-Gen +jvm_memory,metric=pools.PS-Survivor-Space +jvm_memory,metric=total +jvm_threads +logging,level=all +logging,level=debug +logging,level=error +logging,level=info +logging,level=trace +logging,level=warn +resources,method=get,resource=AdminResource +thread_pools,pool=dw +``` + + +## Installation + +### Maven + +Add the following dependency to your `pom.xml`: + +```xml + + com.kickstarter.dropwizard + dropwizard-influxdb-reporter + ${dropwizard-influxdb-reporter.version} + +``` + +### Gradle + +Add the following dependency to your gradle file: + +```groovy +repositories { + mavenCentral() +} + +dependencies { + compile 'com.kickstarter.dropwizard:dropwizard-influxdb-reporter${dropwizardInfluxdbReporterVersion}' +} +``` + +## Configuration + +To begin scheduling metric reports at a regular interval, add a reporter to your Dropwizard config. A barebones reporter will look like this: + +```yml +metrics: + reporters: + - type: influxdb + sender: + type: http + database: mydb +``` + +This will send metrics to an InfluxDB instance on localhost:8086 over HTTP. + +A more complex sender may look like this: + +```yml + - type: influxdb + globalTags: + env: production + metricTemplates: + services: + pattern: com\.kickstarter\.services\.(?[A-Za-z]+).* + tagKeys: ["service"] + groupGauges: true + groupCounters: false + sender: + type: tcp + host: localhost + port: 90210 + timeout: 5000000 days # keep alive as long as possible +``` + +## Features + +#### Global Tagging + +Add tags to everything that gets reported. + +```yml +globalTags: + env: production + service: recommendations +``` + +#### Metric grouping for Gauges and Counters + +Group gauges and counters together under a single measurement. Enabled by default. + +```java +<"org.eclipse.jetty.util.thread.QueuedThreadPool.dw.jobs" value=0> +<"org.eclipse.jetty.util.thread.QueuedThreadPool.dw.size" value=4> +<"org.eclipse.jetty.util.thread.QueuedThreadPool.dw.utilization" value=0.455> +<"org.eclipse.jetty.util.thread.QueuedThreadPool.dw.utilization-max" value=0.0068301848> + +// => Output +name: org.eclipse.jetty.util.thread.QueuedThreadPool.dw +------------------------------------------------------- +time jobs size utilization utilization-max +2017-06-10T10:18:00Z 0 4 0.455 0.0068301848 +``` + +```yml +groupGauges: true +groupCounters: true +``` + +#### Regex-based Templating + +Use custom templating to convert dropwizard-style metric names into reasonable measurement names and tag sets. By default, the reporter comes with its own templates. Thread pool metrics, for example, are renamed under `thread_pools`: + +``` +name: thread_pools +------------------------------------------------------- +time jobs size utilization utilization-max +2017-06-10T10:18:00Z 0 4 0.455 0.0068301848 +``` + +While timed Dropwizard resource metrics, for example, are grouped and tagged under `resources`: + +``` +resources,resource=InfoResource,method=stats +resources,resource=RecommendedProjectsResource,method=get +resources,resource=BatchRecommendedProjectsResource,method=get +``` + +```yml +metricTemplates: + services: + pattern: com\.kickstarter\.services\.(?[A-Za-z]+).* + tagKeys: ["service"] +```` + +#### Per-metric measurement/field naming and tags + +The reporter is able to deserialize custom InfluxDb-style measurements passed to it via Dropwizard's instrumentation layer. This allows you to fully customize the InfluxDB output of a particular metric through Dropwizard. + +```java +final Timer restoreTimer = metricRegistry.timer( + influxName("Recommendations", ImmutableMap.of( + "action", "restore", + "model", "fun-model" + )) +); + +restoreTimer.time(...) + +// => Output +name: Recommendations,action=restore,model=fun-model +------------------------------------------------------- +50-percentile 75-percentile 95-percentile 99-percentile ... +``` + +#### HTTP/TCP Senders + +##### HTTP Sender + +The HTTP sender transmits InfluxDB lines directly to the database, and must provide a database for usage. It uses a Jersey client to send metrics to InfluxDB, giving us some request-level timing measurements. + +screen shot 2017-06-10 at 9 27 31 pm + +The jersey client can be customized using all of the options provided by [JerseyClientConfiguration](https://github.com/dropwizard/dropwizard/blob/master/dropwizard-client/src/main/java/io/dropwizard/client/JerseyClientConfiguration.java). + +```yml +metrics: + reporters: + type: influxdb + sender: + type: http + host: localhost + port: 8086 + database: mydb + jersey: + connectionTimeout: 500 milliseconds +``` + +##### TCP Sender + +You may wish to send InfluxDB lines to a collector instance, like Telegraf, instead of using the direct HTTP protocol. You can use the TCP sender to transmit metrics to your collector in InfluxDB line format. + +```yml +metrics: + reporters: + type: influxdb + sender: + type: tcp + host: localhost + port: 8086 + timeout: 500 milliseconds +``` + +##### Exception Handling + +A Sender sends a batch of InfluxDbMeasurements to a receiver at the Dropwizard-configured frequency. If the sender catches an exception while writing to the receiver, the exception is logged and the connection is closed. The sender will reconnect to the receiver when the next batch is scheduled to be sent. + +The measurements that failed to send are stored in a queue and retried in subsequent batches. When things get **real bad™️** and the queue gets backed up, we'll start dropping old metrics — this logic is all handled by Guava's `EvictingQueue`. + +## Contributing + +Have questions or feedback? The best way to submit feedback and report bugs is to open a GitHub issue. We'd love to see you contribute — talk to you soon! + +## License + +``` +Copyright 2017 Kickstarter, PBC. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` diff --git a/bin/checkstyle-6.19-all.jar b/bin/checkstyle-6.19-all.jar new file mode 100644 index 0000000..690bd2d Binary files /dev/null and b/bin/checkstyle-6.19-all.jar differ diff --git a/bin/lint b/bin/lint new file mode 100755 index 0000000..15f14ce --- /dev/null +++ b/bin/lint @@ -0,0 +1,9 @@ +#!/bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +CHECKSTYLE_JAR_PATH="$DIR/checkstyle-6.19-all.jar" +CHECKSTYLE_CONFIG_PATH="$DIR/../checkstyle.xml" + +find src -name "*.java" | \ + grep -v generated-sources | grep -v generated-test-sources | + xargs java -jar $CHECKSTYLE_JAR_PATH -c $CHECKSTYLE_CONFIG_PATH diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 0000000..bbb92ef --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..3160dc1 --- /dev/null +++ b/circle.yml @@ -0,0 +1,22 @@ +machine: + java: + version: oraclejdk8 + +dependencies: + override: + - mvn -DskipTests clean install dependency:resolve-plugins dependency:go-offline + +test: + override: + - bin/lint + - mvn -o surefire:test + - mvn findbugs:check + post: + - mkdir -p $CIRCLE_TEST_REPORTS/junit/ + - find . -type f -regex ".*/target/.*-reports/.*.xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit/ \; + +experimental: + notify: + branches: + only: + - master diff --git a/findbugs/exclude-filters.xml b/findbugs/exclude-filters.xml new file mode 100644 index 0000000..ac72932 --- /dev/null +++ b/findbugs/exclude-filters.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2db8939 --- /dev/null +++ b/pom.xml @@ -0,0 +1,181 @@ + + + 4.0.0 + + com.kickstarter.dropwizards + dropwizard-influxdb-reporter + 1.0.0 + jar + + Dropwizard InfluxDB Reporter + https://github.com/kickstarter/dropwizard-influxdb-reporter + Dropwizard instrumentation using InfluxDB. + + + + Apache License 2.0 + http://www.apache.org/licenses/LICENSE-2.0.html + repo + + + + + https://github.com/kickstarter/dropwizard-influxdb-reporter + scm:git:git://github.com/kickstarter/dropwizard-influxdb-reporter.git + scm:git:git:@github.com:kickstarter/dropwizard-influxdb-reporter.git + HEAD + + + + UTF-8 + UTF-8 + + + + + + io.dropwizard.metrics + metrics-core + 3.1.2 + + + + io.dropwizard + dropwizard-client + 1.0.3 + + + + com.fasterxml.jackson.module + jackson-module-parameter-names + 2.7.6 + + + + + com.google.auto.value + auto-value + 1.2 + provided + + + + + junit + junit + 4.12 + test + + + + org.mockito + mockito-core + 2.0.54-beta + test + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + UTF-8 + + -parameters + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.apache.maven.surefire + surefire-junit4 + 2.19.1 + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.3 + + Max + Low + true + true + findbugs/exclude-filters.xml + + + + com.h3xstream.findsecbugs + findsecbugs-plugin + 1.4.5 + + + + + + + check + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.4 + + + test + generate-sources + + add-source + + + + ${basedir}/target/generated-sources/annotations + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.0 + + + attach-sources + + jar + + + + + + + diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/MetricsUtils.java b/src/main/java/com/kickstarter/dropwizard/metrics/MetricsUtils.java new file mode 100644 index 0000000..675857c --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/MetricsUtils.java @@ -0,0 +1,35 @@ +package com.kickstarter.dropwizard.metrics; + +import com.kickstarter.dropwizard.metrics.influxdb.transformer.DropwizardMeasurement; + +import java.util.Map; +import java.util.Optional; + +public final class MetricsUtils { + private MetricsUtils(){} + + /** + * Generate the name for an InfluxDb metric with the given measurement name and tags. + * + *
{@code
+   *   final Timer timer = registry.timer(influxName("Measurement", ImmutableMap.of("action", "restore")));
+   * }
+ */ + public static String influxName(final String measurement, final Map tags) { + return DropwizardMeasurement.create(measurement, tags, Optional.empty()).toString(); + } + + /** + * Generate the name for an InfluxDb metric with the given measurement name, field name, and tags. + * + *
{@code
+   * registry.gauge(
+   *     influxName("Measurement", "results", ImmutableMap.of("method", "retrieveCats")),
+   *     results::size
+   * );
+   * }
+ */ + public static String influxName(final String measurement, final String field, final Map tags) { + return DropwizardMeasurement.create(measurement, tags, Optional.of(field)).toString(); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurement.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurement.java new file mode 100644 index 0000000..39e7bce --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurement.java @@ -0,0 +1,184 @@ +package com.kickstarter.dropwizard.metrics.influxdb; + +import com.google.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import static java.util.stream.Collectors.toList; + +/** + * An InfluxDB measurement representation. + */ +@AutoValue +public abstract class InfluxDbMeasurement { + public abstract String name(); + public abstract Map tags(); + public abstract Map fields(); + public abstract long timestamp(); + + /** + * Creates a new {@link InfluxDbMeasurement}. + * + * Assumes that these fields have already been escaped; + * by default, any invalid measurement is silently dropped by InfluxDB. + */ + public static InfluxDbMeasurement create(final String name, + final Map tags, + final Map fields, + final long timestamp) { + Preconditions.checkArgument(!name.isEmpty(), "InfluxDbMeasurement must contain a non-empty name"); + Preconditions.checkArgument(!fields.isEmpty(), "InfluxDbMeasurement must contain at least one field"); + return new AutoValue_InfluxDbMeasurement(name, tags, fields, timestamp); + } + + /** + * Returns the measurement in InfluxDB line notation with the provided timestamp precision. + * + *

Does not support sub-millisecond timestamp precision. + */ + public String toLine() { + final StringBuilder sb = new StringBuilder(); + sb.append(name()); + + if (!tags().isEmpty()) { + sb.append(','); + sb.append(joinPairs(tags())); + } + + if (!fields().isEmpty()) { + sb.append(' '); + sb.append(joinPairs(fields())); + } + + sb.append(' '); + sb.append(TimeUnit.NANOSECONDS.convert(timestamp(), TimeUnit.MILLISECONDS)); + return sb.toString(); + } + + @Override + public String toString() { + return toLine(); + } + + /** + * Returns a comma-separated key-value formatting of {@code pairs}. + * + *

e.g. "k1=v1,k2=v2,k3=v3" + */ + @VisibleForTesting static String joinPairs(final Map pairs) { + final List pairStrs = pairs.entrySet() + .parallelStream() + .map(e -> String.join("=", e.getKey(), e.getValue())) + .collect(toList()); + + return String.join(",", pairStrs); + } + + // =================================================================================================================== + // Builder + + /** + * A builder for {@link InfluxDbMeasurement}. + */ + public static class Builder { + private final String name; + private final long timestamp; + private final Map tags = new HashMap<>(); + private final Map fields = new HashMap<>(); + + public Builder(final String name, final long timestamp) { + this.name = name; + this.timestamp = timestamp; + } + + public boolean isValid() { + return !name.isEmpty() && !fields.isEmpty(); + } + + /** + * Adds all key-value pairs to the tags map. + * @param items + * @return + */ + public Builder putTags(final Map items) { + tags.putAll(items); + return this; + } + + /** + * Adds the key-value pair to the tags map. + */ + public Builder putTag(final String key, final String value) { + tags.put(key, value); + return this; + } + + /** + * Adds all key-value pairs to the fields map. + * + * @throws IllegalArgumentException if any value is not a String or primitive. + */ + public Builder putFields(final Map fields) { + fields.forEach(this::putField); + return this; + } + + /** + * Adds all key-value pairs to the fields map. + */ + public Builder tryPutFields(final Map fields, + final Consumer exceptionHandler) { + for(final Map.Entry field: fields.entrySet()) { + try { + putField(field.getKey(), field.getValue()); + } catch (IllegalArgumentException e) { + exceptionHandler.accept(e); + } + } + return this; + } + + /** + * Adds the given key-value pair to the fields map. + * + * @throws IllegalArgumentException if any value is not a String or primitive. + */ + public Builder putField(final String key, final T value) { + if (value == null) { + return this; + } + + if (value instanceof Float) { + final float f = (Float) value; + if (!Float.isNaN(f) && !Float.isInfinite(f)) { + fields.put(key, String.valueOf(f)); + } + } else if (value instanceof Double) { + final double d = (Double) value; + if (!Double.isNaN(d) && !Double.isInfinite(d)) { + fields.put(key, String.valueOf(d)); + } + } else if (value instanceof Integer || value instanceof Long) { + fields.put(key, String.format("%di", ((Number) value).longValue())); + } else if (value instanceof String || value instanceof Boolean) { + fields.put(key, value.toString()); + } else { + throw new IllegalArgumentException( + String.format("InfluxDbMeasurement field '%s' must be String or primitive: %s", key, value) + ); + } + + return this; + } + + public InfluxDbMeasurement build() { + return InfluxDbMeasurement.create(name, tags, fields, timestamp); + } + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporter.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporter.java new file mode 100644 index 0000000..c02ff3a --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporter.java @@ -0,0 +1,77 @@ +package com.kickstarter.dropwizard.metrics.influxdb; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Histogram; +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricFilter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.ScheduledReporter; +import com.codahale.metrics.Timer; +import com.google.common.collect.ImmutableList; +import com.kickstarter.dropwizard.metrics.influxdb.io.Sender; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.DropwizardMeasurement; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.DropwizardTransformer; + +import java.time.Clock; +import java.util.SortedMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * A Dropwizard {@link ScheduledReporter} for reporting measurements in InfluxDB line format. + *

+ *

Supports global tags, tagged templating, counter/gauge grouping, + * and per-metric tagging via {@link DropwizardMeasurement#toString}. + */ +public class InfluxDbMeasurementReporter extends ScheduledReporter { + private final Clock clock; + private final Sender sender; + private final DropwizardTransformer transformer; + + public InfluxDbMeasurementReporter(final Sender sender, + final MetricRegistry registry, + final MetricFilter filter, + final TimeUnit rateUnit, + final TimeUnit durationUnit, + final Clock clock, + final DropwizardTransformer transformer, + final ScheduledExecutorService executor) { + super(registry, "influxdb-measurement-reporter", filter, rateUnit, durationUnit, executor); + this.clock = clock; + this.sender = sender; + this.transformer = transformer; + } + + public InfluxDbMeasurementReporter(final Sender sender, + final MetricRegistry registry, + final MetricFilter filter, + final TimeUnit rateUnit, + final TimeUnit durationUnit, + final Clock clock, + final DropwizardTransformer transformer) { + super(registry, "influxdb-measurement-reporter", filter, rateUnit, durationUnit); + this.clock = clock; + this.sender = sender; + this.transformer = transformer; + } + + @Override + public void report(final SortedMap gauges, + final SortedMap counters, + final SortedMap histograms, + final SortedMap meters, + final SortedMap timers) { + final long timestamp = clock.instant().toEpochMilli(); + + final ImmutableList influxDbMeasurements = ImmutableList.builder() + .addAll(transformer.fromGauges(gauges, timestamp)) + .addAll(transformer.fromCounters(counters, timestamp)) + .addAll(transformer.fromHistograms(histograms, timestamp)) + .addAll(transformer.fromMeters(meters, timestamp)) + .addAll(transformer.fromTimers(timers, timestamp)) + .build(); + + sender.send(influxDbMeasurements); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterFactory.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterFactory.java new file mode 100644 index 0000000..ff18ce1 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterFactory.java @@ -0,0 +1,117 @@ +package com.kickstarter.dropwizard.metrics.influxdb; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.ScheduledReporter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.google.common.annotations.VisibleForTesting; +import com.kickstarter.dropwizard.metrics.influxdb.io.InfluxDbHttpWriter; +import com.kickstarter.dropwizard.metrics.influxdb.io.InfluxDbWriter; +import com.kickstarter.dropwizard.metrics.influxdb.io.Sender; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.DropwizardMeasurementParser; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.DropwizardTransformer; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.TaggedPattern; +import io.dropwizard.metrics.BaseReporterFactory; +import javax.validation.constraints.NotNull; + +import java.time.Clock; +import java.util.HashMap; +import java.util.Map; + +/** + * A factory for {@link InfluxDbMeasurementReporter} instances. + *

+ * Configuration Parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
NameDefaultDescription
globalTagsNonetags for all metrics reported to InfluxDb.
metricTemplatesNonetagged metric templates for converting names passed through MetricRegistry.
groupGaugestrueA boolean to signal whether to group gauges when reporting.
groupCounterstrueA boolean to signal whether to group counters when reporting.
senderhttpThe type and configuration for reporting measurements to a receiver.
+ */ +@JsonTypeName("influxdb") +public class InfluxDbMeasurementReporterFactory extends BaseReporterFactory { + @NotNull + @JsonProperty + private Map globalTags = new HashMap<>(); + @VisibleForTesting Map globalTags() { + return globalTags; + } + + @NotNull + @JsonProperty + private Map metricTemplates = new HashMap<>(); + @VisibleForTesting Map metricTemplates() { + return metricTemplates; + } + + @NotNull + @JsonProperty + private boolean groupGauges = true; + @VisibleForTesting boolean groupGauges() { + return groupGauges; + } + + @NotNull + @JsonProperty + private boolean groupCounters = true; + @VisibleForTesting boolean groupCounters() { + return groupCounters; + } + + @NotNull + @JsonProperty + private InfluxDbWriter.Factory sender = new InfluxDbHttpWriter.Factory(); + @VisibleForTesting InfluxDbWriter.Factory sender() { + return sender; + } + + @Override + public ScheduledReporter build(final MetricRegistry registry) { + final Sender builtSender = new Sender(sender.build(registry)); + final DropwizardTransformer transformer = new DropwizardTransformer( + globalTags, + DropwizardMeasurementParser.withTemplates(metricTemplates), + groupCounters, + groupGauges, + getRateUnit(), + getDurationUnit() + ); + + return new InfluxDbMeasurementReporter( + builtSender, + registry, + getFilter(), + getRateUnit(), + getDurationUnit(), + Clock.systemUTC(), + transformer + ); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbHttpWriter.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbHttpWriter.java new file mode 100644 index 0000000..fbd4e6b --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbHttpWriter.java @@ -0,0 +1,110 @@ +package com.kickstarter.dropwizard.metrics.influxdb.io; + +import com.codahale.metrics.MetricRegistry; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dropwizard.client.JerseyClientConfiguration; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import org.hibernate.validator.constraints.NotBlank; +import org.hibernate.validator.constraints.Range; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; +import java.util.concurrent.Executors; + +/** + * An {@link InfluxDbWriter} that writes to an HTTP/S server using a {@link Client}. + */ +public class InfluxDbHttpWriter implements InfluxDbWriter { + private final Client client; + private final WebTarget influxLines; + + public InfluxDbHttpWriter(final Client client, final String endpoint) { + this.client = client; + this.influxLines = client.target(endpoint); + } + + @Override + public void writeBytes(final byte[] bytes) { + influxLines.request().post(Entity.entity(bytes, MediaType.APPLICATION_OCTET_STREAM_TYPE)); + } + + @Override + public void close() throws IOException { + client.close(); + } + + // =================================================================================================================== + // Builder + + /** + * A factory for {@link InfluxDbHttpWriter}. + *

+ * Configuration Parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
NameDefaultDescription
hostlocalhostthe InfluxDB hostname.
port8086the InfluxDB port.
databasenonethe database to write to.
jerseydefaultthe jersey client configuration.
+ */ + public static class Factory implements InfluxDbWriter.Factory { + @NotBlank + @JsonProperty + private String host = "localhost"; + + @Range(min = 0, max = 49151) + @JsonProperty + private int port = 8086; + + @JsonProperty + private JerseyClientConfiguration jersey = new JerseyClientConfiguration(); + + @NotBlank + @JsonProperty + private String database; + + public InfluxDbWriter build(final MetricRegistry metrics) { + final Client client = new io.dropwizard.client.JerseyClientBuilder(metrics) + .using(jersey) + .using(new ObjectMapper()) + .using(Executors.newSingleThreadExecutor()) + .build("influxdb-http-writer"); + + try { + final String query = "/write?db=" + URLEncoder.encode(database, "UTF-8"); + final URL endpoint = new URL("http", host, port, query); + return new InfluxDbHttpWriter(client, endpoint.toString()); + } catch (MalformedURLException | UnsupportedEncodingException e) { + throw new IllegalArgumentException(e); + } + } + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbTcpWriter.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbTcpWriter.java new file mode 100644 index 0000000..a12e97b --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbTcpWriter.java @@ -0,0 +1,105 @@ +package com.kickstarter.dropwizard.metrics.influxdb.io; + +import com.codahale.metrics.MetricRegistry; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.dropwizard.util.Duration; +import javax.validation.constraints.NotNull; +import org.hibernate.validator.constraints.NotBlank; +import org.hibernate.validator.constraints.Range; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; + +/** + * An {@link InfluxDbWriter} that writes bytes to TCP sockets. + */ +public class InfluxDbTcpWriter implements InfluxDbWriter { + private final String host; + private final int port; + private final Duration timeout; + private Socket tcpSocket; + + public InfluxDbTcpWriter(final String host, final int port, final Duration timeout) { + this.host = host; + this.port = port; + this.timeout = timeout; + } + + @Override + public void writeBytes(final byte[] bytes) throws IOException { + if (tcpSocket == null) { + tcpSocket = new Socket(host, port); + tcpSocket.setSoTimeout((int) timeout.toMilliseconds()); + } + + final OutputStream outputStream = tcpSocket.getOutputStream(); + outputStream.write(bytes); + outputStream.flush(); + } + + @Override + public void close() throws IOException { + if (tcpSocket != null) { + tcpSocket.close(); + tcpSocket = null; + } + } + + // =================================================================================================================== + // Builder + + /** + * A factory for {@link InfluxDbTcpWriter}. + *

+ * Configuration Parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
NameDefaultDescription
hostlocalhostthe consumer hostname.
port8086the consumer port.
timeout500 milliseconds/i>the socket timeout duration.
+ */ + public static class Factory implements InfluxDbWriter.Factory { + @NotBlank + @JsonProperty + private String host = "localhost"; + public String host() { + return host; + } + + @Range(min = 0, max = 49151) + @JsonProperty + private int port = 8086; + public int port() { + return port; + } + + @NotNull + @JsonProperty + private Duration timeout = Duration.milliseconds(500); + public Duration timeout() { + return timeout; + } + + @Override public InfluxDbWriter build(final MetricRegistry __) { + return new InfluxDbTcpWriter(host, port, timeout); + } + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbWriter.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbWriter.java new file mode 100644 index 0000000..e153e29 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbWriter.java @@ -0,0 +1,33 @@ +package com.kickstarter.dropwizard.metrics.influxdb.io; + +import com.codahale.metrics.MetricRegistry; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; + +import java.io.IOException; + +/** + * Writes bytes to an InfluxDB input. + */ +public interface InfluxDbWriter { + /** + * Write the given bytes to the connection. + * + * @throws Exception if an error occurs while writing. + */ + void writeBytes(final byte[] bytes) throws Exception; + /** + * Close the writer connection, if it is open. + * + * @throws IOException if an I/O error occurs when closing the connection. + */ + void close() throws IOException; + + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") + @JsonSubTypes({ + @JsonSubTypes.Type(value = InfluxDbHttpWriter.Factory.class, name = "http"), + @JsonSubTypes.Type(value = InfluxDbTcpWriter.Factory.class, name = "tcp")}) + interface Factory { + InfluxDbWriter build(final MetricRegistry metrics); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/Sender.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/Sender.java new file mode 100644 index 0000000..2de4831 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/io/Sender.java @@ -0,0 +1,81 @@ +package com.kickstarter.dropwizard.metrics.influxdb.io; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.EvictingQueue; +import com.kickstarter.dropwizard.metrics.influxdb.InfluxDbMeasurement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UnsupportedEncodingException; +import java.util.Collection; + +import static java.util.stream.Collectors.toList; + +/** + * Sends measurements to InfluxDB. Uses an {@link EvictingQueue} to store and retry measurements that have + * failed to send, and timestamps measurements at the configured {@code precision}, up to millisecond precision. + */ +public class Sender { + private static final Logger log = LoggerFactory.getLogger(InfluxDbTcpWriter.class); + + public static final int DEFAULT_QUEUE_SIZE = 5000; + private static final String SEPARATOR = "\n"; + + private final InfluxDbWriter writer; + private final EvictingQueue queuedInfluxDbMeasurements; + + public Sender(final InfluxDbWriter writer) { + this(writer, DEFAULT_QUEUE_SIZE); + } + + public Sender(final InfluxDbWriter writer, final int queueSize) { + this.writer = writer; + this.queuedInfluxDbMeasurements = EvictingQueue.create(queueSize); + } + + @VisibleForTesting int queuedMeasures() { + return queuedInfluxDbMeasurements.size(); + } + + /** + * Sends the provided {@link InfluxDbMeasurement measurements} to InfluxDB. + * + * @return true if the measurements were successfully sent. + */ + public boolean send(final Collection influxDbMeasurements) { + queuedInfluxDbMeasurements.addAll(influxDbMeasurements); + + if (queuedInfluxDbMeasurements.isEmpty()) { + return true; + } + + final String measureLines = String.join( + SEPARATOR, + queuedInfluxDbMeasurements.stream() + .map(InfluxDbMeasurement::toLine) + .collect(toList()) + ) + SEPARATOR; + + try { + final byte[] measureBytes = measureLines.getBytes("UTF-8"); + writer.writeBytes(measureBytes); + queuedInfluxDbMeasurements.clear(); + return true; + } catch (final UnsupportedEncodingException e) { + log.warn("failed to send metrics", e); + } catch (final Exception e) { + log.warn("failed to send metrics", e); + try { + writer.close(); + } catch (final Exception e2) { + log.warn("failed to close metrics connection", e2); + } + } + + if (queuedInfluxDbMeasurements.remainingCapacity() == 0) { + log.warn("Queued measurements at capacity"); + } + + return false; + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurement.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurement.java new file mode 100644 index 0000000..63045d3 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurement.java @@ -0,0 +1,123 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import com.google.auto.value.AutoValue; + +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; + +import static java.util.stream.Collectors.toMap; + +/** + * A Dropwizard measurement associated with a {@link MetricRegistry} component + * (e.g. {@link Timer timers}, {@link Counter counters}, and {@link Gauge gauges}). + * + *

Serialized in the following format: + *

{@code + * measurement_name[,tag1=val1,tag2=val2][ field_name] + * } + */ +@AutoValue +public abstract class DropwizardMeasurement { + // Serializer constants. + private static final String FIELD_SEPARATOR = " "; + private static final String TAG_SEPARATOR = ","; + private static final String VALUE_SEPARATOR = "="; + private static final String ESCAPE_CHARS = " |,|="; + private static final String SANITIZER = "-"; + + // Fields + abstract String name(); + abstract Map tags(); + + // Provided field name for gauges; used for gauge grouping. + // This field is only set for non-templated measurements; + // templates will be parsed for field names. + abstract Optional field(); + + /** + * Sanitizes InfluxDb escape characters. + */ + private static String sanitize(final String s) { + return s.replaceAll(ESCAPE_CHARS, SANITIZER); + } + + /** + * Creates a new {@link DropwizardMeasurement} with sanitized values. + */ + public static DropwizardMeasurement create(final String name, final Map tags, final Optional field) { + final Map sanitizedTags = tags.entrySet() + .stream() + .collect(toMap( + e -> DropwizardMeasurement.sanitize(e.getKey()), + e -> DropwizardMeasurement.sanitize(e.getValue()) + )); + + return new AutoValue_DropwizardMeasurement(sanitize(name), sanitizedTags, field.map(DropwizardMeasurement::sanitize)); + } + + /** + * Deserializes a measurement line string in the form: + *

{@code + * measurement_name[,tag1=val1,tag2=val2][ field_name] + * } + */ + public static DropwizardMeasurement fromLine(final String line) { + // split measurement-name/tags and field name + final String[] initialParts = line.split(FIELD_SEPARATOR); + if (initialParts.length > 2) { + throw new IllegalArgumentException("too many spaces in measurement line"); + } + + // parse optional field + final Optional field; + if (initialParts.length == 2) { + field = Optional.of(initialParts[1]); + } else { + field = Optional.empty(); + } + + // split measurement-name and tags + final String[] parts = initialParts[0].split(TAG_SEPARATOR); + final String name = parts[0]; + + // parse tags as a key-val map + final Map tags = Arrays.asList(parts) + .subList(1, parts.length) + .stream() + .map(s -> s.split(VALUE_SEPARATOR)) + .filter(arr -> arr.length == 2) + .collect(toMap(arr -> arr[0], arr -> arr[1])); + + // if the sizes aren't one-to-one, we filtered some tag parts out. + if (tags.size() != parts.length - 1) { + throw new IllegalArgumentException("tags must contain exactly one '=' character"); + } + + return DropwizardMeasurement.create(name, tags, field); + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append(name()); + + tags().forEach((k, v) -> { + builder.append(TAG_SEPARATOR); + builder.append(k); + builder.append(VALUE_SEPARATOR); + builder.append(v); + }); + + if (field().isPresent()) { + builder.append(FIELD_SEPARATOR); + builder.append(field().orElse("")); + } + + return builder.toString(); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementParser.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementParser.java new file mode 100644 index 0000000..ca5cd02 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementParser.java @@ -0,0 +1,94 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * A dropwizard measurement parser that holds metric templates for mapping between + * directory-style dot notation and influx-style tags. If no template matches a + * given metric name, it will attempt to parse it as a {@link DropwizardMeasurement} line. + * + * This transformer caches mappings from metric names to {@link DropwizardMeasurement} + * objects to avoid extraneous regex matching and string parsing. + */ +public class DropwizardMeasurementParser { + /** + * Templates for provided Dropwizard metrics. I'm so sorry for these regexes. + */ + private static final ImmutableMap DROPWIZARD_METRIC_MAPPINGS = ImmutableMap.builder() + .put("health", new TaggedPattern(".*\\.health\\.(?.*)")) + .put("resources", new TaggedPattern(".*\\.resources?\\.(?[A-Za-z]+)\\.(?[A-Za-z]+)", "resource", "method")) + .put("jvm", new TaggedPattern("^jvm$")) + .put("jvm_attribute", new TaggedPattern("jvm\\.attribute.*?")) + .put("jvm_buffers", new TaggedPattern("jvm\\.buffers\\.?(?.*)", "type")) + .put("jvm_classloader", new TaggedPattern("jvm\\.classloader.*")) + .put("jvm_gc", new TaggedPattern("jvm\\.gc\\.?(?.*)", "metric")) + .put("jvm_memory", new TaggedPattern("jvm\\.memory\\.?(?.*)", "metric")) + .put("jvm_threads", new TaggedPattern("jvm\\.threads\\.?(?.*)", "metric")) + .put("logging", new TaggedPattern("ch\\.qos\\.logback\\.core\\.Appender\\.(?.*)", "level")) + .put("raw_sql", new TaggedPattern("org\\.skife\\.jdbi\\.v2\\.DBI\\.raw-sql")) + .put("clients", new TaggedPattern("org\\.apache\\.http\\.client\\.HttpClient\\.(?.*)\\.(?.*)$", "client", "metric")) + .put("client_connections", new TaggedPattern("org\\.apache\\.http\\.conn\\.HttpClientConnectionManager\\.(?.*)", "client")) + .put("connections", new TaggedPattern("org\\.eclipse\\.jetty\\.server\\.HttpConnectionFactory\\.(?[0-9]+).*", "port")) + .put("thread_pools", new TaggedPattern("org\\.eclipse\\.jetty\\.util\\.thread\\.QueuedThreadPool\\.(?.*)", "pool")) + .put("http_server", new TaggedPattern("io\\.dropwizard\\.jetty\\.MutableServletContextHandler\\.?(?.*)", "metric")) + .put("data_sources", new TaggedPattern("io\\.dropwizard\\.db\\.ManagedPooledDataSource\\.(?.*)", "metric")) + .build(); + + private final ImmutableMap metricTemplates; + private final Map cache = new HashMap<>(); + + @VisibleForTesting DropwizardMeasurementParser(final ImmutableMap metricTemplates) { + this.metricTemplates = metricTemplates; + } + + /** + * Returns a new {@link DropwizardMeasurementParser} with default {@link #DROPWIZARD_METRIC_MAPPINGS} + * and the user-provided {@code metricTemplates}. + */ + public static DropwizardMeasurementParser withTemplates(final Map metricTemplates) { + return new DropwizardMeasurementParser( + new ImmutableMap.Builder() + .putAll(metricTemplates) + .putAll(DROPWIZARD_METRIC_MAPPINGS) + .build() + ); + } + + /** + * Returns a {@link DropwizardMeasurement} from a matched template, or parses it as a measurement line. + * + * @throws IllegalArgumentException if the measurement does not match a template and is not in the + * measurement line format. + * + * @see DropwizardMeasurement#fromLine(String) + */ + /*package*/ DropwizardMeasurement parse(final String metricName) { + return cache.computeIfAbsent(metricName, __ -> + templatedMeasurement(metricName) + .orElseGet(() -> DropwizardMeasurement.fromLine(metricName)) + ); + } + + /** + * Searches the configured templates for a match to {@code metricName}. + * + * @return an Optional-wrapped {@link DropwizardMeasurement} with any matched tags, and no field value. + */ + private Optional templatedMeasurement(final String metricName) { + for (final Map.Entry entry : metricTemplates.entrySet()) { + final Optional measurement = entry.getValue().tags(metricName) + .map(tags -> DropwizardMeasurement.create(entry.getKey(), tags, Optional.empty())); + + if (measurement.isPresent()) { + return measurement; + } + } + + return Optional.empty(); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardTransformer.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardTransformer.java new file mode 100644 index 0000000..50ebd72 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardTransformer.java @@ -0,0 +1,344 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Histogram; +import com.codahale.metrics.Meter; +import com.codahale.metrics.Snapshot; +import com.codahale.metrics.Timer; +import com.google.common.annotations.VisibleForTesting; +import com.kickstarter.dropwizard.metrics.influxdb.InfluxDbMeasurement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +import static java.util.stream.Collectors.toList; + +/** + * A transformer from Dropwizard metric objects to tagged and grouped {@link InfluxDbMeasurement}s. + * + *

Supports global tags, tagged templating, counter/gauge grouping, and per-metric tagging. + */ +public class DropwizardTransformer { + private static final Logger log = LoggerFactory.getLogger(DropwizardTransformer.class); + + private final Map baseTags; + private final DropwizardMeasurementParser parser; + private final boolean groupCounters; + private final boolean groupGauges; + + private final long rateFactor; + private final long durationFactor; + + public DropwizardTransformer(final Map baseTags, + final DropwizardMeasurementParser parser, + final boolean groupCounters, + final boolean groupGauges, + final TimeUnit rateUnit, + final TimeUnit durationUnit) { + this.baseTags = baseTags; + this.parser = parser; + this.groupCounters = groupCounters; + this.groupGauges = groupGauges; + this.rateFactor = rateUnit.toSeconds(1); + this.durationFactor = durationUnit.toNanos(1); + } + + @VisibleForTesting double convertDuration(final double duration) { + return duration / durationFactor; + } + + @VisibleForTesting double convertRate(final double rate) { + return rate * rateFactor; + } + + // =================================================================================================================== + // timers + + /** + * Build a List of {@link InfluxDbMeasurement}s from a timer map. + */ + public List fromTimers(final Map timers, final long timestamp) { + return timers.entrySet().stream() + .map(e -> fromTimer(e.getKey(), e.getValue(), timestamp)) + .collect(toList()); + } + + /** + * Build an {@link InfluxDbMeasurement} from a timer. + */ + @VisibleForTesting InfluxDbMeasurement fromTimer(final String metricName, final Timer t, final long timestamp) { + final Snapshot snapshot = t.getSnapshot(); + final DropwizardMeasurement measurement = parser.parse(metricName); + + final Map tags = new HashMap<>(baseTags); + tags.putAll(measurement.tags()); + + return new InfluxDbMeasurement.Builder(measurement.name(), timestamp) + .putTags(tags) + .putField("count", snapshot.size()) + .putField("min", convertDuration(snapshot.getMin())) + .putField("max", convertDuration(snapshot.getMax())) + .putField("mean", convertDuration(snapshot.getMean())) + .putField("std-dev", convertDuration(snapshot.getStdDev())) + .putField("50-percentile", convertDuration(snapshot.getMedian())) + .putField("75-percentile", convertDuration(snapshot.get75thPercentile())) + .putField("95-percentile", convertDuration(snapshot.get95thPercentile())) + .putField("99-percentile", convertDuration(snapshot.get99thPercentile())) + .putField("999-percentile", convertDuration(snapshot.get999thPercentile())) + .putField("one-minute", convertRate(t.getOneMinuteRate())) + .putField("five-minute", convertRate(t.getFiveMinuteRate())) + .putField("fifteen-minute", convertRate(t.getFifteenMinuteRate())) + .putField("mean-minute", convertRate(t.getMeanRate())) + .putField("run-count", t.getCount()) + .build(); + } + + // =================================================================================================================== + // meters + + /** + * Build a List of {@link InfluxDbMeasurement}s from a meter map. + */ + public List fromMeters(final Map meters, final long timestamp) { + return meters.entrySet().stream() + .map(e -> fromMeter(e.getKey(), e.getValue(), timestamp)) + .collect(toList()); + } + + /** + * Build an {@link InfluxDbMeasurement} from a meter. + */ + @VisibleForTesting InfluxDbMeasurement fromMeter(final String metricName, final Meter mt, final long timestamp) { + final DropwizardMeasurement measurement = parser.parse(metricName); + + final Map tags = new HashMap<>(baseTags); + tags.putAll(measurement.tags()); + + return new InfluxDbMeasurement.Builder(measurement.name(), timestamp) + .putTags(tags) + .putField("count", mt.getCount()) + .putField("one-minute", convertRate(mt.getOneMinuteRate())) + .putField("five-minute", convertRate(mt.getFiveMinuteRate())) + .putField("fifteen-minute", convertRate(mt.getFifteenMinuteRate())) + .putField("mean-minute", convertRate(mt.getMeanRate())) + .build(); + } + + // =================================================================================================================== + // histograms + + /** + * Build a List of {@link InfluxDbMeasurement}s from a histogram map. + */ + public List fromHistograms(final Map histograms, final long timestamp) { + return histograms.entrySet().stream() + .map(e -> fromHistogram(e.getKey(), e.getValue(), timestamp)) + .collect(toList()); + } + + /** + * Build an {@link InfluxDbMeasurement} from a histogram. + */ + @VisibleForTesting InfluxDbMeasurement fromHistogram(final String metricName, final Histogram h, final long timestamp) { + final Snapshot snapshot = h.getSnapshot(); + final DropwizardMeasurement measurement = parser.parse(metricName); + + final Map tags = new HashMap<>(baseTags); + tags.putAll(measurement.tags()); + + return new InfluxDbMeasurement.Builder(measurement.name(), timestamp) + .putTags(tags) + .putField("count", snapshot.size()) + .putField("min", snapshot.getMin()) + .putField("max", snapshot.getMax()) + .putField("mean", snapshot.getMean()) + .putField("std-dev", snapshot.getStdDev()) + .putField("50-percentile", snapshot.getMedian()) + .putField("75-percentile", snapshot.get75thPercentile()) + .putField("95-percentile", snapshot.get95thPercentile()) + .putField("99-percentile", snapshot.get99thPercentile()) + .putField("999-percentile", snapshot.get999thPercentile()) + .putField("run-count", h.getCount()) + .build(); + } + + // =================================================================================================================== + // groupable measurements: counters and gauges + + /** + * Build a List of {@link InfluxDbMeasurement}s from a counter map. + */ + public List fromCounters(final Map counters, final long timestamp) { + return fromCounterOrGauge(counters, "count", Counter::getCount, timestamp, groupCounters); + } + + /** + * Build a List of {@link InfluxDbMeasurement}s from a gauge map. + */ + public List fromGauges(final Map gauges, final long timestamp) { + return fromCounterOrGauge(gauges, "value", Gauge::getValue, timestamp, groupGauges); + } + + private List fromCounterOrGauge(final Map items, + final String defaultFieldName, + final Function valueExtractor, + final long timestamp, + final boolean group) { + if (group) { + final Map> groupedItems = groupValues(items, defaultFieldName, valueExtractor); + return groupedItems.entrySet().stream() + .map(entry -> fromValueGroup(entry.getKey(), entry.getValue(), timestamp)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(toList()); + } else { + return items.entrySet().stream() + .map(entry -> fromKeyValue(entry.getKey(), defaultFieldName, valueExtractor.apply(entry.getValue()), timestamp)) + .collect(toList()); + } + } + + /** + * Build an {@link InfluxDbMeasurement} directly from given values. + */ + @VisibleForTesting InfluxDbMeasurement fromKeyValue(final String metricName, + final String defaultFieldName, + final T value, + final long timestamp) { + final DropwizardMeasurement measurement = parser.parse(metricName); + final Map tags = new HashMap<>(baseTags); + tags.putAll(measurement.tags()); + + return new InfluxDbMeasurement.Builder(measurement.name(), timestamp) + .putTags(tags) + .putField(defaultFieldName, value) + .build(); + } + + /** + * Groups {@code items} into a set of measurements marked by the measurement name and tag set. + * + *

This method separates measurement names and fields by the following logic: + + *

For {@link DropwizardMeasurement}-style line notation, we parse measurements and fields + * directly via {@link DropwizardMeasurement#fromLine(String)}. + * + * + *

For Dropwizard-style dot notation, we read the last part as a field name. If there is + * only one part, we use the provided default field name. + *

Metrics that end with `.count` will be parsed with the last two parts as a field name. + * + *

e.g. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Full KeyParsed GroupParsed Field
Measurement,action=restore someFieldMeasurement,action=restoresomeField
Measurement,action=restoreMeasurement,action=restoredefaultFieldName
jvmjvmdefaultFieldName
jvm.threads.deadlockjvm.threadsdeadlock
jvm.threads.deadlock.countjvm.threadsdeadlock.count
+ * + * @return A map from {@link GroupKey GroupKeys} to the group's field map. + */ + @VisibleForTesting Map> groupValues(final Map items, + final String defaultFieldName, + final Function valueExtractor) { + final Map> groupedValues = new HashMap<>(); + + items.forEach((key, item) -> { + final String measurementKey; + final String field; + + if (key.contains(" ") || key.contains(",")) { + // Inlined key with tag or field -- formatted as seen in Measurement.toString(). + measurementKey = key; + field = parser.parse(key).field().orElse(defaultFieldName); + } else { + // Templated key -- formatted in Dropwizard/Graphite-style directory notation. + + // If the key ends in `.count`, we try to include the previous part in the field name. + // This is an odd hack to group Dropwizard jvm.threads gauge measurements. + // e.g. for 'jvm.threads.deadlock.count': key=jvm.threads, field=deadlock.count. + final boolean hasCountPostfix = key.endsWith(".count"); + final String mainKey = hasCountPostfix ? key.substring(0, key.length() - 6) : key; + + // Parse the field name from the last key part. + // e.g. for `jvm.memory.heap`: key=jvm.memory, field=heap. + final int lastDotIndex = mainKey.lastIndexOf("."); + if (lastDotIndex == -1) { + // only one key part; use the default field name. + measurementKey = mainKey; + field = hasCountPostfix ? "count" : defaultFieldName; + } else { + // parse the last part as a field name. + measurementKey = mainKey.substring(0, lastDotIndex); + // use `key` instead of `mainKey` here + // to include `.count` in the field name. + field = key.substring(lastDotIndex + 1); + } + } + + final DropwizardMeasurement measurement = parser.parse(measurementKey); + final GroupKey groupKey = GroupKey.create(measurement.name(), measurement.tags()); + + final Map fields = groupedValues.getOrDefault(groupKey, new HashMap<>()); + fields.put(field, valueExtractor.apply(item)); + groupedValues.put(groupKey, fields); + }); + + return groupedValues; + } + + /** + * Build an {@link InfluxDbMeasurement} from a group key and field map. + */ + @VisibleForTesting Optional fromValueGroup(final GroupKey groupKey, + final Map fields, + final long timestamp) { + final Map tags = new HashMap<>(baseTags); + tags.putAll(groupKey.tags()); + + final InfluxDbMeasurement.Builder builder = + new InfluxDbMeasurement.Builder(groupKey.measurement(), timestamp) + .putTags(tags) + .tryPutFields(fields, e -> log.warn(e.getMessage())); + + if (builder.isValid()) { + log.warn("Measurement has no valid fields: {}", groupKey.measurement()); + return Optional.of(builder.build()); + } + + return Optional.empty(); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/GroupKey.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/GroupKey.java new file mode 100644 index 0000000..91b537b --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/GroupKey.java @@ -0,0 +1,16 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.google.auto.value.AutoValue; +import com.google.common.annotations.VisibleForTesting; + +import java.util.Map; + +@AutoValue +abstract class GroupKey { + abstract String measurement(); + abstract Map tags(); + + @VisibleForTesting static GroupKey create(final String measurement, final Map tags) { + return new AutoValue_GroupKey(measurement, tags); + } +} diff --git a/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java new file mode 100644 index 0000000..6942203 --- /dev/null +++ b/src/main/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPattern.java @@ -0,0 +1,67 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.annotations.VisibleForTesting; +import javax.validation.constraints.NotNull; +import org.hibernate.validator.constraints.NotBlank; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static java.util.stream.Collectors.toMap; + +/** + * A wrapper for {@link Pattern regex patterns}, used for extracting tags. + */ +public class TaggedPattern { + @NotBlank + @JsonProperty + private String pattern; + public String pattern() { + return pattern; + } + + @NotNull + @JsonProperty + private List tagKeys; + public List tagKeys() { + return tagKeys; + } + + // for internal use. + private final Pattern compiledPattern; + + @JsonCreator + public TaggedPattern(final String pattern, final List tagKeys) { + this.pattern = pattern; + this.tagKeys = tagKeys; + this.compiledPattern = Pattern.compile(pattern); + } + + @VisibleForTesting TaggedPattern(final String pattern, final String... tagKeys) { + this(pattern, Arrays.asList(tagKeys)); + } + + /** + * Matches the {@code input} string and returns a map of matched tags. + * The tag map will not contain any empty pattern matcher groups. + * + * @return An Optional-wrapped mapping of tags, or {@link Optional#empty()} + * if the input does not match the pattern. + */ + /*package*/ Optional> tags(final CharSequence input) { + final Matcher matcher = compiledPattern.matcher(input); + if (matcher.matches()) { + return Optional.of(tagKeys.stream() + .filter(tag -> !matcher.group(tag).isEmpty()) + .collect(toMap(Function.identity(), matcher::group))); + } + return Optional.empty(); + } +} diff --git a/src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory b/src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory new file mode 100644 index 0000000..a3b7ed7 --- /dev/null +++ b/src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory @@ -0,0 +1 @@ +com.kickstarter.dropwizard.metrics.influxdb.InfluxDbMeasurementReporterFactory \ No newline at end of file diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterFactoryTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterFactoryTest.java new file mode 100644 index 0000000..15da434 --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterFactoryTest.java @@ -0,0 +1,71 @@ +package com.kickstarter.dropwizard.metrics.influxdb; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.kickstarter.dropwizard.metrics.influxdb.io.InfluxDbTcpWriter; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.TaggedPattern; +import io.dropwizard.jackson.Jackson; +import io.dropwizard.setup.Environment; +import io.dropwizard.util.Duration; +import org.junit.Test; + +import java.io.IOException; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class InfluxDbMeasurementReporterFactoryTest { + @Test + public void testSerialization() throws IOException { + final String json = + "{" + + "\"type\": \"influxdb\"," + + "\"globalTags\": {\"some\": \"tag\"}," + + "\"metricTemplates\": {" + + "\"measurement\": {" + + "\"pattern\": \".*blah\"," + + "\"tagKeys\": [\"blah\"]" + + "}" + + "}," + + "\"groupGauges\": true," + + "\"groupCounters\": true," + + "\"sender\": {" + + "\"type\": \"tcp\"," + + "\"host\": \"i am a host\"," + + "\"port\": \"12345\"," + + "\"timeout\": \"5 minutes\"" + + "}" + + "}"; + + final ObjectMapper mapper = Jackson.newObjectMapper(); + mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); + + final Environment env = mock(Environment.class); + when(env.getObjectMapper()).thenReturn(mapper); + + final InfluxDbMeasurementReporterFactory factory = mapper.readValue(json, InfluxDbMeasurementReporterFactory.class); + assertEquals("expected global tags", ImmutableMap.of("some", "tag"), factory.globalTags()); + + final Map templates = factory.metricTemplates(); + assertEquals("expected a single template", 1, templates.size()); + + final TaggedPattern template = templates.get("measurement"); + assertEquals("expected template pattern", ".*blah", template.pattern()); + assertEquals("expected template tag keys", ImmutableList.of("blah"), template.tagKeys()); + + + assertTrue("expected group_guages", factory.groupGauges()); + assertTrue("expected group_counters", factory.groupCounters()); + + final InfluxDbTcpWriter.Factory tcp = (InfluxDbTcpWriter.Factory) factory.sender(); + assertEquals("expected TCP host", "i am a host", tcp.host()); + assertEquals("expected TCP port", 12345, tcp.port()); + assertEquals("expected TCP timeout", Duration.minutes(5), tcp.timeout()); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterTest.java new file mode 100644 index 0000000..e99bc59 --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementReporterTest.java @@ -0,0 +1,86 @@ +package com.kickstarter.dropwizard.metrics.influxdb; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.ExponentiallyDecayingReservoir; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Histogram; +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricFilter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSortedMap; +import com.kickstarter.dropwizard.metrics.influxdb.io.Sender; +import com.kickstarter.dropwizard.metrics.influxdb.transformer.DropwizardTransformer; +import org.junit.Test; + +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; +import java.util.List; +import java.util.SortedMap; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class InfluxDbMeasurementReporterTest { + private final Clock clock = Clock.fixed(Instant.now(), ZoneId.of("UTC")); + + @Test + public void testReport() { + final Sender sender = mock(Sender.class); + final DropwizardTransformer transformer = mock(DropwizardTransformer.class); + + final InfluxDbMeasurementReporter reporter = new InfluxDbMeasurementReporter( + sender, + new MetricRegistry(), + MetricFilter.ALL, + TimeUnit.SECONDS, + TimeUnit.MILLISECONDS, + clock, + transformer + ); + + final long timestamp = clock.instant().toEpochMilli(); + final SortedMap gauges = ImmutableSortedMap.of("some", () -> 5); + final SortedMap counters = ImmutableSortedMap.of("more", new Counter()); + final SortedMap histograms = ImmutableSortedMap.of("metrics", new Histogram(new ExponentiallyDecayingReservoir())); + final SortedMap meters = ImmutableSortedMap.of("for", new Meter()); + final SortedMap timers = ImmutableSortedMap.of("for", new Timer()); + + final List expectedMeasurements = + Stream + .of("gauge", "counter", "histogram", "meter", "timer") + .map(name -> + InfluxDbMeasurement.create( + "some", + ImmutableMap.of(), + ImmutableMap.of(name, "stuff"), + timestamp + ) + ).collect(toList()); + + when(transformer.fromGauges(gauges, timestamp)) + .thenReturn(ImmutableList.of(expectedMeasurements.get(0))); + + when(transformer.fromCounters(counters, timestamp)) + .thenReturn(ImmutableList.of(expectedMeasurements.get(1))); + + when(transformer.fromHistograms(histograms, timestamp)) + .thenReturn(ImmutableList.of(expectedMeasurements.get(2))); + + when(transformer.fromMeters(meters, timestamp)) + .thenReturn(ImmutableList.of(expectedMeasurements.get(3))); + + when(transformer.fromTimers(timers, timestamp)) + .thenReturn(ImmutableList.of(expectedMeasurements.get(4))); + + reporter.report(gauges, counters, histograms, meters, timers); + verify(sender).send(expectedMeasurements); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementTest.java new file mode 100644 index 0000000..7d8049a --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/InfluxDbMeasurementTest.java @@ -0,0 +1,279 @@ +package com.kickstarter.dropwizard.metrics.influxdb; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Map; + +import static java.util.stream.Collectors.toList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class InfluxDbMeasurementTest { + @Test + public void testCreate_EmptyName() { + try { + InfluxDbMeasurement.create("", ImmutableMap.of(), ImmutableMap.of("some", "field"), 123456L); + fail("Expected an Exception to be thrown"); + } catch (final Exception thrown) { + assertEquals("Expected an IllegalArgumentException", IllegalArgumentException.class, thrown.getClass()); + assertEquals("InfluxDbMeasurement must contain a non-empty name", thrown.getMessage()); + } + } + + @Test + public void testCreate_EmptyFields() { + try { + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of(), 123456L); + fail("Expected an Exception to be thrown"); + } catch (final Exception thrown) { + assertEquals("Expected an IllegalArgumentException", IllegalArgumentException.class, thrown.getClass()); + assertEquals("InfluxDbMeasurement must contain at least one field", thrown.getMessage()); + } + } + + @Test + public void testToLine_WithNoTags() { + final InfluxDbMeasurement Measurement = InfluxDbMeasurement.create( + "Measurement", ImmutableMap.of(), ImmutableMap.of("val", "5i"), 1304695L + ); + assertEquals("Measurement val=5i 1304695000000", Measurement.toLine()); + } + + @Test + public void testToLine_WithSingleTag() { + final InfluxDbMeasurement Measurement = InfluxDbMeasurement.create( + "Measurement", ImmutableMap.of("action", "restore"), ImmutableMap.of("val", "5i"), 1304695L + ); + assertEquals("Measurement,action=restore val=5i 1304695000000", Measurement.toLine()); + } + + @Test + public void testToLine_WithMultipleTags() { + final InfluxDbMeasurement Measurement = InfluxDbMeasurement.create( + "Measurement", ImmutableMap.of("action", "restore", "model", "cf-2-005"), ImmutableMap.of("val", "5i"), 1304695L + ); + assertEquals("Measurement,action=restore,model=cf-2-005 val=5i 1304695000000", Measurement.toLine()); + } + + @Test + public void testToLine_WithMultipleFields() { + final InfluxDbMeasurement Measurement = InfluxDbMeasurement.create( + "Measurement", ImmutableMap.of(), ImmutableMap.of("val", "5i", "other-val", "true"), 1304695L + ); + assertEquals("Measurement val=5i,other-val=true 1304695000000", Measurement.toLine()); + } + + @Test + public void testToLine_WithMultipleTagsAndFields() { + final InfluxDbMeasurement Measurement = InfluxDbMeasurement.create( + "Measurement", + ImmutableMap.of("action", "restore", "model", "cf-2-005"), + ImmutableMap.of("val", "5i", "other-val", "true"), + 1304695L + ); + + assertEquals( + "Measurement,action=restore,model=cf-2-005 val=5i,other-val=true 1304695000000", + Measurement.toLine()); + } + + @Test + public void testJoinPairs_WithNoPairs() { + assertTrue("should return an empty string", InfluxDbMeasurement.joinPairs(ImmutableMap.of()).isEmpty()); + } + + @Test + public void testJoinPairs_WithOnePair() { + final String joined = InfluxDbMeasurement.joinPairs(ImmutableMap.of("action", "restore")); + assertEquals("should join a pair without commas", "action=restore", joined); + } + + @Test + public void testJoinPairs_WithMultiplePairs() { + final String joined = InfluxDbMeasurement.joinPairs(ImmutableMap.of("action", "restore", "greetings", "pleasings")); + assertEquals("should join pairs with commas", "action=restore,greetings=pleasings", joined); + } + + @Test + public void testBuilder_PutTags() { + final Map tags = ImmutableMap.of("pleased", "to", "meet", "you"); + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putTags(tags) + .putField("welcome", "manatee") + .build(); + + assertEquals("should add all tags to the measurement", + InfluxDbMeasurement.create("Measurement", tags, ImmutableMap.of("welcome", "manatee"), 90210L), + measurement); + } + + @Test + public void testBuilder_IsInvalidWithEmptyName() { + final InfluxDbMeasurement.Builder measurement = new InfluxDbMeasurement.Builder("", 90210L) + .putField("some-float", 15321F); + + assertFalse("should be invalid with empty name", measurement.isValid()); + } + + @Test + public void testBuilder_IsInvalidWithEmptyFields() { + final InfluxDbMeasurement.Builder measurement = new InfluxDbMeasurement.Builder("hi", 90210L) + .tryPutFields(ImmutableMap.of("some-float", Float.NaN), __ -> {}); + + assertFalse("should be invalid with empty fields", measurement.isValid()); + } + + @Test + public void testBuilder_PutFloatField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-float", 15321F) + .build(); + + assertEquals("should add double field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("some-float", "15321.0"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutNanFloatField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-float", Float.NaN) + .putField("need", "val") + .build(); + + assertEquals("should not add NaN float field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("need", "val"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutInfiniteFloatField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-float", Float.POSITIVE_INFINITY) + .putField("need", "val") + .build(); + + assertEquals("should not add infinite float field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("need", "val"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutDoubleField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-double", 15321D) + .build(); + + assertEquals("should add formatted double field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("some-double", "15321.0"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutNanDoubleField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-double", Double.NaN) + .putField("need", "val") + .build(); + + assertEquals("should not add NaN double field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("need", "val"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutInfiniteDoubleField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-double", Double.POSITIVE_INFINITY) + .putField("need", "val") + .build(); + + assertEquals("should not add infinite double field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("need", "val"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutIntegerField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-int", 15321) + .build(); + + assertEquals("should add integer field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("some-int", "15321i"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutLongField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-long", 15321L) + .build(); + + assertEquals("should add long field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("some-long", "15321i"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutStringField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("some-str", "cookies") + .build(); + + assertEquals("should add String field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("some-str", "cookies"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutBooleanField() { + final InfluxDbMeasurement measurement = new InfluxDbMeasurement.Builder("Measurement", 90210L) + .putField("eating", true) + .build(); + + assertEquals("should add Boolean field to the measurement", + InfluxDbMeasurement.create("Measurement", ImmutableMap.of(), ImmutableMap.of("eating", "true"), 90210L), + measurement); + } + + @Test + public void testBuilder_PutNonStringOrPrimitiveField() { + try { + new InfluxDbMeasurement.Builder("Measurement", 90210L).putField("val", Arrays.asList(1, 2, 3)); + fail("Expected an Exception when adding a non-String or -primitive field"); + } catch (final Exception thrown) { + assertEquals("Expected an IllegalArgumentException", IllegalArgumentException.class, thrown.getClass()); + assertEquals("InfluxDbMeasurement field 'val' must be String or primitive: [1, 2, 3]", thrown.getMessage()); + } + } + + @Test + public void testBuilder_TryPutFields() { + final ArrayList exceptions = new ArrayList<>(); + + new InfluxDbMeasurement.Builder("Measurement", 90210L).tryPutFields(ImmutableMap.of( + "a", ImmutableSet.of(), + "c", "d", + "e", Arrays.asList(1, 2, 3) + ), exceptions::add); + + assertEquals("should catch two exceptions", 2, exceptions.size()); + assertEquals("should catch IllegalArgumentExceptions", IllegalArgumentException.class, exceptions.get(0).getClass()); + assertEquals("should catch IllegalArgumentExceptions", IllegalArgumentException.class, exceptions.get(1).getClass()); + assertEquals("should describe the invalid key-value pair", + ImmutableList.of( + "InfluxDbMeasurement field 'a' must be String or primitive: []", + "InfluxDbMeasurement field 'e' must be String or primitive: [1, 2, 3]" + ), + exceptions.stream().map(Exception::getMessage).collect(toList()) + ); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbTcpWriterTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbTcpWriterTest.java new file mode 100644 index 0000000..f3ff0b4 --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/io/InfluxDbTcpWriterTest.java @@ -0,0 +1,39 @@ +package com.kickstarter.dropwizard.metrics.influxdb.io; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; +import io.dropwizard.jackson.Jackson; +import io.dropwizard.setup.Environment; +import io.dropwizard.util.Duration; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class InfluxDbTcpWriterTest { + @Test + public void testSerialization() throws IOException { + final String json = + "{" + + "\"type\": \"tcp\"," + + "\"host\": \"i am a host\"," + + "\"port\": \"12345\"," + + "\"timeout\": \"5 minutes\"" + + "}"; + + final ObjectMapper mapper = Jackson.newObjectMapper(); + mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); + + final Environment env = mock(Environment.class); + when(env.getObjectMapper()).thenReturn(mapper); + + final InfluxDbTcpWriter.Factory factory = mapper.readValue(json, InfluxDbTcpWriter.Factory.class); + assertEquals("expected TCP host", "i am a host", factory.host()); + assertEquals("expected TCP port", 12345, factory.port()); + assertEquals("expected TCP timeout", Duration.minutes(5), factory.timeout()); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/io/SenderTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/io/SenderTest.java new file mode 100644 index 0000000..f5e1b61 --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/io/SenderTest.java @@ -0,0 +1,73 @@ +package com.kickstarter.dropwizard.metrics.influxdb.io; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.kickstarter.dropwizard.metrics.influxdb.InfluxDbMeasurement; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class SenderTest { + @Test + public void testSend_SendsMeasures() throws Exception { + final InfluxDbWriter writer = mock(InfluxDbWriter.class); + final Sender sender = new Sender(writer); + sender.send(ImmutableList.of( + InfluxDbMeasurement.create("hello", ImmutableMap.of("x", "y"), ImmutableMap.of("a", "b"), 90210L), + InfluxDbMeasurement.create("hello", ImmutableMap.of("x", "y"), ImmutableMap.of("e", "d"), 90210L) + )); + + verify(writer, only()).writeBytes("hello,x=y a=b 90210000000\nhello,x=y e=d 90210000000\n".getBytes()); + assertEquals("should clear measure queue", 0, sender.queuedMeasures()); + } + + @Test + public void testSend_EmptyMeasures() throws Exception { + final InfluxDbWriter writer = mock(InfluxDbWriter.class); + final Sender sender = new Sender(writer); + sender.send(ImmutableList.of()); + + verify(writer, never()).writeBytes(any()); + verify(writer, never()).close(); + } + + @Test + public void testSend_HandlesExceptions() throws Exception { + final InfluxDbWriter writer = mock(InfluxDbWriter.class); + final Sender sender = new Sender(writer); + + doThrow(new RuntimeException("what did you do")).when(writer).writeBytes(any()); + sender.send(ImmutableList.of( + InfluxDbMeasurement.create("hello", ImmutableMap.of("x", "y"), ImmutableMap.of("a", "b"), 90210L)) + ); + + verify(writer, times(1)).writeBytes("hello,x=y a=b 90210000000\n".getBytes()); + verify(writer, times(1)).close(); + } + + @Test + public void testSend_RetriesSend() throws Exception { + final InfluxDbWriter writer = mock(InfluxDbWriter.class); + final Sender sender = new Sender(writer); + + doThrow(new RuntimeException("what did you do")) + .doNothing().when(writer).writeBytes(any()); + + sender.send(ImmutableList.of( + InfluxDbMeasurement.create("hello", ImmutableMap.of("x", "y"), ImmutableMap.of("a", "b"), 90210L)) + ); + + verify(writer, times(1)).writeBytes("hello,x=y a=b 90210000000\n".getBytes()); + verify(writer, times(1)).close(); + + sender.send(ImmutableList.of()); + verify(writer, times(2)).writeBytes("hello,x=y a=b 90210000000\n".getBytes()); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementParserTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementParserTest.java new file mode 100644 index 0000000..77c4fa1 --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementParserTest.java @@ -0,0 +1,38 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.google.common.collect.ImmutableMap; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; + +public class DropwizardMeasurementParserTest { + private static final DropwizardMeasurementParser transformer = new DropwizardMeasurementParser( + ImmutableMap.of("custom_metric", new TaggedPattern("\\.holy\\.(?[A-Za-z]+)\\.cow\\.", "adjective")) + ); + + @Test + public void testMeasurement_FromTemplate() { + final DropwizardMeasurement measurement = transformer.parse(".holy.jumping.cow."); + assertEquals( + DropwizardMeasurement.create("custom_metric", ImmutableMap.of("adjective", "jumping"), Optional.empty()), + measurement + ); + } + + @Test + public void testMeasurement_FromMeasurementLine() { + final DropwizardMeasurement expected = DropwizardMeasurement.create( + "custom_metric", + ImmutableMap.of("adjective", "jumping", "more", "stuff"), + Optional.of("im-a-field") + ); + + final DropwizardMeasurement measurement = transformer.parse( + "custom_metric,adjective=jumping,more=stuff im-a-field" + ); + + assertEquals(expected, measurement); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementTest.java new file mode 100644 index 0000000..240db88 --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardMeasurementTest.java @@ -0,0 +1,218 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.google.common.collect.ImmutableMap; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class DropwizardMeasurementTest { + @Test + public void testCreate_EscapesValues() { + final DropwizardMeasurement expected = DropwizardMeasurement.create( + "Mode-lS-e-rv-e-r", + ImmutableMap.of("k-e-y-", "-v-a-l-u-e-"), + Optional.of("f-i-e--ld") + ); + + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Mode,lS,e=rv e r", + ImmutableMap.of("k,e=y ", " v=a l,u=e "), + Optional.of("f i=e,,ld") + ); + + assertEquals(expected, measurement); + } + + @Test + public void testFromLine_Deserializes() { + final DropwizardMeasurement expected = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of(), + Optional.empty() + ); + assertEquals(expected, DropwizardMeasurement.fromLine("Measurement")); + } + + @Test + public void testFromLine_DeserializesWithTags() { + final DropwizardMeasurement expected = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of("action", "restore", "model", "cf-2-005"), + Optional.empty() + ); + assertEquals(expected, DropwizardMeasurement.fromLine("Measurement,action=restore,model=cf-2-005")); + } + + @Test + public void testFromLine_DeserializesWithField() { + final DropwizardMeasurement expected = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of(), + Optional.of("i-am-a-field") + ); + + assertEquals(expected, DropwizardMeasurement.fromLine("Measurement i-am-a-field")); + } + + @Test + public void testFromLine_DeserializesWithTagsAndField() { + final DropwizardMeasurement expected = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of("action", "restore", "model", "cf-2-005"), + Optional.of("i-am-a-field") + ); + + assertEquals(expected, DropwizardMeasurement.fromLine("Measurement,action=restore,model=cf-2-005 i-am-a-field")); + } + + @Test + public void testFromLine_TooManyFieldSeparators() { + try { + DropwizardMeasurement.fromLine("Measurement,action=restore,model=cf-2-005 one_field too_many_fields"); + fail("Expected an exception to be thrown"); + } catch (final Exception thrown) { + assertEquals( + String.format("Unexpected exception: %s", thrown.getClass()), + IllegalArgumentException.class, + thrown.getClass() + ); + + assertEquals("too many spaces in measurement line", thrown.getMessage()); + } + } + + @Test + public void testFromLine_TooManyValueSeparators() { + try { + DropwizardMeasurement.fromLine("Measurement,action=restore=why,model=cf-2-005"); + fail("Expected an exception to be thrown"); + } catch (final Exception thrown) { + assertEquals( + String.format("Unexpected exception: %s", thrown.getClass()), + IllegalArgumentException.class, + thrown.getClass() + ); + + assertEquals("tags must contain exactly one '=' character", thrown.getMessage()); + } + } + + @Test + public void testFromLine_MissingValueSeparators() { + try { + DropwizardMeasurement.fromLine("Measurement,actionrestore,model=cf-2-005"); + fail("Expected an exception to be thrown"); + } catch (final Exception thrown) { + assertEquals( + String.format("Unexpected exception: %s", thrown.getClass()), + IllegalArgumentException.class, + thrown.getClass() + ); + + assertEquals("tags must contain exactly one '=' character", thrown.getMessage()); + } + } + + @Test + public void testToString_Serializes() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of(), + Optional.empty() + ); + assertEquals("Measurement", measurement.toString()); + } + + @Test + public void testToString_SerializesWithTags() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of("some", "tags", "here", "i-guess"), + Optional.empty() + ); + assertEquals("Measurement,here=i-guess,some=tags", measurement.toString()); + } + + @Test + public void testToString_SerializesWithField() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of(), + Optional.of("wow-a-field") + ); + assertEquals("Measurement wow-a-field", measurement.toString()); + } + + @Test + public void testToString_SerializesWithTagsAndField() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of("some", "tags", "here", "i-guess"), + Optional.of("wow-a-field") + ); + assertEquals("Measurement,here=i-guess,some=tags wow-a-field", measurement.toString()); + } + + @Test + public void testIntegrated_Serialization() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of(), + Optional.empty() + ); + + final String measurementStr = measurement.toString(); + assertEquals("Measurement", measurementStr); + + final DropwizardMeasurement fromLine = DropwizardMeasurement.fromLine(measurementStr); + assertEquals(measurement, fromLine); + } + + @Test + public void testIntegrated_SerializationWithTags() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of("action", "restore", "model", "cf-2-005"), + Optional.empty() + ); + + final String measurementStr = measurement.toString(); + assertEquals("Measurement,action=restore,model=cf-2-005", measurementStr); + + final DropwizardMeasurement fromLine = DropwizardMeasurement.fromLine(measurementStr); + assertEquals(measurement, fromLine); + } + + @Test + public void testIntegrated_SerializationWithField() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of(), + Optional.of("i-like-being-a-field") + ); + + final String measurementStr = measurement.toString(); + assertEquals("Measurement i-like-being-a-field", measurementStr); + + final DropwizardMeasurement fromLine = DropwizardMeasurement.fromLine(measurementStr); + assertEquals(measurement, fromLine); + } + + @Test + public void testIntegrated_SerializationWithTagsAndField() { + final DropwizardMeasurement measurement = DropwizardMeasurement.create( + "Measurement", + ImmutableMap.of("action", "restore", "model", "cf-2-005"), + Optional.of("i-like-being-a-field") + ); + + final String measurementStr = measurement.toString(); + assertEquals("Measurement,action=restore,model=cf-2-005 i-like-being-a-field", measurementStr); + + final DropwizardMeasurement fromLine = DropwizardMeasurement.fromLine(measurementStr); + assertEquals(measurement, fromLine); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardTransformerTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardTransformerTest.java new file mode 100644 index 0000000..d08cdfd --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/DropwizardTransformerTest.java @@ -0,0 +1,503 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.ExponentiallyDecayingReservoir; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Histogram; +import com.codahale.metrics.Meter; +import com.codahale.metrics.Timer; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.kickstarter.dropwizard.metrics.influxdb.InfluxDbMeasurement; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class DropwizardTransformerTest { + @Test + public void testConvertDuration_Milliseconds() { + final DropwizardTransformer transformer = transformerWithUnits(TimeUnit.SECONDS, TimeUnit.MILLISECONDS); + assertEquals("Should convert duration to milliseconds precisely", 2.0E-5, transformer.convertDuration(20), 0.0); + } + + @Test + public void testConvertDuration_Seconds() { + final DropwizardTransformer transformer = transformerWithUnits(TimeUnit.SECONDS, TimeUnit.SECONDS); + assertEquals("Should convert duration to seconds precisely", 2.0E-8, transformer.convertDuration(20), 0.0); + } + + @Test + public void testConvertRate_Seconds() { + final DropwizardTransformer transformer = transformerWithUnits(TimeUnit.SECONDS, TimeUnit.MILLISECONDS); + assertEquals("Should convert rate to seconds precisely", 20.0, transformer.convertRate(20), 0.0); + } + + @Test + public void testConvertRate_Minutes() { + final DropwizardTransformer transformer = transformerWithUnits(TimeUnit.MINUTES, TimeUnit.MILLISECONDS); + assertEquals("Should convert rate to minutes precisely", 1200.0, transformer.convertRate(20), 0.0); + } + + @Test + public void testFromTimer() { + final Set fieldKeys = ImmutableSet.of( + "count", + "min", + "max", + "mean", + "std-dev", + "50-percentile", + "75-percentile", + "95-percentile", + "99-percentile", + "999-percentile", + "one-minute", + "five-minute", + "fifteen-minute", + "mean-minute", + "run-count" + ); + + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + when(parser.parse("some.metric.name")).thenReturn( + DropwizardMeasurement.create("Measurement", MEASUREMENT_TAGS, Optional.empty()) + ); + + final Timer timer = new Timer(); + timer.update(50, TimeUnit.MILLISECONDS); + timer.update(70, TimeUnit.MILLISECONDS); + timer.update(100, TimeUnit.MILLISECONDS); + + final InfluxDbMeasurement measurement = transformer.fromTimer("some.metric.name", timer, 90210L); + assertEquals("should parse name from full metric key", "Measurement", measurement.name()); + assertEquals("should add global and measurement tags", ALL_TAGS, measurement.tags()); + assertEquals("should timestamp measurement", 90210L, measurement.timestamp()); + assertEquals("should add all timer fields", fieldKeys, measurement.fields().keySet()); + } + + @Test + public void testFromMeter() { + final Set fieldKeys = ImmutableSet.of( + "count", + "one-minute", + "five-minute", + "fifteen-minute", + "mean-minute" + ); + + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + when(parser.parse("some.metric.name")).thenReturn( + DropwizardMeasurement.create("Measurement", MEASUREMENT_TAGS, Optional.empty()) + ); + + final Meter meter = new Meter(); + meter.mark(50L); + meter.mark(64L); + meter.mark(80L); + + final InfluxDbMeasurement measurement = transformer.fromMeter("some.metric.name", meter, 90210L); + assertEquals("should parse name from full metric key", "Measurement", measurement.name()); + assertEquals("should add global and measurement tags", ALL_TAGS, measurement.tags()); + assertEquals("should timestamp measurement", 90210L, measurement.timestamp()); + assertEquals("should add all meter fields", fieldKeys, measurement.fields().keySet()); + } + + @Test + public void testFromHistogram() { + final Set fieldKeys = ImmutableSet.of( + "count", + "min", + "max", + "mean", + "std-dev", + "50-percentile", + "75-percentile", + "95-percentile", + "99-percentile", + "999-percentile", + "run-count" + ); + + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + when(parser.parse("some.metric.name")).thenReturn( + DropwizardMeasurement.create("Measurement", MEASUREMENT_TAGS, Optional.empty()) + ); + + final Histogram histogram = new Histogram(new ExponentiallyDecayingReservoir()); + histogram.update(15L); + histogram.update(70L); + histogram.update(100L); + + final InfluxDbMeasurement measurement = transformer.fromHistogram("some.metric.name", histogram, 90210L); + assertEquals("should parse name from full metric key", "Measurement", measurement.name()); + assertEquals("should add global and measurement tags", ALL_TAGS, measurement.tags()); + assertEquals("should timestamp measurement", 90210L, measurement.timestamp()); + assertEquals("should add all histogram fields", fieldKeys, measurement.fields().keySet()); + } + + @Test + public void testFromCounters_Ungrouped() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, false); + + final List counters = ImmutableList.of(new Counter(), new Counter()); + counters.get(0).inc(15L); + counters.get(1).inc(6L); + + final Map cMap = ImmutableMap.of( + "some.stuff.queued", counters.get(0), + "some.stuff.processed", counters.get(1) + ); + + when(parser.parse("some.stuff.queued")).thenReturn( + DropwizardMeasurement.create("some.stuff.queued", MEASUREMENT_TAGS, Optional.empty()) + ); + + when(parser.parse("some.stuff.processed")).thenReturn( + DropwizardMeasurement.create("some.stuff.processed", MEASUREMENT_TAGS, Optional.empty()) + ); + + final List expected = ImmutableList.of( + InfluxDbMeasurement.create("some.stuff.queued", ALL_TAGS, ImmutableMap.of("count", "15i"), 90210L), + InfluxDbMeasurement.create("some.stuff.processed", ALL_TAGS, ImmutableMap.of("count", "6i"), 90210L) + ); + + final List measurements = transformer.fromCounters(cMap, 90210L); + assertEquals("should not group counter measurements", expected, measurements); + } + + @Test + public void testFromCounters_Grouped() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final List counters = ImmutableList.of(new Counter(), new Counter()); + counters.get(0).inc(15L); + counters.get(1).inc(6L); + + final Map cMap = ImmutableMap.of( + "some.stuff.queued", counters.get(0), + "some.stuff.processed", counters.get(1) + ); + + when(parser.parse("some.stuff")).thenReturn( + DropwizardMeasurement.create("some.stuff", MEASUREMENT_TAGS, Optional.empty()) + ); + + final List expected = ImmutableList.of( + InfluxDbMeasurement.create("some.stuff", ALL_TAGS, ImmutableMap.of("queued", "15i", "processed", "6i"), 90210L) + ); + + final List measurements = transformer.fromCounters(cMap, 90210L); + assertEquals("should group counters by tags and prefix", expected, measurements); + } + + @Test + public void testFromGauges_Ungrouped() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, false); + + final Map gauges = ImmutableMap.of( + "some.stuff.queued", () -> 12, + "some.stuff.processed", () -> 15 + ); + + when(parser.parse("some.stuff.queued")).thenReturn( + DropwizardMeasurement.create("some.stuff.queued", MEASUREMENT_TAGS, Optional.empty()) + ); + + when(parser.parse("some.stuff.processed")).thenReturn( + DropwizardMeasurement.create("some.stuff.processed", MEASUREMENT_TAGS, Optional.empty()) + ); + + final List expected = ImmutableList.of( + InfluxDbMeasurement.create("some.stuff.queued", ALL_TAGS, ImmutableMap.of("value", "12i"), 90210L), + InfluxDbMeasurement.create("some.stuff.processed", ALL_TAGS, ImmutableMap.of("value", "15i"), 90210L) + ); + + final List measurements = transformer.fromGauges(gauges, 90210L); + assertEquals("should not group gauge measurements", expected, measurements); + } + + @Test + public void testFromGauges_Grouped() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map gauges = ImmutableMap.of( + "some.stuff.queued", () -> 12, + "some.stuff.processed", () -> 15 + ); + + when(parser.parse("some.stuff")).thenReturn( + DropwizardMeasurement.create("some.stuff", MEASUREMENT_TAGS, Optional.empty()) + ); + + final List expected = ImmutableList.of( + InfluxDbMeasurement.create("some.stuff", ALL_TAGS, ImmutableMap.of("queued", "12i", "processed", "15i"), 90210L) + ); + + final List measurements = transformer.fromGauges(gauges, 90210L); + assertEquals("should group gauges by tags and prefix", expected, measurements); + } + + @Test + public void testFromKeyValue() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, false); + + when(parser.parse("some.stuff.queued")).thenReturn( + DropwizardMeasurement.create("some.stuff.queued", MEASUREMENT_TAGS, Optional.empty()) + ); + + final InfluxDbMeasurement measurement = transformer.fromKeyValue("some.stuff.queued", "some_key", 12L, 90210L); + + assertEquals("should map values directly to measurement", + InfluxDbMeasurement.create("some.stuff.queued", ALL_TAGS, ImmutableMap.of("some_key", "12i"), 90210L), + measurement); + } + + @Test + public void testGroupValues_Inline() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map gauges = ImmutableMap.of( + "Measurement queued", () -> 12, + "Measurement processed", () -> 15 + ); + + when(parser.parse("Measurement queued")).thenReturn( + DropwizardMeasurement.create("Measurement", MEASUREMENT_TAGS, Optional.of("queued")) + ); + + when(parser.parse("Measurement processed")).thenReturn( + DropwizardMeasurement.create("Measurement", MEASUREMENT_TAGS, Optional.of("processed")) + ); + + final Map> expected = ImmutableMap.of( + GroupKey.create("Measurement", MEASUREMENT_TAGS), ImmutableMap.of("queued", 12, "processed", 15) + ); + + final Map> groups = transformer.groupValues(gauges, "unused_default_key", Gauge::getValue); + assertEquals("should group values with inlined keys", expected, groups); + } + + @Test + public void testGroupValues_CountingGauges() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map gauges = ImmutableMap.of( + "some.stuff.queued.count", () -> 12, + "some.stuff.processed.count", () -> 15 + ); + + when(parser.parse("some.stuff")).thenReturn( + DropwizardMeasurement.create("some.stuff", MEASUREMENT_TAGS, Optional.empty()) + ); + + final Map> expected = ImmutableMap.of( + GroupKey.create("some.stuff", MEASUREMENT_TAGS), ImmutableMap.of("queued.count", 12, "processed.count", 15) + ); + + final Map> groups = transformer.groupValues(gauges, "unused_default_key", Gauge::getValue); + assertEquals("should ignore .count postfix when parsing groups", expected, groups); + } + + @Test + public void testGroupValues_NoDotIndex() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map gauges = ImmutableMap.of( + "some_stuff_queued", () -> 12, + "some_stuff_processed", () -> 15 + ); + + when(parser.parse("some_stuff_queued")).thenReturn( + DropwizardMeasurement.create("some.stuff.queued", MEASUREMENT_TAGS, Optional.empty()) + ); + + when(parser.parse("some_stuff_processed")).thenReturn( + DropwizardMeasurement.create("some.stuff.processed", MEASUREMENT_TAGS, Optional.empty()) + ); + + final Map> expected = ImmutableMap.of( + GroupKey.create("some.stuff.queued", MEASUREMENT_TAGS), ImmutableMap.of("default_key", 12), + GroupKey.create("some.stuff.processed", MEASUREMENT_TAGS), ImmutableMap.of("default_key", 15) + ); + + final Map> groups = transformer.groupValues(gauges, "default_key", Gauge::getValue); + assertEquals("should use fully parsed key and default value", expected, groups); + } + + @Test + public void testGroupValues_WithDotIndex() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map gauges = ImmutableMap.of( + "some.stuff.queued", () -> 12, + "some.stuff.processed", () -> 15 + ); + + when(parser.parse("some.stuff")).thenReturn( + DropwizardMeasurement.create("some.stuff", MEASUREMENT_TAGS, Optional.empty()) + ); + + final Map> expected = ImmutableMap.of( + GroupKey.create("some.stuff", MEASUREMENT_TAGS), ImmutableMap.of("queued", 12, "processed", 15) + ); + + final Map> groups = transformer.groupValues(gauges, "unused_default_key", Gauge::getValue); + assertEquals("should group values by field postfix", expected, groups); + } + + @Test + public void testGroupValues_WithSeparateTags() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map gauges = ImmutableMap.of( + "some.jumping.stuff.queued", () -> 12, + "some.leaning.stuff.processed", () -> 15 + ); + + final ImmutableMap jumpingTag = ImmutableMap.of("action", "jumping"); + final ImmutableMap leaningTag = ImmutableMap.of("action", "leaning"); + + when(parser.parse("some.jumping.stuff")).thenReturn( + DropwizardMeasurement.create("some.stuff", jumpingTag, Optional.empty()) + ); + + when(parser.parse("some.leaning.stuff")).thenReturn( + DropwizardMeasurement.create("some.stuff", leaningTag, Optional.empty()) + ); + + final Map> expected = ImmutableMap.of( + GroupKey.create("some.stuff", jumpingTag), ImmutableMap.of("queued", 12), + GroupKey.create("some.stuff", leaningTag), ImmutableMap.of("processed", 15) + ); + + final Map> groups = transformer.groupValues(gauges, "unused_default_key", Gauge::getValue); + assertEquals("should separate measurements by tag", expected, groups); + } + + @Test + public void testFromValueGroup() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + + final Map objFields = ImmutableMap.of( + "some", true, + "fields", 5 + ); + + final Map strFields = ImmutableMap.of( + "some", "true", + "fields", "5i" + ); + + assertEquals("should convert value group to influx measurement with global tags and stringy fields", + Optional.of(InfluxDbMeasurement.create("Measurement", ALL_TAGS, strFields, 90210L)), + transformer.fromValueGroup(GroupKey.create("Measurement", MEASUREMENT_TAGS), objFields, 90210L)); + } + + @Test + public void testFromValueGroup_InvalidField() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + final GroupKey key = GroupKey.create("Measurement", MEASUREMENT_TAGS); + + final Map objFields = ImmutableMap.of( + "some", true, + "bad", Arrays.asList(1, 2, 3), + "fields", 5 + ); + + final Map strFields = ImmutableMap.of( + "some", "true", + "fields", "5i" + ); + + assertEquals("should silently drop invalid values", + Optional.of(InfluxDbMeasurement.create("Measurement", ALL_TAGS, strFields, 90210L)), + transformer.fromValueGroup(key, objFields, 90210L)); + } + + @Test + public void testFromValueGroup_InvalidMeasurement() { + final DropwizardMeasurementParser parser = mock(DropwizardMeasurementParser.class); + final DropwizardTransformer transformer = transformerWithParser(parser, true); + final GroupKey key = GroupKey.create("Measurement", MEASUREMENT_TAGS); + + final Map objFields = ImmutableMap.of( + "all", Float.NaN, + "bad", Arrays.asList(1, 2, 3), + "fields", Float.NaN + ); + + assertEquals("should silently drop invalid measurements", + Optional.empty(), + transformer.fromValueGroup(key, objFields, 90210L)); + } + + // =================================================================================================================== + // Test helpers + + private static final ImmutableMap BASE_TAGS = ImmutableMap.of( + "some", "simple", + "global", "tags" + ); + + private static final ImmutableMap MEASUREMENT_TAGS = ImmutableMap.of( + "more", "specific", + "measurement", "tags" + ); + + private static final ImmutableMap ALL_TAGS = new ImmutableMap.Builder() + .putAll(BASE_TAGS) + .putAll(MEASUREMENT_TAGS) + .build(); + + private static DropwizardTransformer transformerWithParser(final DropwizardMeasurementParser parser, final boolean group) { + return new DropwizardTransformer( + BASE_TAGS, + parser, + group, + group, + // Dropwizard Defaults + TimeUnit.SECONDS, + TimeUnit.MILLISECONDS + ); + } + + private static DropwizardTransformer transformerWithUnits(final TimeUnit rateUnits, final TimeUnit durationUnits) { + return new DropwizardTransformer( + ImmutableMap.of(), + DropwizardMeasurementParser.withTemplates(ImmutableMap.of()), + true, + true, + rateUnits, + durationUnits + ); + } +} diff --git a/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java new file mode 100644 index 0000000..be1c43b --- /dev/null +++ b/src/test/java/com/kickstarter/dropwizard/metrics/influxdb/transformer/TaggedPatternTest.java @@ -0,0 +1,48 @@ +package com.kickstarter.dropwizard.metrics.influxdb.transformer; + +import com.google.common.collect.ImmutableMap; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; + +public class TaggedPatternTest { + private static final TaggedPattern taggedPattern = new TaggedPattern( + "\\.hello\\.(?[A-Za-z]+)\\.?(?.*)", + "action", "model" + ); + + @Test + public void testTags_noMatch() { + assertEquals( + Optional.empty(), + taggedPattern.tags(".hello.123") + ); + } + + @Test + public void testTags_partialMatch() { + assertEquals( + Optional.of(ImmutableMap.of("action", "restore")), + taggedPattern.tags(".hello.restore") + ); + } + + @Test + public void testTags_fullMatch() { + assertEquals( + Optional.of(ImmutableMap.of("action", "restore", "model", "collab-2-005")), + taggedPattern.tags(".hello.restore.collab-2-005") + ); + } + + @Test + public void testTags_matchWithoutTags() { + final TaggedPattern untaggedPattern = new TaggedPattern("\\.hello\\..*\\.whatever"); + assertEquals( + Optional.of(ImmutableMap.of()), + untaggedPattern.tags(".hello.anything.whatever") + ); + } +} diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..c4f9d84 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,23 @@ + + + + %-4relative [%thread] %-5level %logger{35} - %msg%n + + + OFF + + + + + target/test.log + true + + %-4relative [%thread] %-5level %logger{35} - %msg%n + + + + + + + +