├── .github ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTORS ├── Gemfile ├── LICENSE ├── NOTICE.TXT ├── README.md ├── Rakefile ├── docs └── index.asciidoc ├── lib └── logstash │ └── filters │ └── metrics.rb ├── logstash-filter-metrics.gemspec └── spec └── filters └── metrics_spec.rb /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Logstash 2 | 3 | All contributions are welcome: ideas, patches, documentation, bug reports, 4 | complaints, etc! 5 | 6 | Programming is not a required skill, and there are many ways to help out! 7 | It is more important to us that you are able to contribute. 8 | 9 | That said, some basic guidelines, which you are free to ignore :) 10 | 11 | ## Want to learn? 12 | 13 | Want to lurk about and see what others are doing with Logstash? 14 | 15 | * The irc channel (#logstash on irc.freenode.org) is a good place for this 16 | * The [forum](https://discuss.elastic.co/c/logstash) is also 17 | great for learning from others. 18 | 19 | ## Got Questions? 20 | 21 | Have a problem you want Logstash to solve for you? 22 | 23 | * You can ask a question in the [forum](https://discuss.elastic.co/c/logstash) 24 | * Alternately, you are welcome to join the IRC channel #logstash on 25 | irc.freenode.org and ask for help there! 26 | 27 | ## Have an Idea or Feature Request? 28 | 29 | * File a ticket on [GitHub](https://github.com/elastic/logstash/issues). Please remember that GitHub is used only for issues and feature requests. If you have a general question, the [forum](https://discuss.elastic.co/c/logstash) or IRC would be the best place to ask. 30 | 31 | ## Something Not Working? Found a Bug? 32 | 33 | If you think you found a bug, it probably is a bug. 34 | 35 | * If it is a general Logstash or a pipeline issue, file it in [Logstash GitHub](https://github.com/elasticsearch/logstash/issues) 36 | * If it is specific to a plugin, please file it in the respective repository under [logstash-plugins](https://github.com/logstash-plugins) 37 | * or ask the [forum](https://discuss.elastic.co/c/logstash). 38 | 39 | # Contributing Documentation and Code Changes 40 | 41 | If you have a bugfix or new feature that you would like to contribute to 42 | logstash, and you think it will take more than a few minutes to produce the fix 43 | (ie; write code), it is worth discussing the change with the Logstash users and developers first! You can reach us via [GitHub](https://github.com/elastic/logstash/issues), the [forum](https://discuss.elastic.co/c/logstash), or via IRC (#logstash on freenode irc) 44 | Please note that Pull Requests without tests will not be merged. If you would like to contribute but do not have experience with writing tests, please ping us on IRC/forum or create a PR and ask our help. 45 | 46 | ## Contributing to plugins 47 | 48 | Check our [documentation](https://www.elastic.co/guide/en/logstash/current/contributing-to-logstash.html) on how to contribute to plugins or write your own! It is super easy! 49 | 50 | ## Contribution Steps 51 | 52 | 1. Test your changes! [Run](https://github.com/elastic/logstash#testing) the test suite 53 | 2. Please make sure you have signed our [Contributor License 54 | Agreement](https://www.elastic.co/contributor-agreement/). We are not 55 | asking you to assign copyright to us, but to give us the right to distribute 56 | your code without restriction. We ask this of all contributors in order to 57 | assure our users of the origin and continuing existence of the code. You 58 | only need to sign the CLA once. 59 | 3. Send a pull request! Push your changes to your fork of the repository and 60 | [submit a pull 61 | request](https://help.github.com/articles/using-pull-requests). In the pull 62 | request, describe what your changes do and mention any bugs/issues related 63 | to the pull request. 64 | 65 | 66 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please post all product and debugging questions on our [forum](https://discuss.elastic.co/c/logstash). Your questions will reach our wider community members there, and if we confirm that there is a bug, then we can open a new issue here. 2 | 3 | For all general issues, please provide the following details for fast resolution: 4 | 5 | - Version: 6 | - Operating System: 7 | - Config File (if you have sensitive info, please remove it): 8 | - Sample Data: 9 | - Steps to Reproduce: 10 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thanks for contributing to Logstash! If you haven't already signed our CLA, here's a handy link: https://www.elastic.co/contributor-agreement/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | Gemfile.lock 3 | .bundle 4 | vendor 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | import: 2 | - logstash-plugins/.ci:travis/travis.yml@1.x -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 4.0.7 2 | - Fixed typo in documentation 3 | 4 | ## 4.0.6 5 | - Fixed two minor typos in documentation 6 | 7 | ## 4.0.5 8 | - Update gemspec summary 9 | 10 | ## 4.0.4 11 | - Fix some documentation issues 12 | 13 | ## 4.0.2 14 | - Relax constraint on logstash-core-plugin-api to >= 1.60 <= 2.99 15 | 16 | ## 4.0.1 17 | - Republish all the gems under jruby. 18 | 19 | ## 4.0.0 20 | - Update the plugin to the version 2.0 of the plugin api, this change is required for Logstash 5.0 compatibility. See https://github.com/elastic/logstash/issues/5141 21 | 22 | # 3.0.2 23 | - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash 24 | 25 | # 3.0.1 26 | - New dependency requirements for logstash-core for the 5.0 release 27 | 28 | ## 3.0.0 29 | - Elasticsearch 2.0 does not allow for dots in field names. This change changes to use sub-field syntax instead of 30 | dotted syntax. This is a breaking change. 31 | 32 | ## 2.0.2 33 | - Fix test that used deprecated "tags" syntax 34 | 35 | ## 2.0.0 36 | - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully, 37 | instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895 38 | - Dependency on logstash-core update to 2.0 39 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | The following is a list of people who have contributed ideas, code, bug 2 | reports, or in general have helped logstash along its way. 3 | 4 | Contributors: 5 | * Alejandro E Brito Monedero (aebm) 6 | * Bernd Ahlers (bernd) 7 | * Colin Surprenant (colinsurprenant) 8 | * Jordan Sissel (jordansissel) 9 | * Juarez Bochi (jbochi) 10 | * Nick Ethier (nickethier) 11 | * Philippe Weber (wiibaa) 12 | * Pier-Hugues Pellerin (ph) 13 | * R. Toma (rtoma) 14 | * Richard Pijnenburg (electrical) 15 | * Suyog Rao (suyograo) 16 | * piavlo 17 | 18 | Note: If you've sent us patches, bug reports, or otherwise contributed to 19 | Logstash, and you aren't on the list above and want to be, please let us know 20 | and we'll make sure you're here. Contributions from folks like you are what make 21 | open source awesome. 22 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash" 6 | use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1" 7 | 8 | if Dir.exist?(logstash_path) && use_logstash_source 9 | gem 'logstash-core', :path => "#{logstash_path}/logstash-core" 10 | gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api" 11 | end 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2020 Elastic and contributors 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE.TXT: -------------------------------------------------------------------------------- 1 | Elasticsearch 2 | Copyright 2012-2015 Elasticsearch 3 | 4 | This product includes software developed by The Apache Software 5 | Foundation (http://www.apache.org/). -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Logstash Plugin 2 | 3 | [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-filter-metrics.svg)](https://travis-ci.com/logstash-plugins/logstash-filter-metrics) 4 | 5 | This is a plugin for [Logstash](https://github.com/elastic/logstash). 6 | 7 | It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way. 8 | 9 | ## Documentation 10 | 11 | Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/). 12 | 13 | - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive 14 | - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide 15 | 16 | ## Need Help? 17 | 18 | Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum. 19 | 20 | ## Developing 21 | 22 | ### 1. Plugin Developement and Testing 23 | 24 | #### Code 25 | - To get started, you'll need JRuby with the Bundler gem installed. 26 | 27 | - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example). 28 | 29 | - Install dependencies 30 | ```sh 31 | bundle install 32 | ``` 33 | 34 | #### Test 35 | 36 | - Update your dependencies 37 | 38 | ```sh 39 | bundle install 40 | ``` 41 | 42 | - Run tests 43 | 44 | ```sh 45 | bundle exec rspec 46 | ``` 47 | 48 | ### 2. Running your unpublished Plugin in Logstash 49 | 50 | #### 2.1 Run in a local Logstash clone 51 | 52 | - Edit Logstash `Gemfile` and add the local plugin path, for example: 53 | ```ruby 54 | gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome" 55 | ``` 56 | - Install plugin 57 | ```sh 58 | # Logstash 2.3 and higher 59 | bin/logstash-plugin install --no-verify 60 | 61 | # Prior to Logstash 2.3 62 | bin/plugin install --no-verify 63 | 64 | ``` 65 | - Run Logstash with your plugin 66 | ```sh 67 | bin/logstash -e 'filter {awesome {}}' 68 | ``` 69 | At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash. 70 | 71 | #### 2.2 Run in an installed Logstash 72 | 73 | You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using: 74 | 75 | - Build your plugin gem 76 | ```sh 77 | gem build logstash-filter-awesome.gemspec 78 | ``` 79 | - Install the plugin from the Logstash home 80 | ```sh 81 | # Logstash 2.3 and higher 82 | bin/logstash-plugin install --no-verify 83 | 84 | # Prior to Logstash 2.3 85 | bin/plugin install --no-verify 86 | 87 | ``` 88 | - Start Logstash and proceed to test the plugin 89 | 90 | ## Contributing 91 | 92 | All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin. 93 | 94 | Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here. 95 | 96 | It is more important to the community that you are able to contribute. 97 | 98 | For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file. -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | @files=[] 2 | 3 | task :default do 4 | system("rake -T") 5 | end 6 | 7 | require "logstash/devutils/rake" 8 | -------------------------------------------------------------------------------- /docs/index.asciidoc: -------------------------------------------------------------------------------- 1 | :plugin: metrics 2 | :type: filter 3 | 4 | /////////////////////////////////////////// 5 | START - GENERATED VARIABLES, DO NOT EDIT! 6 | /////////////////////////////////////////// 7 | :version: %VERSION% 8 | :release_date: %RELEASE_DATE% 9 | :changelog_url: %CHANGELOG_URL% 10 | :include_path: ../../../../logstash/docs/include 11 | /////////////////////////////////////////// 12 | END - GENERATED VARIABLES, DO NOT EDIT! 13 | /////////////////////////////////////////// 14 | 15 | [id="plugins-{type}s-{plugin}"] 16 | 17 | === Metrics filter plugin 18 | 19 | include::{include_path}/plugin_header.asciidoc[] 20 | 21 | ==== Description 22 | 23 | The metrics filter is useful for aggregating metrics. 24 | 25 | IMPORTANT: Elasticsearch 2.0 no longer allows field names with dots. Version 3.0 26 | of the metrics filter plugin changes behavior to use nested fields rather than 27 | dotted notation to avoid colliding with versions of Elasticsearch 2.0+. Please 28 | note the changes in the documentation (underscores and sub-fields used). 29 | 30 | For example, if you have a field `response` that is 31 | a http response code, and you want to count each 32 | kind of response, you can do this: 33 | [source,ruby] 34 | filter { 35 | metrics { 36 | meter => [ "http_%{response}" ] 37 | add_tag => "metric" 38 | } 39 | } 40 | 41 | Metrics are flushed every 5 seconds by default or according to 42 | `flush_interval`. Metrics appear as 43 | new events in the event stream and go through any filters 44 | that occur after as well as outputs. 45 | 46 | In general, you will want to add a tag to your metrics and have an output 47 | explicitly look for that tag. 48 | 49 | The event that is flushed will include every 'meter' and 'timer' 50 | metric in the following way: 51 | 52 | ==== `meter` values 53 | 54 | For a `meter => "thing"` you will receive the following fields: 55 | 56 | * "[thing][count]" - the total count of events 57 | * "[thing][rate_1m]" - the per-second event rate in a 1-minute sliding window 58 | * "[thing][rate_5m]" - the per-second event rate in a 5-minute sliding window 59 | * "[thing][rate_15m]" - the per-second event rate in a 15-minute sliding window 60 | 61 | ==== `timer` values 62 | 63 | For a `timer => { "thing" => "%{duration}" }` you will receive the following fields: 64 | 65 | * "[thing][count]" - the total count of events 66 | * "[thing][rate_1m]" - the per-second average value in a 1-minute sliding window 67 | * "[thing][rate_5m]" - the per-second average value in a 5-minute sliding window 68 | * "[thing][rate_15m]" - the per-second average value in a 15-minute sliding window 69 | * "[thing][min]" - the minimum value seen for this metric 70 | * "[thing][max]" - the maximum value seen for this metric 71 | * "[thing][stddev]" - the standard deviation for this metric 72 | * "[thing][mean]" - the mean for this metric 73 | * "[thing][pXX]" - the XXth percentile for this metric (see `percentiles`) 74 | 75 | The default lengths of the event rate window (1, 5, and 15 minutes) 76 | can be configured with the `rates` option. 77 | 78 | ==== Example: Computing event rate 79 | 80 | For a simple example, let's track how many events per second are running 81 | through logstash: 82 | [source,ruby] 83 | ---- 84 | input { 85 | generator { 86 | type => "generated" 87 | } 88 | } 89 | 90 | filter { 91 | if [type] == "generated" { 92 | metrics { 93 | meter => "events" 94 | add_tag => "metric" 95 | } 96 | } 97 | } 98 | 99 | output { 100 | # only emit events with the 'metric' tag 101 | if "metric" in [tags] { 102 | stdout { 103 | codec => line { 104 | format => "rate: %{[events][rate_1m]}" 105 | } 106 | } 107 | } 108 | } 109 | ---- 110 | 111 | Running the above: 112 | [source,ruby] 113 | % bin/logstash -f example.conf 114 | rate: 23721.983566819246 115 | rate: 24811.395722536377 116 | rate: 25875.892745934525 117 | rate: 26836.42375967113 118 | 119 | We see the output includes our events' 1-minute rate. 120 | 121 | In the real world, you would emit this to graphite or another metrics store, 122 | like so: 123 | [source,ruby] 124 | output { 125 | graphite { 126 | metrics => [ "events.rate_1m", "%{[events][rate_1m]}" ] 127 | } 128 | } 129 | 130 | [id="plugins-{type}s-{plugin}-options"] 131 | ==== Metrics Filter Configuration Options 132 | 133 | This plugin supports the following configuration options plus the <> described later. 134 | 135 | [cols="<,<,<",options="header",] 136 | |======================================================================= 137 | |Setting |Input type|Required 138 | | <> |<>|No 139 | | <> |<>|No 140 | | <> |<>|No 141 | | <> |<>|No 142 | | <> |<>|No 143 | | <> |<>|No 144 | | <> |<>|No 145 | |======================================================================= 146 | 147 | Also see <> for a list of options supported by all 148 | filter plugins. 149 | 150 |   151 | 152 | [id="plugins-{type}s-{plugin}-clear_interval"] 153 | ===== `clear_interval` 154 | 155 | * Value type is <> 156 | * Default value is `-1` 157 | 158 | The clear interval, when all counters are reset. 159 | 160 | If set to -1, the default value, the metrics will never be cleared. 161 | Otherwise, should be a multiple of 5s. 162 | 163 | [id="plugins-{type}s-{plugin}-flush_interval"] 164 | ===== `flush_interval` 165 | 166 | * Value type is <> 167 | * Default value is `5` 168 | 169 | The flush interval, when the metrics event is created. Must be a multiple of 5s. 170 | 171 | [id="plugins-{type}s-{plugin}-ignore_older_than"] 172 | ===== `ignore_older_than` 173 | 174 | * Value type is <> 175 | * Default value is `0` 176 | 177 | Don't track events that have `@timestamp` older than some number of seconds. 178 | 179 | This is useful if you want to only include events that are near real-time 180 | in your metrics. 181 | 182 | For example, to only count events that are within 10 seconds of real-time, you 183 | would do this: 184 | 185 | filter { 186 | metrics { 187 | meter => [ "hits" ] 188 | ignore_older_than => 10 189 | } 190 | } 191 | 192 | [id="plugins-{type}s-{plugin}-meter"] 193 | ===== `meter` 194 | 195 | * Value type is <> 196 | * Default value is `[]` 197 | 198 | syntax: `meter => [ "name of metric", "name of metric" ]` 199 | 200 | [id="plugins-{type}s-{plugin}-percentiles"] 201 | ===== `percentiles` 202 | 203 | * Value type is <> 204 | * Default value is `[1, 5, 10, 90, 95, 99, 100]` 205 | 206 | The percentiles that should be measured and emitted for timer values. 207 | 208 | [id="plugins-{type}s-{plugin}-rates"] 209 | ===== `rates` 210 | 211 | * Value type is <> 212 | * Default value is `[1, 5, 15]` 213 | 214 | The rates that should be measured, in minutes. 215 | Possible values are 1, 5, and 15. 216 | 217 | [id="plugins-{type}s-{plugin}-timer"] 218 | ===== `timer` 219 | 220 | * Value type is <> 221 | * Default value is `{}` 222 | 223 | syntax: `timer => [ "name of metric", "%{time_value}" ]` 224 | 225 | 226 | 227 | [id="plugins-{type}s-{plugin}-common-options"] 228 | include::{include_path}/{type}.asciidoc[] 229 | -------------------------------------------------------------------------------- /lib/logstash/filters/metrics.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require "securerandom" 3 | require "logstash/filters/base" 4 | require "logstash/namespace" 5 | 6 | # The metrics filter is useful for aggregating metrics. 7 | # 8 | # IMPORTANT: Elasticsearch 2.0 no longer allows field names with dots. Version 3.0 9 | # of the metrics filter plugin changes behavior to use nested fields rather than 10 | # dotted notation to avoid colliding with versions of Elasticsearch 2.0+. Please 11 | # note the changes in the documentation (underscores and sub-fields used). 12 | # 13 | # For example, if you have a field `response` that is 14 | # a http response code, and you want to count each 15 | # kind of response, you can do this: 16 | # [source,ruby] 17 | # filter { 18 | # metrics { 19 | # meter => [ "http_%{response}" ] 20 | # add_tag => "metric" 21 | # } 22 | # } 23 | # 24 | # Metrics are flushed every 5 seconds by default or according to 25 | # `flush_interval`. Metrics appear as 26 | # new events in the event stream and go through any filters 27 | # that occur after as well as outputs. 28 | # 29 | # In general, you will want to add a tag to your metrics and have an output 30 | # explicitly look for that tag. 31 | # 32 | # The event that is flushed will include every 'meter' and 'timer' 33 | # metric in the following way: 34 | # 35 | # ==== `meter` values 36 | # 37 | # For a `meter => "thing"` you will receive the following fields: 38 | # 39 | # * "[thing][count]" - the total count of events 40 | # * "[thing][rate_1m]" - the per-second event rate in a 1-minute sliding window 41 | # * "[thing][rate_5m]" - the per-second event rate in a 5-minute sliding window 42 | # * "[thing][rate_15m]" - the per-second event rate in a 15-minute sliding window 43 | # 44 | # ==== `timer` values 45 | # 46 | # For a `timer => [ "thing", "%{duration}" ]` you will receive the following fields: 47 | # 48 | # * "[thing][count]" - the total count of events 49 | # * "[thing][rate_1m]" - the per-second event rate in a 1-minute sliding window 50 | # * "[thing][rate_5m]" - the per-second event rate in a 5-minute sliding window 51 | # * "[thing][rate_15m]" - the per-second event rate in a 15-minute sliding window 52 | # * "[thing][min]" - the minimum value seen for this metric 53 | # * "[thing][max]" - the maximum value seen for this metric 54 | # * "[thing][stddev]" - the standard deviation for this metric 55 | # * "[thing][mean]" - the mean for this metric 56 | # * "[thing][pXX]" - the XXth percentile for this metric (see `percentiles`) 57 | # 58 | # The default lengths of the event rate window (1, 5, and 15 minutes) 59 | # can be configured with the `rates` option. 60 | # 61 | # ==== Example: Computing event rate 62 | # 63 | # For a simple example, let's track how many events per second are running 64 | # through logstash: 65 | # [source,ruby] 66 | # ---- 67 | # input { 68 | # generator { 69 | # type => "generated" 70 | # } 71 | # } 72 | # 73 | # filter { 74 | # if [type] == "generated" { 75 | # metrics { 76 | # meter => "events" 77 | # add_tag => "metric" 78 | # } 79 | # } 80 | # } 81 | # 82 | # output { 83 | # # only emit events with the 'metric' tag 84 | # if "metric" in [tags] { 85 | # stdout { 86 | # codec => line { 87 | # format => "rate: %{[events][rate_1m]}" 88 | # } 89 | # } 90 | # } 91 | # } 92 | # ---- 93 | # 94 | # Running the above: 95 | # [source,ruby] 96 | # % bin/logstash -f example.conf 97 | # rate: 23721.983566819246 98 | # rate: 24811.395722536377 99 | # rate: 25875.892745934525 100 | # rate: 26836.42375967113 101 | # 102 | # We see the output includes our events' 1-minute rate. 103 | # 104 | # In the real world, you would emit this to graphite or another metrics store, 105 | # like so: 106 | # [source,ruby] 107 | # output { 108 | # graphite { 109 | # metrics => [ "events.rate_1m", "%{[events][rate_1m]}" ] 110 | # } 111 | # } 112 | class LogStash::Filters::Metrics < LogStash::Filters::Base 113 | config_name "metrics" 114 | 115 | # syntax: `meter => [ "name of metric", "name of metric" ]` 116 | config :meter, :validate => :array, :default => [] 117 | 118 | # syntax: `timer => [ "name of metric", "%{time_value}" ]` 119 | config :timer, :validate => :hash, :default => {} 120 | 121 | # Don't track events that have `@timestamp` older than some number of seconds. 122 | # 123 | # This is useful if you want to only include events that are near real-time 124 | # in your metrics. 125 | # 126 | # For example, to only count events that are within 10 seconds of real-time, you 127 | # would do this: 128 | # 129 | # filter { 130 | # metrics { 131 | # meter => [ "hits" ] 132 | # ignore_older_than => 10 133 | # } 134 | # } 135 | config :ignore_older_than, :validate => :number, :default => 0 136 | 137 | # The flush interval, when the metrics event is created. Must be a multiple of 5s. 138 | config :flush_interval, :validate => :number, :default => 5 139 | 140 | # The clear interval, when all counter are reset. 141 | # 142 | # If set to -1, the default value, the metrics will never be cleared. 143 | # Otherwise, should be a multiple of 5s. 144 | config :clear_interval, :validate => :number, :default => -1 145 | 146 | # The rates that should be measured, in minutes. 147 | # Possible values are 1, 5, and 15. 148 | config :rates, :validate => :array, :default => [1, 5, 15] 149 | 150 | # The percentiles that should be measured and emitted for timer values. 151 | config :percentiles, :validate => :array, :default => [1, 5, 10, 90, 95, 99, 100] 152 | 153 | def register 154 | require "metriks" 155 | require "socket" 156 | require "atomic" 157 | require "thread_safe" 158 | @last_flush = Atomic.new(0) # how many seconds ago the metrics where flushed. 159 | @last_clear = Atomic.new(0) # how many seconds ago the metrics where cleared. 160 | @random_key_preffix = SecureRandom.hex 161 | # Same as logstash-input-file 162 | @host = Socket.gethostname.force_encoding(Encoding::UTF_8) 163 | unless (@rates - [1, 5, 15]).empty? 164 | raise LogStash::ConfigurationError, "Invalid rates configuration. possible rates are 1, 5, 15. Rates: #{rates}." 165 | end 166 | @metric_meters = ThreadSafe::Cache.new { |h,k| h[k] = Metriks.meter metric_key(k) } 167 | @metric_timers = ThreadSafe::Cache.new { |h,k| h[k] = Metriks.timer metric_key(k) } 168 | end # def register 169 | 170 | def filter(event) 171 | 172 | 173 | # TODO(piavlo): This should probably be moved to base filter class. 174 | if @ignore_older_than > 0 && Time.now - event.timestamp.time > @ignore_older_than 175 | @logger.debug("Skipping metriks for old event", :event => event) 176 | return 177 | end 178 | 179 | @meter.each do |m| 180 | @metric_meters[event.sprintf(m)].mark 181 | end 182 | 183 | @timer.each do |name, value| 184 | @metric_timers[event.sprintf(name)].update(event.sprintf(value).to_f) 185 | end 186 | end # def filter 187 | 188 | def flush(options = {}) 189 | # Add 5 seconds to @last_flush and @last_clear counters 190 | # since this method is called every 5 seconds. 191 | @last_flush.update { |v| v + 5 } 192 | @last_clear.update { |v| v + 5 } 193 | 194 | # Do nothing if there's nothing to do ;) 195 | return unless should_flush? 196 | 197 | event = LogStash::Event.new 198 | event.set("message", @host) 199 | @metric_meters.each_pair do |name, metric| 200 | flush_rates event, name, metric 201 | metric.clear if should_clear? 202 | end 203 | 204 | @metric_timers.each_pair do |name, metric| 205 | flush_rates event, name, metric 206 | # These 4 values are not sliding, so they probably are not useful. 207 | event.set("[#{name}][min]", metric.min) 208 | event.set("[#{name}][max]", metric.max) 209 | # timer's stddev currently returns variance, fix it. 210 | event.set("[#{name}][stddev]", metric.stddev ** 0.5) 211 | event.set("[#{name}][mean]", metric.mean) 212 | 213 | @percentiles.each do |percentile| 214 | event.set("[#{name}][p#{percentile}]", metric.snapshot.value(percentile / 100.0)) 215 | end 216 | metric.clear if should_clear? 217 | end 218 | 219 | # Reset counter since metrics were flushed 220 | @last_flush.value = 0 221 | 222 | if should_clear? 223 | #Reset counter since metrics were cleared 224 | @last_clear.value = 0 225 | @metric_meters.clear 226 | @metric_timers.clear 227 | end 228 | 229 | filter_matched(event) 230 | return [event] 231 | end 232 | 233 | # this is a temporary fix to enable periodic flushes without using the plugin config: 234 | # config :periodic_flush, :validate => :boolean, :default => true 235 | # because this is not optional here and should not be configurable. 236 | # this is until we refactor the periodic_flush mechanism per 237 | # https://github.com/elasticsearch/logstash/issues/1839 238 | def periodic_flush 239 | true 240 | end 241 | 242 | private 243 | 244 | def flush_rates(event, name, metric) 245 | event.set("[#{name}][count]", metric.count) 246 | event.set("[#{name}][rate_1m]", metric.one_minute_rate) if @rates.include? 1 247 | event.set("[#{name}][rate_5m]", metric.five_minute_rate) if @rates.include? 5 248 | event.set("[#{name}][rate_15m]", metric.fifteen_minute_rate) if @rates.include? 15 249 | end 250 | 251 | def metric_key(key) 252 | "#{@random_key_preffix}_#{key}" 253 | end 254 | 255 | def should_flush? 256 | @last_flush.value >= @flush_interval && (!@metric_meters.empty? || !@metric_timers.empty?) 257 | end 258 | 259 | def should_clear? 260 | @clear_interval > 0 && @last_clear.value >= @clear_interval 261 | end 262 | end # class LogStash::Filters::Metrics 263 | -------------------------------------------------------------------------------- /logstash-filter-metrics.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | 3 | s.name = 'logstash-filter-metrics' 4 | s.version = '4.0.7' 5 | s.licenses = ['Apache License (2.0)'] 6 | s.summary = "Aggregates metrics" 7 | s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program" 8 | s.authors = ["Elastic"] 9 | s.email = 'info@elastic.co' 10 | s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html" 11 | s.require_paths = ["lib"] 12 | 13 | # Files 14 | s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"] 15 | 16 | # Tests 17 | s.test_files = s.files.grep(%r{^(test|spec|features)/}) 18 | 19 | # Special flag to let us know this is actually a logstash plugin 20 | s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" } 21 | 22 | # Gem dependencies 23 | s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" 24 | s.add_runtime_dependency "metriks" #(MIT license) 25 | s.add_runtime_dependency "thread_safe" 26 | 27 | s.add_development_dependency 'logstash-devutils' 28 | s.add_development_dependency 'insist' 29 | end 30 | -------------------------------------------------------------------------------- /spec/filters/metrics_spec.rb: -------------------------------------------------------------------------------- 1 | require "logstash/devutils/rspec/spec_helper" 2 | require "insist" 3 | require "logstash/filters/metrics" 4 | 5 | describe LogStash::Filters::Metrics do 6 | 7 | context "with basic meter config" do 8 | context "when no events were received" do 9 | it "should not flush" do 10 | config = {"meter" => ["http_%{response}"]} 11 | filter = LogStash::Filters::Metrics.new config 12 | filter.register 13 | 14 | events = filter.flush 15 | insist { events }.nil? 16 | end 17 | end 18 | 19 | context "when events are received" do 20 | context "on the first flush" do 21 | subject { 22 | config = {"meter" => ["http_%{response}"]} 23 | filter = LogStash::Filters::Metrics.new config 24 | filter.register 25 | filter.filter LogStash::Event.new({"response" => 200}) 26 | filter.filter LogStash::Event.new({"response" => 200}) 27 | filter.filter LogStash::Event.new({"response" => 404}) 28 | filter.flush 29 | } 30 | 31 | it "should flush counts" do 32 | insist { subject.length } == 1 33 | insist { subject.first.get("http_200")["count"] } == 2 34 | insist { subject.first.get("http_404")["count"] } == 1 35 | end 36 | 37 | it "should include rates and percentiles" do 38 | meters = [ "http_200", "http_404" ] 39 | rates = [ "rate_1m", "rate_5m", "rate_15m" ] 40 | meters.each do |meter| 41 | rates.each do |rate| 42 | insist { subject.first.get(meter) }.include? rate 43 | end 44 | end 45 | end 46 | 47 | it "should have host in message" do 48 | insist { subject.length } == 1 49 | reject { subject.first.get("message") }.nil? 50 | end 51 | end 52 | 53 | context "on the second flush" do 54 | it "should not reset counts" do 55 | config = {"meter" => ["http_%{response}"]} 56 | filter = LogStash::Filters::Metrics.new config 57 | filter.register 58 | filter.filter LogStash::Event.new({"response" => 200}) 59 | filter.filter LogStash::Event.new({"response" => 200}) 60 | filter.filter LogStash::Event.new({"response" => 404}) 61 | 62 | events = filter.flush 63 | events = filter.flush 64 | insist { events.length } == 1 65 | insist { events.first.get("http_200")["count"] } == 2 66 | insist { events.first.get("http_404")["count"] } == 1 67 | end 68 | end 69 | end 70 | 71 | context "when custom rates and percentiles are selected" do 72 | context "on the first flush" do 73 | subject { 74 | config = { 75 | "meter" => ["http_%{response}"], 76 | "rates" => [1] 77 | } 78 | filter = LogStash::Filters::Metrics.new config 79 | filter.register 80 | filter.filter LogStash::Event.new({"response" => 200}) 81 | filter.filter LogStash::Event.new({"response" => 200}) 82 | filter.filter LogStash::Event.new({"response" => 404}) 83 | filter.flush 84 | } 85 | 86 | it "should include only the requested rates" do 87 | rate_fields = subject.first.get("http_200").to_hash.keys.select {|field| field.start_with?("rate") } 88 | insist { rate_fields.length } == 1 89 | insist { rate_fields }.include? "rate_1m" 90 | end 91 | end 92 | end 93 | end 94 | 95 | context "with multiple instances" do 96 | it "counts should be independent" do 97 | config1 = {"meter" => ["http_%{response}"]} 98 | config2 = {"meter" => ["http_%{response}"]} 99 | filter1 = LogStash::Filters::Metrics.new config1 100 | filter2 = LogStash::Filters::Metrics.new config2 101 | events1 = [ 102 | LogStash::Event.new({"response" => 200}), 103 | LogStash::Event.new({"response" => 404}) 104 | ] 105 | events2 = [ 106 | LogStash::Event.new({"response" => 200}), 107 | LogStash::Event.new({"response" => 200}) 108 | ] 109 | filter1.register 110 | filter2.register 111 | 112 | events1.each do |event| 113 | filter1.filter event 114 | end 115 | 116 | events2.each do |event| 117 | filter2.filter event 118 | end 119 | 120 | events1 = filter1.flush 121 | events2 = filter2.flush 122 | 123 | insist { events1.first.get("http_200")["count"] } == 1 124 | insist { events2.first.get("http_200")["count"] } == 2 125 | insist { events1.first.get("http_404")["count"] } == 1 126 | insist { events2.first.get("http_404") } == nil 127 | end 128 | end 129 | 130 | context "with timer config" do 131 | context "on the first flush" do 132 | subject { 133 | config = {"timer" => ["http_request_time", "%{request_time}"]} 134 | filter = LogStash::Filters::Metrics.new config 135 | filter.register 136 | filter.filter LogStash::Event.new({"request_time" => 10}) 137 | filter.filter LogStash::Event.new({"request_time" => 20}) 138 | filter.filter LogStash::Event.new({"request_time" => 30}) 139 | filter.flush 140 | } 141 | 142 | it "should flush counts" do 143 | insist { subject.length } == 1 144 | insist { subject.first.get("http_request_time")["count"] } == 3 145 | end 146 | 147 | it "should include rates and percentiles keys" do 148 | metrics = ["rate_1m", "rate_5m", "rate_15m", "p1", "p5", "p10", "p90", "p95", "p99"] 149 | metrics.each do |metric| 150 | insist { subject.first.get("http_request_time") }.include? metric 151 | end 152 | end 153 | 154 | it "should include min value" do 155 | insist { subject.first.get("http_request_time")['min'] } == 10.0 156 | end 157 | 158 | it "should include mean value" do 159 | insist { subject.first.get("http_request_time")['mean'] } == 20.0 160 | end 161 | 162 | it "should include stddev value" do 163 | insist { subject.first.get("http_request_time")['stddev'] } == Math.sqrt(10.0) 164 | end 165 | 166 | it "should include max value" do 167 | insist { subject.first.get("http_request_time")['max'] } == 30.0 168 | end 169 | 170 | it "should include percentile value" do 171 | insist { subject.first.get("http_request_time")['p99'] } == 30.0 172 | end 173 | end 174 | end 175 | 176 | context "when custom rates and percentiles are selected" do 177 | context "on the first flush" do 178 | subject { 179 | config = { 180 | "timer" => ["http_request_time", "request_time"], 181 | "rates" => [1], 182 | "percentiles" => [1, 2] 183 | } 184 | filter = LogStash::Filters::Metrics.new config 185 | filter.register 186 | filter.filter LogStash::Event.new({"request_time" => 1}) 187 | filter.flush 188 | } 189 | 190 | it "should flush counts" do 191 | insist { subject.length } == 1 192 | insist { subject.first.get("http_request_time")["count"] } == 1 193 | end 194 | 195 | it "should include only the requested rates" do 196 | rate_fields = subject.first.get("http_request_time").to_hash.keys.select {|field| field.start_with?("rate") } 197 | insist { rate_fields.length } == 1 198 | insist { rate_fields }.include? "rate_1m" 199 | end 200 | 201 | it "should include only the requested percentiles" do 202 | percentile_fields = subject.first.get("http_request_time").to_hash.keys.select {|field| field.start_with?("p") } 203 | insist { percentile_fields.length } == 2 204 | insist { percentile_fields }.include? "p1" 205 | insist { percentile_fields }.include? "p2" 206 | end 207 | end 208 | end 209 | 210 | 211 | context "when a custom flush_interval is set" do 212 | it "should flush only when required" do 213 | config = {"meter" => ["http_%{response}"], "flush_interval" => 15} 214 | filter = LogStash::Filters::Metrics.new config 215 | filter.register 216 | filter.filter LogStash::Event.new({"response" => 200}) 217 | 218 | insist { filter.flush }.nil? # 5s 219 | insist { filter.flush }.nil? # 10s 220 | insist { filter.flush.length } == 1 # 15s 221 | insist { filter.flush }.nil? # 20s 222 | insist { filter.flush }.nil? # 25s 223 | insist { filter.flush.length } == 1 # 30s 224 | end 225 | end 226 | 227 | context "when a custom clear_interval is set" do 228 | it "should clear the metrics after interval has passed" do 229 | config = {"meter" => ["http_%{response}"], "clear_interval" => 15} 230 | filter = LogStash::Filters::Metrics.new config 231 | filter.register 232 | filter.filter LogStash::Event.new({"response" => 200}) 233 | 234 | insist { filter.flush.first.get("http_200")["count"] } == 1 # 5s 235 | insist { filter.flush.first.get("http_200")["count"] } == 1 # 10s 236 | insist { filter.flush.first.get("http_200")["count"] } == 1 # 15s 237 | insist { filter.flush }.nil? # 20s 238 | end 239 | end 240 | 241 | context "when invalid rates are set" do 242 | subject { 243 | config = {"meter" => ["http_%{response}"], "rates" => [90]} 244 | filter = LogStash::Filters::Metrics.new config 245 | } 246 | 247 | it "should raise an error" do 248 | insist {subject.register }.raises(LogStash::ConfigurationError) 249 | end 250 | end 251 | end 252 | --------------------------------------------------------------------------------