opsgenie-heartbeat 0.1.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 90ab82ef80b3460608df4482d3cfec7854abe08d
4
- data.tar.gz: 860eef58b7f9ee13b7e4ed72734ac938987e878d
2
+ SHA256:
3
+ metadata.gz: 40ad93c29a50145b21401962688273ffb38cd994bfd6d222d82ea43baeafaa5e
4
+ data.tar.gz: 055e1f4ab36b7d670ef8a85b9989f60a862833cd3085f0865c1997bd0950d6ac
5
5
  SHA512:
6
- metadata.gz: dd2e2930a0a5b714580839cdf235f27794e2d52175a643dc08a564ef9cb960f5213d790fe982517158eb7d98d199318d90e2797cfc4734eae6385fdb38d056d8
7
- data.tar.gz: 5d6dae499406c7c1f2e713c74ab8099e60059f6f55090ae48c251d5501c52b08ba417864a5e79315a1fa60c6246e0d8a81bc5ad314a873dc70712e1ffc0479c3
6
+ metadata.gz: 1b366b335612b50cf981e6be0e16f11be22cde7c6aa36804ab940451f4ddc443d025fd804011935b2ae4230f5e4879323a1f52bfeebe1ef305dd2435fbd5163f
7
+ data.tar.gz: bd62e408cda81d65e689e28bbc2b6f109f761cab3e29e398a1d5dd1f7df137b3a46a9729777197a284bed2c5e4a18b78c4f2e8540c8830119d7c54e8e022cc6e
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
1
  --format documentation
2
2
  --color
3
- --warnings
data/CHANGELOG.md CHANGED
@@ -1,7 +1,24 @@
1
- Updates in version 0.1.1:
1
+ ## Updates in version 0.4:
2
+ - ability to configure retries
2
3
 
3
- No need to supply name_transformer in initializer file unless wanted.
4
- Now there is default value -> name itself.
4
+ ## Updates in version 0.3:
5
+ - ability to configure a default team
5
6
 
6
- Updates in version 0.1.2:
7
- Instead of require 'opsgenie/heartbeat/heartbeat' now is require 'opsgenie/heartbeat'
7
+ ## Updates in version 0.2.1:
8
+ - fix ensure always trying to create heartbeat
9
+ - use get instead of post for pinging
10
+
11
+ ## Updates in version 0.2.0:
12
+
13
+ - Allow updating of heartbeats
14
+ - Allow setting of team
15
+
16
+
17
+ ##Updates in version 0.1.2:
18
+
19
+ - Instead of require 'opsgenie/heartbeat/heartbeat' now is require 'opsgenie/heartbeat'
20
+
21
+ ## Updates in version 0.1.1:
22
+
23
+ - No need to supply name_transformer in initializer file unless wanted.
24
+ - Now there is default value -> name itself.
data/README.md CHANGED
@@ -49,6 +49,14 @@ In yours initializers folder, define new file with:
49
49
  # - if config.raise_error = true is set then it raises error
50
50
  # - if nothing is defined, then it ignores error
51
51
  config.logger = Rails.logger
52
+
53
+ #will set the team to Myteam on all new heartbeats
54
+ #you can override when you create the heartbeat.
55
+ config.default_team = 'Myteam'
56
+
57
+ #retry timedout api calls upto the specified number of times, with a random backoff
58
+ #default is no retries
59
+ config.retries = 2
52
60
  end
53
61
  ```
54
62
 
@@ -9,7 +9,7 @@ module Opsgenie
9
9
  end
10
10
 
11
11
  class Config
12
- attr_accessor :enabled, :api_key, :name_transformer, :logger, :raise_error
12
+ attr_accessor :enabled, :api_key, :name_transformer, :logger, :raise_error, :default_team, :retries
13
13
 
14
14
  def name_transformer
15
15
  @name_transformer || ->(name){name}
@@ -1,5 +1,5 @@
1
1
  module Opsgenie
2
2
  module Heartbeat
3
- VERSION = "0.1.2"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -6,75 +6,131 @@ require 'rack'
6
6
 
7
7
  module Opsgenie
8
8
  module Heartbeat
9
+ class << self
10
+ def pulse(name)
11
+ return unless configuration.enabled
12
+ name = configuration.name_transformer.call(name)
13
+ begin
14
+ uri = URI.parse("https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}/ping")
15
+ with_timeout_retries do
16
+ http = Net::HTTP.new(uri.host, uri.port)
17
+ http.use_ssl = true
18
+ response = http.get(uri.path, {'Authorization': "GenieKey #{configuration.api_key}"})
19
+ if !response.is_a?(Net::HTTPSuccess)
20
+ configuration.logger.info("Error creating or updating heartbeat: #{response}") if configuration.logger
21
+ end
22
+ end
23
+ rescue => e
24
+ resolve_exception e
25
+ end
26
+ end
27
+
28
+ def ensure(name:, interval:, unit: , description:, enabled: true, team: nil)
29
+ return unless configuration.enabled
30
+ original_name = name
31
+ name = configuration.name_transformer.call(name)
9
32
 
10
- def self.pulse(name)
11
- return unless configuration.enabled
12
- name = configuration.name_transformer.call(name)
13
- begin
14
- uri = URI.parse("https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}/ping")
15
- http = Net::HTTP.new(uri.host, uri.port)
16
- http.use_ssl = true
17
- data = {name: name}
18
- http.post(uri.path, data.to_json, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})
19
- rescue => e
20
- resolve_exception e
33
+ uri = URI.parse(url_for_resource(:get, name))
34
+ with_timeout_retries do
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = true
37
+ response = http.get(uri.path, {'Authorization': "GenieKey #{configuration.api_key}"})
38
+ unless response.is_a?(Net::HTTPSuccess)
39
+ create(name: original_name, description: description, interval: interval, unit: unit, enabled: enabled, team: team)
40
+ end
41
+ end
21
42
  end
22
- end
23
43
 
24
- def self.ensure(name:, interval:, unit: , description:, enabled: true)
25
- return unless configuration.enabled
26
- original_name = name
27
- name = configuration.name_transformer.call(name)
28
-
29
- uri = URI.parse("https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}")
30
- http = Net::HTTP.new(uri.host, uri.port)
31
- http.use_ssl = true
32
- response = http.get(uri.path)
33
- unless response.is_a?(Net::HTTPSuccess)
34
- create(name: original_name, description: description, interval: interval, unit: unit, enabled: enabled)
44
+ def delete(name)
45
+ return unless configuration.enabled
46
+ name = configuration.name_transformer.call(name)
47
+
48
+ begin
49
+ uri = URI.parse(url_for_resource(:delete, name))
50
+ http = Net::HTTP.new(uri.host, uri.port)
51
+ http.use_ssl = true
52
+
53
+ http.delete(uri.path, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})
54
+ rescue => e
55
+ resolve_exception e
56
+ end
57
+ end
58
+
59
+ def update(name:, interval: nil, unit: nil, description: nil, enabled: nil, team: nil)
60
+ return unless configuration.enabled
61
+ create_or_update(:patch, name: name, description: description, interval: interval, unit: unit, enabled: enabled, team: team)
62
+ end
63
+
64
+ def create(name:, interval: nil, unit: nil, description: nil, enabled: nil, team: nil)
65
+ return unless configuration.enabled
66
+ create_or_update(:post, name: name, description: description, interval: interval, unit: unit, enabled: enabled, team: team)
35
67
  end
36
- end
37
68
 
38
- def self.delete(name)
39
- return unless configuration.enabled
40
- name = configuration.name_transformer.call(name)
41
69
 
42
- begin
43
- uri = URI.parse("https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}")
44
- http = Net::HTTP.new(uri.host, uri.port)
45
- http.use_ssl = true
70
+ private
46
71
 
47
- http.delete(uri.path, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})
48
- rescue => e
49
- resolve_exception e
72
+ def with_timeout_retries
73
+ attempts = 0
74
+ begin
75
+ yield
76
+ rescue Net::OpenTimeout, Net::ReadTimeout => e
77
+ if attempts < (configuration.retries || 0)
78
+ configuration.logger.info("Retrying opsgenie api call after timeout #{e.message}")
79
+ attempts += 1
80
+ sleep(rand(2**attempts))
81
+ retry
82
+ else
83
+ raise
84
+ end
85
+ end
50
86
  end
51
- end
52
87
 
53
- private
54
-
55
- def self.create(name:,description:,interval:,unit:, enabled:)
56
- name = configuration.name_transformer.call(name)
57
-
58
- begin
59
- uri = URI.parse('https://api.opsgenie.com/v2/heartbeats')
60
- http = Net::HTTP.new(uri.host, uri.port)
61
- http.use_ssl = true
62
- doc = {
63
- name: name,
64
- description: description,
65
- interval: interval,
66
- intervalUnit: unit,
67
- enabled: enabled
68
- }
69
- http.post(uri.path, doc.to_json, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})
70
- rescue => e
71
- resolve_exception e
88
+
89
+
90
+ def url_for_resource(verb, name)
91
+ if verb == :post
92
+ 'https://api.opsgenie.com/v2/heartbeats'
93
+ else
94
+ "https://api.opsgenie.com/v2/heartbeats/#{Rack::Utils.escape name}"
95
+ end
72
96
  end
73
- end
74
97
 
75
- def self.resolve_exception e
76
- configuration.logger.info("Exception raised during heartbeat: #{e.message} #{e.backtrace}") if configuration.logger
77
- raise if configuration.raise_error
98
+ def create_or_update(verb, name:,description:,interval:,unit:, enabled:, team:)
99
+ name = configuration.name_transformer.call(name)
100
+
101
+ begin
102
+ uri = URI.parse(url_for_resource(verb, name))
103
+ http = Net::HTTP.new(uri.host, uri.port)
104
+ http.use_ssl = true
105
+ team_to_assign = case team
106
+ when nil then configuration.default_team
107
+ when false then nil
108
+ else
109
+ team
110
+ end
111
+ doc = {
112
+ name: name,
113
+ description: description,
114
+ interval: interval,
115
+ intervalUnit: unit,
116
+ enabled: enabled,
117
+ ownerTeam: team_to_assign
118
+ }.reject {|_, value| value.nil?}
119
+ response = http.public_send(verb, uri.path, doc.to_json, {'Authorization': "GenieKey #{configuration.api_key}", "Content-Type": "application/json"})
120
+
121
+ if !response.is_a?(Net::HTTPSuccess)
122
+ configuration.logger.info("Error creating or updating heartbeat: #{response}") if configuration.logger
123
+ end
124
+
125
+ rescue => e
126
+ resolve_exception e
127
+ end
128
+ end
129
+
130
+ def resolve_exception e
131
+ configuration.logger.info("Exception raised during heartbeat: #{e.message} #{e.backtrace}") if configuration.logger
132
+ raise if configuration.raise_error
133
+ end
78
134
  end
79
135
  end
80
136
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opsgenie-heartbeat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dressipi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-07 00:00:00.000000000 Z
11
+ date: 2018-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project:
99
- rubygems_version: 2.5.2
99
+ rubygems_version: 2.7.6
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: OpsGenie Heartbeat version 2