alerty-plugin-stackdriver 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfe5672c5435de5e687dde50cb6f59456e8b57b6
4
+ data.tar.gz: 0ea6b49908522bf2d8ed2b03b333f4f0ff22456f
5
+ SHA512:
6
+ metadata.gz: 3f661c604da93e10943ee3cec15e1bffaff8093cc1bc2bd4f1ffa8dde10478aed3e6397112cdfac326dddc263e2eba97989b25c12bf0f2e3f1a563c608224b95
7
+ data.tar.gz: 2f0d435a6135995b035f327f9810bd34ce79c1ab475fcffc1879201a42d9d0935009eec37091eef06c0b9d2f232194446c016042a104f5984e92747b4f57bd59
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in alerty-plugin-stackdriver.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Alerty::Plugin::Stackdriver
2
+
3
+ Google Stackdriver plugin for alerty (https://github.com/sonots/alerty)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'alerty-plugin-stackdriver'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install alerty-plugin-stackdriver
20
+
21
+ ## Configuration
22
+
23
+ * **type**: must be `stackdriver`.
24
+ * **keyfile**: service account keyfile path.
25
+ * **project_id**: your GCP project id.
26
+ * **log_name**: Stackdriver log name.
27
+ * **resource_type**: Stackdriver Logging monitored resource types. https://cloud.google.com/logging/docs/api/v2/resource-list
28
+ * **resource_labels**: Stackdriver Logging monitored resource labels. https://cloud.google.com/logging/docs/api/v2/resource-list
29
+ * **message**: message of alert. `${command}` is replaced with a given command, `${hostname}` is replaced with the hostname ran the command, `${output}` is replaced with the output. The default is `${output}`.
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/potato2003/alerty-plugin-stackdriver.
40
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "alerty-plugin-stackdriver"
4
+ spec.version = "0.1.0"
5
+ spec.authors = ["potato2003"]
6
+ spec.email = ["potato2003@gmail.com"]
7
+
8
+ spec.summary = "Google Stackdriver plugin for alerty"
9
+ spec.description = "Google Stackdriver plugin for alerty"
10
+ spec.homepage = "https://github.com/potato2003/alerty-plugin-stackdriver"
11
+
12
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
13
+ f.match(%r{^(test|spec|features)/})
14
+ end
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.license = 'MIT'
20
+
21
+ spec.add_runtime_dependency "alerty"
22
+ spec.add_runtime_dependency "google-cloud-logging", "~> 1.5"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "alerty/plugin/stackdriver"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ log_path: STDOUT
2
+ log_level: debug
3
+ plugins:
4
+ - type: stackdriver
5
+ keyfile: "example_keyfile.json"
6
+ project_id: "example_project"
7
+ log_name: "example_alerty_log"
8
+ resource_type: "gae_app"
9
+ resource_labels:
10
+ module_id: "1"
11
+ version_id: "20150925t173233"
@@ -0,0 +1,86 @@
1
+ require 'alerty'
2
+ require "google/cloud/logging"
3
+
4
+ class Alerty
5
+ class Plugin
6
+ class Stackdriver
7
+
8
+ DEFAULT_NUM_OF_RETIRES = 3
9
+
10
+ def initialize(config)
11
+ # https://cloud.google.com/logging/docs/api/v2/resource-list
12
+ @resource_type = config.resource_type
13
+ @resource_labels = config.resource_labels
14
+ @log_name = config.log_name || 'alerty'
15
+ @message = config.message
16
+ @num_retries = config.num_retries || DEFAULT_NUM_OF_RETIRES
17
+
18
+ @stackdriver = Google::Cloud::Logging.new(
19
+ project_id: config.project_id,
20
+ keyfile: config.keyfile ? ensure_keyfile(config.keyfile) : nil
21
+ )
22
+ end
23
+
24
+ def alert(record)
25
+ entry = make_entry(record)
26
+
27
+ retrable(tries: @num_retries) do
28
+ @stackdriver.write_entries(entry)
29
+ Alerty.logger.info "Sent #{{log_entry: entry.inspect}} to #{@log_name}"
30
+ end
31
+ end
32
+
33
+ private
34
+ def retrable(options = {}, &block)
35
+ tries = options[:tries] || DEFAULT_NUM_OF_RETIRES
36
+
37
+ retries = 0
38
+ begin
39
+ yield
40
+ rescue => e
41
+ retries += 1
42
+ sleep 1
43
+ if retries <= tries
44
+ retry
45
+ else
46
+ raise e
47
+ end
48
+ end
49
+ end
50
+
51
+ def make_entry(record)
52
+ e = @stackdriver.entry
53
+
54
+ e.resource.type = @resource_type
55
+ e.resource.labels = @resource_labels if @resource_labels
56
+ e.log_name = @log_name
57
+ e.payload = payload(record)
58
+
59
+ e
60
+ end
61
+
62
+ def payload(record)
63
+ if @message
64
+ expand_placeholder(@message, record)
65
+ else
66
+ record[:output]
67
+ end
68
+ end
69
+
70
+ def expand_placeholder(str, record)
71
+ replaced = str.gsub('${command}', record[:command])
72
+ replaced.gsub!('${hostname}', record[:hostname])
73
+ replaced.gsub!('${output}', record[:output])
74
+ replaced
75
+ end
76
+
77
+ def ensure_keyfile(v)
78
+ if v.is_a?(String)
79
+ v
80
+ elsif v.is_a?(Hash)
81
+ JSON.parse(v['content'])
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alerty-plugin-stackdriver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - potato2003
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: alerty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-cloud-logging
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Google Stackdriver plugin for alerty
84
+ email:
85
+ - potato2003@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - alerty-plugin-stackdriver.gemspec
95
+ - bin/console
96
+ - bin/setup
97
+ - example_config.yml
98
+ - lib/alerty/plugin/stackdriver.rb
99
+ homepage: https://github.com/potato2003/alerty-plugin-stackdriver
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.6.11
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Google Stackdriver plugin for alerty
123
+ test_files: []