telegraf 0.6.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f68b6dfd1bcf5659a4ee22bbe80665502a4fc09e55b90834ec85ab09e040893
4
- data.tar.gz: d5be579cd9bec77152ef0af33ed0c7040a2f5cfe59923da85f096be604e07ba5
3
+ metadata.gz: 132cb999b16a37b5dbb280da213c44c1a227165c0f28a4dcb568a42c6d149033
4
+ data.tar.gz: 511605f1d7ed94572d4cce1431cc7c48c82b502154d19a1de63ec098b026c177
5
5
  SHA512:
6
- metadata.gz: c7b349c501e843458c7232f4695638a763927895c4001efe4b23cc76cda6ba4b6143153f0f43a9b037130b192cb1ca8152509488413947ae3de9cd8e120361be
7
- data.tar.gz: 88a520e880d0507da05f9af9c58b69288bf59968dc4fc54238f6cdf1a5a8ca92359fe5bf6ab558171cbaf09111e1634a827c0607e1afbcf24c35285e8b8bbe15
6
+ metadata.gz: 570b4cafae14dab8ed1d43072366c4df5a4e2ea37862e3643ebd96cc7f556f7a644c9aa60afb2ce7e3b00d1ec8bee78cd8c525390bcd30d64c310ac79769410e
7
+ data.tar.gz: f595be285d5edaf4c84ba6ed97be15f6097ba747ea493f6f4fef5f15fa604f9a44392b7794c979da06751df081599cd06b3aee5ec54d328c0067e80e4e1320a2
@@ -0,0 +1,19 @@
1
+ on: [push, pull_request]
2
+ jobs:
3
+ rubocop:
4
+ name: rubocop
5
+ runs-on: ubuntu-20.04
6
+
7
+ env:
8
+ BUNDLE_WITHOUT: development
9
+ BUNDLE_JOBS: 4
10
+ BUNDLE_RETRY: 3
11
+
12
+ steps:
13
+ - uses: actions/checkout@master
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: 3.0
17
+ bundler-cache: true
18
+
19
+ - run: bundle exec rubocop --parallel --color
@@ -0,0 +1,40 @@
1
+ on: [push, pull_request]
2
+ jobs:
3
+ test:
4
+ name: Ruby ${{ matrix.ruby }} / ${{ matrix.gemfile }}
5
+ runs-on: ubuntu-20.04
6
+
7
+ strategy:
8
+ matrix:
9
+ ruby:
10
+ - '3.0'
11
+ - '2.7'
12
+ - '2.6'
13
+ - '2.5'
14
+ gemfile:
15
+ - rails_5.2.gemfile
16
+ - rails_6.0.gemfile
17
+ - rails_6.1.gemfile
18
+ - rack_2.0.gemfile
19
+ - rack_2.1.gemfile
20
+ - rack_2.2.gemfile
21
+ exclude:
22
+ - ruby: '3.0'
23
+ gemfile: rails_5.2.gemfile
24
+ fail-fast: false
25
+
26
+ env:
27
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
28
+ BUNDLE_WITHOUT: development
29
+ BUNDLE_JOBS: 4
30
+ BUNDLE_RETRY: 3
31
+
32
+ steps:
33
+ - uses: actions/checkout@master
34
+
35
+ - uses: ruby/setup-ruby@v1
36
+ with:
37
+ ruby-version: ${{ matrix.ruby }}
38
+ bundler-cache: true
39
+
40
+ - run: bundle exec rspec --color --format documentation
data/.rubocop.yml CHANGED
@@ -1,6 +1,8 @@
1
1
  AllCops:
2
2
  Include:
3
- - '**/*.rb'
3
+ - "**/*.rb"
4
+ NewCops: enable
5
+ SuggestExtensions: false
4
6
  TargetRubyVersion: 2.5
5
7
 
6
8
 
@@ -62,6 +64,9 @@ Style/HashTransformKeys:
62
64
  Style/HashTransformValues:
63
65
  Enabled: false
64
66
 
67
+ Style/IfUnlessModifier:
68
+ Enabled: false
69
+
65
70
  Style/RescueModifier:
66
71
  Exclude:
67
72
  - spec/**/*_spec.rb
data/Appraisals CHANGED
@@ -1,29 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- appraise "rails-5.0" do
4
- gem "railties", "~> 5.0.0"
3
+ appraise 'rails-5.2' do
4
+ group :test do
5
+ gem 'rails', '~> 5.2.0'
6
+ end
5
7
  end
6
8
 
7
- appraise "rails-5.1" do
8
- gem "railties", "~> 5.1.0"
9
+ appraise 'rails-6.0' do
10
+ group :test do
11
+ gem 'rails', '~> 6.0.0'
12
+ end
9
13
  end
10
14
 
11
- appraise "rails-5.2" do
12
- gem "railties", "~> 5.2.0"
15
+ appraise 'rails-6.1' do
16
+ group :test do
17
+ gem 'rails', '~> 6.1.0'
18
+ end
13
19
  end
14
20
 
15
- appraise "rails-6.0" do
16
- gem "railties", "~> 6.0.0"
21
+ appraise 'rack-2.0' do
22
+ group :test do
23
+ gem 'rack', '~> 2.0.0'
24
+ end
17
25
  end
18
26
 
19
- appraise "rack-2.0" do
20
- gem "rack", "~> 2.0.0"
27
+ appraise 'rack-2.1' do
28
+ group :test do
29
+ gem 'rack', '~> 2.1.0'
30
+ end
21
31
  end
22
32
 
23
- appraise "rack-2.1" do
24
- gem "rack", "~> 2.1.0"
25
- end
26
-
27
- appraise "rack-2.2" do
28
- gem "rack", "~> 2.2.0"
33
+ appraise 'rack-2.2' do
34
+ group :test do
35
+ gem 'rack', '~> 2.2.0'
36
+ end
29
37
  end
data/CHANGELOG.md CHANGED
@@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.0.0] - 2021-09-30
10
+ ### Changed
11
+ - The sidekiq middleware does not use keyword arguments as sidekiq does not handle them correctly on Ruby 3.0 (#14)
12
+
13
+ ## [1.0.0] - 2021-01-26
14
+ ### Added
15
+ - Global tags (#6)
16
+
17
+ ## [0.8.0] - 2020-12-02
18
+ ### Added
19
+ - ActiveJob instrumentation (#10)
20
+
21
+ ## [0.7.0] - 2020-05-07
22
+ ### Added
23
+ - Sidekiq middleware (#8)
24
+
9
25
  ## [0.6.1] - 2020-04-01
10
26
  ### Fixed
11
27
  - Fix type in instrumentation option (#7)
@@ -14,10 +30,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
14
30
  ### Added
15
31
  - New Rack middleware and Rails plugin to collect request events (#5)
16
32
 
17
- ## 0.5.0
33
+ ## 0.5.0 - undefined
18
34
  ### Changed
19
35
  - Remove `influxdb` not unnecessarily restrict users needing a specific influxdb client.
20
36
 
21
- [unreleased]: https://github.com/:jgraichen/telegraf-ruby/compare/v0.6.1...HEAD
22
- [0.6.1]: https://github.com/:jgraichen/telegraf-ruby/compare/v0.6.0...v0.6.1
23
- [0.6.0]: https://github.com/:jgraichen/telegraf-ruby/compare/v0.5.0...v0.6.0
37
+ [Unreleased]: https://github.com/jgraichen/telegraf-ruby/compare/v2.0.0...HEAD
38
+ [2.0.0]: https://github.com/jgraichen/telegraf-ruby/compare/v1.0.0...v2.0.0
39
+ [1.0.0]: https://github.com/jgraichen/telegraf-ruby/compare/v0.8.0...v1.0.0
40
+ [0.8.0]: https://github.com/jgraichen/telegraf-ruby/compare/v0.7.0...v0.8.0
41
+ [0.7.0]: https://github.com/jgraichen/telegraf-ruby/compare/v0.6.1...v0.7.0
42
+ [0.6.1]: https://github.com/jgraichen/telegraf-ruby/compare/v0.6.0...v0.6.1
43
+ [0.6.0]: https://github.com/jgraichen/telegraf-ruby/compare/v0.5.0...v0.6.0
data/Gemfile CHANGED
@@ -2,14 +2,21 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- gem 'appraisal'
6
- gem 'rake-release', '~> 1.2'
5
+ # Specify your gem's dependencies in telegraf.gemspec
6
+ gemspec
7
+
7
8
  gem 'rake'
8
9
  gem 'rspec', '~> 3.8'
9
- gem 'rubocop', '~> 0.80.0'
10
+ gem 'rubocop', '~> 1.7'
11
+ gem 'rubocop-rspec', '~> 1.41'
10
12
 
11
- gem 'rack'
12
- gem 'railties'
13
+ group :test do
14
+ gem 'rack'
15
+ gem 'rails'
16
+ gem 'sidekiq', '~> 6.0'
17
+ end
13
18
 
14
- # Specify your gem's dependencies in telegraf.gemspec
15
- gemspec
19
+ group :development do
20
+ gem 'appraisal'
21
+ gem 'rake-release', '~> 1.2'
22
+ end
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Send events to a local [Telegraf](https://github.com/influxdata/telegraf) agent or anything that can receive the InfluxDB line protocol.
4
4
 
5
- It further includes plugins for Rack and Rails to collect request events. See plugin usage details below.
5
+ It further includes plugins for Rack, Rails, ActiveJob and Sidekiq to collect request events. See plugin usage details below.
6
6
 
7
7
  This gem only uses the line protocol from the `influxdb` gem and does not depend on any specific version. This may break in the future but does not restrict you in using a your preferred `influxdb` gem version.
8
8
 
@@ -51,7 +51,7 @@ There is no exception handling.
51
51
 
52
52
  ## Using the Rack and Rails plugins
53
53
 
54
- This gem include a Rails plugin and a rack middleware to collect request events. They need to be explicitly required to be used:
54
+ This gem include a Rails plugin and middlewares / adapters for Rack, ActiveJob and Sidekiq to collect request and background worker events. They need to be explicitly required to be used:
55
55
 
56
56
  ### Rack
57
57
 
@@ -68,22 +68,37 @@ The Rack middleware supports parsing the `X-Request-Start: t=<timestamp>` header
68
68
 
69
69
  ### Rails
70
70
 
71
- The Rails plugin needs to required too but by default automatically installs required components.
71
+ The Rails plugin needs to be required, too, but will automatically install additional components (Rack, ActiveJob, Sidekiq and Rails-specific instrumentation).
72
72
 
73
73
  ```ruby
74
74
  # e.g. in application.rb
75
75
 
76
+ # Load rails plugin (!) or add `require: 'telegraf/rails'` to Gemfile
76
77
  require "telegraf/rails"
77
78
 
78
79
  class MyApplication > ::Rails::Application
79
80
  # Configure receiver
80
- config.telegraf.connect = "udp://localhost:9084"
81
+ config.telegraf.connect = "udp://localhost:8094"
82
+
83
+ # Global tags added to all events. These will override
84
+ # any local tag with the same name.
85
+ config.telegraf.tags = {}
81
86
 
82
87
  # By default the Rack middleware to collect events is installed
83
88
  config.telegraf.rack.enabled = true
84
89
  config.telegraf.rack.series = "requests"
85
90
  config.telegraf.rack.tags = {}
86
91
 
92
+ # These are the default settings when ActiveJob is detected
93
+ config.telegraf.active_job.enabled = true
94
+ config.telegraf.active_job.series = "active_job"
95
+ config.telegraf.active_job.tags = {}
96
+
97
+ # These are the default settings when Sidekiq is detected
98
+ config.telegraf.sidekiq.enabled = true
99
+ config.telegraf.sidekiq.series = "sidekiq"
100
+ config.telegraf.sidekiq.tags = {}
101
+
87
102
  # Additionally the application is instrumented to tag events with
88
103
  # controller and action as well as to collect app, database and view timings
89
104
  config.telegraf.instrumentation = true
@@ -96,7 +111,41 @@ Received event example:
96
111
  requests,action=index,controller=TestController,instance=TestController#index,method=GET,status=200 db_ms=0.0,view_ms=2.6217450003969134,action_ms=2.702335,app_ms=4.603561000294576,send_ms=0.09295000018028077,request_ms=4.699011000411701,queue_ms=0.00003000028323014
97
112
  ```
98
113
 
99
- See the rack middleware [class documentation](lib/telegraf/rack.rb) and the Rails plugin [class documentation](lib/telegraf/railtie.rb) for more details on the collected tags and values.
114
+ See the various classes' documentation for more details on the collected tags and values:
115
+ - [Rack middleware](lib/telegraf/rack.rb)
116
+ - [Rails plugin](lib/telegraf/railtie.rb)
117
+ - [ActiveJob plugin](lib/telegraf/active_job.rb)
118
+ - [Sidekiq middleware](lib/telegraf/sidekiq.rb)
119
+
120
+ ### ActiveJob
121
+
122
+ ```ruby
123
+ require "telegraf/active_job"
124
+
125
+ agent = ::Telegraf::Agent.new
126
+ ActiveSupport::Notifications.subscribe(
127
+ 'perform.active_job',
128
+ Telegraf::ActiveJob.new(agent: agent, series: 'active_job', tags: {global: 'tag'})
129
+ )
130
+ ```
131
+
132
+ See plugin [class documentation](lib/telegraf/active_job.rb) for more details.
133
+
134
+ ### Sidekiq
135
+
136
+ ```ruby
137
+ require "telegraf/sidekiq"
138
+
139
+ agent = ::Telegraf::Agent.new
140
+ Sidekiq.configure_server do |config|
141
+ config.server_middleware do |chain|
142
+ chain.add ::Telegraf::Sidekiq::Middleware, agent: agent, series: 'sidekiq', tags: {global: 'tag'}
143
+ end
144
+ end
145
+ ```
146
+
147
+ See middleware [class documentation](lib/telegraf/sidekiq.rb) for more details.
148
+
100
149
 
101
150
  ## License
102
151
 
@@ -2,11 +2,20 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rubocop", "~> 0.80.0"
6
5
  gem "rake"
7
6
  gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack", "~> 2.0.0"
10
- gem "railties"
7
+ gem "rubocop", "~> 1.7"
8
+ gem "rubocop-rspec", "~> 1.41"
9
+
10
+ group :test do
11
+ gem "rack", "~> 2.0.0"
12
+ gem "rails"
13
+ gem "sidekiq", "~> 6.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "appraisal"
18
+ gem "rake-release", "~> 1.2"
19
+ end
11
20
 
12
21
  gemspec path: "../"
@@ -2,11 +2,20 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rubocop", "~> 0.80.0"
6
5
  gem "rake"
7
6
  gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack", "~> 2.1.0"
10
- gem "railties"
7
+ gem "rubocop", "~> 1.7"
8
+ gem "rubocop-rspec", "~> 1.41"
9
+
10
+ group :test do
11
+ gem "rack", "~> 2.1.0"
12
+ gem "rails"
13
+ gem "sidekiq", "~> 6.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "appraisal"
18
+ gem "rake-release", "~> 1.2"
19
+ end
11
20
 
12
21
  gemspec path: "../"
@@ -2,11 +2,20 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rubocop", "~> 0.80.0"
6
5
  gem "rake"
7
6
  gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack", "~> 2.2.0"
10
- gem "railties"
7
+ gem "rubocop", "~> 1.7"
8
+ gem "rubocop-rspec", "~> 1.41"
9
+
10
+ group :test do
11
+ gem "rack", "~> 2.2.0"
12
+ gem "rails"
13
+ gem "sidekiq", "~> 6.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "appraisal"
18
+ gem "rake-release", "~> 1.2"
19
+ end
11
20
 
12
21
  gemspec path: "../"
@@ -2,11 +2,20 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rubocop", "~> 0.80.0"
6
5
  gem "rake"
7
6
  gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack"
10
- gem "railties", "~> 5.2.0"
7
+ gem "rubocop", "~> 1.7"
8
+ gem "rubocop-rspec", "~> 1.41"
9
+
10
+ group :test do
11
+ gem "rack"
12
+ gem "rails", "~> 5.2.0"
13
+ gem "sidekiq", "~> 6.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "appraisal"
18
+ gem "rake-release", "~> 1.2"
19
+ end
11
20
 
12
21
  gemspec path: "../"
@@ -2,11 +2,20 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rubocop", "~> 0.80.0"
6
5
  gem "rake"
7
6
  gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack"
10
- gem "railties", "~> 6.0.0"
7
+ gem "rubocop", "~> 1.7"
8
+ gem "rubocop-rspec", "~> 1.41"
9
+
10
+ group :test do
11
+ gem "rack"
12
+ gem "rails", "~> 6.0.0"
13
+ gem "sidekiq", "~> 6.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "appraisal"
18
+ gem "rake-release", "~> 1.2"
19
+ end
11
20
 
12
21
  gemspec path: "../"
@@ -0,0 +1,21 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake"
6
+ gem "rspec", "~> 3.8"
7
+ gem "rubocop", "~> 1.7"
8
+ gem "rubocop-rspec", "~> 1.41"
9
+
10
+ group :test do
11
+ gem "rack"
12
+ gem "rails", "~> 6.1.0"
13
+ gem "sidekiq", "~> 6.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "appraisal"
18
+ gem "rake-release", "~> 1.2"
19
+ end
20
+
21
+ gemspec path: "../"
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telegraf
4
+ # Telegraf::ActiveJob
5
+ #
6
+ # This class collects ActiveJob queue metrics and sends them to telegraf.
7
+ #
8
+ #
9
+ # Tags:
10
+ #
11
+ # * `queue`:
12
+ # The queue this job landed on.
13
+ #
14
+ # * `job`:
15
+ # The name of the job class that was executed.
16
+ #
17
+ # * `errors`:
18
+ # Whether or not this job errored.
19
+ #
20
+ #
21
+ # Values:
22
+ #
23
+ # * `app_ms`:
24
+ # Total job processing time.
25
+ #
26
+ class ActiveJob
27
+ def initialize(agent:, series: 'active_job', tags: {})
28
+ @agent = agent
29
+ @series = series.to_s.freeze
30
+ @tags = tags.freeze
31
+ end
32
+
33
+ def call(_name, start, finish, _id, payload)
34
+ job = payload[:job]
35
+
36
+ @agent.write(
37
+ @series,
38
+ tags: {
39
+ **@tags,
40
+ job: job.class.name,
41
+ queue: job.queue_name,
42
+ errors: payload.key?(:exception_object)
43
+ },
44
+ values: {
45
+ app_ms: ((finish - start) * 1000.0) # milliseconds
46
+ }
47
+ )
48
+ end
49
+ end
50
+ end
@@ -4,11 +4,11 @@ module Telegraf
4
4
  class Agent
5
5
  DEFAULT_CONNECTION = 'udp://localhost:8094'
6
6
 
7
- attr_reader :uri
8
- attr_reader :logger
7
+ attr_reader :uri, :logger, :tags
9
8
 
10
- def initialize(uri = nil, logger: nil)
9
+ def initialize(uri = nil, logger: nil, tags: {})
11
10
  @uri = URI.parse(uri || DEFAULT_CONNECTION)
11
+ @tags = tags
12
12
  @logger = logger
13
13
  end
14
14
 
@@ -21,6 +21,8 @@ module Telegraf
21
21
  end
22
22
 
23
23
  def write!(data, series: nil, tags: nil, values: nil)
24
+ tags = tags.merge(@tags) unless @tags.empty?
25
+
24
26
  if values
25
27
  data = [{series: series || data.to_s, tags: tags, values: values}]
26
28
  end
data/lib/telegraf/rack.rb CHANGED
@@ -44,7 +44,13 @@ module Telegraf
44
44
  FIELD_NAME = 'telegraf.rack.point'
45
45
  HEADER_REGEX = /t=(\d+(\.\d+)?)/.freeze
46
46
 
47
+ # Warning: `:values` member overrides `Struct#values` and it may be
48
+ # unexpected, but nothing we can change here as this is an import public API
49
+ # right now.
50
+ #
51
+ # rubocop:disable Lint/StructNewOverride
47
52
  Point = Struct.new(:tags, :values)
53
+ # rubocop:enable Lint/StructNewOverride
48
54
 
49
55
  def initialize(app, agent:, series: 'rack', tags: {}, logger: nil)
50
56
  @app = app
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails'
4
+ require 'telegraf'
5
+ require 'telegraf/active_job'
4
6
  require 'telegraf/rack'
7
+ require 'telegraf/sidekiq'
5
8
 
6
9
  module Telegraf
7
10
  # Telegraf::Railtie
@@ -43,8 +46,9 @@ module Telegraf
43
46
 
44
47
  # Connect URI or tuple
45
48
  config.telegraf.connect = ::Telegraf::Agent::DEFAULT_CONNECTION
49
+ config.telegraf.tags = {}
46
50
 
47
- # Install rackmiddlewares
51
+ # Install Rack middlewares
48
52
  config.telegraf.rack = ::ActiveSupport::OrderedOptions.new
49
53
  config.telegraf.rack.enabled = true
50
54
  config.telegraf.rack.series = 'requests'
@@ -53,12 +57,24 @@ module Telegraf
53
57
  # Install request instrumentation
54
58
  config.telegraf.instrumentation = true
55
59
 
60
+ # Install ActiveJob instrumentation
61
+ config.telegraf.active_job = ::ActiveSupport::OrderedOptions.new
62
+ config.telegraf.active_job.enabled = defined?(::ActiveJob)
63
+ config.telegraf.active_job.series = 'active_job'
64
+ config.telegraf.active_job.tags = {}
65
+
66
+ # Install Sidekiq middleware
67
+ config.telegraf.sidekiq = ::ActiveSupport::OrderedOptions.new
68
+ config.telegraf.sidekiq.enabled = defined?(::Sidekiq)
69
+ config.telegraf.sidekiq.series = 'sidekiq'
70
+ config.telegraf.sidekiq.tags = {}
71
+
56
72
  initializer 'telegraf.agent' do |app|
57
- app.config.telegraf.agent ||= begin
73
+ app.config.telegraf.agent ||= \
58
74
  ::Telegraf::Agent.new \
59
75
  app.config.telegraf.connect,
60
- logger: Rails.logger
61
- end
76
+ logger: Rails.logger,
77
+ tags: app.config.telegraf.tags
62
78
  end
63
79
 
64
80
  initializer 'telegraf.rack' do |app|
@@ -90,5 +106,33 @@ module Telegraf
90
106
  point.values[:action_ms] = ((finish - start) * 1000.0) # milliseconds
91
107
  end
92
108
  end
109
+
110
+ initializer 'telegraf.active_job' do |app|
111
+ next unless app.config.telegraf.active_job.enabled
112
+
113
+ ActiveSupport::Notifications.subscribe(
114
+ 'perform.active_job',
115
+ Telegraf::ActiveJob.new(
116
+ agent: app.config.telegraf.agent,
117
+ series: app.config.telegraf.active_job.series,
118
+ tags: app.config.telegraf.active_job.tags
119
+ )
120
+ )
121
+ end
122
+
123
+ initializer 'telegraf.sidekiq' do |app|
124
+ next unless app.config.telegraf.sidekiq.enabled
125
+
126
+ ::Sidekiq.configure_server do |config|
127
+ config.server_middleware do |chain|
128
+ chain.add Telegraf::Sidekiq::Middleware, \
129
+ app.config.telegraf.agent,
130
+ {
131
+ series: app.config.telegraf.sidekiq.series,
132
+ tags: app.config.telegraf.sidekiq.tags
133
+ }
134
+ end
135
+ end
136
+ end
93
137
  end
94
138
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+
5
+ module Telegraf
6
+ module Sidekiq
7
+ # Telegraf::Sidekiq::Middleware
8
+ #
9
+ # This Sidekiq middleware collects queue metrics and sends them to telegraf.
10
+ #
11
+ #
12
+ # Tags:
13
+ #
14
+ # * `type`:
15
+ # One of "job" or "scheduled_job".
16
+ #
17
+ # * `queue`:
18
+ # The queue this job landed on.
19
+ #
20
+ # * `worker`:
21
+ # The name of the worker class that was executed.
22
+ #
23
+ # * `errors`:
24
+ # Whether or not this job errored.
25
+ #
26
+ # * `retry`:
27
+ # Whether or not this execution was a retry of a previously failed one.
28
+ #
29
+ #
30
+ # Values:
31
+ #
32
+ # * `app_ms`:
33
+ # Total worker processing time.
34
+ #
35
+ # * `queue_ms`:
36
+ # How long did this job wait in the queue before being processed?
37
+ # Only present for "normal" (async) jobs (with tag `type` of "job").
38
+ #
39
+ class Middleware
40
+ def initialize(agent, options = {})
41
+ @agent = agent
42
+ @series = options.fetch(:series, 'sidekiq').to_str.freeze
43
+ @tags = options.fetch(:tags, {}).to_hash.freeze
44
+ end
45
+
46
+ def call(worker, job, queue)
47
+ job_start = ::Time.now.utc
48
+
49
+ tags = {
50
+ **@tags,
51
+ type: 'job',
52
+ errors: true,
53
+ retry: job.key?('retried_at'),
54
+ queue: queue,
55
+ worker: worker.class.name
56
+ }
57
+
58
+ values = {
59
+ retry_count: job['retry_count']
60
+ }.compact
61
+
62
+ # The "enqueued_at" key is not present for scheduled jobs.
63
+ # See https://github.com/mperham/sidekiq/wiki/Job-Format.
64
+ if job.key?('enqueued_at')
65
+ enqueued_at = ::Time.at(job['enqueued_at'].to_f).utc
66
+ values[:queue_ms] = (job_start - enqueued_at) * 1000 # milliseconds
67
+ end
68
+
69
+ # The "at" key is only present for scheduled jobs.
70
+ tags[:type] = 'scheduled_job' if job.key?('at')
71
+
72
+ begin
73
+ yield
74
+
75
+ # If we get here, this was a successful execution
76
+ tags[:errors] = false
77
+ ensure
78
+ job_stop = ::Time.now.utc
79
+
80
+ values[:app_ms] = (job_stop - job_start) * 1000 # milliseconds
81
+
82
+ @agent.write(
83
+ @series, tags: tags, values: values
84
+ )
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Telegraf
4
4
  module VERSION
5
- MAJOR = 0
6
- MINOR = 6
7
- PATCH = 1
5
+ MAJOR = 2
6
+ MINOR = 0
7
+ PATCH = 0
8
8
  STAGE = nil
9
9
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
10
10
 
data/telegraf.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.summary = 'Metric Reporter to local telegraf agent'
15
15
  spec.description = 'Metric Reporter to local telegraf agent'
16
- spec.homepage = 'https://github.com/jgraichen/ruby-telegraf'
16
+ spec.homepage = 'https://github.com/jgraichen/telegraf-ruby'
17
17
  spec.license = 'LGPLv3'
18
18
 
19
19
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegraf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: influxdb
@@ -46,10 +46,11 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".editorconfig"
49
+ - ".github/workflows/lint.yml"
50
+ - ".github/workflows/test.yml"
49
51
  - ".gitignore"
50
52
  - ".rspec"
51
53
  - ".rubocop.yml"
52
- - ".travis.yml"
53
54
  - Appraisals
54
55
  - CHANGELOG.md
55
56
  - Gemfile
@@ -61,18 +62,19 @@ files:
61
62
  - gemfiles/rack_2.0.gemfile
62
63
  - gemfiles/rack_2.1.gemfile
63
64
  - gemfiles/rack_2.2.gemfile
64
- - gemfiles/rails_5.0.gemfile
65
- - gemfiles/rails_5.1.gemfile
66
65
  - gemfiles/rails_5.2.gemfile
67
66
  - gemfiles/rails_6.0.gemfile
67
+ - gemfiles/rails_6.1.gemfile
68
68
  - lib/telegraf.rb
69
+ - lib/telegraf/active_job.rb
69
70
  - lib/telegraf/agent.rb
70
71
  - lib/telegraf/rack.rb
71
72
  - lib/telegraf/rails.rb
72
73
  - lib/telegraf/railtie.rb
74
+ - lib/telegraf/sidekiq.rb
73
75
  - lib/telegraf/version.rb
74
76
  - telegraf.gemspec
75
- homepage: https://github.com/jgraichen/ruby-telegraf
77
+ homepage: https://github.com/jgraichen/telegraf-ruby
76
78
  licenses:
77
79
  - LGPLv3
78
80
  metadata: {}
@@ -91,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
93
  - !ruby/object:Gem::Version
92
94
  version: '0'
93
95
  requirements: []
94
- rubygems_version: 3.1.2
96
+ rubygems_version: 3.2.22
95
97
  signing_key:
96
98
  specification_version: 4
97
99
  summary: Metric Reporter to local telegraf agent
data/.travis.yml DELETED
@@ -1,15 +0,0 @@
1
- sudo: false
2
- dist: bionic
3
- language: ruby
4
- rvm:
5
- - 2.7.0
6
- - 2.6.5
7
- - 2.5.7
8
- gemfile:
9
- - gemfiles/rails_5.0.gemfile
10
- - gemfiles/rails_5.1.gemfile
11
- - gemfiles/rails_5.2.gemfile
12
- - gemfiles/rails_6.0.gemfile
13
- - gemfiles/rack_2.0.gemfile
14
- - gemfiles/rack_2.1.gemfile
15
- - gemfiles/rack_2.2.gemfile
@@ -1,12 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "rubocop", "~> 0.80.0"
6
- gem "rake"
7
- gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack"
10
- gem "railties", "~> 5.0.0"
11
-
12
- gemspec path: "../"
@@ -1,12 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "rubocop", "~> 0.80.0"
6
- gem "rake"
7
- gem "rspec", "~> 3.8"
8
- gem "appraisal"
9
- gem "rack"
10
- gem "railties", "~> 5.1.0"
11
-
12
- gemspec path: "../"