Skip to content
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

Support gruf interceptors #16

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## UNRELEASED

- Feature: Support gruf Interceptors.

# 0.1.20 - 2024-09-10
- Fix: Repeated float values weren't being treated as arrays and were crashing.

Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,18 @@ GEM
grpc (1.62.0-x86_64-linux)
google-protobuf (~> 3.25)
googleapis-common-protos-types (~> 1.0)
grpc-tools (1.64.0)
gruf (2.20.0)
activesupport (> 4)
concurrent-ruby (> 1)
grpc (~> 1.10)
grpc-tools (~> 1.10)
json (>= 2.3)
slop (>= 4.6)
zeitwerk (>= 2)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
json (2.7.2)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -169,6 +179,7 @@ GEM
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
slop (4.10.1)
thor (1.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
Expand All @@ -184,6 +195,7 @@ PLATFORMS

DEPENDENCIES
grpc-rest!
gruf
rspec-rails

BUNDLED WITH
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ service MyService {
}
```

## Gruf Interceptors

grpc-rest supports [gruf](https://github.com/bigcommerce/gruf) Interceptors. As long as you're not using a custom interceptor
registry, your interceptors will be called normally around the controller.

## To Do

Expand Down
1 change: 1 addition & 0 deletions grpc-rest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency('rails', '>= 6.0')

spec.add_development_dependency('rspec-rails')
spec.add_development_dependency('gruf')
end
13 changes: 12 additions & 1 deletion lib/grpc_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,18 @@ def send_gruf_request(klass, service_obj, method, request)
active_call: nil,
message: request
)
handler.send(method.to_sym)
Gruf::Interceptors::Context.new(gruf_interceptors(request)).intercept! do
Copy link
Contributor

@nkadovic nkadovic Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering for my own knowledge, do you know if a span has already been created by datadog at this point in execution?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be one, yes.

handler.send(method.to_sym)
end
end

# @param request [Google::Protobuf::AbstractMessage]
# @return [Array<Gruf::Interceptors::Base>]
def gruf_interceptors(request)
error = Gruf::Error.new
interceptors = Gruf.interceptors.prepare(request, error)
interceptors.delete_if { |k| k.class.name.split('::').first == 'Gruf' }
interceptors
end

def send_grpc_request(service, method, request)
Expand Down
51 changes: 51 additions & 0 deletions spec/gruf_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require_relative './spec_helper'
require_relative './test_service_services_pb'
require 'gruf'

class GrufServerImpl < Gruf::Controllers::Base

bind ::Testdata::MyService::Service

def test
Testdata::TestResponse.new(some_int: 1, full_response: request.message.to_json)
end

def test_2
Testdata::TestResponse.new(some_int: 2, full_response: request.message.to_json)
end

def test_3
Testdata::TestResponse.new(some_int: 3, full_response: request.message.to_json)
end

def test_4
Testdata::TestResponse.new(some_int: 4, full_response: request.message.to_json)
end

cattr_accessor :intercepted
end

class TestInterceptor < ::Gruf::Interceptors::ServerInterceptor
def call
GrufServerImpl.intercepted = true
yield
end
end

Gruf::Server.new.add_interceptor(TestInterceptor, option_foo: 'value 123')

RSpec.describe MyServiceController, type: :request do
describe 'using get' do
it 'should be successful and call interceptors' do
GrufServerImpl.intercepted = false
get "/test/blah/xyz?test_id=abc"
expect(response).to be_successful
expect(response.parsed_body).to eq({
'someInt' => 1,
'fullResponse' => %({"testId":"abc","foobar":"xyz"}),
"ignoredKey" => ''
})
expect(GrufServerImpl.intercepted).to eq(true)
end
end
end
Loading