├── .gitignore ├── Gemfile ├── .github ├── CODEOWNERS └── workflows │ ├── pull_request.yaml │ └── publish.yaml ├── Rakefile ├── ci └── build.sh ├── test ├── helper.rb └── plugin │ └── test_out_sumologic.rb ├── vagrant ├── provision.sh └── README.md ├── .travis.yml ├── CONTRIBUTING.md ├── Vagrantfile ├── fluent-plugin-sumologic_output.gemspec ├── CHANGELOG.md ├── README.md ├── LICENSE └── lib └── fluent └── plugin └── out_sumologic.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | Gemfile.lock 4 | .idea/ 5 | TAGS 6 | .vagrant 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | group :test do 4 | gem 'codecov' 5 | gem 'simplecov' 6 | gem 'webmock' 7 | end 8 | 9 | gemspec -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Learn about CODEOWNERS file format: 2 | # https://help.github.com/en/articles/about-code-owners 3 | 4 | * @SumoLogic/open-source-collection-team 5 | 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | require 'rake/testtask' 3 | 4 | Rake::TestTask.new(:test) do |test| 5 | test.libs << 'test' 6 | test.pattern = 'test/**/test_*.rb' 7 | test.verbose = true 8 | test.warning = false 9 | end 10 | 11 | task :default => :test 12 | -------------------------------------------------------------------------------- /.github/workflows/pull_request.yaml: -------------------------------------------------------------------------------- 1 | name: PR check 2 | 3 | on: 4 | pull_request: 5 | 6 | jobs: 7 | test: 8 | runs-on: ubuntu-20.04 9 | steps: 10 | - uses: actions/checkout@v4 11 | - uses: ruby/setup-ruby@v1 12 | with: 13 | ruby-version: '3.3' 14 | - run: bundle install 15 | - run: bundle exec rake 16 | -------------------------------------------------------------------------------- /ci/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Starting build process in: `pwd`" 4 | set -e 5 | 6 | VERSION="${TRAVIS_TAG:=0.0.0}" 7 | echo "Building for tag $VERSION, modify .gemspec file..." 8 | sed -i.bak "s/0.0.0/$VERSION/g" ./fluent-plugin-sumologic_output.gemspec 9 | rm -f ./fluent-plugin-sumologic_output.gemspec.bak 10 | 11 | echo "Install bundler..." 12 | bundle install 13 | 14 | echo "Run unit tests..." 15 | bundle exec rake 16 | 17 | echo "DONE" 18 | -------------------------------------------------------------------------------- /test/helper.rb: -------------------------------------------------------------------------------- 1 | require 'simplecov' 2 | SimpleCov.start 3 | 4 | if ENV['CI'] == 'true' 5 | require 'codecov' 6 | SimpleCov.formatter = SimpleCov::Formatter::Codecov 7 | end 8 | 9 | $LOAD_PATH.unshift(File.expand_path("../../", __FILE__)) 10 | require "test-unit" 11 | require "fluent/test" 12 | require "fluent/test/driver/output" 13 | require "fluent/test/helpers" 14 | 15 | Test::Unit::TestCase.include(Fluent::Test::Helpers) 16 | Test::Unit::TestCase.extend(Fluent::Test::Helpers) 17 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | tags: 6 | - '[0-9]+.[0-9]+.[0-9]+' 7 | - '[0-9]+.[0-9]+.[0-9]+-alpha.[0-9]+' 8 | - '[0-9]+.[0-9]+.[0-9]+-beta.[0-9]+' 9 | - '[0-9]+.[0-9]+.[0-9]+-rc.[0-9]+' 10 | 11 | jobs: 12 | publish: 13 | runs-on: ubuntu-20.04 14 | 15 | steps: 16 | - uses: actions/checkout@v4 17 | 18 | - name: Build 19 | run: gem build fluent-plugin-sumologic_output.gemspec 20 | - name: Publish 21 | env: 22 | GEM_HOST_API_KEY: ${{ secrets.RUGYGEMS_APIKEY }} 23 | run: gem push fluent-plugin-sumologic_output-*.gem 24 | -------------------------------------------------------------------------------- /vagrant/provision.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -x 4 | 5 | export DEBIAN_FRONTEND=noninteractive 6 | ARCH="$(dpkg --print-architecture)" 7 | 8 | apt-get update 9 | apt-get --yes upgrade 10 | apt-get --yes install apt-transport-https 11 | 12 | echo "export EDITOR=vim" >> /home/vagrant/.bashrc 13 | 14 | # Install docker 15 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - 16 | add-apt-repository \ 17 | "deb [arch=${ARCH}] https://download.docker.com/linux/ubuntu \ 18 | $(lsb_release -cs) \ 19 | stable" 20 | apt-get install -y docker-ce docker-ce-cli containerd.io 21 | usermod -aG docker vagrant 22 | 23 | # Install make 24 | apt-get install -y make 25 | 26 | # install requirements for ruby 27 | apt install -y \ 28 | gcc \ 29 | g++ \ 30 | libsnappy-dev \ 31 | libicu-dev \ 32 | zlib1g-dev \ 33 | cmake \ 34 | pkg-config \ 35 | libssl-dev \ 36 | ruby-dev \ 37 | ruby-bundler 38 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | lang: ruby 2 | before_install: gem install bundler 3 | script: ci/build.sh 4 | deploy: 5 | provider: rubygems 6 | skip_cleanup: true 7 | api_key: 8 | secure: mzu7CaM8x5BLZ2QvgQJFfY/xvzZN2+gpMieRoBnmefjhhs+zGjqzBn9wN+4/IZNQUKhDE729CEx1xSEegz44Ru73sNEsS4Y/jLNqr24Swcr4xyYcqxX58x18o1K28HD1Mp9NhS8gLIkd/sucCcSAHPag6Zkk00gETGsGCXFHxYP5HpXbBMKy1OFZ3o7dGv+Hv4g5XtDaM1Kjwcku398PsQsBmh0jJyag+NL804dUL66weTA3P7q94/TEVZ1lbvUZlokzxHBhkO0eHM2fuCdKzW1BneePxYnMRFzjRxXcYoalfEVoy9KHi3mDBCtj4Bw6hSgfnrDFVjZzoMPybOLJDt9DlYEplcWlXlS3EuDr5aYgyfkUbuv1OZm/94EdkMa4epH51vq0r6AHstG7ZCmrBxiLUMs0wgwRSPTIK1hJG+UFuCxGASU+IH8OD8bTav21cXEhVLDw5dEzk6smnfY+mpAexiW5ytIc9wf0qkP6bbICH9zRHYUWHxcCTqouhArHPyyESSngAYkpdHbRql+uGll8ab6IaVp33WvKqYQK2QnTcb1A7pgtiAxzhl0yBTR/oNIRdj6X6y7FmRuaEmkU29jzuetcq4xdgQTkNfbeLITwyRn2hJBGHIEcvHPwilzZGIfr96QcqBw/AGrcIirkD3NuZ86DJZ8Ucn3BbKXEmTU= 9 | gem: fluent-plugin-sumologic_output 10 | on: 11 | tags: true 12 | repo: SumoLogic/fluentd-output-sumologic 13 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## How to release 4 | 5 | 1. Create a pull request named `chore: release x.y.z` with the following changes: 6 | 7 | - Set `gem.version` to `"x.y.z"` in [fluent-plugin-sumologic_output.gemspec](fluent-plugin-sumologic_output.gemspec). 8 | - Add new version to [CHANGELOG.md](./CHANGELOG.md). 9 | 10 | Example PR: [#92](https://github.com/SumoLogic/fluentd-output-sumologic/pull/92) 11 | 12 | 2. Create and push the release tag: 13 | 14 | ```bash 15 | git checkout main 16 | git pull 17 | export VERSION=x.y.z 18 | git tag -a "${VERSION}" -m "Release ${VERSION}" 19 | git push origin "${VERSION}" 20 | ``` 21 | 22 | This will trigger the GitHub Actions [publish](./.github/workflows/publish.yaml) action to pubilsh the gem in Ruby Gems. 23 | 24 | 3. Go to https://github.com/SumoLogic/fluentd-output-sumologic/releases and create a new release for the tag. 25 | Copy the changes from Changelog and publish the release. 26 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | Vagrant.configure('2') do |config| 5 | config.vm.box = 'ubuntu/focal64' 6 | config.vm.disk :disk, size: "50GB", primary: true 7 | config.vm.box_check_update = false 8 | config.vm.host_name = 'fluentd-output-sumologic' 9 | # Vbox 6.1.28+ restricts host-only adapters to 192.168.56.0/21 10 | # See: https://www.virtualbox.org/manual/ch06.html#network_hostonly 11 | config.vm.network :private_network, ip: "192.168.56.45" 12 | 13 | config.vm.provider 'virtualbox' do |vb| 14 | vb.gui = false 15 | vb.cpus = 8 16 | vb.memory = 16384 17 | vb.name = 'fluentd-output-sumologic' 18 | end 19 | 20 | config.vm.provider "qemu" do |qe, override| 21 | override.vm.box = "perk/ubuntu-2204-arm64" 22 | qe.gui = false 23 | qe.smp = 8 24 | qe.memory = 16384 25 | qe.name = 'fluentd-output-sumologic' 26 | end 27 | 28 | config.vm.provision 'shell', path: 'vagrant/provision.sh' 29 | 30 | config.vm.synced_folder ".", "/sumologic" 31 | end 32 | -------------------------------------------------------------------------------- /fluent-plugin-sumologic_output.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | 5 | Gem::Specification.new do |gem| 6 | gem.name = "fluent-plugin-sumologic_output" 7 | gem.version = "1.10.0" 8 | gem.authors = ["Steven Adams", "Frank Reno"] 9 | gem.email = ["stevezau@gmail.com", "frank.reno@me.com"] 10 | gem.description = %q{Output plugin to SumoLogic HTTP Endpoint} 11 | gem.summary = %q{Output plugin to SumoLogic HTTP Endpoint} 12 | gem.homepage = "https://github.com/SumoLogic/fluentd-output-sumologic" 13 | gem.license = "Apache-2.0" 14 | 15 | gem.files = `git ls-files`.split($/) 16 | gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) } 17 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 18 | gem.require_paths = ["lib"] 19 | 20 | gem.add_development_dependency "bundler" 21 | gem.add_development_dependency "rake" 22 | gem.add_development_dependency 'test-unit' 23 | gem.add_development_dependency "codecov" 24 | gem.add_runtime_dependency "fluentd" 25 | gem.add_runtime_dependency 'httpclient' 26 | end 27 | -------------------------------------------------------------------------------- /vagrant/README.md: -------------------------------------------------------------------------------- 1 | # Vagrant 2 | 3 | ## Prerequisites 4 | 5 | Please install the following: 6 | 7 | - [VirtualBox](https://www.virtualbox.org/) 8 | - [Vagrant](https://developer.hashicorp.com/vagrant/downloads) 9 | 10 | ### General 11 | 12 | 1. Install `vagrant` as per 13 | 2. Configure a provider 14 | 3. Run `vagrant up` and then `vagrant ssh` 15 | 16 | ### MacOS 17 | 18 | ```bash 19 | brew cask install virtualbox 20 | brew cask install vagrant 21 | ``` 22 | 23 | ### ARM hosts (Apple M1, and so on) 24 | 25 | You'll need to use QEMU instead of VirtualBox to use Vagrant on ARM. The following instructions will assume an M1 Mac as the host: 26 | 27 | 1. Install QEMU: `brew install qemu` 28 | 2. Install the QEMU vagrant provider: `vagrant plugin install vagrant-qemu` 29 | 3. Provision the VM with the provider: `vagrant up --provider=qemu` 30 | 31 | ## Setting up 32 | 33 | You can set up the Vagrant environment with just one command: 34 | 35 | ```bash 36 | vagrant up 37 | ``` 38 | 39 | After successfull installation you can ssh to the virtual machine with: 40 | 41 | ```bash 42 | vagrant ssh 43 | ``` 44 | 45 | NOTICE: The directory with fluentd-output-sumologic repository on the host is synced with `/sumologic/` directory on the virtual machine. 46 | 47 | ## Runing tests 48 | 49 | You can run tests using following commands: 50 | 51 | ```bash 52 | cd /sumologic 53 | bundle install 54 | bundle exec rake 55 | ``` 56 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [1.10.0] 9 | 10 | Released 2024-10-08 11 | 12 | - feat: make receive_timeout configurable [#96](https://github.com/SumoLogic/fluentd-output-sumologic/pull/96) 13 | - chore: add arm support to vagrant [#95](https://github.com/SumoLogic/fluentd-output-sumologic/pull/95) 14 | 15 | [1.10.0]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.10.0 16 | 17 | ## [1.9.0] 18 | 19 | Released 2024-02-14 20 | 21 | - feat: enable compression by default [#87](https://github.com/SumoLogic/fluentd-output-sumologic/pull/87) 22 | - feat: log warning if `log_key` does not exist in log [#86](https://github.com/SumoLogic/fluentd-output-sumologic/pull/86) 23 | - fix: fix how `compress` configuration flag works [#90](https://github.com/SumoLogic/fluentd-output-sumologic/pull/90) 24 | 25 | In `v1.8.0`, setting `compress` flag to either `true` or `false` caused compression to be enabled. 26 | This is now fixed. 27 | 28 | [1.9.0]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.9.0 29 | 30 | ## [1.8.0] (2022-04-22) 31 | 32 | - feat: add exponential backoff for sending data [#76](https://github.com/SumoLogic/fluentd-output-sumologic/pull/76) 33 | - feat(max_request_size): add max_request_size to limit size of requests [#78](https://github.com/SumoLogic/fluentd-output-sumologic/pull/78) 34 | 35 | [1.8.0]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.8.0 36 | 37 | ## [1.7.5] (2022-04-11) 38 | 39 | - refactor: add a debug log on sending [#75](https://github.com/SumoLogic/fluentd-output-sumologic/pull/75) 40 | 41 | [1.7.5]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.7.5 42 | 43 | ## [1.7.4] (2021-04-08) 44 | 45 | - fix: handle receiver warning messages [#73](https://github.com/SumoLogic/fluentd-output-sumologic/pull/73) 46 | 47 | [1.7.4]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.7.4 48 | 49 | ## [1.7.3] (2021-10-19) 50 | 51 | - Expose httpclient send_timeout [#66](https://github.com/SumoLogic/fluentd-output-sumologic/pull/68) 52 | - Fix json parsing [#69](https://github.com/SumoLogic/fluentd-output-sumologic/pull/69) 53 | 54 | [1.7.3]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.7.3 55 | 56 | ## [1.7.2] (2020-11-23) 57 | 58 | - Fix configuration for older fluentd versions [#63](https://github.com/SumoLogic/fluentd-output-sumologic/pull/63) 59 | 60 | [1.7.2]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.7.2 61 | 62 | ## [1.7.1] (2020-04-28) 63 | 64 | - Fix configuration for older fluentd versions [#63](https://github.com/SumoLogic/fluentd-output-sumologic/pull/63) 65 | 66 | [1.7.1]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.7.1 67 | 68 | ## [1.7.0] (2020-04-23) 69 | 70 | - Add option for specifing custom fields for logs: [#56](https://github.com/SumoLogic/fluentd-output-sumologic/pull/56) 71 | - Add option for specifing custom dimensions for metrics: [#57](https://github.com/SumoLogic/fluentd-output-sumologic/pull/57) 72 | - Add support for compression: [#58](https://github.com/SumoLogic/fluentd-output-sumologic/pull/58) 73 | 74 | [1.7.0]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.7.0 75 | 76 | ## [1.5.0] (2019-06-26) 77 | 78 | - Add support for new log format fields: [#49](https://github.com/SumoLogic/fluentd-output-sumologic/pull/49) 79 | 80 | [1.5.0]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.5.0 81 | 82 | ## [1.4.1] (2019-03-13) 83 | 84 | - Add option for sending metrics in Prometheus format [#39](https://github.com/SumoLogic/fluentd-output-sumologic/pull/39) 85 | - Use the build-in extract_placeholders method for header expanding [#40](https://github.com/SumoLogic/fluentd-output-sumologic/pull/40) 86 | 87 | __NOTE:__ there is a breaking change in the placeholders: `tag_parts[n]` is replaced by `tag[n]` [#47](https://github.com/SumoLogic/fluentd-output-sumologic/issues/47) 88 | 89 | [1.4.1]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/1.4.1 90 | 91 | ## [1.4.0] (2019-01-16) 92 | 93 | - [Add timestamp_key, prefer log message when merging](https://github.com/SumoLogic/fluentd-output-sumologic/pull/37) 94 | 95 | [1.4.0]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/tag/1.4.0 96 | 97 | ## [1.3.2] (2018-12-05) 98 | 99 | - Fix verify SSL bug 100 | 101 | [1.3.2]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/tag/1.3.2 102 | 103 | ## [1.3.1] (2018-08-30) 104 | 105 | - [Sumo Logic endpoint is a secret](https://github.com/SumoLogic/fluentd-output-sumologic/pull/32) 106 | 107 | [1.3.1]: https://github.com/SumoLogic/fluentd-output-sumologic/releases/tag/1.3.1 108 | 109 | ## 1.3.0 (2018-08-08) 110 | 111 | ## 1.2.0 (2018-07-18) 112 | 113 | - add support for multi worker 114 | 115 | ## 1.1.1 (2018-07-12) 116 | 117 | - if `record[@log_key]` is json, parse it as JSON and not as string. 118 | 119 | ## 1.1.0 (2018-06-29) 120 | 121 | - Add support for sending metrics. 122 | 123 | ## 1.0.3 (2018-05-07) 124 | 125 | - [Fix #26 -- Don't chomp if the message is not chomp-able](https://github.com/SumoLogic/fluentd-output-sumologic/pull/29) 126 | 127 | ## 1.0.2 (2018-04-09) 128 | 129 | - [add option to turn off adding timestamp to logs](https://github.com/SumoLogic/fluentd-output-sumologic/pull/27) 130 | 131 | ## 1.0.1 (2017-12-19) 132 | 133 | - [Add client header for fluentd output](https://github.com/SumoLogic/fluentd-output-sumologic/pull/22) 134 | 135 | ## 1.0.0 (2017-11-06) 136 | 137 | - [Upgrade to 0.14 API, send with ms precision](https://github.com/SumoLogic/fluentd-output-sumologic/pull/12) 138 | - [Switch to httpclient](https://github.com/SumoLogic/fluentd-output-sumologic/pull/16) 139 | - [Fix missing variable and improve config example](https://github.com/SumoLogic/fluentd-output-sumologic/pull/17) 140 | 141 | ## 0.0.7 (2017-10-26) 142 | 143 | - [Expand parameters in the output configuration](https://github.com/SumoLogic/fluentd-output-sumologic/pull/14) 144 | - [add open_timeout option](https://github.com/SumoLogic/fluentd-output-sumologic/pull/15) 145 | 146 | ## 0.0.6 (2017-08-23) 147 | 148 | - Fix 0.0.5 149 | 150 | ## 0.0.5 (2017-08-18) 151 | 152 | - [Ignore garbage records. Fix inspired by other plugins](https://github.com/SumoLogic/fluentd-output-sumologic/pull/7) 153 | - [Extract the source_name from FluentD's buffer](https://github.com/SumoLogic/fluentd-output-sumologic/pull/8) 154 | 155 | ## 0.0.4 (2017-07-05) 156 | 157 | - [Raise an exception for all non HTTP Success response codes](https://github.com/SumoLogic/fluentd-output-sumologic/pull/5) 158 | 159 | ## 0.0.3 (2016-12-10) 160 | 161 | - Initial Release 162 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/SumoLogic/fluentd-output-sumologic.svg?branch=master)](https://travis-ci.org/SumoLogic/fluentd-output-sumologic) [![Gem Version](https://badge.fury.io/rb/fluent-plugin-sumologic_output.svg)](https://badge.fury.io/rb/fluent-plugin-sumologic_output) ![](https://ruby-gem-downloads-badge.herokuapp.com/fluent-plugin-sumologic_output?type=total) [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/SumoLogic/fluentd-output-sumologic/issues) 2 | 3 | # fluent-plugin-sumologic_output, a plugin for [Fluentd](http://fluentd.org) 4 | 5 | This plugin has been designed to output logs or metrics to [SumoLogic](http://www.sumologic.com) via a [HTTP collector endpoint](http://help.sumologic.com/Send_Data/Sources/02Sources_for_Hosted_Collectors/HTTP_Source) 6 | 7 | ## License 8 | Released under Apache 2.0 License. 9 | 10 | ## Installation 11 | 12 | gem install fluent-plugin-sumologic_output 13 | 14 | ## Configuration 15 | 16 | Configuration options for fluent.conf are: 17 | 18 | * `data_type` - The type of data that will be sent to Sumo Logic, either `logs` or `metrics` (Default is `logs `) 19 | * `endpoint` - SumoLogic HTTP Collector URL 20 | * `verify_ssl` - Verify ssl certificate. (default is `true`) 21 | * `source_category`* - Set _sourceCategory metadata field within SumoLogic (default is `nil`) 22 | * `source_name`* - Set _sourceName metadata field within SumoLogic - overrides source_name_key (default is `nil`) 23 | * `source_name_key` - Set as source::path_key's value so that the source_name can be extracted from Fluentd's buffer (default `source_name`) 24 | * `source_host`* - Set _sourceHost metadata field within SumoLogic (default is `nil`) 25 | * `log_format` - Format to post logs into Sumo. (default `json`) 26 | * text - Logs will appear in SumoLogic in text format (taken from the field specified in `log_key`) 27 | * json - Logs will appear in SumoLogic in json format. 28 | * json_merge - Same as json but merge content of `log_key` into the top level and strip `log_key` 29 | * `log_key` - Used to specify the key when merging json or sending logs in text format (default `message`) 30 | * `open_timeout` - Set timeout seconds to wait until connection is opened. 31 | * `receieve_timeout` - Set timeout seconds to wait for a response from SumoLogic in seconds. Don't modify unless you see `HTTPClient::ReceiveTimeoutError` in your Fluentd logs. 32 | * `send_timeout` - Timeout for sending to SumoLogic in seconds. Don't modify unless you see `HTTPClient::SendTimeoutError` in your Fluentd logs. (default `120`) 33 | * `add_timestamp` - Add `timestamp` (or `timestamp_key`) field to logs before sending to sumologic (default `true`) 34 | * `timestamp_key` - Field name when `add_timestamp` is on (default `timestamp`) 35 | * `proxy_uri` - Add the `uri` of the `proxy` environment if present. 36 | * `metric_data_format` - The format of metrics you will be sending, either `graphite` or `carbon2` or `prometheus` (Default is `graphite `) 37 | * `disable_cookies` - Option to disable cookies on the HTTP Client. (Default is `false `) 38 | * `compress` - Option to enable compression (default `true`) 39 | * `compress_encoding` - Compression encoding format, either `gzip` or `deflate` (default `gzip`) 40 | * `custom_fields` - Comma-separated key=value list of fields to apply to every log. [more information](https://help.sumologic.com/Manage/Fields#http-source-fields) 41 | * `custom_dimensions` - Comma-separated key=value list of dimensions to apply to every metric. [more information](https://help.sumologic.com/03Send-Data/Sources/02Sources-for-Hosted-Collectors/HTTP-Source/Upload-Metrics-to-an-HTTP-Source#supported-http-headers) 42 | * `use_internal_retry` - Enable custom retry mechanism. As this is `false` by default due to backward compatibility, 43 | we recommend to enable it and configure the following parameters (`retry_min_interval`, `retry_max_interval`, `retry_timeout`, `retry_max_times`) 44 | * `retry_min_interval` - Minimum interval to wait between sending tries (default is `1s`) 45 | * `retry_max_interval` - Maximum interval to wait between sending tries (default is `5m`) 46 | * `retry_timeout` - Time after which the data is going to be dropped (default is `72h`) (`0s` means that there is no timeout) 47 | * `retry_max_times` - Maximum number of retries (default is `0`) (`0` means that there is no max retry times, retries will happen forever) 48 | * `max_request_size` - Maximum request size (before applying compression). Default is `0k` which means no limit 49 | 50 | __NOTE:__ * [Placeholders](https://docs.fluentd.org/v1.0/articles/buffer-section#placeholders) are supported 51 | 52 | ### Example Configuration 53 | Reading from the JSON formatted log files with `in_tail` and wildcard filenames: 54 | ``` 55 | 56 | @type tail 57 | format json 58 | time_key time 59 | path /path/to/*.log 60 | pos_file /path/to/pos/ggcp-app.log.pos 61 | time_format %Y-%m-%dT%H:%M:%S.%NZ 62 | tag appa.* 63 | read_from_head false 64 | 65 | 66 | 67 | @type sumologic 68 | endpoint https://collectors.sumologic.com/receiver/v1/http/XXXXXXXXXX 69 | log_format json 70 | source_category prod/someapp/logs 71 | source_name AppA 72 | open_timeout 10 73 | 74 | ``` 75 | 76 | Sending metrics to Sumo Logic using `in_http`: 77 | ``` 78 | 79 | @type http 80 | port 8888 81 | bind 0.0.0.0 82 | 83 | 84 | 85 | @type sumologic 86 | endpoint https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/ZaVnC4dhaV1hYfCAiqSH-PDY6gUOIgZvO60U_-y8SPQfK0Ks-ht7owrbk1AkX_ACp0uUxuLZOCw5QjBg1ndVPZ5TOJCFgNGRtFDoTDuQ2hzs3sn6FlfBSw== 87 | data_type metrics 88 | metric_data_format carbon2 89 | flush_interval 1s 90 | 91 | 92 | 93 | @type sumologic 94 | endpoint https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/ZaVnC4dhaV1hYfCAiqSH-PDY6gUOIgZvO60U_-y8SPQfK0Ks-ht7owrbk1AkX_ACp0uUxuLZOCw5QjBg1ndVPZ5TOJCFgNGRtFDoTDuQ2hzs3sn6FlfBSw== 95 | data_type metrics 96 | metric_data_format graphite 97 | flush_interval 1s 98 | 99 | ``` 100 | 101 | ## Example input/output 102 | 103 | Assuming following inputs are coming from a log file named `/var/log/appa_webserver.log` 104 | ``` 105 | {"asctime": "2016-12-10 03:56:35+0000", "levelname": "INFO", "name": "appa", "funcName": "do_something", "lineno": 29, "message": "processing something", "source_ip": "123.123.123.123"} 106 | ``` 107 | 108 | Then output becomes as below within SumoLogic 109 | ``` 110 | { 111 | "timestamp":1481343785000, 112 | "asctime":"2016-12-10 03:56:35+0000", 113 | "levelname":"INFO", 114 | "name":"appa", 115 | "funcName":"do_something", 116 | "lineno":29, 117 | "message":"processing something", 118 | "source_ip":"123.123.123.123" 119 | } 120 | ``` 121 | 122 | ## Dynamic Configuration within log message 123 | 124 | The plugin supports overriding SumoLogic metadata and log_format parameters within each log message by attaching the field `_sumo_metadata` to the log message. 125 | 126 | NOTE: The `_sumo_metadata` field will be stripped before posting to SumoLogic. 127 | 128 | Example 129 | 130 | ``` 131 | { 132 | "name": "appa", 133 | "source_ip": "123.123.123.123", 134 | "funcName": "do_something", 135 | "lineno": 29, 136 | "asctime": "2016-12-10 03:56:35+0000", 137 | "message": "processing something", 138 | "_sumo_metadata": { 139 | "category": "new_sourceCategory", 140 | "source": "override_sourceName", 141 | "host": "new_sourceHost", 142 | "log_format": "merge_json_log" 143 | }, 144 | "levelname": "INFO" 145 | } 146 | ``` 147 | 148 | ## Retry Mechanism 149 | 150 | `retry_min_interval`, `retry_max_interval`, `retry_timeout`, `retry_max_times` are not the [buffer retries parameters][buffer_retries]. 151 | Due to technical reason, this plugin implements it's own retrying back-off exponential mechanism. 152 | It is disabled by default, but we recommend to enable it by setting `use_internal_retry` to `true`. 153 | 154 | [buffer_retries]: https://docs.fluentd.org/configuration/buffer-section#retries-parameters 155 | 156 | ### TLS 1.2 Requirement 157 | 158 | Sumo Logic only accepts connections from clients using TLS version 1.2 or greater. To utilize the content of this repo, ensure that it's running in an execution environment that is configured to use TLS 1.2 or greater. 159 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /lib/fluent/plugin/out_sumologic.rb: -------------------------------------------------------------------------------- 1 | require 'fluent/plugin/output' 2 | require 'net/https' 3 | require 'json' 4 | require 'yajl' 5 | require 'httpclient' 6 | require 'zlib' 7 | require 'stringio' 8 | 9 | class SumologicConnection 10 | 11 | attr_reader :http 12 | 13 | COMPRESS_DEFLATE = 'deflate' 14 | COMPRESS_GZIP = 'gzip' 15 | 16 | def initialize(endpoint, verify_ssl, connect_timeout, receive_timeout, send_timeout, proxy_uri, disable_cookies, sumo_client, compress_enabled, compress_encoding, logger) 17 | @endpoint = endpoint 18 | @sumo_client = sumo_client 19 | create_http_client(verify_ssl, connect_timeout, receive_timeout, send_timeout, proxy_uri, disable_cookies) 20 | @compress = compress_enabled 21 | @compress_encoding = (compress_encoding ||= COMPRESS_GZIP).downcase 22 | @logger = logger 23 | 24 | unless [COMPRESS_DEFLATE, COMPRESS_GZIP].include? @compress_encoding 25 | raise "Invalid compression encoding #{@compress_encoding} must be gzip or deflate" 26 | end 27 | end 28 | 29 | def publish(raw_data, source_host=nil, source_category=nil, source_name=nil, data_type, metric_data_type, collected_fields, dimensions) 30 | response = http.post(@endpoint, compress(raw_data), request_headers(source_host, source_category, source_name, data_type, metric_data_type, collected_fields, dimensions)) 31 | unless response.ok? 32 | raise RuntimeError, "Failed to send data to HTTP Source. #{response.code} - #{response.body}" 33 | end 34 | 35 | # response is 20x, check response content 36 | return if response.content.length == 0 37 | 38 | # if we get a non-empty response, check it 39 | begin 40 | response_map = JSON.load(response.content) 41 | rescue JSON::ParserError 42 | @logger.warn "Error decoding receiver response: #{response.content}" 43 | return 44 | end 45 | 46 | # log a warning with the present keys 47 | response_keys = ["id", "code", "status", "message", "errors"] 48 | log_params = [] 49 | response_keys.each do |key| 50 | if response_map.has_key?(key) then 51 | value = response_map[key] 52 | log_params.append("#{key}: #{value}") 53 | end 54 | end 55 | log_params_str = log_params.join(", ") 56 | @logger.warn "There was an issue sending data: #{log_params_str}" 57 | end 58 | 59 | def request_headers(source_host, source_category, source_name, data_type, metric_data_format, collected_fields, dimensions) 60 | headers = { 61 | 'X-Sumo-Name' => source_name, 62 | 'X-Sumo-Category' => source_category, 63 | 'X-Sumo-Host' => source_host, 64 | 'X-Sumo-Client' => @sumo_client, 65 | } 66 | 67 | if @compress 68 | headers['Content-Encoding'] = @compress_encoding 69 | end 70 | 71 | if data_type == 'metrics' 72 | case metric_data_format 73 | when 'graphite' 74 | headers['Content-Type'] = 'application/vnd.sumologic.graphite' 75 | when 'carbon2' 76 | headers['Content-Type'] = 'application/vnd.sumologic.carbon2' 77 | when 'prometheus' 78 | headers['Content-Type'] = 'application/vnd.sumologic.prometheus' 79 | else 80 | raise RuntimeError, "Invalid #{metric_data_format}, must be graphite or carbon2 or prometheus" 81 | end 82 | 83 | unless dimensions.nil? 84 | headers['X-Sumo-Dimensions'] = dimensions 85 | end 86 | end 87 | unless collected_fields.nil? 88 | headers['X-Sumo-Fields'] = collected_fields 89 | end 90 | return headers 91 | end 92 | 93 | def ssl_options(verify_ssl) 94 | verify_ssl==true ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE 95 | end 96 | 97 | def create_http_client(verify_ssl, connect_timeout, receive_timeout, send_timeout, proxy_uri, disable_cookies) 98 | @http = HTTPClient.new(proxy_uri) 99 | @http.ssl_config.verify_mode = ssl_options(verify_ssl) 100 | @http.connect_timeout = connect_timeout 101 | @http.receive_timeout = receive_timeout 102 | @http.send_timeout = send_timeout 103 | if disable_cookies 104 | @http.cookie_manager = nil 105 | end 106 | end 107 | 108 | def compress(content) 109 | if @compress 110 | if @compress_encoding == COMPRESS_GZIP 111 | result = gzip(content) 112 | result.bytes.to_a.pack("c*") 113 | else 114 | Zlib::Deflate.deflate(content) 115 | end 116 | else 117 | content 118 | end 119 | end # def compress 120 | 121 | def gzip(content) 122 | stream = StringIO.new("w") 123 | stream.set_encoding("ASCII") 124 | gz = Zlib::GzipWriter.new(stream) 125 | gz.mtime=1 # Ensure that for same content there is same output 126 | gz.write(content) 127 | gz.close 128 | stream.string.bytes.to_a.pack("c*") 129 | end # def gzip 130 | end 131 | 132 | class Fluent::Plugin::Sumologic < Fluent::Plugin::Output 133 | # First, register the plugin. NAME is the name of this plugin 134 | # and identifies the plugin in the configuration file. 135 | Fluent::Plugin.register_output('sumologic', self) 136 | 137 | helpers :compat_parameters 138 | DEFAULT_BUFFER_TYPE = "memory" 139 | LOGS_DATA_TYPE = "logs" 140 | METRICS_DATA_TYPE = "metrics" 141 | DEFAULT_DATA_TYPE = LOGS_DATA_TYPE 142 | DEFAULT_METRIC_FORMAT_TYPE = 'graphite' 143 | 144 | config_param :data_type, :string, :default => DEFAULT_DATA_TYPE 145 | config_param :metric_data_format, :default => DEFAULT_METRIC_FORMAT_TYPE 146 | config_param :endpoint, :string, secret: true 147 | config_param :log_format, :string, :default => 'json' 148 | config_param :log_key, :string, :default => 'message' 149 | config_param :source_category, :string, :default => nil 150 | config_param :source_name, :string, :default => nil 151 | config_param :source_name_key, :string, :default => 'source_name' 152 | config_param :source_host, :string, :default => nil 153 | config_param :verify_ssl, :bool, :default => true 154 | config_param :delimiter, :string, :default => "." 155 | config_param :open_timeout, :integer, :default => 60 156 | config_param :receive_timeout, :integer, :default => 60 157 | config_param :send_timeout, :integer, :default => 120 158 | config_param :add_timestamp, :bool, :default => true 159 | config_param :timestamp_key, :string, :default => 'timestamp' 160 | config_param :proxy_uri, :string, :default => nil 161 | config_param :disable_cookies, :bool, :default => false 162 | 163 | config_param :use_internal_retry, :bool, :default => false 164 | config_param :retry_timeout, :time, :default => 72 * 3600 # 72h 165 | config_param :retry_max_times, :integer, :default => 0 166 | config_param :retry_min_interval, :time, :default => 1 # 1s 167 | config_param :retry_max_interval, :time, :default => 5*60 # 5m 168 | 169 | config_param :max_request_size, :size, :default => 0 170 | 171 | # https://help.sumologic.com/Manage/Fields 172 | desc 'Fields string (eg "cluster=payment, service=credit_card") which is going to be added to every log record.' 173 | config_param :custom_fields, :string, :default => nil 174 | desc 'Name of sumo client which is send as X-Sumo-Client header' 175 | config_param :sumo_client, :string, :default => 'fluentd-output' 176 | desc 'Compress payload' 177 | config_param :compress, :bool, :default => true 178 | desc 'Encoding method of compresssion (either gzip or deflate)' 179 | config_param :compress_encoding, :string, :default => SumologicConnection::COMPRESS_GZIP 180 | # https://help.sumologic.com/03Send-Data/Sources/02Sources-for-Hosted-Collectors/HTTP-Source/Upload-Metrics-to-an-HTTP-Source#supported-http-headers 181 | desc 'Dimensions string (eg "cluster=payment, service=credit_card") which is going to be added to every metric record.' 182 | config_param :custom_dimensions, :string, :default => nil 183 | 184 | config_section :buffer do 185 | config_set_default :@type, DEFAULT_BUFFER_TYPE 186 | config_set_default :chunk_keys, ['tag'] 187 | end 188 | 189 | def initialize 190 | super 191 | end 192 | 193 | def multi_workers_ready? 194 | true 195 | end 196 | 197 | # This method is called before starting. 198 | def configure(conf) 199 | 200 | compat_parameters_convert(conf, :buffer) 201 | super 202 | 203 | unless @endpoint =~ URI::regexp 204 | raise Fluent::ConfigError, "Invalid SumoLogic endpoint url: #{@endpoint}" 205 | end 206 | 207 | unless @data_type =~ /\A(?:logs|metrics)\z/ 208 | raise Fluent::ConfigError, "Invalid data_type #{@data_type} must be logs or metrics" 209 | end 210 | 211 | if @data_type == LOGS_DATA_TYPE 212 | unless @log_format =~ /\A(?:json|text|json_merge|fields)\z/ 213 | raise Fluent::ConfigError, "Invalid log_format #{@log_format} must be text, json, json_merge or fields" 214 | end 215 | end 216 | 217 | if @data_type == METRICS_DATA_TYPE 218 | unless @metric_data_format =~ /\A(?:graphite|carbon2|prometheus)\z/ 219 | raise Fluent::ConfigError, "Invalid metric_data_format #{@metric_data_format} must be graphite or carbon2 or prometheus" 220 | end 221 | end 222 | 223 | @custom_fields = validate_key_value_pairs(@custom_fields) 224 | if @custom_fields 225 | @log.debug "Custom fields: #{@custom_fields}" 226 | end 227 | 228 | @custom_dimensions = validate_key_value_pairs(@custom_dimensions) 229 | if @custom_dimensions 230 | @log.debug "Custom dimensions: #{@custom_dimensions}" 231 | end 232 | 233 | @sumo_conn = SumologicConnection.new( 234 | @endpoint, 235 | @verify_ssl, 236 | @open_timeout, 237 | @receive_timeout, 238 | @send_timeout, 239 | @proxy_uri, 240 | @disable_cookies, 241 | @sumo_client, 242 | @compress, 243 | @compress_encoding, 244 | @log, 245 | ) 246 | end 247 | 248 | # This method is called when starting. 249 | def start 250 | super 251 | end 252 | 253 | # This method is called when shutting down. 254 | def shutdown 255 | super 256 | end 257 | 258 | # Used to merge log record into top level json 259 | def merge_json(record) 260 | if record.has_key?(@log_key) 261 | log = record[@log_key].strip 262 | if log[0].eql?('{') && log[-1].eql?('}') 263 | begin 264 | record = record.merge(JSON.parse(log)) 265 | record.delete(@log_key) 266 | rescue JSON::ParserError 267 | # do nothing, ignore 268 | end 269 | end 270 | end 271 | record 272 | end 273 | 274 | # Strip sumo_metadata and dump to json 275 | def dump_log(log) 276 | log.delete('_sumo_metadata') 277 | begin 278 | hash = JSON.parse(log[@log_key]) 279 | log[@log_key] = hash 280 | Yajl.dump(log) 281 | rescue 282 | Yajl.dump(log) 283 | end 284 | end 285 | 286 | def format(tag, time, record) 287 | if defined? time.nsec 288 | mstime = time * 1000 + (time.nsec / 1000000) 289 | [mstime, record].to_msgpack 290 | else 291 | [time, record].to_msgpack 292 | end 293 | end 294 | 295 | def formatted_to_msgpack_binary 296 | true 297 | end 298 | 299 | def sumo_key(sumo_metadata, chunk) 300 | source_name = sumo_metadata['source'] || @source_name 301 | source_name = extract_placeholders(source_name, chunk) unless source_name.nil? 302 | 303 | source_category = sumo_metadata['category'] || @source_category 304 | source_category = extract_placeholders(source_category, chunk) unless source_category.nil? 305 | 306 | source_host = sumo_metadata['host'] || @source_host 307 | source_host = extract_placeholders(source_host, chunk) unless source_host.nil? 308 | 309 | fields = sumo_metadata['fields'] || "" 310 | fields = extract_placeholders(fields, chunk) unless fields.nil? 311 | 312 | { :source_name => "#{source_name}", :source_category => "#{source_category}", 313 | :source_host => "#{source_host}", :fields => "#{fields}" } 314 | end 315 | 316 | # Convert timestamp to 13 digit epoch if necessary 317 | def sumo_timestamp(time) 318 | time.to_s.length == 13 ? time : time * 1000 319 | end 320 | 321 | # Convert log to string and strip it 322 | def log_to_str(log) 323 | if log.is_a?(Array) or log.is_a?(Hash) 324 | log = Yajl.dump(log) 325 | end 326 | 327 | unless log.nil? 328 | log.strip! 329 | end 330 | 331 | return log 332 | end 333 | 334 | # This method is called every flush interval. Write the buffer chunk 335 | def write(chunk) 336 | messages_list = {} 337 | 338 | # Sort messages 339 | chunk.msgpack_each do |time, record| 340 | # plugin dies randomly 341 | # https://github.com/uken/fluent-plugin-elasticsearch/commit/8597b5d1faf34dd1f1523bfec45852d380b26601#diff-ae62a005780cc730c558e3e4f47cc544R94 342 | next unless record.is_a? Hash 343 | sumo_metadata = record.fetch('_sumo_metadata', {:source => record[@source_name_key] }) 344 | key = sumo_key(sumo_metadata, chunk) 345 | log_format = sumo_metadata['log_format'] || @log_format 346 | 347 | # Strip any unwanted newlines 348 | record[@log_key].chomp! if record[@log_key] && record[@log_key].respond_to?(:chomp!) 349 | 350 | case @data_type 351 | when 'logs' 352 | case log_format 353 | when 'text' 354 | if !record.has_key?(@log_key) 355 | log.warn "log key `#{@log_key}` has not been found in the log" 356 | end 357 | log = log_to_str(record[@log_key]) 358 | when 'json_merge' 359 | if @add_timestamp 360 | record = { @timestamp_key => sumo_timestamp(time) }.merge(record) 361 | end 362 | log = dump_log(merge_json(record)) 363 | when 'fields' 364 | if @add_timestamp 365 | record = { @timestamp_key => sumo_timestamp(time) }.merge(record) 366 | end 367 | log = dump_log(record) 368 | else 369 | if @add_timestamp 370 | record = { @timestamp_key => sumo_timestamp(time) }.merge(record) 371 | end 372 | log = dump_log(record) 373 | end 374 | when 'metrics' 375 | log = log_to_str(record[@log_key]) 376 | end 377 | 378 | unless log.nil? 379 | if messages_list.key?(key) 380 | messages_list[key].push(log) 381 | else 382 | messages_list[key] = [log] 383 | end 384 | end 385 | 386 | end 387 | 388 | chunk_id = "##{chunk.dump_unique_id_hex(chunk.unique_id)}" 389 | # Push logs to sumo 390 | messages_list.each do |key, messages| 391 | source_name, source_category, source_host, fields = key[:source_name], key[:source_category], 392 | key[:source_host], key[:fields] 393 | 394 | # Merge custom and record fields 395 | if fields.nil? || fields.strip.length == 0 396 | fields = @custom_fields 397 | else 398 | fields = [fields,@custom_fields].compact.join(",") 399 | end 400 | 401 | if @max_request_size <= 0 402 | messages_to_send = [messages] 403 | else 404 | messages_to_send = [] 405 | current_message = [] 406 | current_length = 0 407 | messages.each do |message| 408 | current_message.push message 409 | current_length += message.length 410 | 411 | if current_length > @max_request_size 412 | messages_to_send.push(current_message) 413 | current_message = [] 414 | current_length = 0 415 | end 416 | current_length += 1 # this is for newline 417 | end 418 | if current_message.length > 0 419 | messages_to_send.push(current_message) 420 | end 421 | end 422 | 423 | messages_to_send.each_with_index do |message, i| 424 | retries = 0 425 | start_time = Time.now 426 | sleep_time = @retry_min_interval 427 | 428 | while true 429 | common_log_part = "#{@data_type} records with source category '#{source_category}', source host '#{source_host}', source name '#{source_name}', chunk #{chunk_id}, try #{retries}, batch #{i}" 430 | 431 | begin 432 | @log.debug { "Sending #{message.count}; #{common_log_part}" } 433 | 434 | @sumo_conn.publish( 435 | message.join("\n"), 436 | source_host =source_host, 437 | source_category =source_category, 438 | source_name =source_name, 439 | data_type =@data_type, 440 | metric_data_format =@metric_data_format, 441 | collected_fields =fields, 442 | dimensions =@custom_dimensions 443 | ) 444 | break 445 | rescue => e 446 | if !@use_internal_retry 447 | raise e 448 | end 449 | # increment retries 450 | retries += 1 451 | 452 | log.warn "error while sending request to sumo: #{e}; #{common_log_part}" 453 | log.warn_backtrace e.backtrace 454 | 455 | # drop data if 456 | # - we reached out the @retry_max_times retries 457 | # - or we exceeded @retry_timeout 458 | if (retries >= @retry_max_times && @retry_max_times > 0) || (Time.now > start_time + @retry_timeout && @retry_timeout > 0) 459 | log.warn "dropping records; #{common_log_part}" 460 | break 461 | end 462 | 463 | log.info "going to retry to send data at #{Time.now + sleep_time}; #{common_log_part}" 464 | sleep sleep_time 465 | 466 | sleep_time *= 2 467 | if sleep_time > @retry_max_interval 468 | sleep_time = @retry_max_interval 469 | end 470 | end 471 | end 472 | end 473 | end 474 | 475 | end 476 | 477 | def validate_key_value_pairs(fields) 478 | if fields.nil? 479 | return fields 480 | end 481 | 482 | fields = fields.split(",").select { |field| 483 | field.split('=').length == 2 484 | } 485 | 486 | if fields.length == 0 487 | return nil 488 | end 489 | 490 | fields.join(',') 491 | end 492 | end 493 | -------------------------------------------------------------------------------- /test/plugin/test_out_sumologic.rb: -------------------------------------------------------------------------------- 1 | require "test-unit" 2 | require "fluent/test" 3 | require "fluent/test/driver/output" 4 | require "fluent/test/helpers" 5 | require 'webmock/test_unit' 6 | require 'fluent/plugin/out_sumologic' 7 | 8 | class SumologicOutput < Test::Unit::TestCase 9 | include Fluent::Test::Helpers 10 | 11 | def setup 12 | Fluent::Test.setup 13 | end 14 | 15 | def create_driver(conf = CONFIG) 16 | Fluent::Test::Driver::Output.new(Fluent::Plugin::Sumologic).configure(conf) 17 | end 18 | 19 | def test_no_endpoint_configure 20 | config = %{} 21 | exception = assert_raise(Fluent::ConfigError) {create_driver(config)} 22 | assert_equal("'endpoint' parameter is required", exception.message) 23 | end 24 | 25 | def test_invalid_endpoint 26 | config = %{ 27 | endpoint Not-a-URL 28 | } 29 | exception = assert_raise(Fluent::ConfigError) {create_driver(config)} 30 | assert_equal("Invalid SumoLogic endpoint url: Not-a-URL", exception.message) 31 | end 32 | 33 | def test_invalid_data_type_configure 34 | config = %{ 35 | endpoint https://SUMOLOGIC_URL 36 | data_type 'foo' 37 | } 38 | exception = assert_raise(Fluent::ConfigError) {create_driver(config)} 39 | assert_equal("Invalid data_type foo must be logs or metrics", exception.message) 40 | end 41 | 42 | def test_invalid_log_format_configure 43 | config = %{ 44 | endpoint https://SUMOLOGIC_URL 45 | log_format foo 46 | } 47 | exception = assert_raise(Fluent::ConfigError) {create_driver(config)} 48 | assert_equal("Invalid log_format foo must be text, json, json_merge or fields", exception.message) 49 | end 50 | 51 | def test_invalid_metric_data_format 52 | config = %{ 53 | endpoint https://SUMOLOGIC_URL 54 | data_type metrics 55 | metric_data_format foo 56 | } 57 | exception = assert_raise(Fluent::ConfigError) {create_driver(config)} 58 | assert_equal("Invalid metric_data_format foo must be graphite or carbon2 or prometheus", exception.message) 59 | end 60 | 61 | def test_default_configure 62 | config = %{ 63 | endpoint https://SUMOLOGIC_URL 64 | } 65 | instance = create_driver(config).instance 66 | 67 | assert_equal instance.data_type, 'logs' 68 | assert_equal instance.metric_data_format, 'graphite' 69 | assert_equal instance.endpoint, 'https://SUMOLOGIC_URL' 70 | assert_equal instance.log_format, 'json' 71 | assert_equal instance.log_key, 'message' 72 | assert_equal instance.source_category, nil 73 | assert_equal instance.source_name, nil 74 | assert_equal instance.source_name_key, 'source_name' 75 | assert_equal instance.source_host, nil 76 | assert_equal instance.verify_ssl, true 77 | assert_equal instance.delimiter, '.' 78 | assert_equal instance.open_timeout, 60 79 | assert_equal instance.add_timestamp, true 80 | assert_equal instance.timestamp_key, 'timestamp' 81 | assert_equal instance.proxy_uri, nil 82 | assert_equal instance.disable_cookies, false 83 | assert_equal instance.sumo_client, 'fluentd-output' 84 | assert_equal instance.compress, true 85 | assert_equal instance.compress_encoding, 'gzip' 86 | end 87 | 88 | def test_emit_text 89 | config = %{ 90 | compress false 91 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 92 | log_format text 93 | source_category test 94 | source_host test 95 | source_name test 96 | 97 | } 98 | driver = create_driver(config) 99 | time = event_time 100 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 101 | driver.run do 102 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => 'test'}) 103 | end 104 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 105 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 106 | body: "test", 107 | times:1 108 | end 109 | 110 | def test_emit_text_custom_sumo_client 111 | config = %{ 112 | compress false 113 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 114 | log_format text 115 | source_category test 116 | source_host test 117 | source_name test 118 | sumo_client 'fluentd-custom-sender' 119 | 120 | } 121 | driver = create_driver(config) 122 | time = event_time 123 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 124 | driver.run do 125 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => 'test'}) 126 | end 127 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 128 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-custom-sender', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 129 | body: "test", 130 | times:1 131 | end 132 | 133 | def test_emit_json 134 | config = %{ 135 | compress false 136 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 137 | log_format json 138 | source_category test 139 | source_host test 140 | source_name test 141 | 142 | } 143 | driver = create_driver(config) 144 | time = event_time 145 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 146 | driver.run do 147 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => 'test'}) 148 | end 149 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 150 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 151 | body: /\A{"timestamp":\d+.,"foo":"bar","message":"test"}\z/, 152 | times:1 153 | end 154 | 155 | def test_emit_empty_fields 156 | config = %{ 157 | compress false 158 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 159 | log_format fields 160 | source_category test 161 | source_host test 162 | source_name test 163 | 164 | } 165 | driver = create_driver(config) 166 | time = event_time 167 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 168 | driver.run do 169 | driver.feed("output.test", time, {'message' => 'test'}) 170 | end 171 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 172 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 173 | body: /\A{"timestamp":\d+.,"message":"test"}\z/, 174 | times:1 175 | end 176 | 177 | def test_emit_json_double_encoded 178 | config = %{ 179 | compress false 180 | endpoint https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/1234 181 | log_format json 182 | source_category test 183 | source_host test 184 | source_name test 185 | 186 | } 187 | driver = create_driver(config) 188 | time = event_time 189 | stub_request(:post, 'https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/1234') 190 | driver.run do 191 | driver.feed("output.test", time, {'message' => '{"bar":"foo"}'}) 192 | end 193 | assert_requested :post, "https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/1234", 194 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 195 | body: /\A{"timestamp":\d+.,"message":{"bar":"foo"}}\z/, 196 | times:1 197 | end 198 | 199 | def test_emit_text_format_as_json 200 | config = %{ 201 | compress false 202 | endpoint https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/1234 203 | log_format json 204 | source_category test 205 | source_host test 206 | source_name test 207 | 208 | } 209 | driver = create_driver(config) 210 | time = event_time 211 | stub_request(:post, 'https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/1234') 212 | driver.run do 213 | driver.feed("output.test", time, {'message' => 'some message'}) 214 | end 215 | assert_requested :post, "https://endpoint3.collection.us2.sumologic.com/receiver/v1/http/1234", 216 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 217 | body: /\A{"timestamp":\d+.,"message":"some message"}\z/, 218 | times:1 219 | end 220 | 221 | def test_emit_json_merge 222 | config = %{ 223 | compress false 224 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 225 | log_format json_merge 226 | source_category test 227 | source_host test 228 | source_name test 229 | 230 | } 231 | driver = create_driver(config) 232 | time = event_time 233 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 234 | driver.run do 235 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => '{"foo2":"bar2"}'}) 236 | end 237 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 238 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 239 | body: /\A{"timestamp":\d+,"foo":"bar","foo2":"bar2"}\z/, 240 | times:1 241 | end 242 | 243 | def test_emit_json_merge_timestamp 244 | config = %{ 245 | compress false 246 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 247 | log_format json_merge 248 | source_category test 249 | source_host test 250 | source_name test 251 | 252 | } 253 | driver = create_driver(config) 254 | time = event_time 255 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 256 | driver.run do 257 | driver.feed("output.test", time, {'message' => '{"timestamp":123}'}) 258 | end 259 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 260 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 261 | body: /\A{"timestamp":123}\z/, 262 | times:1 263 | end 264 | 265 | def test_emit_with_sumo_metadata_with_fields_json_format 266 | config = %{ 267 | compress false 268 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 269 | log_format json 270 | } 271 | driver = create_driver(config) 272 | time = event_time 273 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 274 | ENV['HOST'] = "foo" 275 | driver.run do 276 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => 'test', '_sumo_metadata' => { 277 | "host": "#{ENV['HOST']}", 278 | "source": "${tag}", 279 | "category": "test", 280 | "fields": "foo=bar, sumo = logic" 281 | }}) 282 | end 283 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 284 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test'}, 285 | body: /\A{"timestamp":\d+.,"foo":"bar","message":"test"}\z/, 286 | times:1 287 | end 288 | 289 | def test_emit_with_sumo_metadata_with_fields_fields_format 290 | config = %{ 291 | compress false 292 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 293 | log_format fields 294 | } 295 | driver = create_driver(config) 296 | time = event_time 297 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 298 | ENV['HOST'] = "foo" 299 | driver.run do 300 | driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => { 301 | "host": "#{ENV['HOST']}", 302 | "source": "${tag}", 303 | "category": "test", 304 | "fields": "foo=bar, sumo = logic" 305 | }}) 306 | end 307 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 308 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'foo=bar, sumo = logic'}, 309 | body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/, 310 | times:1 311 | end 312 | 313 | def test_emit_with_sumo_metadata_with_fields_and_custom_fields_fields_format 314 | config = %{ 315 | compress false 316 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 317 | log_format fields 318 | custom_fields "lorem=ipsum,dolor=amet" 319 | } 320 | driver = create_driver(config) 321 | time = event_time 322 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 323 | ENV['HOST'] = "foo" 324 | driver.run do 325 | driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => { 326 | "host": "#{ENV['HOST']}", 327 | "source": "${tag}", 328 | "category": "test", 329 | "fields": "foo=bar, sumo = logic" 330 | }}) 331 | end 332 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 333 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'foo=bar, sumo = logic,lorem=ipsum,dolor=amet'}, 334 | body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/, 335 | times:1 336 | end 337 | 338 | def test_emit_with_sumo_metadata_with_fields_and_empty_custom_fields_fields_format 339 | config = %{ 340 | compress false 341 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 342 | log_format fields 343 | custom_fields "" 344 | } 345 | driver = create_driver(config) 346 | time = event_time 347 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 348 | ENV['HOST'] = "foo" 349 | driver.run do 350 | driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => { 351 | "host": "#{ENV['HOST']}", 352 | "source": "${tag}", 353 | "category": "test", 354 | "fields": "foo=bar, sumo = logic" 355 | }}) 356 | end 357 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 358 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'foo=bar, sumo = logic'}, 359 | body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/, 360 | times:1 361 | end 362 | 363 | def test_emit_with_sumo_metadata_with_empty_fields_and_custom_fields_fields_format 364 | config = %{ 365 | compress false 366 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 367 | log_format fields 368 | custom_fields "lorem=ipsum,invalid" 369 | } 370 | driver = create_driver(config) 371 | time = event_time 372 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 373 | ENV['HOST'] = "foo" 374 | driver.run do 375 | driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => { 376 | "host": "#{ENV['HOST']}", 377 | "source": "${tag}", 378 | "category": "test", 379 | "fields": "" 380 | }}) 381 | end 382 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 383 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'lorem=ipsum'}, 384 | body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/, 385 | times:1 386 | end 387 | 388 | def test_emit_with_sumo_metadata 389 | config = %{ 390 | compress false 391 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 392 | log_format json 393 | } 394 | driver = create_driver(config) 395 | time = event_time 396 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 397 | ENV['HOST'] = "foo" 398 | driver.run do 399 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => 'test', '_sumo_metadata' => { 400 | "host": "#{ENV['HOST']}", 401 | "source": "${tag}", 402 | "category": "${tag[1]}" 403 | }}) 404 | end 405 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 406 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test'}, 407 | body: /\A{"timestamp":\d+.,"foo":"bar","message":"test"}\z/, 408 | times:1 409 | end 410 | 411 | def test_emit_json_no_timestamp 412 | config = %{ 413 | compress false 414 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 415 | log_format json 416 | source_category test 417 | source_host test 418 | source_name test 419 | add_timestamp false 420 | } 421 | driver = create_driver(config) 422 | time = event_time 423 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 424 | driver.run do 425 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => 'test'}) 426 | end 427 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 428 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 429 | body: /\A{"foo":"bar","message":"test"}\z/, 430 | times:1 431 | end 432 | 433 | def test_emit_json_timestamp_key 434 | config = %{ 435 | compress false 436 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 437 | log_format json 438 | source_category test 439 | source_host test 440 | source_name test 441 | timestamp_key ts 442 | } 443 | driver = create_driver(config) 444 | time = event_time 445 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 446 | driver.run do 447 | driver.feed("output.test", time, {'message' => 'test'}) 448 | end 449 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 450 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 451 | body: /\A{"ts":\d+.,"message":"test"}\z/, 452 | times:1 453 | end 454 | 455 | def test_emit_graphite 456 | config = %{ 457 | compress false 458 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 459 | data_type metrics 460 | metric_data_format graphite 461 | source_category test 462 | source_host test 463 | source_name test 464 | } 465 | driver = create_driver(config) 466 | time = event_time 467 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 468 | driver.run do 469 | driver.feed("output.test", time, {'message' =>'prod.lb-1.cpu 87.2 1501753030'}) 470 | end 471 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 472 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'Content-Type'=>'application/vnd.sumologic.graphite'}, 473 | body: /\Aprod.lb-1.cpu 87.2 1501753030\z/, 474 | times:1 475 | end 476 | 477 | def test_emit_carbon 478 | config = %{ 479 | compress false 480 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 481 | data_type metrics 482 | metric_data_format carbon2 483 | source_category test 484 | source_host test 485 | source_name test 486 | } 487 | driver = create_driver(config) 488 | time = event_time 489 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 490 | driver.run do 491 | driver.feed("output.test", time, {'message' =>'cluster=prod node=lb-1 metric=cpu ip=2.2.3.4 team=infra 87.2 1501753030'}) 492 | end 493 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 494 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'Content-Type'=>'application/vnd.sumologic.carbon2'}, 495 | body: /\Acluster=prod node=lb-1 metric=cpu ip=2.2.3.4 team=infra 87.2 1501753030\z/, 496 | times:1 497 | end 498 | 499 | def test_emit_prometheus 500 | config = %{ 501 | compress false 502 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 503 | data_type metrics 504 | metric_data_format prometheus 505 | source_category test 506 | source_host test 507 | source_name test 508 | } 509 | driver = create_driver(config) 510 | time = event_time 511 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 512 | driver.run do 513 | driver.feed("output.test", time, {'message' =>'cpu{cluster="prod", node="lb-1"} 87.2 1501753030'}) 514 | end 515 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 516 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'Content-Type'=>'application/vnd.sumologic.prometheus'}, 517 | body: 'cpu{cluster="prod", node="lb-1"} 87.2 1501753030', 518 | times:1 519 | end 520 | 521 | def test_emit_prometheus_with_custom_dimensions 522 | config = %{ 523 | compress false 524 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 525 | data_type metrics 526 | metric_data_format prometheus 527 | source_category test 528 | source_host test 529 | source_name test 530 | custom_dimensions 'foo=bar, dolor=sit,amet,test' 531 | } 532 | driver = create_driver(config) 533 | time = event_time 534 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 535 | driver.run do 536 | driver.feed("output.test", time, {'message' =>'cpu{cluster="prod", node="lb-1"} 87.2 1501753030'}) 537 | end 538 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 539 | headers: { 540 | 'X-Sumo-Category'=>'test', 541 | 'X-Sumo-Client'=>'fluentd-output', 542 | 'X-Sumo-Host'=>'test', 543 | 'X-Sumo-Name'=>'test', 544 | 'X-Sumo-Dimensions'=>'foo=bar, dolor=sit', 545 | 'Content-Type'=>'application/vnd.sumologic.prometheus'}, 546 | body: 'cpu{cluster="prod", node="lb-1"} 87.2 1501753030', 547 | times:1 548 | end 549 | 550 | def test_emit_prometheus_with_empty_custom_metadata 551 | config = %{ 552 | compress false 553 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 554 | data_type metrics 555 | metric_data_format prometheus 556 | source_category test 557 | source_host test 558 | source_name test 559 | custom_metadata " " 560 | } 561 | driver = create_driver(config) 562 | time = event_time 563 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 564 | driver.run do 565 | driver.feed("output.test", time, {'message' =>'cpu{cluster="prod", node="lb-1"} 87.2 1501753030'}) 566 | end 567 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 568 | headers: { 569 | 'X-Sumo-Category'=>'test', 570 | 'X-Sumo-Client'=>'fluentd-output', 571 | 'X-Sumo-Host'=>'test', 572 | 'X-Sumo-Name'=>'test', 573 | 'Content-Type'=>'application/vnd.sumologic.prometheus'}, 574 | body: 'cpu{cluster="prod", node="lb-1"} 87.2 1501753030', 575 | times:1 576 | end 577 | 578 | def test_batching_same_headers 579 | config = %{ 580 | compress false 581 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 582 | log_format json 583 | source_category test 584 | source_host test 585 | source_name test 586 | } 587 | driver = create_driver(config) 588 | time = event_time 589 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 590 | driver.run do 591 | driver.feed("output.test", time, {'message' => 'test1'}) 592 | driver.feed("output.test", time, {'message' => 'test2'}) 593 | end 594 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 595 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 596 | body: /\A{"timestamp":\d+.,"message":"test1"}\n{"timestamp":\d+.,"message":"test2"}\z/, 597 | times:1 598 | end 599 | 600 | def test_batching_different_headers 601 | config = %{ 602 | compress false 603 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 604 | log_format json 605 | source_category test 606 | source_host test 607 | source_name test 608 | } 609 | driver = create_driver(config) 610 | time = event_time 611 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 612 | driver.run do 613 | driver.feed("output.test", time, {'message' => 'test1', '_sumo_metadata' => {"category": "cat1"}}) 614 | driver.feed("output.test", time, {'message' => 'test2', '_sumo_metadata' => {"category": "cat2"}}) 615 | end 616 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 617 | headers: {'X-Sumo-Category'=>'cat1', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 618 | body: /\A{"timestamp":\d+.,"message":"test1"}\z/, 619 | times:1 620 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 621 | headers: {'X-Sumo-Category'=>'cat2', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 622 | body: /\A{"timestamp":\d+.,"message":"test2"}\z/, 623 | times:1 624 | end 625 | 626 | def test_batching_different_fields 627 | config = %{ 628 | compress false 629 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 630 | log_format fields 631 | source_category test 632 | source_host test 633 | source_name test 634 | } 635 | driver = create_driver(config) 636 | time = event_time 637 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 638 | driver.run do 639 | driver.feed("output.test", time, {'message' => 'test1'}) 640 | driver.feed("output.test", time, {'message' => 'test2', '_sumo_metadata' => {"fields": "foo=bar"}}) 641 | driver.feed("output.test", time, {'message' => 'test3', '_sumo_metadata' => {"fields": "foo=bar,sumo=logic"}}) 642 | driver.feed("output.test", time, {'message' => 'test4', '_sumo_metadata' => {"fields": "foo=bar,master_url=https://100.64.0.1:443"}}) 643 | end 644 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 645 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 646 | body: /\A{"timestamp":\d+.,"message":"test1"}\z/, 647 | times:1 648 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 649 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'X-Sumo-Fields' => 'foo=bar'}, 650 | body: /\A{"timestamp":\d+.,"message":"test2"}\z/, 651 | times:1 652 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 653 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'X-Sumo-Fields' => 'foo=bar,sumo=logic'}, 654 | body: /\A{"timestamp":\d+.,"message":"test3"}\z/, 655 | times:1 656 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 657 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'X-Sumo-Fields' => 'foo=bar,master_url=https://100.64.0.1:443'}, 658 | body: /\A{"timestamp":\d+.,"message":"test4"}\z/, 659 | times:1 660 | end 661 | 662 | def test_emit_json_merge_timestamp_compress_deflate 663 | config = %{ 664 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 665 | log_format json_merge 666 | source_category test 667 | source_host test 668 | source_name test 669 | compress true 670 | compress_encoding deflate 671 | } 672 | driver = create_driver(config) 673 | time = event_time 674 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 675 | driver.run do 676 | driver.feed("output.test", time, {'message' => '{"timestamp":123}'}) 677 | end 678 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 679 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'Content-Encoding'=>'deflate'}, 680 | body: "\x78\x9c\xab\x56\x2a\xc9\xcc\x4d\x2d\x2e\x49\xcc\x2d\x50\xb2\x32\x34\x32\xae\x05\x00\x38\xb0\x05\xe1".force_encoding("ASCII-8BIT"), 681 | times:1 682 | end 683 | 684 | def test_emit_json_merge_timestamp_compress_gzip 685 | config = %{ 686 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 687 | log_format json_merge 688 | source_category test 689 | source_host test 690 | source_name test 691 | compress true 692 | } 693 | driver = create_driver(config) 694 | time = event_time 695 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 696 | driver.run do 697 | driver.feed("output.test", time, {'message' => '{"timestamp":1234}'}) 698 | end 699 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 700 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test', 'Content-Encoding'=>'gzip'}, 701 | body: "\x1f\x8b\x08\x00\x01\x00\x00\x00\x00\x03\xab\x56\x2a\xc9\xcc\x4d\x2d\x2e\x49\xcc\x2d\x50\xb2\x32\x34\x32\x36\xa9\x05\x00\xfe\x53\xbe\x14\x12\x00\x00\x00".force_encoding("ASCII-8BIT"), 702 | times:1 703 | end 704 | 705 | def test_emit_text_from_array 706 | config = %{ 707 | compress false 708 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 709 | log_format text 710 | source_category test 711 | source_host test 712 | source_name test 713 | 714 | } 715 | driver = create_driver(config) 716 | time = event_time 717 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 718 | driver.run do 719 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => ['test', 'test2']}) 720 | end 721 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 722 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 723 | body: '["test","test2"]', 724 | times:1 725 | end 726 | 727 | def test_emit_text_from_dict 728 | config = %{ 729 | compress false 730 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 731 | log_format text 732 | source_category test 733 | source_host test 734 | source_name test 735 | 736 | } 737 | driver = create_driver(config) 738 | time = event_time 739 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 740 | driver.run do 741 | driver.feed("output.test", time, {'foo' => 'bar', 'message' => {'test': 'test2', 'test3': 'test4'}}) 742 | end 743 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 744 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 745 | body: '{"test":"test2","test3":"test4"}', 746 | times:1 747 | end 748 | 749 | def test_emit_fields_string_based 750 | config = %{ 751 | compress false 752 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 753 | log_format fields 754 | source_category test 755 | source_host test 756 | source_name test 757 | 758 | } 759 | driver = create_driver(config) 760 | time = event_time 761 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 762 | driver.run do 763 | driver.feed("output.test", time, {'message' => '{"foo": "bar", "message": "test"}'}) 764 | end 765 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 766 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 767 | body: /\A{"timestamp":\d+.,"message":{"foo":"bar","message":"test"}}\z/, 768 | times:1 769 | end 770 | 771 | def test_emit_fields_invalid_json_string_based_1 772 | config = %{ 773 | compress false 774 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 775 | log_format fields 776 | source_category test 777 | source_host test 778 | source_name test 779 | 780 | } 781 | driver = create_driver(config) 782 | time = event_time 783 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 784 | driver.run do 785 | driver.feed("output.test", time, {'message' => '{"foo": "bar", "message": "test"'}) 786 | end 787 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 788 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 789 | body: /\A{"timestamp":\d+.,"message":"{\\"foo\\": \\"bar\\", \\"message\\": \\"test\\""}\z/, 790 | times:1 791 | end 792 | 793 | def test_emit_fields_invalid_json_string_based_2 794 | config = %{ 795 | compress false 796 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 797 | log_format fields 798 | source_category test 799 | source_host test 800 | source_name test 801 | 802 | } 803 | driver = create_driver(config) 804 | time = event_time 805 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 806 | driver.run do 807 | driver.feed("output.test", time, {'message' => '{"foo": "bar", "message"'}) 808 | end 809 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 810 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 811 | body: /\A{"timestamp":\d+.,"message":"{\\"foo\\": \\"bar\\", \\"message\\""}\z/, 812 | times:1 813 | end 814 | 815 | def test_emit_fields_invalid_json_string_based_3 816 | config = %{ 817 | compress false 818 | endpoint https://collectors.sumologic.com/v1/receivers/http/1234 819 | log_format fields 820 | source_category test 821 | source_host test 822 | source_name test 823 | 824 | } 825 | driver = create_driver(config) 826 | time = event_time 827 | stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234') 828 | driver.run do 829 | driver.feed("output.test", time, {'message' => '"foo\": \"bar\", \"mess'}) 830 | end 831 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 832 | headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'test', 'X-Sumo-Name'=>'test'}, 833 | body: /\A{"timestamp":\d+.,"message":"\\"foo\\\\\\": \\\\\\"bar\\\\\\", \\\\\\"mess"}\z/, 834 | times:1 835 | end 836 | 837 | def test_warning_response_from_receiver 838 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 839 | config = %{ 840 | compress false 841 | endpoint #{endpoint} 842 | } 843 | testdata = [ 844 | [ 845 | '{"id":"1TIRY-KGIVX-TPQRJ","errors":[{"code":"internal.error","message":"Internal server error."}]}', 846 | 'There was an issue sending data: id: 1TIRY-KGIVX-TPQRJ, errors: [{"code"=>"internal.error", "message"=>"Internal server error."}]' 847 | ], 848 | [ 849 | '{"id":"1TIRY-KGIVX-TPQRX","code": 200, "status": "Fields dropped", "message": "Dropped fields above the 30 field limit"}', 850 | 'There was an issue sending data: id: 1TIRY-KGIVX-TPQRX, code: 200, status: Fields dropped, message: Dropped fields above the 30 field limit' 851 | ], 852 | ] 853 | time = event_time 854 | 855 | testdata.each do |data, log| 856 | driver = create_driver(config) 857 | stub_request(:post, endpoint).to_return(body: data, headers: {content_type: 'application/json'}) 858 | driver.run do 859 | driver.feed("test", time, {"message": "test"}) 860 | end 861 | assert_equal driver.logs.length, 1 862 | assert driver.logs[0].end_with?(log + "\n") 863 | end 864 | end 865 | 866 | def test_resend 867 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 868 | config = %{ 869 | compress false 870 | endpoint #{endpoint} 871 | retry_min_interval 0s 872 | retry_max_times 3 873 | use_internal_retry true 874 | } 875 | time = event_time 876 | 877 | driver = create_driver(config) 878 | stub_request(:post, endpoint).to_return( 879 | {status: 500, headers: {content_type: 'application/json'}}, 880 | {status: 200, headers: {content_type: 'application/json'}} 881 | ) 882 | driver.run do 883 | driver.feed("test", time, {"message": "test"}) 884 | end 885 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 886 | body: /\A{"timestamp":\d+.,"message":"test"}\z/, 887 | times:2 888 | end 889 | 890 | def test_resend_failed 891 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 892 | config = %{ 893 | compress false 894 | endpoint #{endpoint} 895 | retry_min_interval 0s 896 | retry_max_times 15 897 | use_internal_retry true 898 | } 899 | time = event_time 900 | 901 | driver = create_driver(config) 902 | stub_request(:post, endpoint).to_return( 903 | status: 500, headers: {content_type: 'application/json'} 904 | ) 905 | driver.run do 906 | driver.feed("test", time, {"message": "test"}) 907 | end 908 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 909 | body: /\A{"timestamp":\d+.,"message":"test"}\z/, 910 | times:15 911 | end 912 | 913 | def test_resend_forever 914 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 915 | config = %{ 916 | compress false 917 | endpoint #{endpoint} 918 | retry_min_interval 0s 919 | retry_max_times 0 920 | retry_timeout 0s 921 | use_internal_retry true 922 | } 923 | time = event_time 924 | 925 | driver = create_driver(config) 926 | stub_request(:post, endpoint).to_return( 927 | *[{status: 500, headers: {content_type: 'application/json'}}]*123, 928 | {status: 200, headers: {content_type: 'application/json'}} 929 | ) 930 | driver.run do 931 | driver.feed("test", time, {"message": "test"}) 932 | end 933 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 934 | body: /\A{"timestamp":\d+.,"message":"test"}\z/, 935 | times:124 936 | end 937 | 938 | def test_skip_retry 939 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 940 | config = %{ 941 | compress false 942 | endpoint #{endpoint} 943 | } 944 | time = event_time 945 | 946 | driver = create_driver(config) 947 | stub_request(:post, endpoint).to_return(status: 500, headers: {content_type: 'application/json'}) 948 | 949 | exception = assert_raise(RuntimeError) { 950 | driver.run do 951 | driver.feed("test", time, {"message": "test"}) 952 | end 953 | } 954 | assert_equal("Failed to send data to HTTP Source. 500 - ", exception.message) 955 | end 956 | 957 | def test_split_negative_or_zero 958 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 959 | 960 | configs = [ 961 | %{ 962 | compress false 963 | endpoint #{endpoint} 964 | max_request_size -5 965 | }, 966 | %{ 967 | compress false 968 | endpoint #{endpoint} 969 | max_request_size 0 970 | } 971 | ] 972 | 973 | time = event_time 974 | 975 | configs.each do |config| 976 | WebMock.reset_executed_requests! 977 | driver = create_driver(config) 978 | stub_request(:post, endpoint).to_return(status: 200, headers: {content_type: 'application/json'}) 979 | 980 | driver.run do 981 | driver.feed("test", time, {"message": "test"}) 982 | driver.feed("test", time, {"message": "test"}) 983 | driver.feed("test", time, {"message": "test"}) 984 | end 985 | 986 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 987 | body: /\A{"timestamp":\d+.,"message":"test"}\n{"timestamp":\d+.,"message":"test"}\n{"timestamp":\d+.,"message":"test"}\z/, 988 | times:1 989 | end 990 | end 991 | 992 | def test_split 993 | endpoint = "https://collectors.sumologic.com/v1/receivers/http/1234" 994 | 995 | config = %{ 996 | compress false 997 | endpoint #{endpoint} 998 | max_request_size 80 999 | } 1000 | 1001 | time = event_time 1002 | 1003 | WebMock.reset_executed_requests! 1004 | driver = create_driver(config) 1005 | stub_request(:post, endpoint).to_return(status: 200, headers: {content_type: 'application/json'}) 1006 | 1007 | driver.run do 1008 | driver.feed("test", time, {"message": "test1"}) 1009 | driver.feed("test", time, {"message": "test2"}) 1010 | driver.feed("test", time, {"message": "test3"}) 1011 | driver.feed("test", time, {"message": "test4"}) 1012 | driver.feed("test", time, {"message": "test5"}) 1013 | end 1014 | 1015 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 1016 | body: /\A{"timestamp":\d+.,"message":"test1"}\n{"timestamp":\d+.,"message":"test2"}\z/, 1017 | times:1 1018 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 1019 | body: /\A{"timestamp":\d+.,"message":"test3"}\n{"timestamp":\d+.,"message":"test4"}\z/, 1020 | times:1 1021 | assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234", 1022 | body: /\A{"timestamp":\d+.,"message":"test5"}\z/, 1023 | times:1 1024 | end 1025 | 1026 | end 1027 | --------------------------------------------------------------------------------