oppex_sdk 1.0.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 +7 -0
- data/LICENSE +201 -0
- data/README.md +166 -0
- data/lib/oppex_sdk/endpoint.rb +6 -0
- data/lib/oppex_sdk/errors.rb +44 -0
- data/lib/oppex_sdk/incident_client.rb +187 -0
- data/lib/oppex_sdk/incident_request.rb +82 -0
- data/lib/oppex_sdk/incident_response.rb +14 -0
- data/lib/oppex_sdk/internal/async_dispatcher.rb +135 -0
- data/lib/oppex_sdk/internal/http_executor.rb +106 -0
- data/lib/oppex_sdk/internal/interrupt.rb +56 -0
- data/lib/oppex_sdk/internal/rate_limited_drop_logger.rb +40 -0
- data/lib/oppex_sdk/internal/retry_executor.rb +56 -0
- data/lib/oppex_sdk/internal/wire_codec.rb +89 -0
- data/lib/oppex_sdk/logging.rb +24 -0
- data/lib/oppex_sdk/severity.rb +49 -0
- data/lib/oppex_sdk/version.rb +6 -0
- data/lib/oppex_sdk.rb +32 -0
- metadata +60 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6ce463caa1bcc1b906080cb3d1ba1dbd4692a0fc9c4006e2a69671f8322b1e5e
|
|
4
|
+
data.tar.gz: dc4fc5631407d977ea9a1c793e93d5a5e438b1d7edfc4f74bb9c164b8d759de2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6715b69c38246a93df9d9f9972375768f7d2ffceedd96990713cb58e2f23feb5629bb7b1f75ec04ccfe2f32f9495932427c3d69b76f8e7d2687295b415027c9e
|
|
7
|
+
data.tar.gz: 89ed9f0eafe9fa8895b25e21f4f8dc45feca6d1aec1ae8d21532ffb1e3b61a491d122a51c3576aced2b3563f1603b869e5d5096c11b3fef1b1f02cde7ff0cfd3
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Oppex Ruby SDK
|
|
2
|
+
|
|
3
|
+
A dependency-free Ruby client for posting incidents to Oppex.
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
POST https://api.oppex.ai/api/v1/incident/post
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
Ruby 3.2 or newer. This SDK supports the current stable Ruby release only, and
|
|
12
|
+
its `required_ruby_version` floor moves forward with it. The standard library is
|
|
13
|
+
the only dependency.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```shell
|
|
18
|
+
gem install oppex_sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem "oppex_sdk", "~> 1.0"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Create one client per application, share it across threads, and close it during
|
|
28
|
+
shutdown.
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
require "oppex_sdk"
|
|
32
|
+
|
|
33
|
+
client = Oppex::IncidentClient.new(
|
|
34
|
+
api_key: ENV.fetch("OPPEX_API_KEY"),
|
|
35
|
+
service_key: ENV.fetch("OPPEX_SERVICE_KEY")
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
response = client.post(
|
|
39
|
+
Oppex::IncidentRequest.new(
|
|
40
|
+
title: "Checkout latency breached the SLO",
|
|
41
|
+
source: "checkout-api",
|
|
42
|
+
severity: Oppex::Severity::HIGH,
|
|
43
|
+
details: JSON.generate({ p99Millis: 1200 })
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
puts response.incident_id
|
|
47
|
+
|
|
48
|
+
client.close
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`IncidentClient.open` closes the client for you, even if the block raises:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
Oppex::IncidentClient.open(api_key: ENV.fetch("OPPEX_API_KEY")) do |client|
|
|
55
|
+
client.post_with_service_routing(request)
|
|
56
|
+
end
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`title`, `source` and `severity` are required. Every other field is optional, and
|
|
60
|
+
an absent optional field is left out of the payload rather than sent as null. An
|
|
61
|
+
optional field that is *present but blank* is rejected, because that is nearly
|
|
62
|
+
always a bug at the call site. `priority` defaults to 1 and `src_timestamp`
|
|
63
|
+
defaults to the current time in milliseconds since the Unix epoch.
|
|
64
|
+
|
|
65
|
+
`IncidentRequest` validates in its constructor and freezes itself, so a malformed
|
|
66
|
+
incident is rejected at the call site rather than inside a worker thread.
|
|
67
|
+
|
|
68
|
+
### Service routing
|
|
69
|
+
|
|
70
|
+
The service key is optional. A client built with only an `api_key` posts with
|
|
71
|
+
`post_with_service_routing`, which omits `serviceKey` so Oppex resolves the
|
|
72
|
+
target service itself. The request must not carry its own service key in that
|
|
73
|
+
case.
|
|
74
|
+
|
|
75
|
+
A request's own `service_key` overrides the client's.
|
|
76
|
+
|
|
77
|
+
### Fire and forget
|
|
78
|
+
|
|
79
|
+
`post_async` and `post_async_with_service_routing` queue a best-effort delivery
|
|
80
|
+
and return immediately. The closed-client and service-key checks still run on the
|
|
81
|
+
calling thread, so a misuse is raised to you rather than lost in a worker. A
|
|
82
|
+
delivery failure after queueing is logged at debug level.
|
|
83
|
+
|
|
84
|
+
### Errors
|
|
85
|
+
|
|
86
|
+
| Situation | Raised |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| Invalid argument value | `ArgumentError` |
|
|
89
|
+
| Argument of the wrong type | `TypeError` |
|
|
90
|
+
| Any post after `close` | `Oppex::ClientClosedError` |
|
|
91
|
+
| Delivery failure | `Oppex::IncidentError` |
|
|
92
|
+
|
|
93
|
+
`Oppex::ClientClosedError` and `Oppex::IncidentError` both descend from
|
|
94
|
+
`Oppex::Error`, so one `rescue` catches everything this SDK raises on its own.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
begin
|
|
98
|
+
client.post(request)
|
|
99
|
+
rescue Oppex::IncidentError => e
|
|
100
|
+
retry_later if e.retryable?
|
|
101
|
+
logger.error("oppex rejected the incident: HTTP #{e.status_code}") if e.http_status?
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`IncidentError#status_code` is `-1` when the delivery never reached a status
|
|
106
|
+
line; `#http_status?` answers that directly.
|
|
107
|
+
|
|
108
|
+
### Severity
|
|
109
|
+
|
|
110
|
+
`Oppex::Severity::LOWEST` (1) through `Oppex::Severity::CRITICAL` (5). The
|
|
111
|
+
constants are the wire values, so an integer from another system can be passed
|
|
112
|
+
straight through after `Oppex::Severity.validate!`.
|
|
113
|
+
|
|
114
|
+
### Logging
|
|
115
|
+
|
|
116
|
+
Pass any object that responds to `debug`, `info`, `warn` and `error`:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
Oppex::IncidentClient.new(api_key: key, logger: Rails.logger)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The default writes to standard error. This SDK deliberately does not require the
|
|
123
|
+
`logger` standard library, which stops being a default gem in Ruby 4.0 — duck
|
|
124
|
+
typing keeps the gem dependency-free and accepts your logger either way.
|
|
125
|
+
|
|
126
|
+
## Delivery behavior
|
|
127
|
+
|
|
128
|
+
- 3 second open timeout, 5 second read and write timeouts.
|
|
129
|
+
- HTTP 429, 500, 502, 503 and 504, plus failures that never reached a status
|
|
130
|
+
line, retry after 0.5s, 1s, 2s, 4s and 8s. Every other status fails
|
|
131
|
+
immediately.
|
|
132
|
+
- Asynchronous delivery is best effort through a queue bounded at 5000 that drops
|
|
133
|
+
the oldest entry under saturation. Drops are counted and summarized at most once
|
|
134
|
+
a minute.
|
|
135
|
+
- `close` drains for up to 10 seconds, then abandons the rest.
|
|
136
|
+
|
|
137
|
+
None of these are configurable. They are part of the incident contract every
|
|
138
|
+
Oppex SDK shares, not per-caller settings.
|
|
139
|
+
|
|
140
|
+
## Build and test
|
|
141
|
+
|
|
142
|
+
```shell
|
|
143
|
+
cd ruby
|
|
144
|
+
bundle install
|
|
145
|
+
bundle exec rake # rubocop, then the minitest suite
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Tests run against a loopback stub server, so the suite is network-free beyond
|
|
149
|
+
loopback and never reaches the real Oppex service.
|
|
150
|
+
|
|
151
|
+
## Release
|
|
152
|
+
|
|
153
|
+
Tag `ruby-vX.Y.Z`. The release workflow builds the gem, verifies those exact
|
|
154
|
+
bytes against an external consumer, and pushes them to RubyGems without
|
|
155
|
+
rebuilding.
|
|
156
|
+
|
|
157
|
+
```shell
|
|
158
|
+
git tag ruby-v1.0.0
|
|
159
|
+
git push origin ruby-v1.0.0
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The tag's version must match `Oppex::VERSION`; the workflow fails if it does not.
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
[Apache License 2.0](LICENSE).
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oppex
|
|
4
|
+
# Base class for every error this SDK raises on its own. Rescue this to catch
|
|
5
|
+
# them all without also catching an +ArgumentError+ from your own code.
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Raised by any post made after IncidentClient#close.
|
|
9
|
+
class ClientClosedError < Error
|
|
10
|
+
def initialize(message = "the incident client is closed")
|
|
11
|
+
super
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Raised when delivery was attempted and failed.
|
|
16
|
+
class IncidentError < Error
|
|
17
|
+
# The HTTP status, or NO_STATUS_CODE when the delivery never reached a
|
|
18
|
+
# status line.
|
|
19
|
+
NO_STATUS_CODE = -1
|
|
20
|
+
|
|
21
|
+
# @return [Integer] the HTTP status, or {NO_STATUS_CODE}.
|
|
22
|
+
attr_reader :status_code
|
|
23
|
+
|
|
24
|
+
# @return [Exception, nil] the underlying transport or parsing failure.
|
|
25
|
+
attr_reader :cause_error
|
|
26
|
+
|
|
27
|
+
def initialize(message, status_code: NO_STATUS_CODE, retryable: false, cause_error: nil)
|
|
28
|
+
super(message)
|
|
29
|
+
@status_code = status_code
|
|
30
|
+
@retryable = retryable
|
|
31
|
+
@cause_error = cause_error
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Boolean] whether the failure was eligible for retry.
|
|
35
|
+
def retryable?
|
|
36
|
+
@retryable
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [Boolean] whether an HTTP status was received at all.
|
|
40
|
+
def http_status?
|
|
41
|
+
@status_code != NO_STATUS_CODE
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "errors"
|
|
4
|
+
require_relative "incident_request"
|
|
5
|
+
require_relative "internal/async_dispatcher"
|
|
6
|
+
require_relative "internal/http_executor"
|
|
7
|
+
require_relative "internal/interrupt"
|
|
8
|
+
require_relative "internal/retry_executor"
|
|
9
|
+
require_relative "internal/wire_codec"
|
|
10
|
+
require_relative "logging"
|
|
11
|
+
|
|
12
|
+
module Oppex
|
|
13
|
+
# Posts incidents to Oppex.
|
|
14
|
+
#
|
|
15
|
+
# client = Oppex::IncidentClient.new(api_key: "api-key", service_key: "service-key")
|
|
16
|
+
# client.post(request)
|
|
17
|
+
# client.close
|
|
18
|
+
#
|
|
19
|
+
# The client is thread safe and intended to be shared. Create one per
|
|
20
|
+
# application and close it during shutdown. It is also usable as a block:
|
|
21
|
+
#
|
|
22
|
+
# Oppex::IncidentClient.open(api_key: "api-key") do |client|
|
|
23
|
+
# client.post_with_service_routing(request)
|
|
24
|
+
# end
|
|
25
|
+
class IncidentClient
|
|
26
|
+
# Creates a client, yields it, and closes it even if the block raises.
|
|
27
|
+
#
|
|
28
|
+
# @yieldparam client [IncidentClient]
|
|
29
|
+
def self.open(...)
|
|
30
|
+
client = new(...)
|
|
31
|
+
begin
|
|
32
|
+
yield client
|
|
33
|
+
ensure
|
|
34
|
+
client.close
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @param api_key [String] sent in the +X-API-KEY+ header. Required.
|
|
39
|
+
# @param service_key [String, nil] the default service for incidents that do
|
|
40
|
+
# not carry their own. When omitted, incidents must either supply one or be
|
|
41
|
+
# posted with {#post_with_service_routing}.
|
|
42
|
+
# @param logger [#debug, #info, #warn, #error] receives this client's
|
|
43
|
+
# internal logging. Any object with those four methods works, including a
|
|
44
|
+
# standard +Logger+, +Rails.logger+, or your own.
|
|
45
|
+
def initialize(api_key:, service_key: nil, logger: StderrLogger.new)
|
|
46
|
+
@api_key = validate_api_key(api_key)
|
|
47
|
+
@service_key = blank_to_nil(service_key, "service_key")
|
|
48
|
+
@logger = logger
|
|
49
|
+
@http_executor = Internal::HttpExecutor.new(@api_key)
|
|
50
|
+
@interrupt = Internal::Interrupt.new
|
|
51
|
+
@dispatcher = Internal::AsyncDispatcher.new(logger)
|
|
52
|
+
@retry_delays = Internal::RetryExecutor::DEFAULT_DELAYS
|
|
53
|
+
@closed = false
|
|
54
|
+
@lifecycle = Mutex.new
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Posts on the calling thread, including any retry delays. The request's own
|
|
58
|
+
# service key overrides the client's.
|
|
59
|
+
#
|
|
60
|
+
# @return [IncidentResponse]
|
|
61
|
+
# @raise [ClientClosedError] after {#close}
|
|
62
|
+
# @raise [ArgumentError] when neither the client nor the request carries a
|
|
63
|
+
# service key
|
|
64
|
+
# @raise [IncidentError] when delivery fails
|
|
65
|
+
def post(request)
|
|
66
|
+
ensure_open!
|
|
67
|
+
require_service_key!(request)
|
|
68
|
+
deliver(request, @service_key)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Posts without a service key so Oppex resolves the target service itself.
|
|
72
|
+
# The request must not carry its own service key. Otherwise identical to
|
|
73
|
+
# {#post}.
|
|
74
|
+
#
|
|
75
|
+
# @return [IncidentResponse]
|
|
76
|
+
def post_with_service_routing(request)
|
|
77
|
+
ensure_open!
|
|
78
|
+
require_service_routable!(request)
|
|
79
|
+
deliver(request, nil)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Queues a best-effort delivery and returns immediately.
|
|
83
|
+
#
|
|
84
|
+
# The closed-client and service-key checks still run on the calling thread, so
|
|
85
|
+
# a misuse is raised to the caller rather than lost in a worker. A delivery
|
|
86
|
+
# failure after queueing is logged at debug level.
|
|
87
|
+
#
|
|
88
|
+
# @return [void]
|
|
89
|
+
def post_async(request)
|
|
90
|
+
ensure_open!
|
|
91
|
+
require_service_key!(request)
|
|
92
|
+
submit(request, @service_key)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Queues a best-effort service-routed delivery. The request must not carry its
|
|
96
|
+
# own service key.
|
|
97
|
+
#
|
|
98
|
+
# @return [void]
|
|
99
|
+
def post_async_with_service_routing(request)
|
|
100
|
+
ensure_open!
|
|
101
|
+
require_service_routable!(request)
|
|
102
|
+
submit(request, nil)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# @return [Boolean] whether {#close} has been called.
|
|
106
|
+
def closed?
|
|
107
|
+
@lifecycle.synchronize { @closed }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Drains queued work for up to ten seconds, then releases every owned
|
|
111
|
+
# resource. Idempotent, and safe to call from any thread.
|
|
112
|
+
#
|
|
113
|
+
# @return [void]
|
|
114
|
+
def close
|
|
115
|
+
@lifecycle.synchronize do
|
|
116
|
+
return if @closed
|
|
117
|
+
|
|
118
|
+
@closed = true
|
|
119
|
+
end
|
|
120
|
+
# Signalled before the drain so a worker sitting in an eight-second backoff
|
|
121
|
+
# gives up now instead of consuming the whole drain budget.
|
|
122
|
+
@interrupt.signal
|
|
123
|
+
@dispatcher.close
|
|
124
|
+
@http_executor.close
|
|
125
|
+
nil
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
def validate_api_key(api_key)
|
|
131
|
+
raise TypeError, "api_key must be a String" unless api_key.is_a?(String)
|
|
132
|
+
raise ArgumentError, "api_key must not be blank" if api_key.strip.empty?
|
|
133
|
+
|
|
134
|
+
api_key
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def blank_to_nil(value, field)
|
|
138
|
+
return nil if value.nil?
|
|
139
|
+
raise TypeError, "#{field} must be a String" unless value.is_a?(String)
|
|
140
|
+
|
|
141
|
+
value.strip.empty? ? nil : value
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def ensure_open!
|
|
145
|
+
raise ClientClosedError if closed?
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Service routing is the only delivery mode available when no service key is
|
|
149
|
+
# configured anywhere.
|
|
150
|
+
def require_service_key!(request)
|
|
151
|
+
return unless @service_key.nil? && request.service_key.nil?
|
|
152
|
+
|
|
153
|
+
raise ArgumentError, "no service_key is configured on the client or the request; " \
|
|
154
|
+
"supply one or use post_with_service_routing"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def require_service_routable!(request)
|
|
158
|
+
return if request.service_key.nil?
|
|
159
|
+
|
|
160
|
+
raise ArgumentError, "request must not carry a service_key when service routing is used"
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def submit(request, default_service_key)
|
|
164
|
+
accepted = @dispatcher.submit do
|
|
165
|
+
# No closed check here: close drains the dispatcher before releasing the
|
|
166
|
+
# transport, so a task queued before close still has a live connection,
|
|
167
|
+
# and a task submitted after close was already refused above.
|
|
168
|
+
deliver(request, default_service_key)
|
|
169
|
+
rescue IncidentError => e
|
|
170
|
+
@logger.debug("oppex: asynchronous incident delivery failed: #{e.message}")
|
|
171
|
+
end
|
|
172
|
+
raise ClientClosedError unless accepted
|
|
173
|
+
|
|
174
|
+
nil
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Serializes, sends and retries a single incident. The request's own service
|
|
178
|
+
# key wins over +default_service_key+.
|
|
179
|
+
def deliver(request, default_service_key)
|
|
180
|
+
service_key = request.service_key || default_service_key
|
|
181
|
+
payload = Internal::WireCodec.serialize_request(request, service_key)
|
|
182
|
+
Internal::RetryExecutor.execute(@interrupt, delays: @retry_delays) do
|
|
183
|
+
@http_executor.execute(payload)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|