-
Notifications
You must be signed in to change notification settings - Fork 185
Adding notifications support for various lifecycle conditions #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cryptomail
wants to merge
1
commit into
main
Choose a base branch
from
jteitelbaum/add-notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/zendesk_api/middleware/response/zendesk_request_event.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require "faraday/response" | ||
|
|
||
| module ZendeskAPI | ||
| module Middleware | ||
| module Response | ||
| # @private | ||
| class ZendeskRequestEvent < Faraday::Middleware | ||
| def initialize(app, client) | ||
| super(app) | ||
| @client = client | ||
| end | ||
|
|
||
| def call(env) | ||
| start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
| @app.call(env).on_complete do |response_env| | ||
| end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
| duration = (end_time - start_time) * 1000.0 | ||
| instrumentation = @client.config.instrumentation | ||
| if instrumentation | ||
| instrumentation.instrument("zendesk.request", | ||
| { duration: duration, | ||
| endpoint: response_env[:url].path, | ||
| method: response_env[:method], | ||
| status: response_env[:status] }) | ||
| if response_env[:status] < 500 | ||
| instrumentation.instrument("zendesk.rate_limit", | ||
| { | ||
| endpoint: response_env[:url].path, | ||
| status: response_env[:status], | ||
| threshold: response_env[:response_headers] ? response_env[:response_headers][:x_rate_limit_remaining] : nil, | ||
| limit: response_env[:response_headers] ? response_env[:response_headers][:x_rate_limit] : nil, | ||
| reset: response_env[:response_headers] ? response_env[:response_headers][:x_rate_limit_reset] : nil | ||
| }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the header keys will be with hyphens, not underscores? |
||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
spec/core/middleware/response/zendesk_request_event_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| require_relative '../../../spec_helper' | ||
| require 'faraday' | ||
| require 'zendesk_api/middleware/response/zendesk_request_event' | ||
|
|
||
| RSpec.describe ZendeskAPI::Middleware::Response::ZendeskRequestEvent do | ||
| let(:instrumentation) { double('Instrumentation') } | ||
| let(:client) do | ||
| double('Client', config: double('Config', instrumentation: instrumentation)) | ||
| end | ||
| let(:app) { ->(env) { Faraday::Response.new(env) } } | ||
| let(:middleware) { described_class.new(app, client) } | ||
| let(:response_headers) do | ||
| { | ||
| x_rate_limit_remaining: 10, | ||
| x_rate_limit: 100, | ||
| x_rate_limit_reset: 1234567890 | ||
| } | ||
| end | ||
| let(:env) do | ||
| { | ||
| url: URI('https://example.zendesk.com/api/v2/tickets'), | ||
| method: :get, | ||
| status: status, | ||
| response_headers: response_headers | ||
| } | ||
| end | ||
|
|
||
| before do | ||
| allow(instrumentation).to receive(:instrument) | ||
| end | ||
|
|
||
| context 'when the response status is less than 500' do | ||
| let(:status) { 200 } | ||
|
|
||
| it 'instruments zendesk.request and zendesk.rate_limit' do | ||
| expect(instrumentation).to receive(:instrument).with( | ||
| 'zendesk.request', | ||
| hash_including(:duration, endpoint: '/api/v2/tickets', method: :get, status: 200) | ||
| ) | ||
| expect(instrumentation).to receive(:instrument).with( | ||
| 'zendesk.rate_limit', | ||
| hash_including(endpoint: '/api/v2/tickets', status: 200) | ||
| ) | ||
| middleware.call(env).on_complete { |_response_env| 1 } | ||
| end | ||
| end | ||
|
|
||
| context 'when the response status is 500 or greater' do | ||
| let(:status) { 500 } | ||
|
|
||
| it 'instruments only zendesk.request' do | ||
| expect(instrumentation).to receive(:instrument).with( | ||
| 'zendesk.request', | ||
| hash_including(:duration, endpoint: '/api/v2/tickets', method: :get, status: 500) | ||
| ) | ||
| expect(instrumentation).not_to receive(:instrument).with('zendesk.rate_limit', anything) | ||
| middleware.call(env).on_complete { |_response_env| 1 } | ||
| end | ||
| end | ||
|
|
||
| context 'duration calculation' do | ||
| let(:status) { 201 } | ||
|
|
||
| it 'passes a positive duration to instrumentation' do | ||
| expect(instrumentation).to receive(:instrument) do |event, payload| | ||
| if event == 'zendesk.request' | ||
| expect(payload[:duration]).to be > 0 | ||
| end | ||
| end | ||
| expect(instrumentation).to receive(:instrument).with('zendesk.rate_limit', anything) | ||
| middleware.call(env).on_complete { |_response_env| 1 } | ||
| end | ||
| end | ||
|
|
||
| context 'when instrumentation is nil' do | ||
| let(:status) { 200 } | ||
| let(:client) do | ||
| double('Client', config: double('Config', instrumentation: nil)) | ||
| end | ||
| let(:middleware) { described_class.new(app, client) } | ||
|
|
||
| it 'does not raise an error' do | ||
| expect { middleware.call(env).on_complete { |_response_env| 1 } }.not_to raise_error | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require 'rspec' | ||
|
|
||
| RSpec.configure do |config| | ||
| config.expect_with :rspec do |c| | ||
| c.syntax = :expect | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a test for values of these counts?